docs: update virtual table example with simplified and universal transform logic #982
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.
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 applyingtranslateY(${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