fix(react-virtual): size direct DOM container before scroll adjustments #1265
main fromrefs/pull/1265/head·opened 🎯 Changes#
Fixes #1266.
Live reproduction · Standalone source
With anchorTo: 'end' and directDomUpdates, growing an earlier message can leave the viewport above the bottom when the last message keeps its size. resizeItem applies its scroll adjustment before notifying the React adapter, so the browser clamps the write against the old sizer height. The internal offset advances, but the actual DOM position remains behind. In the regression, growing the previous message by 24px leaves a persistent 24px gap.
Apply the adapter's existing, size-guarded applyContainerSize before delegating to scrollToFn. The browser then sees the updated scroll range when applying the adjustment. This covers the resize path in direct DOM mode using the same ordering principle as #1237; related to #1209. The change is scoped to the React adapter's direct DOM mode.
A separate, statically configured chat-resize E2E fixture uses directDomUpdates with useFlushSync: false; the original chat fixture and its default-mode coverage are preserved. The regression grows the previous message while keeping the last message at a fixed viewport height, then checks the real scrollHeight and scrollTop. It uses the native ResizeObserver and browser scrolling, with native scroll anchoring disabled so it cannot mask a missing virtualizer adjustment.
Validation (Node 24.8.0 from .nvmrc, pnpm 11.9.0 from packageManager):
- With only the SDK fix removed, the final
/chat-resize/regression fails with an actual bottom gap of 24px. Restoring the fix makes it pass. NX_NO_CLOUD=true NX_DAEMON=false CI=1 pnpm run test:pr --parallel=2 --skip-nx-cachepasses, including all 34 React browser tests, 7 React library tests, type checks, lint, builds, and package validation. Nx caching was disabled for this run.- Prettier checks pass for all changed files.
✅ 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 patch changeset for
@tanstack/react-virtual. - This change is docs/CI/dev-only (no release).
Summary by CodeRabbit#
Bug Fixes
- Fixed end-anchored virtualized lists so they remain pinned to the bottom when an earlier item increases in height.
- Updated scroll adjustments to use the latest container size, preventing the browser from limiting the intended scroll position.
- Improved chat-style scrolling so users remain at the latest message after earlier content expands.
Tests
- Added end-to-end coverage for chat-style lists where a previous message grows while scrolled to the bottom.
- 1
@tigerBeA Thanks for this, and especially for the reproduction. It made the root cause unambiguous, and your
chat-resizeapp and test are now in #1260 as the permanent regression test for this scenario, credited to you in both files.I'm closing this in favour of #1260, which fixes #1258 and #1266 together in virtual-core. The two issues are the same mechanism:
resizeItemwrites the compensatingscrollTopbefore the consumer has committed the new total size, the browser clamps the write to the old maximum, and nothing re-applies the lost distance afterwards. Your PR prevents the clamp for the ReactdirectDomUpdatespath by sizing the container insidescrollToFn, which works, but only for that one adapter and mode. The core fix records a compensation write whose target exceeds the scroll maximum at write time and re-issues it once the sizer has grown, so it also covers the React-rendered sizer,useWindowVirtualizer, and the other adapters, and it recovers even when no re-render follows the resize.Your diagnosis in #1266 was spot on, including the ordering point about
applyContainerSize. Sizing the container before the write is still a reasonable optimisation for the direct-DOM path, since it avoids the extra write entirely, so if you'd like to rebase that part on top of #1260 once it lands I'd be happy to review it as a follow-up.piecyk closed this
Sign in to join this conversation.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Team
Run ID:
6b8448dc-87da-45e0-afe5-044eec11af71📥 Commits
Reviewing files that changed from the base of the PR and between 0c40261ba98d3329bdca4bf06d53b585ade4e840 and cad117905a8de71b4587c44eee88c43ceb31b04d.
📒 Files selected for processing (1)
packages/react-virtual/src/index.tsx🚧 Files skipped from review as they are similar to previous changes (1)
Included review availability: Your plan provides up to 8 included reviews per hour; 5 remain after this review.
📝 Walkthrough
Walkthrough#
The virtualizer now updates the direct DOM size before applying scroll adjustments. A new chat-resize example and Playwright test verify that end-anchored scrolling remains pinned when a previous message grows.
Changes#
Chat scroll anchoring
packages/react-virtual/src/index.tsx,.changeset/fresh-chats-scroll.mdscrollToFnapplies the current container size before calling the configured scroll function. The changeset documents the patch release.packages/react-virtual/e2e/app/chat-resize/*,packages/react-virtual/e2e/app/vite.config.tspackages/react-virtual/e2e/app/test/chat.spec.tsscrollTopincreases by 24 pixels when the previous message grows.Estimated code review effort: 2 (Simple) | ~10 minutes
Merge Risk: ⚪ Minimal · up to
cad11This updates direct-DOM virtualizer sizing before resize-related scroll adjustment so end-anchored views remain pinned after earlier content grows. No concrete merge-blocking risk is identified.
Sequence Diagram(s)#
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)#
✅ Passed checks (4 passed)
#1266by applying the updated container size before resize-related scroll adjustments, adding regression coverage for direct DOM updates with end anchoring, and pres…#1266.✨ 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