fix(virtual-core): account for scrollMargin when deciding the viewport is at the end #1340
main fromrefs/pull/1340/head·merged by piecyk 🎯 Changes#
Fixes #1339.
getVirtualDistanceFromEnd subtracts two values measured from different origins:
this._getLayoutTotalSize() - this.getSize() - this.getScrollOffset()
_getLayoutTotalSize() is the sizer, which is end - scrollMargin + paddingEnd and therefore starts scrollMargin into the scroll content. getScrollOffset() is measured from the top of that content. The result reads the viewport scrollMargin px closer to the end than it is, so resizeItem's wasAtEnd fires for a reader who is still inside the margin and an item growing below the fold pulls them down.
getDistanceFromEnd() and isAtEnd() are computed from the element's own max scroll and are correct, so today they report "not at the end" for the same viewport the internal check treats as pinned.
The change lifts the total into the element's space before subtracting. scrollMargin defaults to 0, so a list that does not set it is unaffected. Reading the intent the other way round gives the same expression: if the distance is meant to be from the end of the list rather than of the element, the scroll offset is the value that needs the margin taken off it.
With scrollMargin: 300 and the default scrollEndThreshold of 1, growing the last row:
| distance from end | isAtEnd() | before | after |
|---|---|---|---|
| 0 | true | adjusted | adjusted |
| 1 | true | adjusted | adjusted |
| 300 | false | adjusted | not adjusted |
| 900 | false | not adjusted | not adjusted |
Tests. createChatVirtualizer takes a scrollMargin, which also moves the mock element's scrollHeight, and two cases sit next to the existing end-anchor tests: a reader 100px above the end with a 150px margin is left alone, which fails on main, and an end-pinned viewport with the same margin still gets its 70px adjustment, which passes before and after.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested this code locally with
pnpm run test:pr.
For virtual-core: test:lib 440 passing and 1 skipped, test:types, test:eslint, build. At the root: test:sherif, test:docs, test:knip. test:build did not run on this machine, it fails the same way on a clean main here because publint shells out to the system pnpm. I did not run the Playwright e2e.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
🤖 Generated with Claude Code
Summary by CodeRabbit#
- Bug Fixes
- Corrected end-of-viewport detection to account for scroll margin, preventing unintended scrolling for readers who are near—but not at—the end.
- Preserved end-pinned scrolling behavior, including resize adjustments.
- 1
No actionable comments were generated in the recent review.
Recent review info-
Run configuration-
- Source mode: Auto
- Repository: Unavailable
- Branch: Unavailable
- File: No configuration file from this repository applied
- Commit: Unavailable
- Layers: defaults
- Review profile: CHILL
- Plan: Advanced
- Run ID:
fc3757bd-d5bf-427c-8833-5c82f9f5e0dd
Commits-
Reviewing files that changed from the base of the PR and between c462c6141ea434fdbc52b5dd866bb8db7e4fd07e and 5d760dcc37e4e5aa864b6ee5483d9a81640c5d7c.
Files selected for processing: 3-
.changeset/scroll-margin-end-check.mdpackages/virtual-core/src/index.tspackages/virtual-core/tests/anchor-end.test.ts
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
-
Walkthrough
-
The virtual distance from the end now includes
scrollMargin. Regression tests cover streaming item growth when the viewport is away from or pinned to the end. A patch changeset records the update.Changes: Scroll margin end-distance calculation
Priority:Layer / File(s) Summary End-distance calculation and regression coverage
packages/virtual-core/src/index.ts,packages/virtual-core/tests/anchor-end.test.ts,.changeset/scroll-margin-end-check.mdgetVirtualDistanceFromEndaddsscrollMarginbefore calculating the distance and still clamps the result to zero. Tests cover streaming growth when a reader is 100px from the end and when the viewport is pinned to the end. The changeset records a patch release for@tanstack/virtual-core. NormalEstimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix · Severity of issue fixed: Medium
Suggested reviewers:
piecyk
Merge Risk:
Minimal · up to5d760-
The change is ready to merge; the intended nonzero-scroll-margin behavior is covered without identified regressions.
Pre-merge checks:
5-
Check name Status Explanation Title check The title follows the conventional commit format and names the primary change: getVirtualDistanceFromEnd now accounts for scrollMargin when deciding whether the viewport is at the end. It matches the … Description check The description covers the change and its motivation, linked issue #1339, a worked example table, the test changes, and the validation run. It fills in the template's Changes, Checklist, and Release I…Linked Issues check Issue #1339is directly linked and requiresscrollMargin-aware end detection while preserving end pinning and thescrollMargin: 0behavior.getVirtualDistanceFromEndnow adds `this.options.scrol…Out of Scope Changes check The changed source implements the correction requested by issue #1339. The added tests verify the affected nonzero-margin behavior and preserve end pinning. The changeset documents the published packa…Docstring Coverage Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. (1 skipped: 1 … Full details: Title check
-
Explanation
The title follows the conventional commit format and names the primary change: getVirtualDistanceFromEnd now accounts for scrollMargin when deciding whether the viewport is at the end. It matches the code change in packages/virtual-core/src/index.ts and the added tests, so it is specific and accurate.
Full details: Description check
-
Explanation
The description covers the change and its motivation, linked issue
#1339, a worked example table, the test changes, and the validation run. It fills in the template's Changes, Checklist, and Release Impact sections. The Release Impact section checks the changeset box, and the raw summary confirms a changeset was added. The description does not follow the template's emoji headings exactly, but all required information is present.
Full details: Linked Issues check
-
Explanation
Issue
#1339is directly linked and requiresscrollMargin-aware end detection while preserving end pinning and thescrollMargin: 0behavior.getVirtualDistanceFromEndnow addsthis.options.scrollMarginbefore subtracting the viewport size and scroll offset. The result remains clamped to zero. The change therefore aligns the internal distance with the element-space distance used by the public end checks. The added tests cover a reader 100px from the end with a 150px margin and an end-pinned viewport with the same margin. Existing zero-margin behavior remains covered by the existing tests.
Full details: Out of Scope Changes check
-
Explanation
The changed source implements the correction requested by issue
#1339. The added tests verify the affected nonzero-margin behavior and preserve end pinning. The changeset documents the published package fix. No unrelated product behavior or unrelated files appear in the whole-PR diff.
Full details: Docstring Coverage
-
Explanation
Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. (1 skipped: 1 unsupported.)
-
Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Warning
Some tools did not complete. Review the errors below.
🔧 ESLint
-
If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.
packages/virtual-core/tests/anchor-end.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/anchor-end.test.ts
-
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 approved these changes
Thanks @KooYS, nice catch and a really clear write-up. The math checks out, the total is in list space and the offset is in element space, so adding
scrollMarginback makes it line up withgetDistanceFromEnd(). I also ran the new test on main and it fails there, so it covers the bug. LGTM 🚀View your CI Pipeline Execution ↗ for commit 5d760dcc37e4e5aa864b6ee5483d9a81640c5d7c
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 4m 21s View ↗ nx run-many --target=build --exclude=examples/**✅ Succeeded 22s View ↗
☁️ Nx Cloud last updated this comment at
2026-10-26 11:38:10UTCMore 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-chat
- tanstack-marko-virtual-example-chat-pretext
- 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-padding
- tanstack-marko-virtual-example-pretext
- tanstack-marko-virtual-example-scroll-padding
- tanstack-marko-virtual-example-smooth-scroll
- tanstack-marko-virtual-example-ssr
- tanstack-marko-virtual-example-ssr-fetch
- tanstack-marko-virtual-example-ssr-restore
- tanstack-marko-virtual-example-ssr-slice
- tanstack-marko-virtual-example-sticky
- tanstack-marko-virtual-example-table
- tanstack-marko-virtual-example-variable
- tanstack-marko-virtual-example-window
- tanstack-marko-virtual-example-window-ssr-slice
- 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@1340@tanstack/lit-virtual
npm i https://pkg.pr.new/@tanstack/lit-virtual@1340@tanstack/marko-virtual
npm i https://pkg.pr.new/@tanstack/marko-virtual@1340@tanstack/react-virtual
npm i https://pkg.pr.new/@tanstack/react-virtual@1340@tanstack/solid-virtual
npm i https://pkg.pr.new/@tanstack/solid-virtual@1340@tanstack/svelte-virtual
npm i https://pkg.pr.new/@tanstack/svelte-virtual@1340@tanstack/virtual-core
npm i https://pkg.pr.new/@tanstack/virtual-core@1340@tanstack/vue-virtual
npm i https://pkg.pr.new/@tanstack/vue-virtual@1340commit:
5d760dcpiecyk merged this2343004
piecyk closed this
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: 5d760dcc37e4e5aa864b6ee5483d9a81640c5d7c
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 9 packages
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR