fix(virtual-core): sync `scrollOffset` in `applyScrollAdjustment` so end-anchored resize is not lost to browser clamp #1209
main fromrefs/pull/1209/head·merged 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:
- update
itemSizeCache→getTotalSize()is now larger applyScrollAdjustment(newTotal − prevTotal)→_scrollToOffset(getScrollOffset(), {adjustments: delta})→el.scrollTo({top: scrollOffset + delta})notify(false)→ consumer re-renders → sets the sizerstyle.heightto the newgetTotalSize()
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
@piecyk Does this look good to you?
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.
View your CI Pipeline Execution ↗ for commit 75a5a2b14b9802608e6b125f4ca1c6dc2bcb0c74
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 2m 30s View ↗ nx run-many --target=build --exclude=examples/**✅ Succeeded 17s View ↗
☁️ Nx Cloud last updated this comment at
2026-06-26 10:05:18UTCMore templates
- @tanstack/virtual-example-angular-dynamic
- @tanstack/virtual-example-angular-fixed
- @tanstack/virtual-example-angular-infinite-scroll
- @tanstack/virtual-example-angular-padding
- @tanstack/virtual-example-angular-smooth-scroll
- @tanstack/virtual-example-angular-sticky
- @tanstack/virtual-example-angular-table
- @tanstack/virtual-example-angular-variable
- @tanstack/virtual-example-angular-window
- tanstack-lit-virtual-example-dynamic
- tanstack-lit-virtual-example-fixed
- tanstack-react-virtual-example-chat
- tanstack-react-virtual-example-dynamic
- tanstack-react-virtual-example-fixed
- tanstack-react-virtual-example-infinite-scroll
- tanstack-react-virtual-example-padding
- tanstack-react-virtual-example-pretext
- tanstack-react-virtual-example-scroll-padding
- tanstack-react-virtual-example-smooth-scroll
- tanstack-react-virtual-example-sticky
- tanstack-react-virtual-example-table
- tanstack-react-virtual-example-variable
- tanstack-react-virtual-example-window
- tanstack-svelte-virtual-example-dynamic
- tanstack-svelte-virtual-example-fixed
- tanstack-svelte-virtual-example-infinite-scroll
- tanstack-svelte-virtual-example-smooth-scroll
- tanstack-svelte-virtual-example-sticky
- tanstack-svelte-virtual-example-table
- tanstack-vue-virtual-example-dynamic
- tanstack-vue-virtual-example-fixed
- tanstack-vue-virtual-example-infinite-scroll
- tanstack-vue-virtual-example-padding
- tanstack-vue-virtual-example-scroll-padding
- tanstack-vue-virtual-example-smooth-scroll
- tanstack-vue-virtual-example-sticky
- tanstack-vue-virtual-example-table
- tanstack-vue-virtual-example-variable
@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@1209commit:
75a5a2bpiecyk merged this37be284
piecyk closed this
Thanks @mds-ant
Sign in to join this conversation.
📝 Walkthrough
Walkthrough#
applyScrollAdjustmentnow copies pending scroll adjustments intoscrollOffsetand 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
packages/virtual-core/src/index.ts,packages/virtual-core/tests/index.test.ts,.changeset/resize-adjust-scrolloffset-sync.mdapplyScrollAdjustmentnow transfers pending adjustments intoscrollOffsetbefore clearing them; the regression test covers consecutiveanchorTo: 'end'resizes with a clamped scroll write; the changeset records the patch release.Estimated code review effort#
🎯 2 (Simple) | ⏱️ ~10 minutes
Possibly related PRs#
virtual-coreto sync internal scroll state during anchor-to-end handling.Suggested reviewers#
Poem#
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
✨ 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