Repository navigation
fix(virtual-core): hold scroll corrections under WebKit wheel input #1400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7f5110b
9572c54
462bdac
20a4038
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@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. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,12 @@ import { | |
| flingCaught, | ||
| flingSpedUp, | ||
| } from './scroll-classifier' | ||
| import { IosGesture, isIOSWebKit, momentumOverridesWrites } from './ios-gesture' | ||
| import { | ||
| IosGesture, | ||
| isIOSWebKit, | ||
| isWebKit, | ||
| momentumOverridesWrites, | ||
| } from './ios-gesture' | ||
| import type { ScrollPhase, ScrollSample } from './scroll-classifier' | ||
|
|
||
| export { _resetIOSDetectionForTests } from './ios-gesture' | ||
|
|
@@ -540,6 +545,12 @@ 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 | ||
| // Set in WebKit from a wheel event until 150ms pass without one, whether | ||
| // or not that event scrolled this element. Corrections are held meanwhile | ||
| // (see `_canWriteScroll`). | ||
| private _wheelTimer: number | null = null | ||
| // The frame a hold at the top is written in (`_flushHoldAtTop`). | ||
| private _edgeFlushFrame: 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 | ||
|
|
@@ -1087,6 +1098,16 @@ export class Virtualizer< | |
| // just removed), deferring every adjustment on the new element until | ||
| // its next touch cycle. | ||
| this._holdOffset = 0 | ||
| if (this.targetWindow) { | ||
| if (this._wheelTimer !== null) { | ||
| this.targetWindow.clearTimeout(this._wheelTimer) | ||
| } | ||
| if (this._edgeFlushFrame !== null) { | ||
| this.targetWindow.cancelAnimationFrame(this._edgeFlushFrame) | ||
| } | ||
| } | ||
| this._wheelTimer = null | ||
| this._edgeFlushFrame = null | ||
| this._reportedOverscroll = false | ||
| this._trailing = 0 | ||
| // (The gesture was ended with its listeners, in the unsubs above.) | ||
|
|
@@ -1276,6 +1297,7 @@ export class Virtualizer< | |
|
|
||
| // A held correction is written once a write is possible. | ||
| this._flushHoldIfReady(true) | ||
| this._flushHoldAtTop() | ||
|
|
||
| if (this.scrollState) { | ||
| this.scheduleScrollReconcile() | ||
|
|
@@ -1286,9 +1308,24 @@ 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 target = this.scrollElement as unknown as EventTarget | ||
| this.unsubs.push(this._iosGesture.listen(target)) | ||
| if (isWebKit()) { | ||
| const onWheel = () => { | ||
| const win = this.targetWindow | ||
| if (win === null) return | ||
| if (this._wheelTimer !== null) win.clearTimeout(this._wheelTimer) | ||
| this._wheelTimer = win.setTimeout(() => { | ||
| this._wheelTimer = null | ||
| this._flushHoldIfReady(true) | ||
| this._retryClampedAdjustment() | ||
| }, 150) | ||
| // At the top the element stops moving, so no scroll event comes. | ||
| this._flushHoldAtTop() | ||
| } | ||
| target.addEventListener('wheel', onWheel, addEventListenerOptions) | ||
| this.unsubs.push(() => target.removeEventListener('wheel', onWheel)) | ||
| } | ||
| } | ||
|
|
||
| this._scrollToOffset(this.getScrollOffset()) | ||
|
|
@@ -1443,11 +1480,68 @@ export class Virtualizer< | |
| // #1307), so a correction is held through the tail on every version; | ||
| // - not while the element is overscrolled past either edge (the | ||
| // rubber-band bounce): WebKit discards the write and the bounce can | ||
| // stall where it was (#1287). | ||
| // stall where it was (#1287); | ||
| // - not in WebKit within 150ms of a wheel event: the write lands, but | ||
| // WebKit then discards the distance of the next wheel event, and a list | ||
| // 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. | ||
| // 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 | ||
|
|
||
| // A hold shifts the rows and leaves the element where it is, so the | ||
| // element reaches its top while the layout has `_holdOffset` further to | ||
| // go (or, for a negative hold, the layout reaches the top first). Wheel | ||
| // input that never pauses would park the reader there, so there the wheel | ||
| // hold ends early and the hold is written, two frames later (a prepend's | ||
| // anchor written at once can be undone, #1398). The next wheel event | ||
| // 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. | ||
| // 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 = () => { | ||
| if (this._holdOffset === 0) return false | ||
| const offset = this._readElementOffset() | ||
| // 1px for a fractional scrollTop. | ||
| return ( | ||
| offset !== null && | ||
| Math.min(offset, offset + this._holdOffset) < | ||
| this.options.scrollMargin + 1 | ||
| ) | ||
| } | ||
| private _flushHoldAtTop = () => { | ||
| const win = this.targetWindow | ||
| if ( | ||
| win === null || | ||
| this._wheelTimer === null || | ||
| this._edgeFlushFrame !== null || | ||
| !this._holdAtTop() | ||
| ) { | ||
| return | ||
| } | ||
| this._edgeFlushFrame = win.requestAnimationFrame(() => { | ||
| this._edgeFlushFrame = win.requestAnimationFrame(() => { | ||
| this._edgeFlushFrame = null | ||
| // The reader can have turned back within the two frames. | ||
| if (this._wheelTimer === null || !this._holdAtTop()) return | ||
| win.clearTimeout(this._wheelTimer) | ||
| this._wheelTimer = null | ||
| this._flushHoldIfReady(true) | ||
| this._retryClampedAdjustment() | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| // Write a held correction once a write is possible, from the scroll | ||
| // handler, the touch tail timer, `_willUpdate` and a resize notify. The | ||
|
|
@@ -2592,6 +2686,18 @@ export class Virtualizer< | |
| behavior: ScrollBehavior, | ||
| kind: 'absolute' | 'relative', | ||
| ) { | ||
| // The command takes over from the wheel: end the wheel hold and write | ||
| // what it held first. The command is written `_holdOffset` short of its | ||
| // target, so under a hold larger than the target the element clamps at | ||
| // 0, the command never sees its target and the flush after it writes | ||
| // the old position. The command this one replaces must not keep the | ||
| // hold back (`_flushHoldIfReady` waits for a smooth one). | ||
| if (this._wheelTimer !== null && this.targetWindow) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Flush pending wheel corrections after the timer expires too. If a prepend accumulates a 600px hold during an earlier smooth command, the wheel timeout clears A subsequent smooth Track pending wheel corrections independently of timer activity. Release those corrections before the replacement command, including when the wheel timer has already expired. Preserve the existing gesture and overscroll guards.
|
||
| this.targetWindow.clearTimeout(this._wheelTimer) | ||
| this._wheelTimer = null | ||
| this.scrollState = null | ||
| this._flushHoldIfReady(true) | ||
| } | ||
| // A new explicit command supersedes layout-driven work queued before it. | ||
| // Clear both before any iOS deferral so a later commit/retry cannot | ||
| // overwrite the user's newer scroll intent. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.