Skip to content

Conversation

@soren121
Copy link
Contributor

@soren121 soren121 commented Feb 27, 2025

Fixes #936
Fixes #665

This PR resolves two issues related to items being "missed" (i.e. unexpectedly cut-off or not rendered) in masonry layouts.

The first problem is that calculateRange was not changed when we added support for masonry layouts. This function assumes that items are purely sequential in the layout: it finds an item close to the scroll offset, and expands forward until it finds an item that exceeds the window. Masonry layouts do not behave like this. Even though our measurements array is one-dimensional, the view is two-dimensional. Items which are far apart in measurements may still be rendered side-by-side, and that needs to be accounted for.

The solution I came up with is to expand the range in both directions until we've found every item that should be in-view in each lane. This feels somewhat naive, and perhaps there's a more performant solution, but I think for the majority of masonry layouts, it should perform just fine.

The second problem is in the same vein: getTotalSize misses some items because it's not always true that the last items in each lane will be the last N items in measurements (where N is the number of lanes.) This is because measurements is sorted by the start position, and if you have very uneven item sizes, the end positions could be very different from each other. I changed this to work in a similar way: it scans from the bottom up until it's found end positions for all lanes, and then selects the maximum.

Both of the issues can be seen in the reproduction link in #936.

@nx-cloud
Copy link

nx-cloud bot commented Feb 27, 2025

View your CI Pipeline Execution ↗ for commit 88780cf.

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 2m 26s View ↗
nx run-many --target=build --exclude=examples/** ✅ Succeeded 18s View ↗

☁️ Nx Cloud last updated this comment at 2025-03-08 01:19:47 UTC

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 27, 2025

Open in Stackblitz

More templates

@tanstack/angular-virtual

npm i https://pkg.pr.new/@tanstack/angular-virtual@937

@tanstack/lit-virtual

npm i https://pkg.pr.new/@tanstack/lit-virtual@937

@tanstack/solid-virtual

npm i https://pkg.pr.new/@tanstack/solid-virtual@937

@tanstack/react-virtual

npm i https://pkg.pr.new/@tanstack/react-virtual@937

@tanstack/svelte-virtual

npm i https://pkg.pr.new/@tanstack/svelte-virtual@937

@tanstack/virtual-core

npm i https://pkg.pr.new/@tanstack/virtual-core@937

@tanstack/vue-virtual

npm i https://pkg.pr.new/@tanstack/vue-virtual@937

commit: 88780cf

@soren121 soren121 closed this Feb 27, 2025
@soren121
Copy link
Contributor Author

I closed this temporarily because I was still hitting problems in my application, but I was able to verify that the bug was just in my application and not this PR.

@soren121 soren121 reopened this Feb 27, 2025
@changeset-bot
Copy link

changeset-bot bot commented Mar 8, 2025

⚠️ No Changeset found

Latest commit: 06fa335

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

Copy link
Collaborator

@piecyk piecyk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks @soren121 🥇

@piecyk piecyk merged commit 02ef309 into TanStack:main Mar 12, 2025
4 checks passed
@github-actions github-actions bot mentioned this pull request Mar 12, 2025
@soren121 soren121 deleted the long-masonry branch March 17, 2025 06:37
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.

Range is calculated incorrectly for masonry layouts, causing items to be missed Total size is calculated incorrectly for masonry grids

3 participants