From cbc2d664f57f7d94ee84461df1f100f815ca1ca2 Mon Sep 17 00:00:00 2001 From: Damian Pieczynski Date: Sun, 29 Nov 2026 13:12:55 +0100 Subject: [PATCH 1/8] fix(virtual-core): land a smooth command aimed before a wheel hold A wheel doesn't stop a smooth scroll, so a command written before the wheel held a correction travels to where it lands that far from its target and never sees it. When the wheel hold ends, write the hold and the command again from there. Touch and bounce holds keep the wait. Fixes #1406 --- .changeset/smooth-command-wheel-hold.md | 5 + packages/virtual-core/src/index.ts | 41 +++++++-- .../virtual-core/tests/wheel-hold.test.ts | 92 +++++++++++++++++++ 3 files changed, 130 insertions(+), 8 deletions(-) create mode 100644 .changeset/smooth-command-wheel-hold.md diff --git a/.changeset/smooth-command-wheel-hold.md b/.changeset/smooth-command-wheel-hold.md new file mode 100644 index 00000000..d2b69813 --- /dev/null +++ b/.changeset/smooth-command-wheel-hold.md @@ -0,0 +1,5 @@ +--- +'@tanstack/virtual-core': patch +--- + +Fix a smooth `scrollToIndex` or `scrollToOffset` in WebKit stopping short of its target when rows are prepended or measured during wheel input while it travels. The held correction is now written when the wheel rests, and the scroll continues to its target. diff --git a/packages/virtual-core/src/index.ts b/packages/virtual-core/src/index.ts index 033b9fb9..a66deacf 100644 --- a/packages/virtual-core/src/index.ts +++ b/packages/virtual-core/src/index.ts @@ -427,6 +427,9 @@ type ScrollState = { // settling stableFrames: number phase: ScrollPhase + // The hold the last smooth write was aimed with: its animation takes the + // element to `lastTargetOffset - aimedHold` (`_scrollToOffset`). + aimedHold?: number // A `queued` command's view of the fling it waits for: the scroll events // since it was queued, at most the last 200ms (`flingSpedUp`, @@ -1320,7 +1323,7 @@ export class Virtualizer< if (this._wheelTimer !== null) win.clearTimeout(this._wheelTimer) this._wheelTimer = win.setTimeout(() => { this._wheelTimer = null - this._flushHoldIfReady(true) + this._flushHoldIfReady(true, true) this._retryClampedAdjustment() }, 150) // At the top the element stops moving, so no scroll event comes. @@ -1439,11 +1442,16 @@ export class Virtualizer< } // Write the command that `_startScroll` left waiting for the momentum - // tail, against the layout as it is now (rows measured during the fling - // have moved an index target). + // tail. private _writeAwaitingCommand = () => { const state = this.scrollState if (state === null || state.phase !== 'queued') return + this._writeCommand(state) + } + + // Write a command again from where the element is, against the layout as + // it is now (rows measured during a fling have moved an index target). + private _writeCommand = (state: ScrollState) => { const { behavior } = state this.getMeasurements() const offset = this.getScrollStateTarget(state) @@ -1543,7 +1551,7 @@ export class Virtualizer< if (this._wheelTimer === null || !this._holdAtTop()) return win.clearTimeout(this._wheelTimer) this._wheelTimer = null - this._flushHoldIfReady(true) + this._flushHoldIfReady(true, true) this._retryClampedAdjustment() }) }) @@ -1559,12 +1567,24 @@ export class Virtualizer< // (`_willUpdate`), where an update it schedules commits before paint // anyway. A command's start passes true, although `_willUpdate` can reach // it too (a follow on append). - private _flushHoldIfReady = (sync: boolean) => { + private _flushHoldIfReady = (sync: boolean, wheelEnded = false) => { if (this._holdOffset === 0) return if (!this.scrollElement || !this.options.enabled) return if (!this._canWriteScroll()) return - // Let a travelling smooth scroll land first (see `_willUpdate`). - if (this.scrollState?.phase === 'smooth') return + // Let a travelling smooth scroll land first (see `_willUpdate`). Not as + // the wheel hold ends: a wheel does not stop the animation (Playwright + // WebKit and Chromium), and one aimed before the hold changed lands that + // change away from its target, never sees the target, and would keep + // the hold until the safety valve (#1406). Write the hold, which stops + // the animation, and the command again from there. Other holds keep the + // wait: a touch stops the animation, and the command written again would + // pull the user back. + const state = this.scrollState + const reaim = + wheelEnded && + state?.phase === 'smooth' && + state.aimedHold !== this._holdOffset + if (state?.phase === 'smooth' && !reaim) return this._holdOffset = 0 // A held shrink can leave the layout position above the top; the // element clamps that to 0 without an event to say so (#1229). @@ -1581,6 +1601,7 @@ export class Virtualizer< // (#1258). this._clampedAdjustment = this._clampedBy(target) this._scrollToOffset(target) + if (reaim) this._writeCommand(state) } private rafId: number | null = null @@ -2896,7 +2917,11 @@ export class Virtualizer< } this.pendingWrite = write // The element sits `_holdOffset` behind the layout while a correction - // is held; `target` and the read-back stay in layout coordinates. + // is held; `target` and the read-back stay in layout coordinates. Only + // a command writes smooth. + if (write.smooth && this.scrollState !== null) { + this.scrollState.aimedHold = this._holdOffset + } this.options.scrollToFn( offset - this._holdOffset, { behavior, adjustments }, diff --git a/packages/virtual-core/tests/wheel-hold.test.ts b/packages/virtual-core/tests/wheel-hold.test.ts index eadc41ac..1821b8ed 100644 --- a/packages/virtual-core/tests/wheel-hold.test.ts +++ b/packages/virtual-core/tests/wheel-hold.test.ts @@ -229,6 +229,98 @@ for (const behavior of ['auto', 'smooth'] as const) { }) } +test('WebKit (#1406): a hold made while a smooth command travels is written, and the command lands', () => { + withUserAgent(SAFARI, () => { + const h = endAnchoredAt2000() + // A smooth write leaves the element where it is; the test moves it + // along the way as the animation would. + h.v.setOptions({ + ...h.v.options, + scrollToFn: (offset, { adjustments = 0, behavior }) => { + h.writes.push(offset + adjustments) + if (behavior !== 'smooth') { + h.el.scrollTop = Math.max(0, Math.min(offset + adjustments, 4800)) + } + }, + }) + h.v.scrollToIndex(0, { align: 'start', behavior: 'smooth' }) + expect(h.writes).toEqual([0]) + h.scroll(true, 1500) + h.frame() + + h.el._dispatch('wheel') + prepend(h) + expect(h.v['_holdOffset']).toBe(600) + + // The animation travels on to where it was aimed, row 0 600px short. + h.scroll(true, 1000) + h.frame() + // Where the reader sees row 34, as exposed. + const shown = () => + h.v.getVirtualItems().find((item) => item.index === 34)!.start - + h.el.scrollTop + const top = shown() + h.writes.length = 0 + + // The wheel rests: the hold is written where the reader is, which stops + // the animation, and the command is written again from there. + h.expireTail() + expect(h.writes).toEqual([1600, 0]) + expect(shown()).toBe(top) + for (const offset of [800, 0]) { + h.scroll(true, offset) + h.frame() + } + h.scroll(false) + for (let i = 0; i < 5; i++) h.frame() + + expect(h.v['_holdOffset']).toBe(0) + expect(h.el.scrollTop).toBe(0) + expect(h.rowTop(0)).toBe(0) + expect(h.v.scrollState).toBeNull() + }) +}) + +test('WebKit (#1406): a smooth command aimed before a wheel hold lands when the element reaches the top under the wheel', () => { + withUserAgent(SAFARI, () => { + const h = endAnchoredAt2000() + h.v.setOptions({ + ...h.v.options, + scrollToFn: (offset, { adjustments = 0, behavior }) => { + h.writes.push(offset + adjustments) + if (behavior !== 'smooth') { + h.el.scrollTop = Math.max(0, Math.min(offset + adjustments, 4800)) + } + }, + }) + h.v.scrollToIndex(0, { align: 'start', behavior: 'smooth' }) + h.scroll(true, 1500) + h.frame() + h.el._dispatch('wheel') + prepend(h) + h.writes.length = 0 + + // The animation reaches the top, the layout 600px short of row 0. + h.el._dispatch('wheel') + h.scroll(true, 0) + h.frame() + expect(h.writes).toEqual([]) + h.frame() + expect(h.writes).toEqual([600, 0]) + for (const offset of [300, 0]) { + h.scroll(true, offset) + h.frame() + } + h.scroll(false) + for (let i = 0; i < 5; i++) h.frame() + + expect(h.v['_holdOffset']).toBe(0) + expect(h.el.scrollTop).toBe(0) + expect(h.rowTop(0)).toBe(0) + expect(h.v.scrollState).toBeNull() + }) +}) + test('WebKit: a correction with no wheel input is written at once', () => { withUserAgent(SAFARI, () => { const h = endAnchoredAt2000() From 80015b6d93652ccc3c7408753b96157e009b1ffe Mon Sep 17 00:00:00 2001 From: Damian Pieczynski Date: Sun, 29 Nov 2026 14:20:04 +0100 Subject: [PATCH 2/8] fix(virtual-core): don't rewrite a command replaced during the hold's notify --- packages/virtual-core/src/index.ts | 3 +- .../virtual-core/tests/wheel-hold.test.ts | 38 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/packages/virtual-core/src/index.ts b/packages/virtual-core/src/index.ts index a66deacf..2e338206 100644 --- a/packages/virtual-core/src/index.ts +++ b/packages/virtual-core/src/index.ts @@ -1601,7 +1601,8 @@ export class Virtualizer< // (#1258). this._clampedAdjustment = this._clampedBy(target) this._scrollToOffset(target) - if (reaim) this._writeCommand(state) + // Not if `onChange` started another command during the notify. + if (reaim && this.scrollState === state) this._writeCommand(state) } private rafId: number | null = null diff --git a/packages/virtual-core/tests/wheel-hold.test.ts b/packages/virtual-core/tests/wheel-hold.test.ts index 1821b8ed..e3e909cd 100644 --- a/packages/virtual-core/tests/wheel-hold.test.ts +++ b/packages/virtual-core/tests/wheel-hold.test.ts @@ -321,6 +321,44 @@ test('WebKit (#1406): a smooth command aimed before a wheel hold lands when the }) }) +test("WebKit (#1406): a command started from the flush's onChange is not overwritten by the one it replaced", () => { + withUserAgent(SAFARI, () => { + const h = endAnchoredAt2000() + let held = false + let started = false + h.v.setOptions({ + ...h.v.options, + scrollToFn: (offset, { adjustments = 0, behavior }) => { + h.writes.push(offset + adjustments) + if (behavior !== 'smooth') { + h.el.scrollTop = Math.max(0, Math.min(offset + adjustments, 4800)) + } + }, + // The consumer starts a new command while the hold is written. + onChange: (v) => { + if (v['_holdOffset'] !== 0) held = true + else if (held && !started) { + started = true + v.scrollToOffset(3000) + } + }, + }) + h.v.scrollToIndex(0, { align: 'start', behavior: 'smooth' }) + h.scroll(true, 1500) + h.frame() + h.el._dispatch('wheel') + prepend(h) + h.scroll(true, 1000) + h.frame() + h.writes.length = 0 + + h.expireTail() + expect(started).toBe(true) + expect(h.writes.at(-1)).toBe(3000) + expect(h.v.scrollState?.index).toBeNull() + }) +}) + test('WebKit: a correction with no wheel input is written at once', () => { withUserAgent(SAFARI, () => { const h = endAnchoredAt2000() From 2fb9100224514d1290ab993d73fc48c8a21914ec Mon Sep 17 00:00:00 2001 From: Damian Pieczynski Date: Sun, 29 Nov 2026 15:24:44 +0100 Subject: [PATCH 3/8] fix(virtual-core): don't write the hold over a command started in its notify --- packages/virtual-core/src/index.ts | 5 +- .../virtual-core/tests/wheel-hold.test.ts | 79 +++++++++++-------- 2 files changed, 48 insertions(+), 36 deletions(-) diff --git a/packages/virtual-core/src/index.ts b/packages/virtual-core/src/index.ts index 2e338206..af356544 100644 --- a/packages/virtual-core/src/index.ts +++ b/packages/virtual-core/src/index.ts @@ -1592,6 +1592,9 @@ export class Virtualizer< this.scrollOffset = 0 } this.notify(sync) + // `onChange` started another command during the notify: it has written + // its target, and a write here would stop it if it is smooth. + if (this.scrollState !== null && this.scrollState !== state) return // A command whose write has not been reported back yet owns the target: // `scrollOffset` is still the pre-command position until its read-back, // and a command that closes the touch tail makes a write possible with @@ -1601,7 +1604,7 @@ export class Virtualizer< // (#1258). this._clampedAdjustment = this._clampedBy(target) this._scrollToOffset(target) - // Not if `onChange` started another command during the notify. + // Not if `onChange` cancelled the command during the notify. if (reaim && this.scrollState === state) this._writeCommand(state) } diff --git a/packages/virtual-core/tests/wheel-hold.test.ts b/packages/virtual-core/tests/wheel-hold.test.ts index e3e909cd..8229c3c2 100644 --- a/packages/virtual-core/tests/wheel-hold.test.ts +++ b/packages/virtual-core/tests/wheel-hold.test.ts @@ -321,43 +321,52 @@ test('WebKit (#1406): a smooth command aimed before a wheel hold lands when the }) }) -test("WebKit (#1406): a command started from the flush's onChange is not overwritten by the one it replaced", () => { - withUserAgent(SAFARI, () => { - const h = endAnchoredAt2000() - let held = false - let started = false - h.v.setOptions({ - ...h.v.options, - scrollToFn: (offset, { adjustments = 0, behavior }) => { - h.writes.push(offset + adjustments) - if (behavior !== 'smooth') { - h.el.scrollTop = Math.max(0, Math.min(offset + adjustments, 4800)) - } - }, - // The consumer starts a new command while the hold is written. - onChange: (v) => { - if (v['_holdOffset'] !== 0) held = true - else if (held && !started) { - started = true - v.scrollToOffset(3000) - } - }, +for (const behavior of ['auto', 'smooth'] as const) { + test(`WebKit (#1406): a command (${behavior}) started from the flush's onChange is not overwritten`, () => { + withUserAgent(SAFARI, () => { + const h = endAnchoredAt2000() + let held = false + let started = false + const calls: Array<[number, ScrollBehavior | undefined]> = [] + h.v.setOptions({ + ...h.v.options, + scrollToFn: (offset, { adjustments = 0, behavior: b }) => { + h.writes.push(offset + adjustments) + calls.push([offset + adjustments, b]) + if (b !== 'smooth') { + h.el.scrollTop = Math.max(0, Math.min(offset + adjustments, 4800)) + } + }, + // The consumer starts a new command while the hold is written. + onChange: (v) => { + if (v['_holdOffset'] !== 0) held = true + else if (held && !started) { + started = true + v.scrollToOffset(3000, { behavior }) + } + }, + }) + h.v.scrollToIndex(0, { align: 'start', behavior: 'smooth' }) + h.scroll(true, 1500) + h.frame() + h.el._dispatch('wheel') + prepend(h) + h.scroll(true, 1000) + h.frame() + calls.length = 0 + + h.expireTail() + expect(started).toBe(true) + // Nothing is written after the new command. + expect(calls.at(-1)).toEqual([3000, behavior]) + if (behavior === 'smooth') h.scroll(true, 3000) + h.scroll(false, 3000) + for (let i = 0; i < 5; i++) h.frame() + expect(h.v.scrollOffset).toBe(3000) + expect(h.v.scrollState).toBeNull() }) - h.v.scrollToIndex(0, { align: 'start', behavior: 'smooth' }) - h.scroll(true, 1500) - h.frame() - h.el._dispatch('wheel') - prepend(h) - h.scroll(true, 1000) - h.frame() - h.writes.length = 0 - - h.expireTail() - expect(started).toBe(true) - expect(h.writes.at(-1)).toBe(3000) - expect(h.v.scrollState?.index).toBeNull() }) -}) +} test('WebKit: a correction with no wheel input is written at once', () => { withUserAgent(SAFARI, () => { From 281a58f023ebf2ce9e773caccc2f7dc7c7dc62cd Mon Sep 17 00:00:00 2001 From: Damian Pieczynski Date: Sun, 29 Nov 2026 15:35:12 +0100 Subject: [PATCH 4/8] fix(virtual-core): re-aim any smooth command when the wheel hold ends A smooth retarget under the wheel hold set aimedHold to the hold, so the command was not written again and stayed stuck as in #1406. Drop aimedHold: at the wheel's end a travelling smooth command is always written again. Add a test for a command cancelled in the flush's notify. --- packages/virtual-core/src/index.ts | 26 ++----- .../virtual-core/tests/wheel-hold.test.ts | 76 +++++++++++++++++++ 2 files changed, 84 insertions(+), 18 deletions(-) diff --git a/packages/virtual-core/src/index.ts b/packages/virtual-core/src/index.ts index af356544..957fa345 100644 --- a/packages/virtual-core/src/index.ts +++ b/packages/virtual-core/src/index.ts @@ -427,9 +427,6 @@ type ScrollState = { // settling stableFrames: number phase: ScrollPhase - // The hold the last smooth write was aimed with: its animation takes the - // element to `lastTargetOffset - aimedHold` (`_scrollToOffset`). - aimedHold?: number // A `queued` command's view of the fling it waits for: the scroll events // since it was queued, at most the last 200ms (`flingSpedUp`, @@ -1573,17 +1570,14 @@ export class Virtualizer< if (!this._canWriteScroll()) return // Let a travelling smooth scroll land first (see `_willUpdate`). Not as // the wheel hold ends: a wheel does not stop the animation (Playwright - // WebKit and Chromium), and one aimed before the hold changed lands that - // change away from its target, never sees the target, and would keep - // the hold until the safety valve (#1406). Write the hold, which stops - // the animation, and the command again from there. Other holds keep the - // wait: a touch stops the animation, and the command written again would - // pull the user back. + // WebKit and Chromium), and one written while the wheel held a + // correction was aimed the hold short, or clamped at the top. It would + // never see its target and would keep the hold until the safety valve + // (#1406). Write the hold, which stops the animation, and the command + // again from there. Other holds keep the wait: a touch stops the + // animation, and the command written again would pull the user back. const state = this.scrollState - const reaim = - wheelEnded && - state?.phase === 'smooth' && - state.aimedHold !== this._holdOffset + const reaim = wheelEnded && state?.phase === 'smooth' if (state?.phase === 'smooth' && !reaim) return this._holdOffset = 0 // A held shrink can leave the layout position above the top; the @@ -2921,11 +2915,7 @@ export class Virtualizer< } this.pendingWrite = write // The element sits `_holdOffset` behind the layout while a correction - // is held; `target` and the read-back stay in layout coordinates. Only - // a command writes smooth. - if (write.smooth && this.scrollState !== null) { - this.scrollState.aimedHold = this._holdOffset - } + // is held; `target` and the read-back stay in layout coordinates. this.options.scrollToFn( offset - this._holdOffset, { behavior, adjustments }, diff --git a/packages/virtual-core/tests/wheel-hold.test.ts b/packages/virtual-core/tests/wheel-hold.test.ts index 8229c3c2..80199d8e 100644 --- a/packages/virtual-core/tests/wheel-hold.test.ts +++ b/packages/virtual-core/tests/wheel-hold.test.ts @@ -321,6 +321,48 @@ test('WebKit (#1406): a smooth command aimed before a wheel hold lands when the }) }) +test('WebKit (#1406): a smooth command retargeted under the wheel hold lands', () => { + withUserAgent(SAFARI, () => { + const h = endAnchoredAt2000() + h.v.setOptions({ + ...h.v.options, + scrollToFn: (offset, { adjustments = 0, behavior }) => { + h.writes.push(offset + adjustments) + if (behavior !== 'smooth') { + h.el.scrollTop = Math.max(0, Math.min(offset + adjustments, 4800)) + } + }, + }) + h.v.scrollToIndex(5, { align: 'start', behavior: 'smooth' }) + h.scroll(true, 1500) + h.frame() + h.el._dispatch('wheel') + prepend(h) + expect(h.v['_holdOffset']).toBe(600) + + // A row above the target is measured: reconcile retargets the smooth + // command under the hold, past what the element can reach. + h.v.resizeItem(2, 70) + h.frame() + h.scroll(true, 1000) + h.frame() + + // The animation travels to the last write. + h.expireTail() + h.scroll(true, Math.max(0, h.writes.at(-1)!)) + h.frame() + h.scroll(false) + for (let i = 0; i < 10; i++) { + h.frame() + h.scroll(false) + } + + expect(h.v['_holdOffset']).toBe(0) + expect(h.rowTop(5)).toBe(0) + expect(h.v.scrollState).toBeNull() + }) +}) + for (const behavior of ['auto', 'smooth'] as const) { test(`WebKit (#1406): a command (${behavior}) started from the flush's onChange is not overwritten`, () => { withUserAgent(SAFARI, () => { @@ -368,6 +410,40 @@ for (const behavior of ['auto', 'smooth'] as const) { }) } +test("WebKit (#1406): a command cancelled from the flush's onChange is not written again", () => { + withUserAgent(SAFARI, () => { + const h = endAnchoredAt2000() + let held = false + h.v.setOptions({ + ...h.v.options, + scrollToFn: (offset, { adjustments = 0, behavior }) => { + h.writes.push(offset + adjustments) + if (behavior !== 'smooth') { + h.el.scrollTop = Math.max(0, Math.min(offset + adjustments, 4800)) + } + }, + onChange: (v) => { + if (v['_holdOffset'] !== 0) held = true + else if (held) v.cancelScroll() + }, + }) + h.v.scrollToIndex(0, { align: 'start', behavior: 'smooth' }) + h.scroll(true, 1500) + h.frame() + h.el._dispatch('wheel') + prepend(h) + h.scroll(true, 1000) + h.frame() + h.writes.length = 0 + + // The hold is written where the reader is, and nothing after it. + h.expireTail() + expect(h.writes).toEqual([1600]) + expect(h.v['_holdOffset']).toBe(0) + expect(h.v.scrollState).toBeNull() + }) +}) + test('WebKit: a correction with no wheel input is written at once', () => { withUserAgent(SAFARI, () => { const h = endAnchoredAt2000() From 95507967eaade6e90cc5b848e48402972e5e3ae1 Mon Sep 17 00:00:00 2001 From: Damian Pieczynski Date: Sun, 29 Nov 2026 17:41:34 +0100 Subject: [PATCH 5/8] fix(virtual-core): clamp a command written again to the element's current max A consumer that commits the sizer after a hold's flush leaves the element its old max, and reconcile does not write an unchanged offset target again. Clamp the target in _writeCommand, as for a command issued now. Moves anchorEnd/ios27/54: the queued command now lands at the max and the end anchor follows the list's end, instead of staying in flight. --- packages/virtual-core/src/index.ts | 10 +++- .../__snapshots__/scroll-trace.test.ts.snap | 2 +- .../virtual-core/tests/wheel-hold.test.ts | 46 +++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/packages/virtual-core/src/index.ts b/packages/virtual-core/src/index.ts index 957fa345..dfccdb9a 100644 --- a/packages/virtual-core/src/index.ts +++ b/packages/virtual-core/src/index.ts @@ -1451,7 +1451,15 @@ export class Virtualizer< private _writeCommand = (state: ScrollState) => { const { behavior } = state this.getMeasurements() - const offset = this.getScrollStateTarget(state) + // Clamped to the max the element has now, as for a command issued now. + // After a hold's flush, a consumer that commits the sizer later leaves + // the element its old max, and reconcile does not write an unchanged + // offset target again. An index or end target is already clamped, and + // reconcile retargets it once the sizer grows. + const offset = Math.min( + this.getScrollStateTarget(state), + Math.max(this.getMaxScrollOffset(), 0), + ) state.lastTargetOffset = offset // The hold flushed just before can leave a clamp retry (#1258); it // would write its old target over this command. diff --git a/packages/virtual-core/tests/__snapshots__/scroll-trace.test.ts.snap b/packages/virtual-core/tests/__snapshots__/scroll-trace.test.ts.snap index 4ff59755..394b92e1 100644 --- a/packages/virtual-core/tests/__snapshots__/scroll-trace.test.ts.snap +++ b/packages/virtual-core/tests/__snapshots__/scroll-trace.test.ts.snap @@ -18,7 +18,7 @@ exports[`scroll behaviour snapshot > anchorEnd ios27 1`] = ` "20: 4c1a00c0 5b17cebe 39f53c48 43b3722d 4c85db64 0d41dbae 319823eb 0cd61d7a ebd8a220 09a1874e", "30: 0224b245 2a263abe beba95dc 6e007e5d 3ab04872 8000f929 a8da8fd8 4acd8bac 3c2c8710 93ac03b2", "40: 351e701d 81b41eab 7e4c0d8a e8e6cf89 0018ede1 ee1edfdf 49ca9103 334646c6 3e95ee0d 8f0a8fe9", - "50: ca6b1fc6 8cc11ea8 90e19d07 7c8d21a0 11d479bb adc56879 e06a8ddb e46aa4df e9fb7a13 a865bfd7", + "50: ca6b1fc6 8cc11ea8 90e19d07 7c8d21a0 13ccf8d1 adc56879 e06a8ddb e46aa4df e9fb7a13 a865bfd7", ] `; diff --git a/packages/virtual-core/tests/wheel-hold.test.ts b/packages/virtual-core/tests/wheel-hold.test.ts index 80199d8e..942ca33b 100644 --- a/packages/virtual-core/tests/wheel-hold.test.ts +++ b/packages/virtual-core/tests/wheel-hold.test.ts @@ -410,6 +410,52 @@ for (const behavior of ['auto', 'smooth'] as const) { }) } +test('WebKit (#1406): a re-aimed scrollToOffset ends at the max a late sizer leaves', () => { + withUserAgent(SAFARI, () => { + const h = endAnchoredAt2000() + // The consumer commits the sizer after the notify, not inside `onChange`: + // scrollHeight only moves on commit(). + const max = () => h.el.scrollHeight - h.el.clientHeight + const commit = () => { + h.el.scrollHeight = h.v.getTotalSize() + } + h.v.setOptions({ + ...h.v.options, + scrollToFn: (offset, { adjustments = 0, behavior }) => { + h.writes.push(offset + adjustments) + if (behavior !== 'smooth') { + h.el.scrollTop = Math.max(0, Math.min(offset + adjustments, max())) + } + }, + }) + h.v.scrollToOffset(4700, { behavior: 'smooth' }) + h.scroll(true, 3000) + h.frame() + h.el._dispatch('wheel') + prepend(h) + commit() + h.scroll(true, 3500) + h.frame() + h.writes.length = 0 + + // The hold is written and the command again, before the sizer has grown + // by the hold: the command is written at the max the element has. + h.expireTail() + expect(h.writes).toEqual([4100, 4800]) + h.scroll(true, 4800) + commit() + h.frame() + h.scroll(false) + for (let i = 0; i < 10; i++) { + h.frame() + h.scroll(false) + } + + expect(h.el.scrollTop).toBe(4800) + expect(h.v.scrollState).toBeNull() + }) +}) + test("WebKit (#1406): a command cancelled from the flush's onChange is not written again", () => { withUserAgent(SAFARI, () => { const h = endAnchoredAt2000() From cef9ee2baa75c68a45862a2bb2a50f5f7c4c2714 Mon Sep 17 00:00:00 2001 From: Damian Pieczynski Date: Sun, 29 Nov 2026 18:02:24 +0100 Subject: [PATCH 6/8] test: cover the wheel hold behind a smooth command in WebKit Unit tests for two wheel bursts in one smooth travel and for the wheel hold ending in a bounce at the top. A Playwright spec runs the #1406 case in WebKit with a real mouse wheel, so CI installs WebKit too. --- .github/workflows/pr.yml | 2 +- .github/workflows/release.yml | 2 +- .../e2e/app/test/wheel-smooth-prepend.spec.ts | 73 ++++++++++++++++++ .../virtual-core/tests/wheel-hold.test.ts | 74 +++++++++++++++++++ 4 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 packages/react-virtual/e2e/app/test/wheel-smooth-prepend.spec.ts diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 7f07889e..bf8d0a8b 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -30,7 +30,7 @@ jobs: with: main-branch-name: main - name: Install Playwright browsers - run: pnpm exec playwright install chromium + run: pnpm exec playwright install --with-deps chromium webkit - name: Run Checks run: pnpm run test:pr preview: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index cb4ed315..18ebfc1d 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -32,7 +32,7 @@ jobs: - name: Setup Tools uses: tanstack/config/.github/setup@e4b48f16568324f76f467aa4c2aac2f05db632c3 # main - name: Install Playwright browsers - run: pnpm exec playwright install chromium + run: pnpm exec playwright install --with-deps chromium webkit - name: Run Tests run: pnpm run test:ci - name: Run Changesets (version or publish) diff --git a/packages/react-virtual/e2e/app/test/wheel-smooth-prepend.spec.ts b/packages/react-virtual/e2e/app/test/wheel-smooth-prepend.spec.ts new file mode 100644 index 00000000..6d0a9ab6 --- /dev/null +++ b/packages/react-virtual/e2e/app/test/wheel-smooth-prepend.spec.ts @@ -0,0 +1,73 @@ +import { expect, test } from '@playwright/test' +import type { Page } from '@playwright/test' + +// The wheel hold is WebKit only (#1400), so this runs in WebKit. +test.use({ browserName: 'webkit' }) + +const scrollTop = (page: Page) => + page.evaluate(() => document.querySelector('#scroll-container')!.scrollTop) + +// Where row 0 is drawn, relative to the top of the viewport. +const rowZeroTop = (page: Page) => + page.evaluate(() => { + const container = document.querySelector('#scroll-container')! + const row = container.querySelector('[data-index="0"]') + if (!row) return null + return ( + row.getBoundingClientRect().top - + container.getBoundingClientRect().top - + container.clientTop + ) + }) + +// #1406: a smooth scrollToIndex(0) travels, a wheel event comes in, and rows +// are prepended within the wheel's 150ms, so the anchor is held. A real wheel +// does not stop the smooth animation in WebKit, so the element reaches 0 +// while the layout is the held 250px away. The command must still land on +// the new row 0, with the hold written. +test('a prepend held under the wheel does not strand a smooth scrollToIndex', async ({ + page, +}) => { + await page.goto('/smooth-prepend/') + await expect + .poll(() => + page.evaluate(() => { + const c = document.querySelector('#scroll-container')! + return Math.abs(c.scrollHeight - c.scrollTop - c.clientHeight) + }), + ) + .toBeLessThan(1.01) + + // Prepend from inside the wheel event, after the virtualizer's own wheel + // listener: the hold is made inside the wheel window however fast the + // animation is. Record where the animation was. + await page.evaluate(() => { + const container = document.querySelector('#scroll-container')! + container.addEventListener( + 'wheel', + () => { + ;(window as any).prependedAt = container.scrollTop + document.querySelector('#prepend')!.click() + }, + { once: true }, + ) + }) + const box = (await page.locator('#scroll-container').boundingBox())! + await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2) + + await page.evaluate(() => + document.querySelector('#smooth-to-0')!.click(), + ) + await page.mouse.wheel(0, -40) + + // The prepend came while the command was still travelling, not after it. + const prependedAt = await page.evaluate(() => (window as any).prependedAt) + expect(prependedAt).toBeGreaterThan(500) + + await expect.poll(() => rowZeroTop(page), { timeout: 3000 }).toBe(0) + expect(await scrollTop(page)).toBeLessThan(1.01) + await expect(page.locator('[data-index="0"]')).toHaveAttribute( + 'data-message-id', + 'm--5', + ) +}) diff --git a/packages/virtual-core/tests/wheel-hold.test.ts b/packages/virtual-core/tests/wheel-hold.test.ts index 942ca33b..30894f68 100644 --- a/packages/virtual-core/tests/wheel-hold.test.ts +++ b/packages/virtual-core/tests/wheel-hold.test.ts @@ -456,6 +456,80 @@ test('WebKit (#1406): a re-aimed scrollToOffset ends at the max a late sizer lea }) }) +// A smooth write leaves the element where it is; a test moves it along the +// way as the animation would. An instant write clamps to the range. +function animateSmooth(h: ReturnType) { + h.v.setOptions({ + ...h.v.options, + scrollToFn: (offset, { adjustments = 0, behavior }) => { + h.writes.push(offset + adjustments) + if (behavior !== 'smooth') { + h.el.scrollTop = Math.max(0, Math.min(offset + adjustments, 4800)) + } + }, + }) +} + +test('WebKit (#1406): a smooth command is aimed again after each wheel burst', () => { + withUserAgent(SAFARI, () => { + const h = endAnchoredAt2000() + animateSmooth(h) + h.v.scrollToIndex(0, { align: 'start', behavior: 'smooth' }) + h.scroll(true, 1500) + h.frame() + + h.el._dispatch('wheel') + prepend(h) + h.scroll(true, 1000) + h.frame() + h.writes.length = 0 + h.expireTail() + expect(h.writes).toEqual([1600, 0]) + + // A second burst while the command travels again. + h.scroll(true, 1200) + h.frame() + h.el._dispatch('wheel') + h.v.setOptions({ ...h.v.options, count: 124, getItemKey: (i) => i - 24 }) + h.v._willUpdate() + expect(h.v['_holdOffset']).toBe(600) + h.scroll(true, 800) + h.frame() + h.writes.length = 0 + h.expireTail() + expect(h.writes).toEqual([1400, 0]) + + h.scroll(true, 0) + h.frame() + h.scroll(false) + for (let i = 0; i < 5; i++) h.frame() + expect(h.v['_holdOffset']).toBe(0) + expect(h.rowTop(0)).toBe(0) + expect(h.v.scrollState).toBeNull() + }) +}) + +test('WebKit (#1406): the wheel hold ending in a bounce at the top writes nothing', () => { + withUserAgent(SAFARI, () => { + const h = endAnchoredAt2000() + animateSmooth(h) + h.v.scrollToIndex(0, { align: 'start', behavior: 'smooth' }) + h.scroll(true, 1500) + h.frame() + h.el._dispatch('wheel') + prepend(h) + h.writes.length = 0 + + // The element bounces past the top: WebKit would drop a write. + h.scroll(true, -20) + h.frame() + h.frame() + h.expireTail() + expect(h.writes).toEqual([]) + expect(h.v['_holdOffset']).toBe(600) + }) +}) + test("WebKit (#1406): a command cancelled from the flush's onChange is not written again", () => { withUserAgent(SAFARI, () => { const h = endAnchoredAt2000() From 6d142e6b1a8f802b93556eb6f7d5b059c18ebe7c Mon Sep 17 00:00:00 2001 From: Damian Pieczynski Date: Sun, 29 Nov 2026 18:08:04 +0100 Subject: [PATCH 7/8] fix(virtual-core): clamp a command written again to 0 as well A trim above the viewport under the wheel hold can move an offset target below 0. Clamp it to the element's range like getOffsetForAlignment, so the re-aimed command lands at the top and retires. --- packages/virtual-core/src/index.ts | 20 ++++++++----- .../virtual-core/tests/wheel-hold.test.ts | 29 +++++++++++++++++++ 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/packages/virtual-core/src/index.ts b/packages/virtual-core/src/index.ts index dfccdb9a..94de0d99 100644 --- a/packages/virtual-core/src/index.ts +++ b/packages/virtual-core/src/index.ts @@ -1451,14 +1451,18 @@ export class Virtualizer< private _writeCommand = (state: ScrollState) => { const { behavior } = state this.getMeasurements() - // Clamped to the max the element has now, as for a command issued now. - // After a hold's flush, a consumer that commits the sizer later leaves - // the element its old max, and reconcile does not write an unchanged - // offset target again. An index or end target is already clamped, and - // reconcile retargets it once the sizer grows. - const offset = Math.min( - this.getScrollStateTarget(state), - Math.max(this.getMaxScrollOffset(), 0), + // Clamped to the range the element has now, as for a command issued + // now (`getOffsetForAlignment`). After a hold's flush, a consumer that + // commits the sizer later leaves the element its old max, and a trim + // above can have moved an offset target below 0; reconcile does not + // write an unchanged offset target again. An index or end target is + // already clamped, and reconcile retargets it once the sizer grows. + const offset = Math.max( + Math.min( + this.getScrollStateTarget(state), + Math.max(this.getMaxScrollOffset(), 0), + ), + 0, ) state.lastTargetOffset = offset // The hold flushed just before can leave a clamp retry (#1258); it diff --git a/packages/virtual-core/tests/wheel-hold.test.ts b/packages/virtual-core/tests/wheel-hold.test.ts index 30894f68..77323441 100644 --- a/packages/virtual-core/tests/wheel-hold.test.ts +++ b/packages/virtual-core/tests/wheel-hold.test.ts @@ -530,6 +530,35 @@ test('WebKit (#1406): the wheel hold ending in a bounce at the top writes nothin }) }) +test('WebKit (#1406): a re-aimed offset target a trim moved below 0 lands at the top', () => { + withUserAgent(SAFARI, () => { + const h = endAnchoredAt2000() + animateSmooth(h) + h.v.scrollToOffset(100, { behavior: 'smooth' }) + h.scroll(true, 1500) + h.frame() + + // 12 rows of 50px above the reader are trimmed under the wheel: the + // anchor is held, and the offset target moves with it to -500. + h.el._dispatch('wheel') + h.v.setOptions({ ...h.v.options, count: 88, getItemKey: (i) => i + 12 }) + h.v._willUpdate() + expect(h.v['_holdOffset']).toBe(-600) + h.scroll(true, 1000) + h.frame() + h.writes.length = 0 + + h.expireTail() + expect(h.writes).toEqual([400, 0]) + h.scroll(true, 0) + h.frame() + h.scroll(false) + for (let i = 0; i < 5; i++) h.frame() + expect(h.el.scrollTop).toBe(0) + expect(h.v.scrollState).toBeNull() + }) +}) + test("WebKit (#1406): a command cancelled from the flush's onChange is not written again", () => { withUserAgent(SAFARI, () => { const h = endAnchoredAt2000() From fa96d8b288c9e876fec5986f3b5821c606ea4fee Mon Sep 17 00:00:00 2001 From: Damian Pieczynski Date: Sun, 29 Nov 2026 18:19:02 +0100 Subject: [PATCH 8/8] test: start the WebKit wheel spec's command inside the wheel event The CI runner's wheel arrived after WebKit's ~250ms smooth animation had ended. Start the command, open the wheel window and prepend all in the one real wheel event, so no round trip has to fit into the animation. --- .../e2e/app/test/wheel-smooth-prepend.spec.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/react-virtual/e2e/app/test/wheel-smooth-prepend.spec.ts b/packages/react-virtual/e2e/app/test/wheel-smooth-prepend.spec.ts index 6d0a9ab6..afd4e658 100644 --- a/packages/react-virtual/e2e/app/test/wheel-smooth-prepend.spec.ts +++ b/packages/react-virtual/e2e/app/test/wheel-smooth-prepend.spec.ts @@ -38,11 +38,18 @@ test('a prepend held under the wheel does not strand a smooth scrollToIndex', as ) .toBeLessThan(1.01) - // Prepend from inside the wheel event, after the virtualizer's own wheel - // listener: the hold is made inside the wheel window however fast the - // animation is. Record where the animation was. + // All of it inside one real wheel event, so no round trip has to fit into + // WebKit's ~250ms animation: a capture listener on the window starts the + // smooth command before the virtualizer's own wheel listener opens the + // wheel window, and a listener after it prepends, which is then held. The + // wheel's own scroll lands while the command travels. await page.evaluate(() => { const container = document.querySelector('#scroll-container')! + window.addEventListener( + 'wheel', + () => document.querySelector('#smooth-to-0')!.click(), + { capture: true, once: true }, + ) container.addEventListener( 'wheel', () => { @@ -54,10 +61,6 @@ test('a prepend held under the wheel does not strand a smooth scrollToIndex', as }) const box = (await page.locator('#scroll-container').boundingBox())! await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2) - - await page.evaluate(() => - document.querySelector('#smooth-to-0')!.click(), - ) await page.mouse.wheel(0, -40) // The prepend came while the command was still travelling, not after it.