Skip to content

docs: update virtual table example with simplified and universal transform logic #982

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sergiidiak
Copy link

@sergiidiak sergiidiak commented Apr 12, 2025

The current example works only for tables with equal row height and is unnecessarily complicated for static-positioned rows.

The proposed change is to use the first virtual row's start position of the current visible set for as a transform Y offset

How it works
For relatively positioned virtualized rows with dynamic or static heights, all currently rendered rows need to be offset by the starting position of the first virtual row in the current visible set. This offset (virtualRows[0].start) represents the accumulated height of all the virtual rows that have been rendered before the current set. By applying translateY(${virtualRows[0].start}px) to all rendered rows, we shift the entire visible block down to its correct starting point within the larger virtualized scrollable area.

In simpler terms: Imagine the entire virtual list. virtualRows[0].start tells us how far down we've scrolled. We then render a small window of rows. To place this window correctly, we need to shift the entire window down by that scrolled amount. That's what translateY ${virtualRows[0].start}px achieves for all the currently rendered rows.

How to test:
Apply the above change in this example and see it works:
https://tanstack.com/virtual/latest/docs/framework/react/examples/table

Make the height dynamic, and observe the example works for this variant

…sform logic

The current example works only for tables with equal row height and is unnecessarily complicated for static-positioned rows.

The proposed change is to use the first virtual row's start position for 'padding' statically-positioned rows.

For relatively positioned virtualized rows with dynamic heights, all currently rendered rows need to be offset by the starting position of the first virtual row in the current visible set. This offset (virtualRows[0].start) represents the accumulated height of all the virtual rows that have been rendered before the current set. By applying translateY(${virtualRows[0].start}px) to all rendered rows, we shift the entire visible block down to its correct starting point within the larger virtualized scrollable area.
Copy link

changeset-bot bot commented Apr 12, 2025

⚠️ No Changeset found

Latest commit: 78dbe70

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant