diff --git a/.changeset/listed-anchor-fallback.md b/.changeset/listed-anchor-fallback.md new file mode 100644 index 000000000..cb0f075df --- /dev/null +++ b/.changeset/listed-anchor-fallback.md @@ -0,0 +1,5 @@ +--- +'@tanstack/virtual-core': patch +--- + +Fix `anchorTo: 'end'` moving the reader to prepended items when the item at the scroll offset is removed in the same update: the nearest previous item that is still listed now keeps its offset instead. diff --git a/packages/virtual-core/src/index.ts b/packages/virtual-core/src/index.ts index e5b59b31d..9611859f8 100644 --- a/packages/virtual-core/src/index.ts +++ b/packages/virtual-core/src/index.ts @@ -484,6 +484,38 @@ function isAppendWithTrim( return true } +function findListedAnchor( + index: number, + prevCount: number, + getPreviousKey: (index: number) => Key, + nextCount: number, + getNextKey: (index: number) => Key, + getPreviousStart: (index: number) => number, +): number | null { + const listed = new Set() + for (let i = 0; i < nextCount; i++) listed.add(getNextKey(i)) + if (listed.has(getPreviousKey(index))) return index + let after: number | null = null + for (let i = index + 1; i < prevCount; i++) { + if (listed.has(getPreviousKey(i))) { + after = i + break + } + } + let before: number | null = null + for (let i = index - 1; i >= 0; i--) { + if (listed.has(getPreviousKey(i))) { + before = i + break + } + } + if (before === null || after === null) return after ?? before + const start = getPreviousStart(index) + return start - getPreviousStart(before) < getPreviousStart(after) - start + ? before + : after +} + export class Virtualizer< TScrollElement extends Element | Window, TItemElement extends Element, @@ -888,8 +920,23 @@ export class Virtualizer< measurements[0]) : null - if (item) { - anchor = [item.key, this.getScrollOffset() - item.start] + const anchorItem = item + ? findListedAnchor( + item.index, + prevCount, + getPreviousKey, + nextCount, + merged.getItemKey, + (i) => notUndefined(measurements[i]).start, + ) + : null + + if (anchorItem !== null) { + const anchorStart = notUndefined(measurements[anchorItem]).start + anchor = [ + getPreviousKey(anchorItem), + this.getScrollOffset() - anchorStart, + ] } const behavior = diff --git a/packages/virtual-core/tests/anchor-end.test.ts b/packages/virtual-core/tests/anchor-end.test.ts index cc27647de..91b016570 100644 --- a/packages/virtual-core/tests/anchor-end.test.ts +++ b/packages/virtual-core/tests/anchor-end.test.ts @@ -128,6 +128,54 @@ test('anchorTo:end keeps visible content stable when older items are prepended', expect(options.adjustments).toBeUndefined() }) +test('anchorTo:end keeps the next listed item in place when the anchor is removed by a prepend', () => { + const messages = Array.from({ length: 10 }, (_, i) => ({ id: `m-${i}` })) + const { setMessages, scrollToFn } = createChatVirtualizer({ + messages, + offset: 200, + }) + const older = Array.from({ length: 4 }, (_, i) => ({ id: `p-${i}` })) + + setMessages([...older, ...messages.filter((m) => m.id !== 'm-4')]) + + expect(scrollToFn).toHaveBeenCalledTimes(1) + expect(scrollToFn.mock.calls[0]![0]).toBe(350) +}) + +test('anchorTo:end falls back to an earlier listed item when everything after the anchor is removed', () => { + const messages = Array.from({ length: 10 }, (_, i) => ({ id: `m-${i}` })) + const { setMessages, scrollToFn } = createChatVirtualizer({ + messages, + offset: 200, + }) + const older = Array.from({ length: 4 }, (_, i) => ({ id: `p-${i}` })) + const newer = Array.from({ length: 6 }, (_, i) => ({ id: `n-${i}` })) + + setMessages([...older, ...messages.slice(0, 4), ...newer]) + + expect(scrollToFn).toHaveBeenCalledTimes(1) + expect(scrollToFn.mock.calls[0]![0]).toBe(400) +}) + +test('anchorTo:end keeps the nearer listed item in place when rows on screen fold into a new row', () => { + const messages = Array.from({ length: 20 }, (_, i) => ({ id: `m-${i}` })) + const { setMessages, scrollToFn } = createChatVirtualizer({ + messages, + offset: 200, + }) + const older = Array.from({ length: 4 }, (_, i) => ({ id: `p-${i}` })) + + setMessages([ + ...older, + ...messages.slice(0, 4), + { id: 'summary' }, + ...messages.slice(13), + ]) + + expect(scrollToFn).toHaveBeenCalledTimes(1) + expect(scrollToFn.mock.calls[0]![0]).toBe(400) +}) + test('anchorTo:end does not yank a scrolled-up user when items append', () => { const messages = Array.from({ length: 8 }, (_, i) => ({ id: `m-${i}` })) const { setMessages, scrollToFn } = createChatVirtualizer({