virtual icon indicating copy to clipboard operation
virtual copied to clipboard

fix(react-virtual): window virtualizer scroll persistence

Open wlemahieu opened this issue 6 months ago • 1 comments

🎯 Changes

Fix scroll position persistence in useWindowVirtualizer when navigating between routes

This PR removes the automatic initialOffset: () => window.scrollY from useWindowVirtualizer that was causing scroll position to persist incorrectly when navigating between routes in single-page applications.

The Problem

When using useWindowVirtualizer in a SPA with client-side routing:

  1. User scrolls down on Route A (non-virtualized content)
  2. User navigates to Route B (virtualized list)
  3. Bug: Route B starts at the same scroll position as Route A instead of at the top

Open issues:

https://github.com/TanStack/virtual/issues/997 https://github.com/TanStack/router/issues/4107

The Solution

  • Removed the default initialOffset: () => window.scrollY from useWindowVirtualizer
  • The virtualizer now uses the default initialOffset of 0 (from virtual-core)
  • Users can still explicitly set initialOffset when needed (e.g., for SSR)

Tests Added

  • Test verifying useWindowVirtualizer doesn't set initialOffset from window.scrollY by default
  • Test verifying users can still explicitly set initialOffset when needed

This change aligns with the API documentation which states that initialOffset is "usually only useful if you are rendering the virtualizer in a SSR environment."

Checklist

  • I have followed the steps listed in the https://github.com/TanStack/config/blob/main/CONTRIBUTING.md.
  • I have tested and linted this code locally.
  • I have generated a https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md for this PR, or this PR should not release a new version.

wlemahieu avatar Aug 14 '25 22:08 wlemahieu

⚠️ No Changeset found

Latest commit: b11ebdee3f8465fc4d13648d1e9321879ba9d801

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

changeset-bot[bot] avatar Aug 14 '25 22:08 changeset-bot[bot]