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

Commit

Permalink
feat(menu): Use MutationObserver for reactive slot updates (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
matsp committed Apr 23, 2018
1 parent 7804dff commit 7085946
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions components/menu/Menu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export default {
},
data () {
return {
mdcMenu: null
mdcMenu: null,
slotOberserver: null
}
},
computed: {
Expand All @@ -53,26 +54,34 @@ export default {
}
},
mounted () {
if (this.$slots.default) {
this.$slots.default.map(n => {
n.elm.classList.add('mdc-menu__items')
})
this.$slots.default[0].elm.setAttribute('role', 'menu')
this.$slots.default[0].componentOptions.children
.filter(n => n.elm.className.indexOf('mdc-list-item') > -1)
.map(n => {
n.elm.setAttribute('tabindex', '0')
n.elm.setAttribute('role', 'menuitem')
})
}
this.updateSlot()
this.slotOberserver = new MutationObserver( () => this.updateSlot())
this.slotOberserver.observe(this.$el, {
childList: true,
subtree: true
})
this.mdcMenu = MDCMenu.attachTo(this.$el)
},
beforeDestroy () {
this.slotOberserver.disconnect()
this.mdcMenu.destroy()
},
methods: {
updateSlot () {
if (this.$slots.default) {
this.$slots.default.map(n => {
n.elm.classList.add('mdc-menu__items')
})
this.$slots.default[0].elm.setAttribute('role', 'menu')
this.$slots.default[0].componentOptions.children
.filter(n => n.elm.className.indexOf('mdc-list-item') > -1)
.map(n => {
n.elm.setAttribute('tabindex', '0')
n.elm.setAttribute('role', 'menuitem')
})
}
},
onSelect (event) {
this.model = false
this.$emit('select', event.detail)
Expand Down

0 comments on commit 7085946

Please sign in to comment.