From 305bf0e2ee0f41d22a46adb998fd9125a2d0f79c Mon Sep 17 00:00:00 2001 From: Damian Pieczynski Date: Sun, 29 Nov 2026 12:11:36 +0100 Subject: [PATCH] docs(virtual-core): bring the hold comments up to date with the wheel hold Comments only, plus the wording of the #1400 changeset: - the real Safari run from #1400 replaces 'not measured'; - the two-frame lag before the top was seen in Playwright WebKit 26.0 and not in 27.2; - the callers of _flushHoldIfReady and the reasons a write is held now name the wheel timer, the frame at the top and a command's start; - a command writes a wheel hold first, it no longer lands short of it; - the changeset no longer says no distance is lost: a write at the top can still cost a wheel event or two. --- .changeset/webkit-wheel-prepend-anchor.md | 2 +- packages/virtual-core/src/index.ts | 48 +++++++++++++---------- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/.changeset/webkit-wheel-prepend-anchor.md b/.changeset/webkit-wheel-prepend-anchor.md index f016df44..f1c8cbf6 100644 --- a/.changeset/webkit-wheel-prepend-anchor.md +++ b/.changeset/webkit-wheel-prepend-anchor.md @@ -2,4 +2,4 @@ '@tanstack/virtual-core': patch --- -Fix wheel scrolling in WebKit losing distance, or the reader's place, when rows are measured or prepended on the way. Scroll corrections are now held for 150ms after a wheel event and written when the wheel rests, or when the element reaches the top of the list. +Fix wheel scrolling in WebKit dropping wheel events, or the reader's place, when rows are measured or prepended on the way. Scroll corrections are now held for 150ms after a wheel event and written when the wheel rests, or when the element reaches the top of the list. A write at the top of the list can still cost a wheel event or two. diff --git a/packages/virtual-core/src/index.ts b/packages/virtual-core/src/index.ts index b1afcba9..033b9fb9 100644 --- a/packages/virtual-core/src/index.ts +++ b/packages/virtual-core/src/index.ts @@ -976,10 +976,11 @@ export class Virtualizer< if (this._canWriteScroll()) { this.pendingScrollAnchor = { kind: 'anchor', delta: anchorDelta } } else { - // The element cannot take the write (a touch owns it on iOS, or it is - // overscrolled). Hold the delta instead: this render already exposes - // the rows and the total shifted back by it, so the reader's row - // stays put, and the flush writes it once a write is possible. + // The element cannot take the write (a touch owns it on iOS, it is + // overscrolled, or wheel input runs in WebKit). Hold the delta + // instead: this render already exposes the rows and the total + // shifted back by it, so the reader's row stays put, and the flush + // writes it once a write is possible. this._holdOffset += anchorDelta this._followAnchor(anchorDelta) } @@ -1488,7 +1489,9 @@ export class Virtualizer< // of rows measured on the way up writes on most frames. In Playwright // WebKit 26.0 a wheel event per frame lost about a third of its // distance that way (#1398, #1400); a prepend's anchor write could be - // undone whole. Real Safari and trackpad momentum are not measured. + // undone whole. With the hold, Safari 26.5.2 itself lost nothing on + // trackpad momentum and about 2.5% on a continuous mouse wheel, at the + // writes at the top (#1400); without it only Playwright is measured. // 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 = () => @@ -1505,11 +1508,12 @@ export class Virtualizer< // starts the wheel hold again. Not before the top: while the element // still moves, WebKit applies the write about two frames after the rows // have gone back to layout coordinates, and they show `_holdOffset` away - // for those frames (measured with a 1200px and a one-viewport margin, - // #1400). The list's top is `scrollMargin` down the element, where held - // rows would be drawn over what is above the list; the element still - // moves there, so those frames are the lesser evil. Past the top (a - // bounce) the write waits: the next wheel or scroll event tries again. + // for those frames (measured with a 1200px and a one-viewport margin in + // Playwright WebKit 26.0; not seen in 27.2, #1400). The list's top is + // `scrollMargin` down the element, where held rows would be drawn over + // what is above the list; the element still moves there, so those frames + // are the lesser evil. Past the top (a bounce) the write waits: the next + // wheel or scroll event tries again. // The end needs none of this: the total size is exposed short by the // hold, so the element and the layout reach it together. private _holdAtTop = () => { @@ -1546,12 +1550,15 @@ export class Virtualizer< } // Write a held correction once a write is possible, from the scroll - // handler, the touch tail timer, `_willUpdate` and a resize notify. The - // rows go back to layout coordinates and the total grows by the hold in a - // notify issued before the write, so the sizer can take the position and - // both commit in the paint the write lands in. `sync` is false only where - // the caller already runs inside the consumer's commit (`_willUpdate`), - // where an update it schedules commits before paint anyway. + // handler, the touch tail timer, the wheel timer, the frame at the top + // (`_flushHoldAtTop`), a command's start, `_willUpdate` and a resize + // notify. The rows go back to layout coordinates and the total grows by + // the hold in a notify issued before the write, so the sizer can take the + // position and both commit in the paint the write lands in. `sync` is + // false only where the caller already runs inside the consumer's commit + // (`_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) => { if (this._holdOffset === 0) return if (!this.scrollElement || !this.options.enabled) return @@ -2681,10 +2688,11 @@ export class Virtualizer< // Every scroll command starts here: it becomes the in-flight command, // writes its first target and leaves reconcile to retarget and retire it. // - // Targets are layout offsets. A held correction is not dropped by a - // command: the write lands the element `_holdOffset` short of the target, - // under the rows exposed shifted by the same amount, and the flush moves - // both together once a write is possible. + // Targets are layout offsets. A correction held for the wheel is written + // before the command (below). One held for a touch or a bounce is not + // dropped by a command: the write lands the element `_holdOffset` short + // of the target, under the rows exposed shifted by the same amount, and + // the flush moves both together once a write is possible. // // An index or end target that is already at its offset starts a smooth // scroll as landed; an offset target never does, so its first scroll