fix(react-virtual): grow size container before scroll sync on end-anchored prepend #1237
main fromrefs/pull/1237/head·merged by piecyk Summary#
Fixes a whitespace gap at the top of the list after an end-anchored prepend (e.g. "load older messages" in a chat pinned to the bottom) when using directDomUpdates mode.
Root cause#
On a prepend, setOptions grows the total size and bumps scrollOffset to the new bottom in the same render pass. The scroll position is then synced in the _willUpdate layout effect — but the size container's height is written by the separate applyDirectStyles layout effect that runs after it. So _willUpdate writes scrollTop to the new bottom while the container is still at its old, shorter height, and the browser clamps the write to the stale scrollHeight. The list is left with a gap at the top until the next scroll re-reconciles.
It only bites when at-end (that's the only time the new scroll target exceeds the stale scrollHeight), and only in directDomUpdates mode — React-rendered sizers receive their height during render, so they're already tall by the layout-effect phase.
Fix#
Grow the size container to the new total before _willUpdate syncs the scroll position. Item positions are still written afterwards by applyDirectStyles. The size write is extracted into a small applyContainerSize helper (idempotent, guarded by lastSize) reused by both call sites.
Testing#
@tanstack/react-virtual: 7/7 pass;tsc --noEmitclean.- Verified manually in the React chat example: scroll to bottom → "Load older" no longer leaves a gap; the list stays pinned to the bottom.
🤖 Generated with Claude Code
Summary by CodeRabbit#
- Bug Fixes
- Fixed a scrolling issue when items are prepended to the end-anchored list.
- Prevented temporary whitespace caused by scroll position being clamped to an outdated list size.
- Improved direct DOM updates so container dimensions are refreshed before scroll synchronization.
- 1
More templates
- @tanstack/virtual-example-angular-chat
- @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-marko-virtual-example-dynamic
- tanstack-marko-virtual-example-fixed
- tanstack-marko-virtual-example-grid
- tanstack-marko-virtual-example-infinite-scroll
- tanstack-marko-virtual-example-smooth-scroll
- tanstack-marko-virtual-example-variable
- tanstack-marko-virtual-example-window
- 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@1237@tanstack/lit-virtual
npm i https://pkg.pr.new/@tanstack/lit-virtual@1237@tanstack/marko-virtual
npm i https://pkg.pr.new/@tanstack/marko-virtual@1237@tanstack/react-virtual
npm i https://pkg.pr.new/@tanstack/react-virtual@1237@tanstack/solid-virtual
npm i https://pkg.pr.new/@tanstack/solid-virtual@1237@tanstack/svelte-virtual
npm i https://pkg.pr.new/@tanstack/svelte-virtual@1237@tanstack/virtual-core
npm i https://pkg.pr.new/@tanstack/virtual-core@1237@tanstack/vue-virtual
npm i https://pkg.pr.new/@tanstack/vue-virtual@1237commit:
b8c2cb3📝 Walkthrough
Walkthrough#
The direct DOM update path now applies the virtualization container’s total size separately and before item positioning or
_willUpdatescroll synchronization, preventing stale scroll extents during end-anchored prepends. A patch changeset documents the fix.Changes#
Direct DOM scroll synchronization
Layer / File(s) Summary Container sizing and item positioning
packages/react-virtual/src/index.tsxAdds an idempotent applyContainerSizehelper and invokes it before writing direct item positions.Pre-scroll synchronization sizing
packages/react-virtual/src/index.tsx,.changeset/fix-direct-dom-prepend-scroll-clamp.mdApplies the container size before _willUpdatesynchronizes scroll position and documents the patch release.参考Estimated code review effort: 2 (Simple) | ~10 minutes
Possibly related PRs
- TanStack/virtual#1176: Both adjust end-anchored prepend scroll synchronization timing.
- TanStack/virtual#1180: Both modify the direct DOM update path in
react-virtual. - TanStack/virtual#1201: Both modify container sizing and direct style application.
Suggested reviewers:
tannerlinsley🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request. Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request. Title check ✅ Passed The title clearly matches the main change: fixing scroll sync by growing the size container first for end-anchored prepends. Description check ✅ Passed The description covers the summary, root cause, fix, and testing, but omits the template's checklist and release-impact sections. ✨ 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.
Comment `@coderabbitai help` to get the list of available commands.piecyk merged thisaa536e7
piecyk closed this
Sign in to join this conversation.
View your CI Pipeline Execution ↗ for commit b8c2cb34c589ba04b0360985cab63421066b12d1
nx affected --targets=test:sherif,test:knip,tes...nx run-many --target=build --exclude=examples/**☁️ Nx Cloud last updated this comment at
2026-07-20 11:22:24UTC