Virtual List
A variable-height virtualized list that renders only items in the visible window.
Virtual List decouples a complete logical key sequence from physical DOM nodes, creating item views only for the visible window and overscan buffer so large, variable-height lists stay responsive. The examples on this page use the Shadcn Rhea style preset.
Preview
Section titled “Preview”<proto-ui-shadcn-virtual-list-root
id="activity-list"
class="w-full max-w-lg"
>
<proto-ui-shadcn-virtual-list-viewport
id="activity-viewport"
class="h-72"
>
<proto-ui-shadcn-virtual-list-content
id="activity-content"
></proto-ui-shadcn-virtual-list-content>
</proto-ui-shadcn-virtual-list-viewport>
</proto-ui-shadcn-virtual-list-root>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
import { attachWebVirtualList } from '@proto.ui/prototypes-base/virtual-list/web';
const keys = Array.from(
{ length: 1000 },
(_, i) => `event-${String(i + 1).padStart(4, '0')}`
);
const list = document.querySelector('#activity-list');
const viewport = document.querySelector('#activity-viewport');
const content = document.querySelector('#activity-content');
setElementProps(list, {
styleProfile: 'rhea',
itemKeys: keys,
overscanItems: 3,
maxMaterializedItems: 60,
});
const host = attachWebVirtualList({
viewport,
content,
collection: list.getExposes().getCollection(),
estimateSize: 64,
renderItem(key) {
const row = document.createElement('div');
row.className = 'border-b border-border/60 px-4 py-3 text-sm';
row.textContent = `Activity ${key}`;
return row;
},
});
// Call host.dispose() when removing the list.
</script>Installation
Section titled “Installation”Available starting in 0.3.0-rc.1 and later releases. Import the prototypes from @proto.ui/prototypes-shadcn/virtual-list and connect a Web scroll container through @proto.ui/prototypes-base/virtual-list/web.
Virtual List is composed of Root, Viewport, and Content: Root tracks the ordered itemKeys sequence and provides the family style scope, Viewport handles scrolling and keyboard focus, and Content hosts the materialized row elements.
import { AdaptToWebComponent, setElementProps } from '@proto.ui/adapter-web-component';import { virtualListRoot, virtualListViewport, virtualListContent,} from '@proto.ui/prototypes-shadcn/virtual-list';import { attachWebVirtualList } from '@proto.ui/prototypes-base/virtual-list/web';
AdaptToWebComponent(virtualListRoot);AdaptToWebComponent(virtualListViewport);AdaptToWebComponent(virtualListContent);
const list = document.querySelector('shadcn-virtual-list-root')!;const viewport = document.querySelector('shadcn-virtual-list-viewport')!;const content = document.querySelector('shadcn-virtual-list-content')!;
setElementProps(list, { styleProfile: 'rhea', itemKeys: Array.from({ length: 1000 }, (_, i) => `event-${i + 1}`), overscanItems: 3, maxMaterializedItems: 60,});
const host = attachWebVirtualList({ viewport, content, collection: list.getExposes().getCollection(), estimateSize: 64, renderItem(key) { const row = document.createElement('div'); row.className = 'border-b border-border/60 px-4 py-3 text-sm'; row.textContent = key; return row; },});
// Programmatically scroll to a key and dispose on teardownhost.ensureVisible('event-250', 'center');// host.dispose();Anatomy & Accessibility
Section titled “Anatomy & Accessibility”Rootsetsrole="region"witha11yLabeland adapts its radius, border, and surface ring to the activestyleProfile.ViewportinheritsScroll Areascrolling and focus handling, displaying a clipped inset focus ring on keyboard focus.Contentsetsrole="list"; each mounted row receivesrole="listitem",aria-posinset, andaria-setsizeautomatically.
Advanced: Custom Materializers
Section titled “Advanced: Custom Materializers”- Each logical list allows one active materializer at a time;
attachWebVirtualListmanages the lease automatically and cleans up rendered rows and itsResizeObserverwhenRootunmounts or disposes. - Call
dispose()on an active materializer before attaching a replacement. Non-Web hosts can integrate directly throughcollection.acquireMaterializer(onRetired).
Examples
Section titled “Examples”Scroll to Item
Section titled “Scroll to Item”Call ensureVisible(key, align) with 'start', 'center', 'end', or 'nearest' to scroll any logical key into view.
<div class="flex flex-col gap-3">
<div class="flex flex-wrap gap-2">
<proto-ui-shadcn-button id="jump-top">
Jump to #1
</proto-ui-shadcn-button>
<proto-ui-shadcn-button id="jump-mid">
Center #250
</proto-ui-shadcn-button>
<proto-ui-shadcn-button id="jump-end">
Jump to #1000
</proto-ui-shadcn-button>
</div>
<proto-ui-shadcn-virtual-list-root
id="jump-list"
class="w-full max-w-lg"
>
<proto-ui-shadcn-virtual-list-viewport
id="jump-viewport"
class="h-64"
>
<proto-ui-shadcn-virtual-list-content
id="jump-content"
></proto-ui-shadcn-virtual-list-content>
</proto-ui-shadcn-virtual-list-viewport>
</proto-ui-shadcn-virtual-list-root>
</div>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
import { attachWebVirtualList } from '@proto.ui/prototypes-base/virtual-list/web';
const keys = Array.from(
{ length: 1000 },
(_, i) => `item-${String(i + 1).padStart(4, '0')}`
);
const list = document.querySelector('#jump-list');
setElementProps(list, {
styleProfile: 'rhea',
itemKeys: keys,
overscanItems: 3,
maxMaterializedItems: 60,
});
const host = attachWebVirtualList({
viewport: document.querySelector('#jump-viewport'),
content: document.querySelector('#jump-content'),
collection: list.getExposes().getCollection(),
estimateSize: 48,
renderItem(key) {
const row = document.createElement('div');
row.className = 'border-b border-border/60 px-4 py-3 text-sm';
row.textContent = `Release entry ${key}`;
return row;
},
});
document
.querySelector('#jump-top')
.addEventListener('click', () =>
host.ensureVisible('item-0001', 'start')
);
document
.querySelector('#jump-mid')
.addEventListener('click', () =>
host.ensureVisible('item-0250', 'center')
);
document
.querySelector('#jump-end')
.addEventListener('click', () =>
host.ensureVisible('item-1000', 'end')
);
</script>Dynamic Filtering & Empty State
Section titled “Dynamic Filtering & Empty State”Updating itemKeys on Root recomputes the visible window and reconciles mounted rows automatically, making filtering and empty states straightforward.
<div class="flex flex-col gap-3">
<div class="flex flex-wrap gap-2">
<proto-ui-shadcn-button id="filter-all">
All (500)
</proto-ui-shadcn-button>
<proto-ui-shadcn-button id="filter-alerts">
Alerts (125)
</proto-ui-shadcn-button>
<proto-ui-shadcn-button id="filter-empty">
Empty (0)
</proto-ui-shadcn-button>
</div>
<proto-ui-shadcn-virtual-list-root
id="filter-list"
class="w-full max-w-lg"
>
<proto-ui-shadcn-virtual-list-viewport
id="filter-viewport"
class="h-60"
>
<proto-ui-shadcn-virtual-list-content
id="filter-content"
></proto-ui-shadcn-virtual-list-content>
</proto-ui-shadcn-virtual-list-viewport>
</proto-ui-shadcn-virtual-list-root>
</div>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
import { attachWebVirtualList } from '@proto.ui/prototypes-base/virtual-list/web';
const allKeys = Array.from(
{ length: 500 },
(_, i) => `rec-${String(i + 1).padStart(4, '0')}`
);
const alertKeys = allKeys.filter((_, i) => i % 4 === 0);
const list = document.querySelector('#filter-list');
setElementProps(list, {
styleProfile: 'rhea',
itemKeys: allKeys,
overscanItems: 2,
maxMaterializedItems: 50,
});
const host = attachWebVirtualList({
viewport: document.querySelector('#filter-viewport'),
content: document.querySelector('#filter-content'),
collection: list.getExposes().getCollection(),
estimateSize: 58,
renderItem(key) {
const row = document.createElement('div');
row.className = 'border-b border-border/60 px-4 py-3 text-sm';
row.textContent = key;
return row;
},
});
document
.querySelector('#filter-all')
.addEventListener('click', () =>
setElementProps(list, { itemKeys: allKeys })
);
document
.querySelector('#filter-alerts')
.addEventListener('click', () =>
setElementProps(list, { itemKeys: alertKeys })
);
document
.querySelector('#filter-empty')
.addEventListener('click', () =>
setElementProps(list, { itemKeys: [] })
);
</script>Overscan & Bounded Materialization
Section titled “Overscan & Bounded Materialization”Tune overscanItems and maxMaterializedItems to keep the number of mounted DOM rows strictly bounded even across thousands of logical items.
<proto-ui-shadcn-virtual-list-root
id="window-list"
class="w-full max-w-lg"
>
<proto-ui-shadcn-virtual-list-viewport
id="window-viewport"
class="h-60"
>
<proto-ui-shadcn-virtual-list-content
id="window-content"
></proto-ui-shadcn-virtual-list-content>
</proto-ui-shadcn-virtual-list-viewport>
</proto-ui-shadcn-virtual-list-root>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
import { attachWebVirtualList } from '@proto.ui/prototypes-base/virtual-list/web';
const keys = Array.from(
{ length: 2000 },
(_, i) => `row-${String(i + 1).padStart(4, '0')}`
);
const list = document.querySelector('#window-list');
setElementProps(list, {
styleProfile: 'rhea',
itemKeys: keys,
overscanItems: 1,
maxMaterializedItems: 40,
});
const collection = list.getExposes().getCollection();
const host = attachWebVirtualList({
viewport: document.querySelector('#window-viewport'),
content: document.querySelector('#window-content'),
collection,
estimateSize: 44,
renderItem(key) {
const row = document.createElement('div');
row.className =
'border-b border-border/60 px-4 py-2.5 text-sm';
row.textContent = `Metric stream ${key}`;
return row;
},
});
// Inspect bounded materialization at any time:
const snapshot = collection.snapshot();
console.log(
snapshot.keys.length,
snapshot.materializedKeys.length
);
</script>| Prop | Type | Default |
|---|---|---|
itemKeys | readonly string[] | [] |
overscanItems | number | 2 |
maxMaterializedItems | number | 100 |
a11yLabel | string | 'Virtual list' |
styleProfile | 'inherit' 'rhea' 'vega' 'nova' 'maia' 'lyra' 'mira' 'luma' 'sera' 'new-york-v4' | 'inherit' |
Viewport & Content
Section titled “Viewport & Content”| Prop | Type | Default |
|---|---|---|
styleProfile | 'inherit' 'rhea' 'vega' 'nova' 'maia' 'lyra' 'mira' 'luma' 'sera' 'new-york-v4' | 'inherit' |
Root Exposes
Section titled “Root Exposes”| Member | Type | Description |
|---|---|---|
logicalCount | ExposeState<number> | Total number of logical item keys |
getCollection | () => WindowedCollection | Returns the collection model for host attachment |
getWindow | () => WindowSnapshot | Returns the current revision, committed range, and materialized keys |
attachWebVirtualList
Section titled “attachWebVirtualList”| Option / Method | Type | Default / Description |
|---|---|---|
viewport | HTMLElement | Scrollable viewport element (required) |
content | HTMLElement | List element hosting spacers and rows (required) |
collection | WindowedCollection | Collection from Root getCollection() (required) |
renderItem | (key: string) => HTMLElement | Creates a row element for a key; mounted keys are reused (required) |
estimateSize | number | 36 (estimated height in px before measurement) |
refresh() | () => void | Re-evaluates viewport geometry and materialization |
ensureVisible(key, align?) | (key: string, align?: 'nearest' 'start' 'center' 'end') => boolean | Scrolls the target key into view and returns whether it materialized |
dispose() | () => void | Disconnects listeners/observers, clears rows, and releases the lease |