From 90d5b541088dae7cce0db2f54e3e1348904b2d46 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Wed, 25 Nov 2026 09:45:31 -0800 Subject: [PATCH 1/2] feat(virtual-core): add deferAdjustmentsDuringWheel option --- .changeset/defer-adjustments-during-wheel.md | 5 +++ docs/api/virtualizer.md | 12 +++++++ packages/virtual-core/src/index.ts | 35 ++++++++++++++++--- .../virtual-core/tests/ios-deferral.test.ts | 23 ++++++++++++ 4 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 .changeset/defer-adjustments-during-wheel.md diff --git a/.changeset/defer-adjustments-during-wheel.md b/.changeset/defer-adjustments-during-wheel.md new file mode 100644 index 000000000..b3807659d --- /dev/null +++ b/.changeset/defer-adjustments-during-wheel.md @@ -0,0 +1,5 @@ +--- +'@tanstack/virtual-core': minor +--- + +Add opt-in `deferAdjustmentsDuringWheel` option to hold scroll adjustments during wheel gestures and flush them once input has been quiet for 150ms. diff --git a/docs/api/virtualizer.md b/docs/api/virtualizer.md index dba8cc240..75138a103 100644 --- a/docs/api/virtualizer.md +++ b/docs/api/virtualizer.md @@ -329,6 +329,18 @@ scrollEndThreshold?: number The pixel threshold used by `isAtEnd()` and `followOnAppend` to decide whether the viewport is close enough to the end to count as pinned. +### `deferAdjustmentsDuringWheel` + +```tsx +deferAdjustmentsDuringWheel?: boolean +``` + +**Default:** `false` + +When enabled, scroll-position adjustments triggered by prepends or above-viewport item resizes are held while `wheel` input is active and flushed once input has been quiet for 150ms. + +This is useful on desktop WebKit, where synchronous `scrollTop` writes during a wheel or trackpad gesture can be overwritten by the browser's scrolling thread. + ### `isScrollingResetDelay` ```tsx diff --git a/packages/virtual-core/src/index.ts b/packages/virtual-core/src/index.ts index 4bd3531db..116e3d29f 100644 --- a/packages/virtual-core/src/index.ts +++ b/packages/virtual-core/src/index.ts @@ -393,6 +393,7 @@ export interface VirtualizerOptions< anchorTo?: ScrollAnchor followOnAppend?: FollowOnAppend scrollEndThreshold?: number + deferAdjustmentsDuringWheel?: boolean isScrollingResetDelay?: number useScrollendEvent?: boolean enabled?: boolean @@ -540,6 +541,7 @@ export class Virtualizer< // `_flushHoldIfReady`: the rows re-render in layout coordinates and the // element moves by the same amount in one paint. private _holdOffset = 0 + private _wheelTimer: number | null = null // Whether the element's last scroll event reported a position past either // edge (the rubber-band bounce). Judged at report time against the // element's own max, with a 1px tolerance for a fractional scrollTop: a @@ -821,6 +823,7 @@ export class Virtualizer< anchorTo: 'start', followOnAppend: false, scrollEndThreshold: 1, + deferAdjustmentsDuringWheel: false, isScrollingResetDelay: 150, enabled: true, isRtl: false, @@ -1087,6 +1090,10 @@ export class Virtualizer< // just removed), deferring every adjustment on the new element until // its next touch cycle. this._holdOffset = 0 + if (this._wheelTimer !== null && this.targetWindow) { + this.targetWindow.clearTimeout(this._wheelTimer) + } + this._wheelTimer = null this._reportedOverscroll = false this._trailing = 0 // (The gesture was ended with its listeners, in the unsubs above.) @@ -1286,9 +1293,27 @@ export class Virtualizer< // Touch event listeners (iOS-aware deferral). if ('addEventListener' in this.scrollElement) { - this.unsubs.push( - this._iosGesture.listen(this.scrollElement as unknown as EventTarget), - ) + const el = this.scrollElement as unknown as EventTarget + this.unsubs.push(this._iosGesture.listen(el)) + const onWheel = () => { + if (!this.options.deferAdjustmentsDuringWheel || !this.targetWindow) { + return + } + if (this._wheelTimer !== null) { + this.targetWindow.clearTimeout(this._wheelTimer) + } + this._wheelTimer = this.targetWindow.setTimeout(() => { + this._wheelTimer = null + const live = this._readElementOffset() + if (live !== null && this._holdOffset !== 0) { + this.scrollOffset = this._toLayoutOffset(live) + } + this._flushHoldIfReady(true) + this._retryClampedAdjustment() + }, 150) + } + el.addEventListener('wheel', onWheel, addEventListenerOptions) + this.unsubs.push(() => el.removeEventListener('wheel', onWheel)) } this._scrollToOffset(this.getScrollOffset()) @@ -1447,7 +1472,9 @@ export class Virtualizer< // A write that is unwanted for another reason (a travelling smooth scroll // owns the position) is a separate check at the call sites. private _canWriteScroll = () => - !(isIOSWebKit() && this._iosGesture.inMotion()) && !this._reportedOverscroll + !(isIOSWebKit() && this._iosGesture.inMotion()) && + !this._reportedOverscroll && + !(this._wheelTimer !== null && this.options.deferAdjustmentsDuringWheel) // Write a held correction once a write is possible, from the scroll // handler, the touch tail timer, `_willUpdate` and a resize notify. The diff --git a/packages/virtual-core/tests/ios-deferral.test.ts b/packages/virtual-core/tests/ios-deferral.test.ts index 75f7ef92f..79d0f6393 100644 --- a/packages/virtual-core/tests/ios-deferral.test.ts +++ b/packages/virtual-core/tests/ios-deferral.test.ts @@ -1724,3 +1724,26 @@ test('iOS deferral: a clamped compensation is not retried while a touch owns the expect(v['_clampedAdjustment']).toBeNull() }) }) + +test('deferAdjustmentsDuringWheel holds adjustments during wheel input and flushes at rest', () => { + const h = makeDeferralHarness({ ios: false, start: 200 }) + h.writes.length = 0 + + // Disabled by default: wheel does not defer writes. + h.el._dispatch('wheel') + h.v.resizeItem(0, 70) + expect(h.writes).toEqual([220]) + h.scroll(false, 220) + h.writes.length = 0 + + h.v.setOptions({ ...h.v.options, deferAdjustmentsDuringWheel: true }) + h.el._dispatch('wheel') + h.v.resizeItem(1, 100) + expect(h.writes).toEqual([]) + + // After the quiet window, flushes once from the live scrollTop. + h.el.scrollTop = 180 + h.expireTail() + expect(h.writes).toEqual([230]) + expect(h.el.scrollTop).toBe(230) +}) From 22d6f68d933ae8a3075b7fedb71cdf14992fac4b Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Wed, 25 Nov 2026 20:13:01 -0800 Subject: [PATCH 2/2] fix(virtual-core): preserve clamped shortfall on wheel-deferral flush --- packages/virtual-core/src/index.ts | 18 +++++- .../virtual-core/tests/ios-deferral.test.ts | 60 +++++++++++++++++++ 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/packages/virtual-core/src/index.ts b/packages/virtual-core/src/index.ts index 116e3d29f..1bdd2439d 100644 --- a/packages/virtual-core/src/index.ts +++ b/packages/virtual-core/src/index.ts @@ -1305,8 +1305,22 @@ export class Virtualizer< this._wheelTimer = this.targetWindow.setTimeout(() => { this._wheelTimer = null const live = this._readElementOffset() - if (live !== null && this._holdOffset !== 0) { - this.scrollOffset = this._toLayoutOffset(live) + if ( + live !== null && + this._holdOffset !== 0 && + this.options.enabled && + this._canWriteScroll() && + this.scrollState?.phase !== 'smooth' + ) { + // If an earlier write was clamped to `maxAtWrite` and the + // element has not scrolled away from it, keep its shortfall + // when rebasing from the live DOM offset. + const clamped = this._clampedAdjustment + const clampShortfall = + clamped !== null && Math.abs(live - clamped.maxAtWrite) < 1.5 + ? clamped.target - clamped.maxAtWrite + : 0 + this.scrollOffset = this._toLayoutOffset(live) + clampShortfall } this._flushHoldIfReady(true) this._retryClampedAdjustment() diff --git a/packages/virtual-core/tests/ios-deferral.test.ts b/packages/virtual-core/tests/ios-deferral.test.ts index 79d0f6393..0cce30b92 100644 --- a/packages/virtual-core/tests/ios-deferral.test.ts +++ b/packages/virtual-core/tests/ios-deferral.test.ts @@ -1747,3 +1747,63 @@ test('deferAdjustmentsDuringWheel holds adjustments during wheel input and flush expect(h.writes).toEqual([230]) expect(h.el.scrollTop).toBe(230) }) + +test('deferAdjustmentsDuringWheel preserves a pre-wheel clamped adjustment on flush', () => { + const makeClampingHarness = () => { + const h = makeDeferralHarness({ ios: false, start: 4800 }) + h.v.setOptions({ + ...h.v.options, + deferAdjustmentsDuringWheel: true, + scrollToFn: (offset, { adjustments = 0 }) => { + const target = offset + adjustments + h.writes.push(target) + h.el.scrollTop = Math.min(target, h.el.scrollHeight - h.el.clientHeight) + }, + }) + h.writes.length = 0 + return h + } + + // Start at max (4800). A pre-wheel resize requests 4850 and clamps at 4800. + const h = makeClampingHarness() + h.v.resizeItem(0, 100) + expect(h.writes).toEqual([4850]) + expect(h.el.scrollTop).toBe(4800) + expect(h.v['_clampedAdjustment']).toEqual({ target: 4850, maxAtWrite: 4800 }) + h.writes.length = 0 + + // Start wheel input, commit the larger sizer, then hold another 50px resize. + h.el._dispatch('wheel') + h.el.scrollHeight = 5050 + h.v._willUpdate() + h.v.resizeItem(1, 100) + expect(h.writes).toEqual([]) + expect(h.v.scrollOffset).toBe(4900) + + // Flush must keep the 50px clamp shortfall rather than rebasing to 4800 + 50. + h.expireTail() + expect(h.writes).toEqual([4900]) + expect(h.v['_clampedAdjustment']).toEqual({ target: 4900, maxAtWrite: 4850 }) + + // Once the second sizer growth commits, the retry lands at 4900. + h.el.scrollHeight = 5100 + h.v._willUpdate() + expect(h.writes).toEqual([4900, 4900]) + expect(h.el.scrollTop).toBe(4900) + expect(h.v['_clampedAdjustment']).toBeNull() + + // If the element actually scrolled away from the clamp max during wheel, + // drop the clamp shortfall and flush from the live offset. + const moved = makeClampingHarness() + moved.v.resizeItem(0, 100) + moved.writes.length = 0 + moved.el._dispatch('wheel') + moved.el.scrollHeight = 5050 + moved.v._willUpdate() + moved.v.resizeItem(1, 100) + moved.el.scrollTop = 4700 + moved.expireTail() + expect(moved.writes).toEqual([4750]) + expect(moved.el.scrollTop).toBe(4750) + expect(moved.v['_clampedAdjustment']).toBeNull() +})