feat(lit-virtual): add setOptions to update virtualizer options after construction #1334
main fromrefs/pull/1334/head·merged 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
undefinedvalues so a partial update can't wipe a stored option. - It re-wraps
onChangeso the host still re-renders, pushes the merged options to the virtualizer, and calls_willUpdate(). - Called from
willUpdateit 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
setOptionssupport 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.
- Added
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
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.tspackages/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
setOptionsfor partial option updates. The controller merges defined values, preserves itsonChangewrapper, 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.tsThe base controller stores constructor options and resolves them with an onChangewrapper that requests host updates afterupdateComplete.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.mdsetOptionsmerges 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 updateCompleteSuggested 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:
4 · 1-
Failed checks: 1 warningCheck name Status Explanation Resolution Docstring Coverage Docstring 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 checks: 4 passed-
Check name Status Explanation Title check The title clearly and concisely describes the primary change: adding setOptions to update Lit virtualizer options after construction. Description check The 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 check Issue #1251requires changed Lit options to reach the existing virtualizer after construction.VirtualizerControllerBase.setOptionsaccepts partial options, preserves stored options and defaults, sk…Out of Scope Changes check 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 impleme…Full details: Linked Issues check
-
Explanation
Issue
#1251requires changed Lit options to reach the existing virtualizer after construction.VirtualizerControllerBase.setOptionsaccepts partial options, preserves stored options and defaults, skipsundefined, preserves theonChangewrapper, applies the merged options, and calls_willUpdate(). The tests cover reactivecountchanges, 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.
Comment `@coderabbitai help` to get the list of available commands.-
Hi! Could a maintainer approve the CI workflows for this PR when you have a moment? Thanks.
View your CI Pipeline Execution ↗ for commit 628fb3c10a2bcddef0880c1388dd31b67e4ea7c4
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 18s View ↗ nx run-many --target=build --exclude=examples/**✅ Succeeded 19s View ↗
☁️ Nx Cloud last updated this comment at
2026-10-26 11:35:23UTCMore templates
- @tanstack/virtual-example-angular-chat
- @tanstack/virtual-example-angular-dynamic
- @tanstack/virtual-example-angular-fixed
- @tanstack/virtual-example-angular-infinite-scroll
- @tanstack/virtual-example-angular-padding
- @tanstack/virtual-example-angular-smooth-scroll
- @tanstack/virtual-example-angular-sticky
- @tanstack/virtual-example-angular-table
- @tanstack/virtual-example-angular-variable
- @tanstack/virtual-example-angular-window
- tanstack-lit-virtual-example-dynamic
- tanstack-lit-virtual-example-fixed
- tanstack-react-virtual-example-chat
- tanstack-react-virtual-example-dynamic
- tanstack-react-virtual-example-fixed
- tanstack-react-virtual-example-infinite-scroll
- tanstack-react-virtual-example-padding
- tanstack-react-virtual-example-pretext
- tanstack-react-virtual-example-scroll-padding
- tanstack-react-virtual-example-smooth-scroll
- tanstack-react-virtual-example-sticky
- tanstack-react-virtual-example-table
- tanstack-react-virtual-example-variable
- tanstack-react-virtual-example-window
- tanstack-svelte-virtual-example-dynamic
- tanstack-svelte-virtual-example-fixed
- tanstack-svelte-virtual-example-infinite-scroll
- tanstack-svelte-virtual-example-smooth-scroll
- tanstack-svelte-virtual-example-sticky
- tanstack-svelte-virtual-example-table
- tanstack-marko-virtual-example-chat
- tanstack-marko-virtual-example-chat-pretext
- tanstack-marko-virtual-example-dynamic
- tanstack-marko-virtual-example-fixed
- tanstack-marko-virtual-example-grid
- tanstack-marko-virtual-example-infinite-scroll
- tanstack-marko-virtual-example-padding
- tanstack-marko-virtual-example-pretext
- tanstack-marko-virtual-example-scroll-padding
- tanstack-marko-virtual-example-smooth-scroll
- tanstack-marko-virtual-example-ssr
- tanstack-marko-virtual-example-ssr-fetch
- tanstack-marko-virtual-example-ssr-restore
- tanstack-marko-virtual-example-ssr-slice
- tanstack-marko-virtual-example-sticky
- tanstack-marko-virtual-example-table
- tanstack-marko-virtual-example-variable
- tanstack-marko-virtual-example-window
- tanstack-marko-virtual-example-window-ssr-slice
- tanstack-vue-virtual-example-dynamic
- tanstack-vue-virtual-example-fixed
- tanstack-vue-virtual-example-infinite-scroll
- tanstack-vue-virtual-example-padding
- tanstack-vue-virtual-example-scroll-padding
- tanstack-vue-virtual-example-smooth-scroll
- tanstack-vue-virtual-example-sticky
- tanstack-vue-virtual-example-table
- tanstack-vue-virtual-example-variable
@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@1334commit:
628fb3cThanks, this looks good. One thing:
willUpdateruns before the controllers'hostUpdate()(seeperformUpdatein@lit/reactive-element), sohostIsUpdatingis alwaysfalsewhensetOptionsis called fromwillUpdate. It still works, becauserequestUpdate()does nothing whileisUpdatePendingis true, but the flag and its comment are misleading. The "during a host update" test also callshostUpdate()by hand, in an order Lit never uses. Could you remove the flag and thehostUpdatehook and always callrequestUpdate()? Lit already merges that into the update that is in progress.piecyk approved these changes
piecyk merged this91c2931
piecyk closed this
Sign in to join this conversation.
🦋 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
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