Repository navigation
feat(virtual-core): add opt-in deferAdjustmentsDuringWheel for desktop WebKit #1399
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
base: main
Are you sure you want to change the base?
Changes from all commits
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': minor | ||
| --- | ||
|
|
||
| Add opt-in `deferAdjustmentsDuringWheel` option to hold scroll adjustments during wheel gestures and flush them once input has been quiet for 150ms. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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,41 @@ 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.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 | ||
|
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. Keep the clamp shortfall when a held correction shifts the reported offset. While a correction is held, For example, with The clamp must survive a report that leaves the element at its recorded max. The clear condition must account for the hold offset that existed when the write was made. Without a hold, the same report remains a Affected code
|
||
| 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() | ||
| }, 150) | ||
| } | ||
| el.addEventListener('wheel', onWheel, addEventListenerOptions) | ||
| this.unsubs.push(() => el.removeEventListener('wheel', onWheel)) | ||
| } | ||
|
|
||
| this._scrollToOffset(this.getScrollOffset()) | ||
|
|
@@ -1447,7 +1486,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 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we keep the timer: do we need this rebase? Every scroll event already sets
scrollOffsetto the element offset plus the hold, and the iOS tail flushes without it. Removed it locally and only the tests that moveel.scrollTopwithout a scroll event fail. Dropping it also drops theclampShortfallpart and the CodeRabbit thread below.