Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/lit-virtual/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ class VirtualizerControllerBase<

private readonly virtualizer: Virtualizer<TScrollElement, TItemElement>

private options: VirtualizerOptions<TScrollElement, TItemElement>

private cleanup: () => void = () => {}

constructor(
host: ReactiveControllerHost,
options: VirtualizerOptions<TScrollElement, TItemElement>,
) {
this.options = options
const resolvedOptions: VirtualizerOptions<TScrollElement, TItemElement> = {
...options,
onChange: (instance, sync) => {
Expand All @@ -39,6 +42,21 @@ class VirtualizerControllerBase<
return this.virtualizer
}

public setOptions(
options: Partial<VirtualizerOptions<TScrollElement, TItemElement>>,
) {
this.options = { ...this.options, ...options }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

A plain spread lets an explicit undefined wipe a stored option: setOptions({ estimateSize: undefined }) leaves estimateSize undefined for good, and core then falls back to its own default rather than the value set earlier.

Core's setOptions guards against exactly this (it skips keys whose value is undefined) — worth matching here.

const resolvedOptions: VirtualizerOptions<TScrollElement, TItemElement> = {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This resolvedOptions block is a copy of the one in the constructor, and the two close over different things (options vs this.options). Harmless today, but a shared private resolveOptions(options) used by both would stop them drifting.

...this.options,
onChange: (instance, sync) => {
this.host.updateComplete.then(() => this.host.requestUpdate())
this.options.onChange?.(instance, sync)
},
}
this.virtualizer.setOptions(resolvedOptions)
this.virtualizer._willUpdate()
}

hostConnected() {
this.cleanup = this.virtualizer._didMount()
}
Expand Down
83 changes: 83 additions & 0 deletions packages/lit-virtual/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,86 @@ test('should render virtual items', async () => {
'Element did not render virtual items',
)
})

test('should apply updated options via setOptions', async () => {
@customElement('test-list-setoptions' as any)
class ListSetOptions extends LitElement {
private scrollElementRef: Ref<HTMLDivElement> = createRef()

private virtualizerController: VirtualizerController<
HTMLDivElement,
Element
>

constructor() {
super()
this.virtualizerController = new VirtualizerController(this, {
getScrollElement: () => this.scrollElementRef.value,
count: 10,
estimateSize: () => 50,
observeElementRect: (_, cb) => {
cb({ height, width })
},
})
}

render() {
this.virtualizerController.setOptions({

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Calling setOptions inside render() means the count is 20 from the very first paint, so nothing is actually updated reactively — the test would pass without any reactivity at all.

Driving it from a reactive @property in willUpdate is what the feature is for, and it avoids modelling a pattern that mutates controller state during render.

count: 20,
})
const virtualizer = this.virtualizerController.getVirtualizer()
const virtualRows = virtualizer.getVirtualItems()
return html`
<div class="list scroll-container" ${ref(this.scrollElementRef)}>
<div
style="position: relative; height: ${virtualizer.getTotalSize()}px; width: 100%;"
>
<div
style="position:absolute;top:0;left:0;width:100%;transform:translateY(${virtualRows[0]
? virtualRows[0].start
: 0}px);"
>
${repeat(
virtualRows,
(virtualRow: any) => virtualRow.key,
(virtualRow: any) =>
html` <div
data-index="${virtualRow.index}"
class="${virtualRow.index % 2 === 0
? 'list-item-even'
: 'list-item-odd'}"
>
<div style="padding: 10px 0;">
<div>Row ${virtualRow.index}</div>
<div>Item ${virtualRow.index}</div>
</div>
</div>`,
)}
</div>
</div>
</div>
<style>
.list {
border: 1px solid #e6e4dc;
max-width: 100%;
}
.scroll-container {
height: ${height}px;
width: ${width}px;
overflow-y: auto;
}
</style>
`
}
}

const el = await fixture(
html`<test-list-setoptions></test-list-setoptions>` as any,
)
await elementUpdated(el)
await waitUntil(
() => el.shadowRoot.querySelector('[data-index="15"]'),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is what fails. Index 15 is never rendered: the viewport is 400px with estimateSize: () => 50, so only about indexes 0–9 render at scroll offset 0 — with count: 10 and with count: 20 alike. Raising the count doesn't widen the rendered window, so the test would fail even if setOptions were perfect.

getTotalSize() is what actually moves (500 → 1000). Assert that instead, and assert the initial 500 too, so the test also covers the constructor options — right now nothing checks the starting count of 10 took effect.

The follow-up expect(...).toBeTruthy() on line 197 is also redundant; waitUntil already guarantees it.

'Element did not render items beyond initial count of 10',
)
expect(el.shadowRoot.querySelector('[data-index="15"]')).toBeTruthy()
})