Repository navigation
fix: preserve list scroll offsets when a gap is set - #1564
Merged
Merged
Conversation
offsetOf returned -gap for index zero, so restoreStateFrom lost one gap per cycle and index-zero scrolling ignored explicit offsets and headers. Use the same recurrence that builds the offset tree.
🦋 Changeset detectedLatest commit: f1d106b The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 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 |
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A list that uses a CSS
gaploses one gap of scroll position on everygetState→restoreStateFromcycle, and scrolling to index 0 lands one gap short.Reproduction
mainscrollTop2000, then save withgetStateand remount withrestoreStateFrom, five timesscrollToIndex({ index: 0, offset: 100 })Header:scrollToIndex(0)orscrollIntoView({ index: 0 })scrollIntoView({ index: 0 })can also move a first item that is already aligned.Root cause
offsetOfinsizeSystem.tsreturns-gapfor the first item:restoreStateFromturns the snapshot into "index 0 plus an offset", so each restore subtracts one gap.Fix
This is the recurrence
createOffsetTreealready uses to build the tree. Whenever the old expression was positive it gave the same value, and with a gap of 0 nothing changes. Clamping the old result to 0 would still miss the gap after a zero-size first item.Grouped lists: index scrolling still maps to the flat index after the group header, and the real gap between a group header and its items stays. The first
groupOffsetTreevalue changes from-gapto0; its consumer searches at nonnegative offsets, so the selected group is the same.Testing
sizeSystem.test.tsandlistSystem.test.ts. 7 fail onmain, the 3 controls (index 1, gap 0, grouped) pass on both. Full suite with the fix: 273 passed, 4 skipped.gap-headerstory ande2e/gap-header.test.ts. 4 of the 5 tests fail onmain(the index-1 control passes), all pass with the fix. Full Chromium suite: 65 passed, 1 skipped.typecheck,lint,format:check, andbuildpass.Run on macOS x64 with Node 22.14.0 and Node 26.0.0. Firefox, WebKit, CI's Node 24, and horizontal / window-scroll / custom-scroll-parent variants of the new tests were not run. On Node 26 the untouched
reactive-engine-storagetests fail onmaintoo, so that package is not in the numbers above.Includes a patch changeset for
react-virtuoso.