Skip to content

Commit

Permalink
fix(VVirtualScroll): use a dynamic window size, cache offsets (#18392)
Browse files Browse the repository at this point in the history
fixes #18198
fixes #17801
  • Loading branch information
KaelWD authored Nov 1, 2023
1 parent 3ffe9c5 commit 8a71a7d
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 71 deletions.
26 changes: 20 additions & 6 deletions packages/vuetify/src/components/VVirtualScroll/VVirtualScroll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ export const VVirtualScroll = genericComponent<new <T, Renderless extends boolea
const { dimensionStyles } = useDimension(props)
const {
containerRef,
markerRef,
handleScroll,
handleScrollend,
handleItemResize,
scrollToIndex,
paddingTop,
Expand All @@ -71,13 +73,23 @@ export const VVirtualScroll = genericComponent<new <T, Renderless extends boolea
} = useVirtual(props, toRef(props, 'items'))

useToggleScope(() => props.renderless, () => {
function handleListeners (add = false) {
const method = add ? 'addEventListener' : 'removeEventListener'

if (containerRef.value === document.documentElement) {
document[method]('scroll', handleScroll, { passive: true })
document[method]('scrollend', handleScrollend)
} else {
containerRef.value?.[method]('scroll', handleScroll, { passive: true })
containerRef.value?.[method]('scrollend', handleScrollend)
}
}

onMounted(() => {
containerRef.value = getScrollParent(vm.vnode.el as HTMLElement, true)
containerRef.value?.addEventListener('scroll', handleScroll)
})
onScopeDispose(() => {
containerRef.value?.removeEventListener('scroll', handleScroll)
handleListeners(true)
})
onScopeDispose(handleListeners)
})

useRender(() => {
Expand All @@ -93,7 +105,7 @@ export const VVirtualScroll = genericComponent<new <T, Renderless extends boolea

return props.renderless ? (
<>
<div class="v-virtual-scroll__spacer" style={{ paddingTop: convertToUnit(paddingTop.value) }} />
<div ref={ markerRef } class="v-virtual-scroll__spacer" style={{ paddingTop: convertToUnit(paddingTop.value) }} />
{ children }
<div class="v-virtual-scroll__spacer" style={{ paddingBottom: convertToUnit(paddingBottom.value) }} />
</>
Expand All @@ -104,13 +116,15 @@ export const VVirtualScroll = genericComponent<new <T, Renderless extends boolea
'v-virtual-scroll',
props.class,
]}
onScroll={ handleScroll }
onScrollPassive={ handleScroll }
onScrollend={ handleScrollend }
style={[
dimensionStyles.value,
props.style,
]}
>
<div
ref={ markerRef }
class="v-virtual-scroll__container"
style={{
paddingTop: convertToUnit(paddingTop.value),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('VVirtualScroll', () => {
</VVirtualScroll>
))

cy.get('.v-virtual-scroll__item').should('have.length', 30)
cy.get('.v-virtual-scroll__item').should('have.length.above', 10).should('have.length.below', 50)
})

it('reuses the same elements', () => {
Expand Down
Loading

0 comments on commit 8a71a7d

Please sign in to comment.