Skip to content

iOS: scrollToIndex(0) stops short of the top while a size correction is held #1405

Done

Description

@RagDam

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 0

Steps 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)

  1. _scrollToOffset writes offset - _holdOffset = 0 - 40 = -40; the browser clamps it to 0.
  2. The scroll event reports element 0, i.e. layout 40 (holdOffset added). classifyScrollEvent
    has a clampedReadBack for a write clamped at the element's max, but none for a write clamped at
    the element's 0 (which is holdOffset in layout coordinates, as the comment on _noteClampMax
    says), so the event is external and retires the command.
  3. 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

  1. RagDam commented on Nov 29, 2026

    @RagDam
    Author

    A workaround until this is fixed, for anyone who hits it: call _willUpdate() right after the jump.

    v.scrollToIndex(0, { align: 'start' })
    v._willUpdate()

    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: scrollToOffset to 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 as external: the same order as the library's own fling path (_iosGesture.end(), then _flushHoldIfReady).

    Limits we found:

    • Under a resting finger nothing is written, so a jump made while a finger is on the list still stops short.
    • With React, the re-render _willUpdate triggers is asynchronous. From a requestAnimationFrame callback, wrap the jump and the call in flushSync, or one frame is painted with the rows hold px 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: _willUpdate is underscore-prefixed, not something apps should have to call.

  2. piecyk commented on Nov 29, 2026

    @piecyk
    Collaborator

    Thanks @RagDam, the workaround and the scrollToOffset case 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, so scrollToIndex(0) and scrollToOffset(0) / (20) land at 0, auto and smooth. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions