fix(virtual-core): clamp tracked scrollOffset at 0 in end-anchor compensation #1230
main fromrefs/pull/1230/head·merged by piecyk Fixes #1229
Problem#
With anchorTo: 'end' and element scrolling, the tracked scrollOffset was written without clamping to the element's reachable range in two places:
applyScrollAdjustment()— the eagerthis.scrollOffset += this.scrollAdjustmentswrite- the
setOptions()edge-key re-anchor block —this.scrollOffset = anchorItem.start + anchorOffset
When items measure smaller than their estimates and the content is shorter than the viewport, the end-anchor compensation applies a negative delta. The DOM scrollTop write is clamped to 0 by the browser, but the tracked offset keeps the negative value — and since an unscrollable element never fires a scroll event, nothing ever corrects it. For the lifetime of the instance:
getDistanceFromEnd()reports a constant phantom distance andisAtEnd()stays false, so anything steeringscrollToEnd()toward convergence spins forever_flushIosDeferredIfReady()early-returns whilegetScrollOffset() < 0(the rubber-band guard), so on iOS WebKit deferred measurement corrections stay queued forever and items freeze at their estimated positions
Fix#
Clamp only the lower bound in both writes. Upper-bound overflow is deliberately left alone — it is transiently legitimate mid-prepend while the consumer's sizer catches up, but a negative element offset is invalid unconditionally.
Tests#
Two regression tests reproducing the issue's minimal conditions (unscrollable element, anchorTo: 'end', estimates larger than measured sizes). Both fail on main (tracked offset lands at a stable -10, exactly as reported) and pass with the clamps.
🤖 Generated with Claude Code
Summary by CodeRabbit#
Bug Fixes
- Fixed end-anchored scrolling when content is shorter than the viewport.
- Prevented negative scroll offsets from causing incorrect distance-from-end calculations or leaving measurements out of sync.
- Improved scroll correction after item resizing and re-anchoring, including on iOS.
Tests
- Added regression coverage for unscrollable elements and end-anchor adjustments.
- 1
View your CI Pipeline Execution ↗ for commit 3f963ae1129a023bd26e39698e09cdd2c78fb3c4
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 2m 57s View ↗ nx run-many --target=build --exclude=examples/**✅ Succeeded 20s View ↗
☁️ Nx Cloud last updated this comment at
2026-07-17 10:46:31UTCMore 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-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-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-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/angular-virtual
npm i https://pkg.pr.new/@tanstack/angular-virtual@1230@tanstack/lit-virtual
npm i https://pkg.pr.new/@tanstack/lit-virtual@1230@tanstack/marko-virtual
npm i https://pkg.pr.new/@tanstack/marko-virtual@1230@tanstack/react-virtual
npm i https://pkg.pr.new/@tanstack/react-virtual@1230@tanstack/solid-virtual
npm i https://pkg.pr.new/@tanstack/solid-virtual@1230@tanstack/svelte-virtual
npm i https://pkg.pr.new/@tanstack/svelte-virtual@1230@tanstack/virtual-core
npm i https://pkg.pr.new/@tanstack/virtual-core@1230@tanstack/vue-virtual
npm i https://pkg.pr.new/@tanstack/vue-virtual@1230commit:
3f963aepiecyk merged this1e3b908
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: Pro
Run ID:
a424fb82-f2b1-45f9-ab78-adbd9088372d📥 Commits
Reviewing files that changed from the base of the PR and between e2cb096862f5b74aa586957eae207b39999cb654 and 3f963ae1129a023bd26e39698e09cdd2c78fb3c4.
📒 Files selected for processing (3)
.changeset/clamp-negative-end-anchor-scroll-offset.mdpackages/virtual-core/src/index.tspackages/virtual-core/tests/index.test.ts📝 Walkthrough
Walkthrough#
Virtualizernow clamps negative scroll offsets during end-anchor measurement compensation andsetOptionsre-anchoring. New regression tests cover unscrollable elements, end-distance metrics, and transient negative offsets.Changes#
End-anchor offset correction
packages/virtual-core/src/index.ts,.changeset/clamp-negative-end-anchor-scroll-offset.md0; the changeset records a patch release.packages/virtual-core/tests/index.test.tsscrollTop, zero tracked offset, correct end metrics, and recovery from transient negative offsets.Estimated code review effort: 2 (Simple) | ~10 minutes
Possibly related PRs
scrollOffsetresolution during update flows.applyScrollAdjustment().Suggested reviewers:
2wheeh,tannerlinsley🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)#
✅ Passed checks (4 passed)
#1229by clamping negative offsets in both compensation paths and covering the unscrollable end-anchor case.✨ Finishing Touches
🧪 Generate unit tests (beta)
Warning
There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.
🔧 ESLint
packages/virtual-core/tests/index.test.ts
Parsing error: "parserOptions.project" has been provided for
@typescript-eslint/parser. The file was not found in any of the provided project(s): packages/virtual-core/tests/index.test.tsThanks 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