Skip to content

fix: preserve list scroll offsets when a gap is set - #1564

Merged
petyosi merged 1 commit into
petyosi:mainfrom
KooYS:fix/gap-offset-first-item
Nov 11, 2026
Merged

petyosi merged 1 commit into
petyosi:mainfrom
KooYS:fix/gap-offset-first-item

Conversation

@KooYS

@KooYS KooYS commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

A list that uses a CSS gap loses one gap of scroll position on every getState → restoreStateFrom cycle, and scrolling to index 0 lands one gap short.

Reproduction

const List = React.forwardRef(({ style, ...props }, ref) => (
  <div ref={ref} style={{ ...style, display: 'flex', flexDirection: 'column', gap: 10 }} {...props} />
))

<Virtuoso
  ref={ref}
  style={{ height: 300 }}
  totalCount={100}
  components={{ List }}
  itemContent={(index) => <div style={{ height: 30 }}>Item {index}</div>}
/>
Action main This PR
Scroll to scrollTop 2000, then save with getState and remount with restoreStateFrom, five times 1990, 1980, 1970, 1960, 1950 2000 every time
scrollToIndex({ index: 0, offset: 100 }) 90 100
With a 50px Header: scrollToIndex(0) or scrollIntoView({ index: 0 }) 40, a 10px strip of the header stays visible 50

scrollIntoView({ index: 0 }) can also move a first item that is already aligned.

Root cause

offsetOf in sizeSystem.ts returns -gap for the first item:

const itemCount = index - startIndex
const top = size * itemCount + (itemCount - 1) * gap + offset
return top > 0 ? top + gap : top

restoreStateFrom turns the snapshot into "index 0 plus an offset", so each restore subtracts one gap.

Fix

return offset + (index - startIndex) * (size + gap)

This is the recurrence createOffsetTree already 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 groupOffsetTree value changes from -gap to 0; its consumer searches at nonnegative offsets, so the selected group is the same.

Testing

  • Unit: 10 new tests in sizeSystem.test.ts and listSystem.test.ts. 7 fail on main, the 3 controls (index 1, gap 0, grouped) pass on both. Full suite with the fix: 273 passed, 4 skipped.
  • E2E: new gap-header story and e2e/gap-header.test.ts. 4 of the 5 tests fail on main (the index-1 control passes), all pass with the fix. Full Chromium suite: 65 passed, 1 skipped.
  • typecheck, lint, format:check, and build pass.

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-storage tests fail on main too, so that package is not in the numbers above.

Includes a patch changeset for react-virtuoso.

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-bot

changeset-bot Bot commented Nov 11, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f1d106b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
react-virtuoso Patch
@virtuoso.dev/examples Patch

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

@petyosi
petyosi merged commit 0b7cd09 into petyosi:main Nov 11, 2026
@github-actions github-actions Bot mentioned this pull request Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants