Skip to content
Source
Sign inSign up
TanStack/virtual

fix(virtual-core): sync `scrollOffset` in `applyScrollAdjustment` so end-anchored resize is not lost to browser clamp #1209

Mergedmds-ant(GitHub profile, opens in a new tab)merged 1 commit intomain fromrefs/pull/1209/headmerged by piecyk

🎯 Changes

With anchorTo: 'end' and a dynamically growing last item (e.g. a streaming chat reply measured via measureElement), the viewport drifts away from the end starting on the second resize tick.

resizeItem's wasAtEnd branch runs in this order:

  1. update itemSizeCache → getTotalSize() is now larger
  2. applyScrollAdjustment(newTotal − prevTotal) → _scrollToOffset(getScrollOffset(), {adjustments: delta}) → el.scrollTo({top: scrollOffset + delta})
  3. notify(false) → consumer re-renders → sets the sizer style.height to the new getTotalSize()

Step 2 writes scrollTop while the container is still at the old total height (the consumer hasn't re-rendered yet), so the browser clamps the write back to the old max. Because scrollTop didn't move, no scroll event fires, this.scrollOffset stays stale, and the next tick's getVirtualDistanceFromEnd() reads newTotal − viewport − staleOffset > scrollEndThreshold → wasAtEnd = false → no further adjustments.

Fix: in applyScrollAdjustment, after _scrollToOffset has recorded _intendedScrollOffset, also bake the adjustment into this.scrollOffset and zero scrollAdjustments (so their sum stays invariant for callers like the itemStart < getScrollOffset() + scrollAdjustments predicate). The DOM write may still be clamped, but scrollOffset now carries the intended position so the next wasAtEnd check stays true and the adjustment lands once the consumer has grown the sizer. This is the same idea as #1176 (eager scrollOffset adjustment for prepend in setOptions), applied to the resize-adjust path.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Review Change Stack

      📝 Walkthrough

      Walkthrough

      applyScrollAdjustment now copies pending scroll adjustments into scrollOffset and clears the pending delta. A regression test covers consecutive end-anchored resizes when the first scroll write is clamped. A changeset entry documents the patch.

      Changes

      End-anchored scroll adjustment sync

      Layer / File(s)Summary
      Scroll adjustment sync and regression
      packages/virtual-core/src/index.ts, packages/virtual-core/tests/index.test.ts, .changeset/resize-adjust-scrolloffset-sync.md
      applyScrollAdjustment now transfers pending adjustments into scrollOffset before clearing them; the regression test covers consecutive anchorTo: 'end' resizes with a clamped scroll write; the changeset records the patch release.

      Estimated code review effort

      🎯 2 (Simple) | ⏱️ ~10 minutes

      • TanStack/virtual#1173: Introduces the end-anchored virtualizer behavior that this scroll-offset sync change targets.
      • TanStack/virtual#1176: Also updates virtual-core to sync internal scroll state during anchor-to-end handling.
      • TanStack/virtual#1199: Changes resize-driven scroll-compensation logic and related tests in the same package.

      Suggested reviewers

      • tannerlinsley

      Poem

      A rabbit hopped by the scroll bar bright, and tucked the offset in just right. End-anchored pages held their place, no drifting off in browser space. 🐇✨

      🚥 Pre-merge checks | ✅ 5
      ✅ Passed checks (5 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title accurately and specifically describes the virtual-core scrollOffset fix.
      Docstring Coverage✅ PassedNo functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
      Linked Issues check✅ PassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes check✅ PassedCheck skipped because no linked issues were found for this pull request.
      Description check✅ PassedThe PR description matches the required template and includes the changes, checklist, and release impact sections with relevant details.
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create PR with unit tests

      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
      Comment `@coderabbitai help` to get the list of available commands.
    2. @piecyk Does this look good to you?

    3. piecyk approved these changes

      Nice fix 👏 Let's 🚢 🇮🇹

      Just a side note, and non-blocking: the iOS path isn’t covered here. The isIOSWebKit() && scrolling branch defers and never reaches the new code, so the same drift could still happen there.

      There are a few broader issues around isIOSWebKit, so I’ll add this there.

    4. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      View your CI Pipeline Execution ↗ for commit 75a5a2b14b9802608e6b125f4ca1c6dc2bcb0c74

      CommandStatusDurationResult
      nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded2m 30sView ↗
      nx run-many --target=build --exclude=examples/**✅ Succeeded17sView ↗

      ☁️ Nx Cloud last updated this comment at 2026-06-26 10:05:18 UTC

    5. More templates

      @tanstack/angular-virtual

      npm i https://pkg.pr.new/@tanstack/angular-virtual@1209

      @tanstack/lit-virtual

      npm i https://pkg.pr.new/@tanstack/lit-virtual@1209

      @tanstack/react-virtual

      npm i https://pkg.pr.new/@tanstack/react-virtual@1209

      @tanstack/solid-virtual

      npm i https://pkg.pr.new/@tanstack/solid-virtual@1209

      @tanstack/svelte-virtual

      npm i https://pkg.pr.new/@tanstack/svelte-virtual@1209

      @tanstack/virtual-core

      npm i https://pkg.pr.new/@tanstack/virtual-core@1209

      @tanstack/vue-virtual

      npm i https://pkg.pr.new/@tanstack/vue-virtual@1209

      commit: 75a5a2b

    6. piecyk merged this37be284

    7. piecyk closed this

    8. Thanks @mds-ant

Sign in to join this conversation.