Skip to content

Honor initialTopMostItemIndex and scrollToIndex inside VirtuosoMockContext - #1553

Merged
petyosi merged 3 commits into
mainfrom
petyosi/react-virtuoso-pr-1537-f9eb5f
Oct 29, 2026
Merged

petyosi merged 3 commits into
mainfrom
petyosi/react-virtuoso-pr-1537-f9eb5f

Conversation

@petyosi

@petyosi petyosi commented Oct 29, 2026

Copy link
Copy Markdown
Owner

Fixes #1001. Picks up #1537 by @HawkForge, with a small follow-up.

Tests that render a list with initialTopMostItemIndex under VirtuosoMockContext got an empty list, and imperative scrollToIndex calls had no effect. The list waits for the first scrollTop after the initial scrollTo before it renders items. In jsdom that scrollTop never arrives: the scroller's offsetHeight is 0, so scrollToCallback returns early, and no scroll events fire.

When the mock context is present, the scroller now handles scrollTo itself. It clamps the requested top to the mocked list and viewport heights, publishes it as scrollContainerState, and signals smoothScrollTargetReached for smooth scrolls. This mirrors what the real scroller does when it is already at the target. Without the mock context, nothing changes. Virtuoso, GroupedVirtuoso and TableVirtuoso share this scroller.

Follow-up on top of #1537

  • The mock handler reads totalListHeight through a ref fed by useEmitter, not useEmitterValue. With useEmitterValue, the Scroller would re-render on every list height change in production, where the mock path is never used.
  • More test coverage: clamping near the end, align: 'end', imperative and smooth scrollToIndex, GroupedVirtuoso and TableVirtuoso.

Notes for reviewers

  • In the grouped test, item 19 renders behind the sticky group header. scrollToIndex subtracts the header's height so the target item lands just below it, which matches real-browser behaviour.
  • useWindowScroll and VirtuosoGrid (with VirtuosoGridMockContext) still ignore initialTopMostItemIndex under the mock context. They use separate code paths.

HawkForge and others added 2 commits October 29, 2026 09:02
The scroller drops scrollTo requests when the scroll element has no
layout (offsetHeight 0), which is always the case in jsdom. With
initialTopMostItemIndex set, the list waits for the scroll to land
before rendering items, so it stayed empty. Under VirtuosoMockContext
the scroller now reports the requested position as the new scroll
container state.

Fixes #1001
…ll paths

The mock scrollTo handler read totalListHeight through useEmitterValue, which
made the Scroller re-render on every list height change in production too,
where the mock path is never used. Read it through a ref fed by useEmitter
instead.

Add tests for clamping near the end, align: 'end', imperative and smooth
scrollToIndex, GroupedVirtuoso and TableVirtuoso under VirtuosoMockContext.
@changeset-bot

changeset-bot Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0b9275e

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

The troubleshooting section added in #1540 was not propagated to the generated
skill references, so the Validate Skills drift check failed.
@petyosi
petyosi merged commit cc5d11e into main Oct 29, 2026
8 checks passed
@petyosi
petyosi deleted the petyosi/react-virtuoso-pr-1537-f9eb5f branch October 29, 2026 07:29
@github-actions github-actions Bot mentioned this pull request Oct 29, 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.

[BUG] VirtuosoMockContext not working in conjunction with initialTopMostItemIndex

2 participants