Skip to content
Source
Sign inSign up
TanStack/virtual

feat(lit-virtual): add setOptions to update virtualizer options after construction #1334

Mergeditisrmk(GitHub profile, opens in a new tab)merged 2 commits intomain fromrefs/pull/1334/headmerged by piecyk

🎯 Changes

Fixes #1251.

VirtualizerController only took options in its constructor, so options that come from reactive Lit properties (count, estimateSize, horizontal, and so on) never reached the virtualizer after the first render. Calling getVirtualizer().setOptions(...) directly isn't a workaround, because it replaces the options wholesale and drops the controller's onChange wrapper that re-renders the host.

This adds setOptions to VirtualizerController and WindowVirtualizerController:

  • It merges a partial update over the options the controller already holds (including the element/window defaults), and skips undefined values so a partial update can't wipe a stored option.
  • It re-wraps onChange so the host still re-renders, pushes the merged options to the virtualizer, and calls _willUpdate().
  • Called from willUpdate it doesn't schedule an extra host update, since that render pass hasn't happened yet. Called anywhere else it requests one.
protected willUpdate(changed: PropertyValues<this>) {
  if (changed.has('count')) {
    this.virtualizerController.setOptions({ count: this.count })
  }
}

This picks up the feedback on #1253 (closed without merging): the test now checks the rendered total size (500 then 1000) instead of waiting for an item index the viewport can't show, the changeset is minor because it's new public API, and docs/framework/lit/lit-virtual.md plus the package README now cover setOptions and why to use it instead of getVirtualizer().setOptions(...).

✅ Checklist

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

For @tanstack/lit-virtual I ran vitest (13/13 passing), eslint, tsc, the production build, and publint --strict. I didn't run the full test:pr matrix.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • New Features

    • Added setOptions support to Lit virtualizer controllers, enabling partial, reactive option updates.
    • Existing settings are preserved when values are omitted or undefined.
    • Option changes refresh rendering while preserving configured change callbacks and controller defaults.
  • Documentation

    • Added guidance for updating virtualizer options reactively through controller lifecycle updates.
    • Explained option merging behavior and cautioned against replacing options directly through the underlying virtualizer.
    1. changeset-bot[bot](GitHub profile, opens in a new tab)commented

      🦋 Changeset detected

      Latest commit: 628fb3c10a2bcddef0880c1388dd31b67e4ea7c4

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

      This PR includes changesets to release 1 package
      NameType
      @tanstack/lit-virtualMinor

      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

    2. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Review in Change Stack →

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration
      • Source mode: Auto
      • Repository: Unavailable
      • Branch: Unavailable
      • File: No configuration file from this repository applied
      • Commit: Unavailable
      • Layers: defaults
      • Review profile: CHILL
      • Plan: Advanced
      • Run ID: f5293e5f-4a12-4737-a978-8241b3aa4cd3
      📥 Commits

      Reviewing files that changed from the base of the PR and between 366e5d64ffd4c37970c6dca450180e2fb81f2a8d and 628fb3c10a2bcddef0880c1388dd31b67e4ea7c4.

      📒 Files selected for processing: 2
      • packages/lit-virtual/src/index.ts
      • packages/lit-virtual/tests/set-options.test.ts

      Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

      📝 Walkthrough

      The Lit virtualizer controllers now expose setOptions for partial option updates. The controller merges defined values, preserves its onChange wrapper, and requests host updates when options change. Tests and documentation cover reactive updates.

      Changes: Lit virtualizer option updates

      Layer / File(s)Summary
      Store and resolve controller options
      packages/lit-virtual/src/index.ts
      The base controller stores constructor options and resolves them with an onChange wrapper that requests host updates after updateComplete.
      Apply and validate partial option updates
      packages/lit-virtual/src/index.ts, packages/lit-virtual/tests/set-options.test.ts, docs/framework/lit/lit-virtual.md, packages/lit-virtual/README.md, .changeset/lit-controller-set-options.md
      setOptions merges defined values, ignores unchanged options, and requests a host update when options change. Tests cover callback behavior, host update scheduling, window-controller defaults, and reactive rendering. The documentation and changeset describe the API and its behavior.

      Priority: ➖ Normal

      Estimated code review effort: 3 (Moderate) | ~20 minutes

      Change: Feature · Severity of issue fixed: Medium

      Sequence Diagram(s)

      sequenceDiagram
        participant Host
        participant VirtualizerController
        participant Virtualizer
        Host->>VirtualizerController: Call setOptions with partial options
        VirtualizerController->>Virtualizer: Apply resolved options
        Virtualizer->>VirtualizerController: Invoke wrapped onChange
        VirtualizerController->>Host: Request update after updateComplete

      Suggested reviewers: kadoshms

      Merge Risk: ⚪ Minimal · up to 628fb

      The new options API has no identified merge-blocking issue. Normal checks remain appropriate before merging.

      Pre-merge checks: Passed 4 · Failed 1
      Failed Failed checks: 1 warning
      Check nameStatusExplanationResolution
      Docstring CoverageWarningDocstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files.Write docstrings for the functions missing them to satisfy the coverage threshold.
      Passed Passed checks: 4 passed
      Check nameStatusExplanation
      Title checkPassedThe title clearly and concisely describes the primary change: adding setOptions to update Lit virtualizer options after construction.
      Description checkPassedThe description includes the required Changes, Checklist, and Release Impact sections. It explains the implementation, testing performed, the unrun full test matrix, and the generated changeset.
      Linked Issues checkPassedIssue #1251 requires changed Lit options to reach the existing virtualizer after construction. VirtualizerControllerBase.setOptions accepts partial options, preserves stored options and defaults, sk…
      Out of Scope Changes checkPassedThe source change implements the API required by issue #1251. The tests verify the new behavior. The documentation and minor changeset explain the public API and its intended use. No unrelated impleme…
      Full details: Linked Issues check

      Explanation

      Issue #1251 requires changed Lit options to reach the existing virtualizer after construction. VirtualizerControllerBase.setOptions accepts partial options, preserves stored options and defaults, skips undefined, preserves the onChange wrapper, applies the merged options, and calls _willUpdate(). The tests cover reactive count changes, option merging, callbacks, scheduling, and window defaults.

      Full details: Out of Scope Changes check

      Explanation

      The source change implements the API required by issue #1251. The tests verify the new behavior. The documentation and minor changeset explain the public API and its intended use. No unrelated implementation change is identified.

      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create a new PR
      • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

      Warning

      Some tools did not complete. Review the errors below.

      🔧 ESLint

      If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

      packages/lit-virtual/tests/set-options.test.ts

      Parsing error: "parserOptions.project" has been provided for @typescript-eslint/parser. The file was not found in any of the provided project(s): packages/lit-virtual/tests/set-options.test.ts

      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.
    3. Hi! Could a maintainer approve the CI workflows for this PR when you have a moment? Thanks.

    4. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      View your CI Pipeline Execution ↗ for commit 628fb3c10a2bcddef0880c1388dd31b67e4ea7c4

      CommandStatusDurationResult
      nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded18sView ↗
      nx run-many --target=build --exclude=examples/**✅ Succeeded19sView ↗

      ☁️ Nx Cloud last updated this comment at 2026-10-26 11:35:23 UTC

    5. pkg-pr-new[bot](GitHub profile, opens in a new tab)commented
      More templates

      @tanstack/angular-virtual

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

      @tanstack/lit-virtual

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

      @tanstack/marko-virtual

      npm i https://pkg.pr.new/@tanstack/marko-virtual@1334

      @tanstack/react-virtual

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

      @tanstack/solid-virtual

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

      @tanstack/svelte-virtual

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

      @tanstack/virtual-core

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

      @tanstack/vue-virtual

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

      commit: 628fb3c

    6. Thanks, this looks good. One thing: willUpdate runs before the controllers' hostUpdate() (see performUpdate in @lit/reactive-element), so hostIsUpdating is always false when setOptions is called from willUpdate. It still works, because requestUpdate() does nothing while isUpdatePending is true, but the flag and its comment are misleading. The "during a host update" test also calls hostUpdate() by hand, in an order Lit never uses. Could you remove the flag and the hostUpdate hook and always call requestUpdate()? Lit already merges that into the update that is in progress.

    7. piecyk approved these changes

    8. piecyk merged this91c2931

    9. piecyk closed this

Sign in to join this conversation.