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

Commit

Permalink
feat(tabs): 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 df3c08c commit 26b33c3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
24 changes: 18 additions & 6 deletions components/tabs/Tab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export default {
},
data () {
return {
mdcTab: null
mdcTab: undefined,
slotObserver: undefined
}
},
computed: {
Expand All @@ -46,16 +47,27 @@ export default {
}
},
mounted () {
if (this.$slots.icon) {
this.$slots.icon[0].elm.classList.add('mdc-tab__icon')
this.label ? this.$slots.icon[0].elm.setAttribute('aria-label', true)
: this.$slots.icon[0].elm.setAttribute('aria-hidden', true)
}
this.updateSlot()
this.slotObserver = new MutationObserver( () => this.updateSlot())
this.slotObserver.observe(this.$el, {
childList: true,
subtree: true
})
this.mdcTab = MDCTab.attachTo(this.$el)
},
beforeDestroy () {
this.slotObserver.disconnect()
this.mdcTab.destroy()
},
methods: {
updateSlot () {
if (this.$slots.icon) {
this.$slots.icon[0].elm.classList.add('mdc-tab__icon')
this.label ? this.$slots.icon[0].elm.setAttribute('aria-label', true)
: this.$slots.icon[0].elm.setAttribute('aria-hidden', true)
}
}
}
}
</script>
13 changes: 8 additions & 5 deletions components/tabs/TabBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export default {
},
data () {
return {
mdcTabBar: null,
mdcTabBarScroller: null
mdcTabBar: undefined,
mdcTabBarScroller: undefined
}
},
computed: {
Expand All @@ -78,9 +78,12 @@ export default {
: (this.mdcTabBar = MDCTabBar.attachTo(this.$el))
},
beforeDestroy () {
this.scrollable
? this.mdcTabBarScroller.destroy()
: this.mdcTabBar.destroy()
if (typeof this.mdcTabBarScroller !== 'undefined') {
this.mdcTabBarScroller.destroy()
}
if (typeof this.mdcTabBar !== 'undefined') {
this.mdcTabBar.destroy()
}
}
}
</script>

0 comments on commit 26b33c3

Please sign in to comment.