Skip to content

Instantly share code, notes, and snippets.

@iErik
Created June 11, 2019 17:25
Show Gist options
  • Save iErik/93865d2b7f2502b2def67f86d9cb1065 to your computer and use it in GitHub Desktop.
Save iErik/93865d2b7f2502b2def67f86d9cb1065 to your computer and use it in GitHub Desktop.
const Collapsible = (refName = 'contentWrapper') => ({
data: () => ({
hideContents: false,
contentObs: null,
contentHeight: 0,
}),
mounted () {
this.$nextTick(() => {
if (!this.$refs[refName]) return
this.contentObs = new ResizeObserver(ev => {
const rect = ev[0].target.getBoundingClientRect()
this.contentHeight = rect.height
})
const el = this.$refs[refName].hasOwnProperty('$el')
? this.$refs[refName].el
: this.$refs[refName]
if (el) this.contentObs.observe(el.childNodes[0])
})
},
beforeDestroy () {
if (this.contentObs) this.contentObs.disconnect()
}
})
export default Collapsible
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment