Skip to content

Commit 458317f

Browse files
committed
fix(VVirtualScroll): trim leading items when appending to bottom
fixes #20959
1 parent fede124 commit 458317f

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

packages/vuetify/src/components/VVirtualScroll/__tests__/VVirtualScroll.spec.browser.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,37 @@ describe('VVirtualScroll', () => {
5555
expect(paddingBottom).toBeLessThan(800)
5656
})
5757

58+
it('removes previous items when appending while pinned to the bottom', async () => {
59+
const items = ref(createRange(50))
60+
61+
render(() => (
62+
<VVirtualScroll height="400" items={ items.value } itemHeight="24">
63+
{{
64+
default: ({ index }) => (
65+
<div style={{ height: '24px' }}>{ index }</div>
66+
),
67+
}}
68+
</VVirtualScroll>
69+
))
70+
71+
const root = screen.getByCSS('.v-virtual-scroll')
72+
73+
await waitIdle()
74+
root.scrollTop = root.scrollHeight
75+
await waitIdle()
76+
77+
// simulate a log view adding 1 line at a time
78+
for (let i = 0; i < 50; i++) {
79+
items.value = [...items.value, items.value.length]
80+
await waitIdle()
81+
root.scrollTop = root.scrollHeight
82+
await waitIdle()
83+
}
84+
85+
// The window must slide
86+
expect(screen.getAllByCSS('.v-virtual-scroll__item').length).toBeLessThan(50)
87+
})
88+
5889
it('reuses the same elements', async () => {
5990
const items = createRange(1000)
6091

packages/vuetify/src/composables/virtual.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,10 @@ export function useVirtual <T> (props: VirtualProps, items: Ref<readonly T[]>) {
227227
} else {
228228
// Only update the side that's reached its limit if there's still buffer left
229229
if (start <= 0) first.value = start
230-
if (end >= items.value.length) last.value = end
230+
if (end >= items.value.length) {
231+
last.value = end
232+
first.value = start
233+
}
231234
}
232235
}
233236

0 commit comments

Comments
 (0)