Skip to content
This repository was archived by the owner on Nov 30, 2020. It is now read-only.

Commit d6278b5

Browse files
committed
feat(dialog): Use MutationObserver for reactive slot updates (#92)
1 parent cfbf749 commit d6278b5

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

components/dialog/Dialog.vue

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ export default {
5151
},
5252
data () {
5353
return {
54-
mdcDialog: null
54+
mdcDialog: null,
55+
slotOberserver: null
5556
}
5657
},
5758
computed: {
@@ -75,32 +76,38 @@ export default {
7576
}
7677
},
7778
mounted () {
78-
let vm = this
79+
this.updateSlots()
80+
this.slotOberserver = new MutationObserver( () => this.updateSlots())
81+
this.slotOberserver.observe(this.$el, {
82+
childList: true,
83+
subtree: true
84+
})
7985
80-
if (vm.$slots.acceptButton) {
81-
vm.$slots.acceptButton.map(n => {
82-
n.elm.classList.add('mdc-dialog__footer__button')
83-
n.elm.classList.add('mdc-dialog__footer__button--accept')
84-
})
85-
}
86-
if (vm.$slots.cancelButton) {
87-
vm.$slots.cancelButton.map(n => {
88-
n.elm.classList.add('mdc-dialog__footer__button')
89-
n.elm.classList.add('mdc-dialog__footer__button--cancel')
90-
})
91-
}
92-
if (vm.$slots.dialogButton) {
93-
vm.$slots.dialogButton.map(n => {
94-
n.elm.classList.add('mdc-dialog__footer__button')
95-
})
96-
}
97-
98-
vm.mdcDialog = MDCDialog.attachTo(this.$el)
86+
this.mdcDialog = MDCDialog.attachTo(this.$el)
9987
},
10088
beforeDestroy () {
10189
this.mdcDialog.destroy()
10290
},
10391
methods: {
92+
updateSlots () {
93+
if (this.$slots.acceptButton) {
94+
this.$slots.acceptButton.map(n => {
95+
n.elm.classList.add('mdc-dialog__footer__button')
96+
n.elm.classList.add('mdc-dialog__footer__button--accept')
97+
})
98+
}
99+
if (this.$slots.cancelButton) {
100+
this.$slots.cancelButton.map(n => {
101+
n.elm.classList.add('mdc-dialog__footer__button')
102+
n.elm.classList.add('mdc-dialog__footer__button--cancel')
103+
})
104+
}
105+
if (this.$slots.dialogButton) {
106+
this.$slots.dialogButton.map(n => {
107+
n.elm.classList.add('mdc-dialog__footer__button')
108+
})
109+
}
110+
},
104111
onAccept () {
105112
this.model = false
106113
this.$emit('accept')

0 commit comments

Comments
 (0)