Skip to content

Commit 8e9888f

Browse files
teleskop150750productdevbook
authored andcommitted
perf: [ScrollArea] onDocumentWheel
1 parent 8b8399d commit 8e9888f

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

packages/vue-primitives/src/accordion/AccordionRoot.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ export function useAccordionRoot<T extends AccordionType>(props: UseAccordionRoo
216216
}
217217
},
218218
onItemClose(itemValue) {
219-
console.error('onItemClose', itemValue, props.collapsible, props.type)
220219
if (props.type === TYPE_MULTIPLE) {
221220
value.value = arrayify<SingleValue>(value.value || []).filter(value => value !== itemValue) as Value<T>
222221
}

packages/vue-primitives/src/scroll-area/ScrollAreaScrollbarVisible.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ export interface UseScrollAreaScrollbarVisibleProps {
2323
orientation?: 'horizontal' | 'vertical'
2424
}
2525

26+
let wheelListeners: ((e: WheelEvent) => void)[] = []
27+
28+
function onDocumentWheel(event: WheelEvent) {
29+
for (const wheelListener of wheelListeners) {
30+
wheelListener(event)
31+
}
32+
}
33+
2634
export function useScrollAreaScrollbarVisible(props: UseScrollAreaScrollbarVisibleProps): RadixPrimitiveReturns {
2735
const isHorizontal = props.orientation === 'horizontal'
2836

@@ -160,14 +168,15 @@ export function useScrollAreaScrollbarVisible(props: UseScrollAreaScrollbarVisib
160168
}
161169

162170
onMounted(() => {
163-
// TODO: optimize this
164-
document.addEventListener('wheel', onImplWheelScroll, {
165-
passive: false,
166-
})
171+
if (wheelListeners.length === 0)
172+
document.addEventListener('wheel', onDocumentWheel, { passive: false })
173+
wheelListeners.push(onImplWheelScroll)
167174
})
168175

169176
onBeforeUnmount(() => {
170-
document.removeEventListener('wheel', onImplWheelScroll)
177+
wheelListeners = wheelListeners.filter(i => i !== onImplWheelScroll)
178+
if (wheelListeners.length === 0)
179+
document.removeEventListener('wheel', onDocumentWheel)
171180
})
172181

173182
/**

0 commit comments

Comments
 (0)