fix(virtual-core): add touch provenance flag to iOS deferral gate #1254
main fromrefs/pull/1254/head·opened Problem#
On iOS WebKit, the iOS scroll-adjustment deferral engages for programmatic scrolls, not just touch-driven ones. The deferral gate is isScrolling, and isScrolling carries no provenance — observeOffset sets it from any scroll event, including the ones a programmatic scroll write generates itself.
The result is that dynamic-measurement compensation which would normally apply pre-paint is deferred past a paint, so a scrollToIndex/scrollToOffset landing paints at a visibly wrong offset and then snaps into place a beat later.
Root Cause#
The gate in applyScrollAdjustment:
if (isIOSWebKit() && (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)) {
The this.isScrolling term over-captures: it's true for ALL scroll events, including the echo of the app's own scrollTop write. The existing touch-provenance flags (_iosTouching, _iosJustTouchEnded) are already correct — they only fire for real touch events — but they only cover the active-touch and 150ms-post-touchend windows. Momentum continues well past that, and isScrolling is the only flag that stays true through the rest of the fling.
Fix#
Introduce _isUserScrolling — a touch-provenance flag that covers the entire user-initiated scroll sequence:
touchstart: set_isUserScrolling = trueisScrollingtransitions to false: clear_isUserScrolling(scroll fully settled)- Deferral gates: use
_isUserScrollinginstead of bareisScrolling
Touch-initiated sequences defer exactly as before (fixing #884). Programmatic scrolls never set _isUserScrolling, so their adjustments stay on the synchronous pre-paint path — landing compensated on the first painted frame.
Fixes #1250
Summary by CodeRabbit#
- Bug Fixes
- Improved scrolling behavior on iOS by ensuring deferred adjustments occur only during user-initiated touch scrolling.
- Prevented programmatic scroll actions from being delayed or incorrectly synchronized.
- Improved anchor positioning during active touch-based scrolling.
- 1
Actionable comments posted: 1
🧹 Nitpick comments (1)
packages/virtual-core/src/index.ts (1)
712-712: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAdd regression coverage for both provenance paths.
The supplied
packages/virtual-core/tests/index.test.ts:1922-1939test covers an active touch._iosTouchingalready defers that case. Add tests that verifyscrollToIndexandscrollToOffsetcompensation remains synchronous before paint, and that momentum remains deferred aftertouchend.Also applies to: 976-976
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/virtual-core/src/index.ts` at line 712, Add regression tests in the existing virtual-core test suite covering both compensation provenance paths: verify scrollToIndex and scrollToOffset remain synchronous before paint during active touch, while momentum compensation remains deferred after touchend. Reuse the existing touch/scroll test setup and assert timing separately for _iosTouching and _iosJustTouchEnded behavior.🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. Inline comments: In `@packages/virtual-core/src/index.ts`: - Around line 881-887: Update the scroll-settling logic around _isUserScrolling so it is not cleared while _iosTouching or _iosJustTouchEnded is active; clear it only when the 150 ms timer expires and !this.isScrolling. Extend the onTouchEnd registration to include touchcancel so cancelled gestures and taps cannot leave stale touch state. --- Nitpick comments: In `@packages/virtual-core/src/index.ts`: - Line 712: Add regression tests in the existing virtual-core test suite covering both compensation provenance paths: verify scrollToIndex and scrollToOffset remain synchronous before paint during active touch, while momentum compensation remains deferred after touchend. Reuse the existing touch/scroll test setup and assert timing separately for _iosTouching and _iosJustTouchEnded behavior.🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID:
f6154467-0c4a-43cd-b91e-8b8e45ce25fb📥 Commits
Reviewing files that changed from the base of the PR and between d2cf98beea1696c7187c06b57c9e724d1957963c and 585b1c72e65a6b8e46705492cad792e563ff3b3e.
📒 Files selected for processing (1)
packages/virtual-core/src/index.ts
@@ -865,3 +878,10 @@865878 : null866879 this.scrollOffset = offset867880 this.isScrolling = isScrolling881+ // When the scroll fully settles, touch provenance for the sequence882+ // ends: programmatic scrolls never set it, and a user sequence is883+ // done once the momentum/subsequent scroll events stop. Clearing it884+ // here lets a later app-initiated scroll (which fires its own885+ // scroll events) take the synchronous adjustment path as intended886+ // (#1250).887+ if (!isScrolling) this._isUserScrolling = false🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash set -euo pipefail rg -n -C 12 \ 'observeElementOffset|isScrollingResetDelay|touch(start|end|cancel)|_isUserScrolling' \ packages/virtual-core/src packages/virtual-core/testsRepository: TanStack/virtual
Length of output: 50373
🏁 Script executed:
#!/bin/bash set -euo pipefail printf '%s\n' '--- observeOffset implementation ---' sed -n '160,225p' packages/virtual-core/src/index.ts printf '%s\n' '--- lifecycle and flush code ---' sed -n '820,1035p' packages/virtual-core/src/index.ts printf '%s\n' '--- relevant tests and test files ---' rg -n -C 8 \ '_iosTouching|_iosJustTouchEnded|_iosTouchEndTimerId|_isUserScrolling|touchcancel|touchend|isScrollingResetDelay|resizeItem' \ packages/virtual-core/tests packages/virtual-core/src/index.ts \ | head -n 500Repository: TanStack/virtual
Length of output: 50372
🏁 Script executed:
#!/bin/bash set -euo pipefail python3 - <<'PY' from dataclasses import dataclass `@dataclass` class State: user: bool = False touching: bool = False just_ended: bool = False scrolling: bool = False def gated(s: State) -> bool: return s.user or s.touching or s.just_ended def existing_tap(): s = State() s.user = True s.touching = True s.touching = False s.just_ended = True s.just_ended = False # 150 ms timer return s, gated(s) def existing_pause_resume(): s = State() s.user = True s.touching = True s.scrolling = True s.scrolling = False # debounce callback during touch pause s.user = False # current reset s.scrolling = True # resumed movement; no second touchstart s.touching = False s.just_ended = True s.just_ended = False # timer expires while momentum continues return s, gated(s) def proposed_tap(): s = State() s.user = True s.touching = True s.touching = False s.just_ended = True s.just_ended = False if not s.scrolling: s.user = False return s, gated(s) def proposed_pause_resume(): s = State() s.user = True s.touching = True s.scrolling = True s.scrolling = False # guarded reset does not clear while touching s.scrolling = True s.touching = False s.just_ended = True s.just_ended = False if not s.scrolling: s.user = False return s, gated(s) for name, probe in [ ("existing tap", existing_tap), ("existing pause/resume", existing_pause_resume), ("proposed tap", proposed_tap), ("proposed pause/resume", proposed_pause_resume), ]: state, blocked = probe() print(f"{name}: {state}; iOS adjustment deferred={blocked}") PYRepository: TanStack/virtual
Length of output: 622
Keep
_isUserScrollingactive until the touch sequence settles.When
isScrolling === falseoccurs during_iosTouchingor_iosJustTouchEnded, do not clear_isUserScrolling. A resumed gesture has no newtouchstart; after_iosJustTouchEndedexpires, momentum can pass the iOS adjustment gates. Clear the flag from the 150 ms timer only when!this.isScrolling. RegisteronTouchEndfortouchcancelto prevent taps and cancelled gestures from leaving stale state.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/virtual-core/src/index.ts` around lines 881 - 887, Update the scroll-settling logic around _isUserScrolling so it is not cleared while _iosTouching or _iosJustTouchEnded is active; clear it only when the 150 ms timer expires and !this.isScrolling. Extend the onTouchEnd registration to include touchcancel so cancelled gestures and taps cannot leave stale touch state.@waterWang Thanks for the writeup — the diagnosis is right.
isScrollinggenuinely does over-capture, and since_iosTouching/_iosJustTouchEndedonly cover the drag plus a 150 ms tail, removingisScrollingfrom the gate does need something that spans momentum. Two findings before this can land, though.1.
_isUserScrollingnever gets cleared if the touch doesn't scroll#It's set on
touchstartand cleared in only two places:cleanup(), and the!isScrollingbranch of the scroll callback. That second clear needs a scroll event to have happened — with the defaultuseScrollendEvent: false,!isScrollingcomes from the debounce inobserveOffset, which is only armed from inside thescrollhandler.So any touch that doesn't scroll latches the flag
truefor good: tapping a row, a long-press, a horizontal swipe in a vertical list. Verified at585b1c7—touchstart+touchend, no scroll events, after the grace window:touching: false justEnded: false userScrolling: true applied sync: false deferred: 20Two consequences after one stray tap: #1250 still reproduces (the gate is stuck, so the next
scrollToIndexdefers, paints sagged, snaps), and the deferred delta is stranded —_flushIosDeferredIfReadyonly runs from the scroll callback and the touchend timer, and its readiness check (!isScrolling && !_iosTouching && !_iosJustTouchEnded) is already satisfied, so nothing re-invokes it. Worth keeping as a rule: the deferral gate and the flush predicate have to agree, otherwise a delta can be deferred by a condition the flush path can never observe.2. A tap-triggered landing still defers — in both this version and mine#
Built the issue's repro as a test (500 rows, estimate 50 / actual 58,
scrollToIndex(300)). The filed case — no touch at all — is fixed. But when the landing is triggered by a tap, e.g. tapping a search result whose click handler callsscrollToIndex, the call lands inside the post-touchendwindow and the compensation defers anyway:A no-touch -> deferred: 0 sync writes: 1 B tap -> deferred: 8 sync writes: 0Since the issue's expectation is the general one ("a programmatic scroll should land compensated on its first painted frame"), closing this needs one more thing:
scrollToOffset/scrollToIndexshould also close the post-touchend window, right where they already drop_iosDeferredAdjustment. The write those commands are about to make cancels any in-flight momentum by itself, so the window has nothing left to protect. (_iosTouchingshould stay — with a finger still down the user owns the scroll.)Suggested shape#
Rather than a new field, re-arm the window that already exists. Momentum fires a scroll event per frame, so pushing
_iosJustTouchEnded's timer out on each one spans the whole fling and self-terminates 150 ms after the last frame:private _armIosTouchWindow = () => { if (!isIOSWebKit() || this.targetWindow == null) return this._iosJustTouchEnded = true if (this._iosTouchEndTimerId !== null) { this.targetWindow.clearTimeout(this._iosTouchEndTimerId) } this._iosTouchEndTimerId = this.targetWindow.setTimeout(() => { this._iosJustTouchEnded = false this._iosTouchEndTimerId = null this._flushIosDeferredIfReady() }, 150) }onTouchEndcalls it, and the scroll callback calls it whenisScrolling && this._iosJustTouchEnded. The gate goes back to_iosTouching || _iosJustTouchEnded, matches the flush predicate again, and every piece of state is timer-bounded so nothing can latch. One caveat to be aware of: a self-write echo arriving inside the window also re-arms it, which is part of why fix #2 above is needed rather than optional.Also#
- The existing tests need updating with this change. At
585b1c7, 10 iOS tests inpackages/virtual-core/tests/index.test.tsfail — they simulate a live scroll with no touch and assert deferral, which is precisely the behavior #1250 calls a bug. They have to move to touch provenance, and one (iOS Phase 1: scroll-event after touchend timer cleanup also flushes) is built entirely on the old premise. Doesn't look like the test job ran on this PR — only CodeRabbit and Socket reported. touchcancelisn't handled (pre-existing): iOS fires it instead oftouchendwhen a system gesture steals the touch, which strands_iosTouchingwith no timer to recover it.- Naming: the neighbouring fields are all
_ios*, and this one is only read on iOS paths.
I have the suggested shape implemented locally with the tests updated and the two new regression cases (tap doesn't latch the gate; momentum re-arms the window) — happy to push it as a commit here or a separate PR, whichever you prefer.
- The existing tests need updating with this change. At
Closing as superseded by #1280, which takes the shape from the review above: the gate is back to
_iosTouching || _iosJustTouchEnded, momentum scroll events re-arm the existing post-touchend timer so it spans the fling and self-terminates, andscrollToOffset/scrollToIndexclose the window. Both cases found here (a tap that never scrolls latching the gate, and a tap-triggered landing still deferring) are regression tests there, along with the rewritten iOS suite.Thanks @waterWang for the diagnosis — it was right, and #1280 credits it.
piecyk closed this
Sign in to join this conversation.
📝 Walkthrough
Walkthrough#
The virtual core now tracks whether scrolling began with a touch gesture. iOS adjustment deferral and anchor synchronization use this state, while programmatic scrolls remain eligible for immediate adjustments.
Changes#
iOS scroll provenance
packages/virtual-core/src/index.tspackages/virtual-core/src/index.ts_isUserScrollinginstead ofisScrolling.Estimated code review effort: 2 (Simple) | ~10 minutes
Suggested reviewers:
piecyk,2wheeh,leolb-wang🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
#1250by separating touch-initiated scrolling from programmatic scrolling while preserving momentum deferral.#1250and contain no unrelated scope.✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share