Skip to content

Commit

Permalink
refactor(kpop): improve kpop performance on page scrolling (#2476)
Browse files Browse the repository at this point in the history
  • Loading branch information
2eha0 authored Oct 24, 2024
1 parent 6e6bf1a commit 355f73d
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/components/KPop/KPop.vue
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,22 @@ const togglePopover = () => {
}
}
const cancelFloatingUpdates = () => {
if (floatingUpdates.value) {
floatingUpdates.value()
}
}
const startFloatingUpdates = () => {
cancelFloatingUpdates()
if (popoverTrigger.value && popoverElement.value) {
// start the auto updates for the popover position
// autoUpdate cleanup function
// docs: https://floating-ui.com/docs/autoUpdate#usage
floatingUpdates.value = autoUpdate(popoverTrigger.value, popoverElement.value, updatePosition)
}
}
const showPopover = async () => {
if (!props.disabled) {
if (timer.value) {
Expand All @@ -195,12 +211,14 @@ const showPopover = async () => {
popoverKey.value++
await nextTick() // wait for the Transition to update to ensure the animation works as expected
}
startFloatingUpdates()
isVisible.value = true
}
}
const hidePopover = () => {
timer.value = setTimeout(() => {
cancelFloatingUpdates()
isVisible.value = false
}, props.trigger === 'hover' ? props.popoverTimeout : 0)
}
Expand Down Expand Up @@ -311,13 +329,6 @@ onMounted(() => {
popoverElement.value.addEventListener('focusout', hidePopover)
}
}
if (popoverTrigger.value && popoverElement.value) {
// start the auto updates for the popover position
// autoUpdate cleanup function
// docs: https://floating-ui.com/docs/autoUpdate#usage
floatingUpdates.value = autoUpdate(popoverTrigger.value, popoverElement.value, updatePosition)
}
})
onBeforeUnmount(() => {
Expand All @@ -339,10 +350,7 @@ onBeforeUnmount(() => {
}
}
if (floatingUpdates.value) {
// need to cleanup the auto updates
floatingUpdates.value()
}
cancelFloatingUpdates()
})
watch(isVisible, (val) => {
Expand Down

0 comments on commit 355f73d

Please sign in to comment.