optimization: only process resize/scroll events when popup is open

This commit is contained in:
Henry Jameson 2022-06-20 23:55:39 +03:00
parent 770d12f7ad
commit cb89646c56
1 changed files with 16 additions and 6 deletions

View File

@ -47,6 +47,7 @@ const Popover = {
hidden: true, hidden: true,
styles: {}, styles: {},
oldSize: { width: 0, height: 0 }, oldSize: { width: 0, height: 0 },
scrollable: null,
// used to avoid blinking if hovered onto popover // used to avoid blinking if hovered onto popover
graceTimeout: null graceTimeout: null
} }
@ -175,14 +176,25 @@ const Popover = {
if (this.disabled) return if (this.disabled) return
const wasHidden = this.hidden const wasHidden = this.hidden
this.hidden = false this.hidden = false
if (this.trigger === 'click') {
document.addEventListener('click', this.onClickOutside)
}
this.scrollable.addEventListener('scroll', this.onScroll)
this.scrollable.addEventListener('resize', this.onResize)
this.$nextTick(() => { this.$nextTick(() => {
if (wasHidden) this.$emit('show') if (wasHidden) this.$emit('show')
this.updateStyles() this.updateStyles()
}) })
}, },
hidePopover () { hidePopover () {
if (this.disabled) return
if (!this.hidden) this.$emit('close') if (!this.hidden) this.$emit('close')
this.hidden = true this.hidden = true
if (this.trigger === 'click') {
document.removeEventListener('click', this.onClickOutside)
}
this.scrollable.removeEventListener('scroll', this.onScroll)
this.scrollable.removeEventListener('resize', this.onResize)
}, },
onMouseenter (e) { onMouseenter (e) {
if (this.trigger === 'hover') { if (this.trigger === 'hover') {
@ -225,6 +237,9 @@ const Popover = {
}, },
onScroll (e) { onScroll (e) {
this.updateStyles() this.updateStyles()
},
onResize (e) {
this.updateStyles()
} }
}, },
updated () { updated () {
@ -241,14 +256,9 @@ const Popover = {
mounted () { mounted () {
let scrollable = this.$refs.trigger.closest('.column.-scrollable') let scrollable = this.$refs.trigger.closest('.column.-scrollable')
if (!scrollable) scrollable = window if (!scrollable) scrollable = window
document.addEventListener('click', this.onClickOutside) this.scrollable = scrollable
scrollable.addEventListener('scroll', this.onScroll)
}, },
beforeUnmount () { beforeUnmount () {
let scrollable = this.$refs.trigger.closest('.column.-scrollable')
if (!scrollable) scrollable = window
document.removeEventListener('click', this.onClickOutside)
scrollable.removeEventListener('scroll', this.onScroll)
this.hidePopover() this.hidePopover()
} }
} }