Repository navigation
Description
Describe the bug
On iOS (the touch gesture's hold, since 3.19.5), a jump whose target is smaller than the held
correction does not reach it. Typical case: a row above the viewport grows during a tap's 150 ms
tail, then the app resets the list to the top with scrollToIndex(0). The list ends hold px below
the top, with row 0 drawn hold px above the viewport.
A jump to a target larger than the hold is fine: it is written hold px short under rows exposed
shifted by the same amount, and the flush moves both together.
Your minimal, reproducible example
Library only (vitest + jsdom, fake timers), no framework adapter:
import { Virtualizer, _resetIOSDetectionForTests } from '@tanstack/virtual-core'
function jumpWhileHeld(target: number) {
vi.spyOn(navigator, 'userAgent', 'get').mockReturnValue('Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X)')
_resetIOSDetectionForTests()
let scrollTop = 0
const writes: number[] = []
const el = document.createElement('div')
Object.defineProperties(el, { scrollHeight: { value: 5000 }, clientHeight: { value: 400 }, scrollTop: { get: () => scrollTop } })
let report: (offset: number, isScrolling: boolean) => void
const v = new Virtualizer<HTMLElement, Element>({
count: 50, estimateSize: () => 100, getScrollElement: () => el, initialOffset: 1000,
// a browser clamps the write into [0, max]
scrollToFn: (offset, { adjustments = 0 }) => { scrollTop = Math.max(0, offset + adjustments); writes.push(scrollTop) },
observeElementRect: (_i, cb) => cb({ width: 400, height: 400 }),
observeElementOffset: (_i, cb) => { report = cb },
})
v._willUpdate()
report!(scrollTop, false)
el.dispatchEvent(new Event('touchstart'))
el.dispatchEvent(new Event('touchend')) // a tap: its tail forbids writes
writes.length = 0
v.resizeItem(0, 140) // row 0, above the viewport, +40: held
v.scrollToIndex(target, { align: 'start' })
for (let frame = 0; frame < 20; frame++) { report!(scrollTop, frame < 10); vi.advanceTimersByTime(16) }
const row = v.getVirtualItems().find((r) => r.index === target)
return { scrollTop, writes, rowOnScreen: (row?.start ?? NaN) - scrollTop }
}
jumpWhileHeld(20) // rowOnScreen 0 -- fine
jumpWhileHeld(0) // { scrollTop: 40, writes: [0, 40], rowOnScreen: -40 } -- expected scrollTop 0, rowOnScreen 0Steps to reproduce
Run the snippet above (vitest, jsdom environment, vi.useFakeTimers()) on virtual-core 3.19.13.
Also seen on 3.19.11 and 3.19.12, with the same writes.
Expected behavior
scrollToIndex(0) ends at scrollTop 0 with row 0 at the top, a correction held or not.
How often does this bug happen?
Every time
How it happens (reading the source)
_scrollToOffsetwritesoffset - _holdOffset= 0 - 40 = -40; the browser clamps it to 0.- The scroll event reports element 0, i.e. layout 40 (
holdOffsetadded).classifyScrollEvent
has aclampedReadBackfor a write clamped at the element's max, but none for a write clamped at
the element's 0 (which isholdOffsetin layout coordinates, as the comment on_noteClampMax
says), so the event isexternaland retires the command. - The flush then writes the held 40 and the rows go back to layout coordinates: the viewport stays
at 40.
Possible directions (for the maintainers to judge): classify a non-smooth write whose target is
below holdOffset, reported at holdOffset, as our own clamped write so the command survives the
flush; or let an absolute command whose target is below the hold release the hold (notify, then
write the target), since that jump moves the reader away from the rows the hold protects.
Platform
iOS Safari (iPhone), touch; reproduced in jsdom with the iPhone user agent.
tanstack-virtual version
@tanstack/virtual-core 3.19.13 (@tanstack/react-virtual 3.14.28); also 3.19.11 and 3.19.12
TypeScript version
n/a
Additional context
On 3.17.0 our app clears the deferred correction itself before each jump
(_iosDeferredAdjustment), which lands at 0; the hold made that workaround unnecessary everywhere
except in this case, so this blocks our upgrade.
Possibly related, but not the same path as far as we can read: #1397 clamps a compensation write
past the top (adjustments), not a command's _scrollToOffset under a hold; #1400 holds corrections
under WebKit wheel input.
Terms & Code of Conduct
- I agree to follow this project's Code of Conduct
- I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
Activity
RagDam commentedon Nov 29, 2026
A workaround until this is fixed, for anyone who hits it: call
_willUpdate()right after the jump.With the snippet above on 3.19.13,
jumpWhileHeld(0)then ends at{ scrollTop: 0, writes: [0, 0], rowOnScreen: 0 };jumpWhileHeld(20)is unchanged (writes: [2000, 2040],rowOnScreen: 0).One more case the issue did not list:
scrollToOffsetto any target below the hold stops short the same way (offsets 0 and 20 with a 40 px hold both end at 40), and the same call fixes it.Why it works, as far as we can read:
_willUpdate(the lifecycle method the React adapter runs after every render, described for adapter-less use in #1402) flushes the hold while the command is still in flight. The flush writes the target, the read-back matches it, and the command is not retired asexternal: the same order as the library's own fling path (_iosGesture.end(), then_flushHoldIfReady).Limits we found:
_willUpdatetriggers is asynchronous. From arequestAnimationFramecallback, wrap the jump and the call influshSync, or one frame is painted with the rowsholdpx off.We ship this on @tanstack/react-virtual 3.14.28 (virtual-core 3.19.13), with a test that turns red once the library alone lands at 0, to drop the workaround then. A fix in the library would still be better:
_willUpdateis underscore-prefixed, not something apps should have to call.piecyk commentedon Nov 29, 2026
Thanks @RagDam, the workaround and the
scrollToOffsetcase help. #1409 should cover both: when the jump closes the tap's tail and its target is below the hold, the hold is written first and then the jump, soscrollToIndex(0)andscrollToOffset(0)/(20)land at 0,autoandsmooth. In your repro the writes become[1040, 0].A jump with a finger still on the list stops short there too, same limit as your workaround, so that one stays open for now. Once it ships your red test should tell you when to drop the
_willUpdate()call.test: cover more commands below a held correction (TanStack#1405)
test: run _willUpdate after a late sizer commit in the TanStack#1405 …