Virtual list
A list of any length that renders only the rows in view; this one holds 100,000 items.
Example
<focsd-virtual-list id="virtual-list-demo" class="full" label="Products" item-height="36" style="block-size: 16rem"></focsd-virtual-list>
<script type="module">const list = document.getElementById('virtual-list-demo');// The data: 100,000 rows. Each shows as text; set renderItem for richer rows.list.items = Array.from({ length: 100000 }, (_, i) => 'Product ' + (i + 1));</script>Usage
Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.
<script type="module"> import '@focsd-dev/components/focsd-virtual-list.js';</script>
<focsd-virtual-list class="full" label="Products" item-height="36"></focsd-virtual-list>import { VirtualList } from '@focsd-dev/react';
export function Example() { return <VirtualList onChange={(e) => console.log(e.target)} class="full" label="Products" item-height="36"></VirtualList>;}<script setup>import '@focsd-dev/components/focsd-virtual-list.js';</script>
<template> <focsd-virtual-list @change="onChange" class="full" label="Products" item-height="36"></focsd-virtual-list></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/components/focsd-virtual-list.js';
// template<focsd-virtual-list (change)="onChange($event)" class="full" label="Products" item-height="36"></focsd-virtual-list><script> import '@focsd-dev/components/focsd-virtual-list.js';</script>
<focsd-virtual-list onchange={(e) => console.log(e.target)} class="full" label="Products" item-height="36"></focsd-virtual-list>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
item-height | itemHeight | number | undefined | — |
overscan | overscan | number | 5 |
label | label | string | — |
selectable | selectable | 'none' | 'single' | 'multiple' | none |
Methods
| Method | Description |
|---|---|
scrollToIndex(index, { align = 'nearest' }, options) | Scroll so row index is visible (align: start, center, end or nearest). |
Events
| Event | Description |
|---|---|
change | Fired when the selection changes. |
CSS parts
Style the inside of the component with ::part(), for example focsd-virtual-list::part(viewport).
| Part | Description |
|---|---|
viewport | The scrolling area (the list or listbox). |
spacer | The full-height content. |
window | The rendered rows, positioned at their offset. |
row | A row. |
Accessibility
- Rows carry
aria-setsizeandaria-posinset, so screen readers announce "item 4,051 of 100,000". - The scrolling area is focusable, so keyboard users can scroll it.
- With
selectable="single"or"multiple"it is an APG listbox witharia-activedescendant, Shift ranges and Ctrl/⌘ + A. - Rows that grow above the view (images loading) keep what you are reading in place.
Keyboard
| Key | Action |
|---|---|
| Arrow Up / Down | Move the active row (listbox); with selectable="single" the row is also selected |
| Home / End | First / last row (listbox) |
| Page Up / Page Down | Move by a page (listbox), or scroll |
| Space / Enter | Toggle the active row (listbox) |
| Shift + Arrow Up / Down, Home, End, Page Up / Down | Extend the selection (multiple) |
| Ctrl / ⌘ + A | Select all, or clear when all are selected (multiple) |
Ask the docs
Hi, what are you building?
Ask about any component, theming, forms or frameworks. I answer with the right component, a snippet and a link to its docs.