Skip to content

feat(virtual-core): add laneAssignmentMode option#1115

Open
2wheeh wants to merge 11 commits intoTanStack:mainfrom
2wheeh:feat/defer-lane-assign
Open

feat(virtual-core): add laneAssignmentMode option#1115
2wheeh wants to merge 11 commits intoTanStack:mainfrom
2wheeh:feat/defer-lane-assign

Conversation

@2wheeh
Copy link
Copy Markdown

@2wheeh 2wheeh commented Jan 8, 2026

🎯 Changes

resolves #1114

Adds deferLaneAssignment option to defer lane caching until items are measured via measureElement.

#1080 introduced lane assignment caching based on estimateSize for visual stability. However, this broke a core use case for masonry layouts where lane assignments should be based on actual measured sizes, not estimates.

This PR adds a new option deferLaneAssignment (default: false):

  • When false (default): Current behavior - lanes cached immediately from estimateSize
  • When true: Lanes are calculated but not cached until first measurement, then cached based on actual sizes

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.

Summary by CodeRabbit

  • New Features

    • Added laneAssignmentMode (default "estimate") to control when lane assignments are cached: estimate-now vs. defer until measured.
  • Documentation

    • Updated lane/lanes docs to describe shortest-lane assignment, caching behavior, and the new laneAssignmentMode options.
  • Tests

    • Added tests covering lane-assignment caching behavior for both modes.
  • Chores

    • Prepared a minor release entry for this change.

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Jan 8, 2026

🦋 Changeset detected

Latest commit: 8496f50

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 7 packages
Name Type
@tanstack/virtual-core Patch
@tanstack/angular-virtual Patch
@tanstack/lit-virtual Patch
@tanstack/react-virtual Patch
@tanstack/solid-virtual Patch
@tanstack/svelte-virtual Patch
@tanstack/vue-virtual 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

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a new deferLaneAssignment option to @tanstack/virtual-core to support masonry-style layouts where lane assignment should be derived from measured sizes (via measureElement/resizeItem) rather than estimateSize, while keeping the existing “stable lanes via caching” behavior as the default.

Changes:

  • Introduces deferLaneAssignment?: boolean (default false) and conditionally skips lane caching until an item has been measured.
  • Adds tests covering deferred vs immediate lane caching behavior.
  • Updates API docs and adds a changeset for release.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
packages/virtual-core/src/index.ts Adds deferLaneAssignment option + logic to skip caching lanes for unmeasured items
packages/virtual-core/tests/index.test.ts Adds regression tests for deferred/immediate lane caching
docs/api/virtualizer.md Documents new option and clarifies lane caching behavior
docs/api/virtual-item.md Updates lane documentation to mention caching + deferral option
.changeset/loud-insects-itch.md Publishes the change as a patch release

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

packages/virtual-core/src/index.ts:732

  • deferLaneAssignment is now part of the memo deps, but toggling it from falsetrue via setOptions will still keep any existing laneAssignments cached from estimateSize, because getMeasurements prefers cachedLane regardless of deferLaneAssignment. Consider detecting changes to deferLaneAssignment (similar to lanesChangedFlag) and clearing laneAssignments when it changes (at least when switching to true) so the new mode can take effect at runtime.
      this.options.deferLaneAssignment,
    ],
    (count, paddingStart, scrollMargin, getItemKey, enabled, lanes, deferLaneAssignment) => {
      const lanesChanged =
        this.prevLanes !== undefined && this.prevLanes !== lanes

      if (lanesChanged) {
        // Set flag for getMeasurements to handle
        this.lanesChangedFlag = true
      }

      this.prevLanes = lanes
      this.pendingMeasuredCacheIndexes = []


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

2wheeh and others added 2 commits March 8, 2026 01:06
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 19, 2026

📝 Walkthrough

Walkthrough

Added a new Virtualizer option laneAssignmentMode?: 'estimate' | 'measured' (default 'estimate'); docs and tests updated. In 'measured' mode lane assignment caching is deferred until an item is measured, while 'estimate' caches immediately using estimateSize.

Changes

Cohort / File(s) Summary
Changeset
\.changeset/loud-insects-itch\.md
New changeset added declaring a minor release and noting feat(virtual-core): add laneAssignmentMode option.
Documentation
docs/api/virtual-item.md, docs/api/virtualizer.md
Clarified masonry lane assignment behavior; added `laneAssignmentMode?: 'estimate'
Core Implementation
packages/virtual-core/src/index.ts
Added `laneAssignmentMode?: 'estimate'
Tests
packages/virtual-core/tests/index.test.ts
Added Vitest cases covering lane-caching behavior: 'measured' verifies laneAssignments remain empty until items are measured via resizeItem, and 'estimate' verifies immediate caching.

Sequence Diagram(s)

mermaid
sequenceDiagram
participant Client as Client
participant Virtualizer as Virtualizer
participant SizeCache as ItemSizeCache
participant DOM as DOM / measureElement

Client->>Virtualizer: setOptions(laneAssignmentMode)
Client->>Virtualizer: getMeasurements(count, estimateSize)
Virtualizer->>SizeCache: has(measured size)?
alt laneAssignmentMode == 'estimate'
Virtualizer->>Virtualizer: compute lane using estimateSize
Virtualizer->>Virtualizer: cache laneAssignments[item]
else laneAssignmentMode == 'measured'
alt SizeCache has measured size
Virtualizer->>Virtualizer: compute lane using measured size
Virtualizer->>Virtualizer: cache laneAssignments[item]
else not measured
Virtualizer->>Virtualizer: compute lane for layout (do NOT cache)
end
end
Note over DOM,SizeCache: when DOM measurement occurs
DOM->>SizeCache: measureElement -> store size
SizeCache->>Virtualizer: resizeItem(index,size) -> trigger caching for that item

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 I hopped through lanes both short and wide,

I guessed a height, then watched with pride,
If you tell me "wait until you see,"
I'll measure first, then store for thee,
A tiny hop, a perfect glide 🥕

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The PR description references 'deferLaneAssignment' option but the actual code implements 'laneAssignmentMode' as a string enum, creating a mismatch between description and implementation. Update the description to accurately reflect that laneAssignmentMode is a string union type ('estimate' | 'measured') rather than a boolean deferLaneAssignment option.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The implementation successfully addresses issue #1114's core requirement to defer lane caching until measurement, allowing dynamic lane assignment based on actual measured sizes rather than estimates.
Out of Scope Changes check ✅ Passed All changes are directly related to implementing the lane assignment deferral feature: option definition, caching logic, documentation updates, tests, and changeset entry are all in scope.
Title check ✅ Passed The title accurately describes the main change: adding a laneAssignmentMode option to virtual-core to control lane caching behavior.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
.changeset/loud-insects-itch.md (1)

1-5: Consider using minor instead of patch for a new feature.

Adding a new option (deferLaneAssignment) is typically a minor version bump according to semver, since it introduces new functionality. While the default behavior is preserved, this may warrant minor rather than patch depending on your project's versioning conventions.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In @.changeset/loud-insects-itch.md around lines 1 - 5, The changeset currently
marks '@tanstack/virtual-core' as a patch but adds a new feature
(deferLaneAssignment); update the release type from "patch" to "minor" in this
changeset so the version bump follows semver for new functionality—edit the
changeset frontmatter replacing 'patch' with 'minor' for
'@tanstack/virtual-core' (the changeset file is the one that declares the
package and the release type).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In @.changeset/loud-insects-itch.md:
- Around line 1-5: The changeset currently marks '@tanstack/virtual-core' as a
patch but adds a new feature (deferLaneAssignment); update the release type from
"patch" to "minor" in this changeset so the version bump follows semver for new
functionality—edit the changeset frontmatter replacing 'patch' with 'minor' for
'@tanstack/virtual-core' (the changeset file is the one that declares the
package and the release type).

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: cc9990a2-b929-4bb5-8fee-543be8140f8c

📥 Commits

Reviewing files that changed from the base of the PR and between c939785 and e71a42b.

📒 Files selected for processing (5)
  • .changeset/loud-insects-itch.md
  • docs/api/virtual-item.md
  • docs/api/virtualizer.md
  • packages/virtual-core/src/index.ts
  • packages/virtual-core/tests/index.test.ts

…ignment

Verify that lane assignments are cached progressively as items are
measured and that earlier cached lanes remain stable when later items
are measured.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@2wheeh
Copy link
Copy Markdown
Author

2wheeh commented Mar 26, 2026

@piecyk Hi, would love to know if you're open to this feature — and if the API shape needs changes (e.g. boolean vs string union), happy to adjust.

@piecyk
Copy link
Copy Markdown
Collaborator

piecyk commented Mar 30, 2026

@piecyk Hi, would love to know if you're open to this feature — and if the API shape needs changes (e.g. boolean vs string union), happy to adjust.

@2wheeh Sorry for the late reply, and thanks for this! Let's replace deferLaneAssignment?: boolean with enum laneAssignmentMode?: 'estimate' | 'measured' (defaulting to 'estimate'). It better reflects the actual policy choice and gives us room to add more modes later without introducing another breaking API change.

@piecyk
Copy link
Copy Markdown
Collaborator

piecyk commented Apr 13, 2026

@2wheeh any updates on this?

@2wheeh
Copy link
Copy Markdown
Author

2wheeh commented Apr 14, 2026

@piecyk Sorry, been busy working... could work tonight later!

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
packages/virtual-core/tests/index.test.ts (1)

310-325: Title says “default”, but the test sets 'estimate' explicitly.

Line 315 makes this an explicit-mode test, so it doesn’t catch regressions in default option wiring. Consider omitting laneAssignmentMode here to validate the real default path.

Suggested tweak
 test("should cache lanes immediately when laneAssignmentMode is 'estimate' (default)", () => {
   const virtualizer = new Virtualizer({
     count: 4,
     lanes: 2,
     estimateSize: () => 100,
-    laneAssignmentMode: 'estimate',
     getScrollElement: () => null,
     scrollToFn: vi.fn(),
     observeElementRect: vi.fn(),
     observeElementOffset: vi.fn(),
   })
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/virtual-core/tests/index.test.ts` around lines 310 - 325, The test
sets laneAssignmentMode explicitly to 'estimate' but claims to verify the
default; update the Virtualizer instantiation in the test (the object passed to
new Virtualizer in index.test.ts) to omit the laneAssignmentMode property so the
default branch is exercised (or alternatively set it to undefined), keeping the
rest of the setup and the assertion against virtualizer['laneAssignments'].size
unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@packages/virtual-core/tests/index.test.ts`:
- Around line 310-325: The test sets laneAssignmentMode explicitly to 'estimate'
but claims to verify the default; update the Virtualizer instantiation in the
test (the object passed to new Virtualizer in index.test.ts) to omit the
laneAssignmentMode property so the default branch is exercised (or alternatively
set it to undefined), keeping the rest of the setup and the assertion against
virtualizer['laneAssignments'].size unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2a7e41a8-0cf0-412e-8e39-2564610e7822

📥 Commits

Reviewing files that changed from the base of the PR and between cf17381 and 22c6598.

📒 Files selected for processing (5)
  • .changeset/loud-insects-itch.md
  • docs/api/virtual-item.md
  • docs/api/virtualizer.md
  • packages/virtual-core/src/index.ts
  • packages/virtual-core/tests/index.test.ts
✅ Files skipped from review due to trivial changes (2)
  • .changeset/loud-insects-itch.md
  • docs/api/virtual-item.md
🚧 Files skipped from review as they are similar to previous changes (2)
  • docs/api/virtualizer.md
  • packages/virtual-core/src/index.ts

@2wheeh
Copy link
Copy Markdown
Author

2wheeh commented Apr 14, 2026

@piecyk updated

@2wheeh 2wheeh changed the title feat(virtual-core): add deferLaneAssignment option feat(virtual-core): add laneAssignmentMode option Apr 14, 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.

Lane assignments should respect measured sizes, not just estimates

3 participants