iOS: size corrections deferred during an in-flight scrollToIndex are replayed after it lands, overshooting the target #1304
Describe the bug#
On iOS WebKit, applyScrollAdjustment defers a size correction into _iosDeferredAdjustment whenever the scroller is live (isScrolling / touch / post-touch grace). That also happens while a scrollToIndex is still in flight. During an index scroll, though, reconcileScroll retargets from the fresh measurements (getOffsetForIndex), so it already accounts for the resize. When the scroll settles, _flushIosDeferredIfReady replays the deferred delta on top of the reconciled position. The correction is applied twice, and the list overshoots the target by the size change.
#1235 drops a deferral that is pending when scrollToIndex / scrollToOffset starts. This is the other half: deltas deferred after the command, while it travels, are still replayed.
Your minimal, reproducible example#
A unit test against main (jsdom, iOS user agent, the withFakeIOSUserAgent helper from tests/index.test.ts): https://github.com/phcorp/virtual/blob/fix/ios-deferral-during-index-scroll/packages/virtual-core/tests/index.test.ts (test "iOS deferral: a resize during an in-flight scrollToIndex is not replayed after it lands")
Steps to reproduce#
- 100 rows estimated at 50px, viewport 200px, iOS user agent.
scrollToIndex(20, { align: 'start' })→ scrollTop 1000, a scroll event arrives withisScrolling: true.- Row 10 (above the target) is first-measured at 100px (+50) while the index scroll is still reconciling.
reconcileScrollretargets to 1050 and lands there.- The scroll settles (
isScrolling: false): the deferred +50 is replayed → scrollTop 1100.
Expected behavior#
The list stays at getOffsetForIndex(20, 'start') = 1050 after the settle.
How often does this bug happen?#
Every time
Screenshots or Videos#
No response
Platform#
iOS Safari and home-screen web apps, dynamic row heights (measureElement). In our app (a Bible reader), tapping a chapter tab calls scrollToIndex on the chapter heading. The reader lands on the heading, then slides past it by the summed size corrections of the rows measured on the way.
tanstack-virtual version#
@tanstack/virtual-core 3.18.0 (still present on main)
TypeScript version#
No response
Additional context#
Fix: in applyScrollAdjustment, skip the deferral while scrollState.index != null. Offset-only scroll states and plain user scrolls keep deferring as today. We have shipped this as a pnpm patch in production. PR incoming.
piecyk closed this
Sign in to join this conversation.