Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .changeset/webkit-wheel-prepend-anchor.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
48 changes: 28 additions & 20 deletions packages/virtual-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 = () =>
Expand All @@ -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 = () => {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading