Fragiola

API: components

ListboxVirtual

A Listbox over thousands of options that renders only the ones in view, with the keys, the typeahead, ranges and select all over every option, and async pages.

A Listbox for a long or loaded list: ListboxVirtual.Root takes every option as items and renders only the ones in view (with 10 000 items, a few dozen options in the DOM), while the keys, the typeahead, the ranges, select all and the form reach every item. Its pages can come from a loader through useAsyncList. It is opt-in, in an entry of its own, /listbox-virtual: a Listbox that does not use it ships no virtualizer.

The APG has no virtual list pattern; it only says that a set whose options are not all in the DOM gives each option aria-setsize and aria-posinset. Everything else is the Listbox's (the APG listbox pattern): a virtual list behaves as the same list rendered whole.

Behaviour

  • The Listbox's part: the Root is a Listbox Root (data-primitives-part="listbox-root", the same roles and state attributes), so a Listbox recipe and its selectors apply. Its options are Listbox.Items that you render for each row in view.
  • Only the rows in view are rendered, plus overscan rows beyond each end and the pinned rows: the focused option and the tab stop (the first selected, else the first). Scrolling the focused option out of view keeps it in the DOM and focused.
  • The keys over every item: the arrows, Home, End, PageUp and PageDown (a page by the rows' sizes), the typeahead over every label, Shift and Ctrl ranges and Ctrl+A. A key scrolls its target into view, renders it within the event and focuses it.
  • The place in the set: each rendered option has aria-setsize (the number of items), aria-posinset (its index + 1) and data-index.
  • Sizes: each row starts at itemSize (36 px) and is measured once rendered (measure), so rows may differ; measure={false} keeps every row at itemSize (fixed sizes). A measured row above the view that changes size keeps the rows in view still.
  • Loading: loading sets data-loading and aria-busy="true" on the Root; onLoadMore runs when the last row comes into view while not loading (load more on scroll end).
  • Forms: the hidden inputs carry every selected value, rendered or not.
  • Vertical only: a virtual list has no orientation, and no groups or separators (a flat list).

The row style

The rows keep the list's flow layout: no wrapper and no spacer element. Each rendered option gets, inline, the room of the unrendered rows before it as margin-block-start (the last one, the room after it as margin-block-end), flex-shrink: 0 and overflow-anchor: none; the list's row-gap is read and counted. So:

  • lay the options out in a flex or grid column, with no margin of their own (space them with their padding or the list's gap);
  • give the Root a height and overflow-y: auto (it is its own scroll container), or put it in an element that scrolls;
  • set itemSize to the typical row height, so the scrollbar is right before the rows are measured.

Anatomy

partelementdata-primitives-partrole
Rootdivlistbox-rootthe listbox, a Listbox Root that renders its options from items
(options)divlistbox-itemyour Listbox.Item for each row in view
(hidden inputs)inputlistbox-inputthe Root's last children: what the form submits and validates

Usage

import { Listbox, ListboxVirtual } from "@fragiola/primitives-react/listbox-virtual";

const ITEMS = Array.from({ length: 10_000 }, (_, i) => ({
    value: `item-${i + 1}`,
    label: `Item ${i + 1}`,
}));

<ListboxVirtual.Root
    aria-label="Items"
    items={ITEMS}
    multiple
    name="items"
    className="flex max-h-80 flex-col overflow-y-auto"
>
    {(item) => (
        <Listbox.Item value={item.value} disabled={item.disabled}>
            {item.label}
            <Listbox.ItemIndicator>✓</Listbox.ItemIndicator>
        </Listbox.Item>
    )}
</ListboxVirtual.Root>;

With useAsyncList, the items come from a loader, page by page:

import { useAsyncList } from "@fragiola/primitives-react/async-list";

const list = useAsyncList({ load: loadItems });

<ListboxVirtual.Root
    aria-label="Items"
    items={list.items}
    loading={list.loading}
    onLoadMore={list.loadMore}
>
    {(item) => <Listbox.Item value={item.value}>{item.label}</Listbox.Item>}
</ListboxVirtual.Root>;

The render function gets each row's item and index, and returns a Listbox.Item with the item's value (and disabled); the Root adds the row's attributes and style to it.

Props

Every part also takes what the primitive contract gives: render (Vue: as and the render slot), a class and a style, a ref, and any attribute, which it forwards. The names below are React's; Vue binds them the same way (v-model:value, @value-change, @load-more), and Angular as inputs and outputs ([(value)], (valueChangeDetails), (loadMore)).

Root

proptypedefaultdescription
items{ value: string; label: string; disabled?: boolean }[]required: every option, in the list's order (the labels typeahead matches, the disabled options a range skips)
multiplebooleanfalseseveral options can be selected (aria-multiselectable)
valuestring | null, or string[]the value, controlled
defaultValuestring | null, or string[]null, or []the initial value, uncontrolled
onValueChange(value, details: { reason }) => voidevery change (item-press, keyboard, none for a form reset)
selectionBehavior"toggle" | "replace""toggle"the APG's recommended model, or the alternative one where the selection follows the focus
loopFocusbooleantruethe arrows wrap at the ends
disabledbooleanfalseevery option is disabled and none takes focus
namestringthe hidden inputs' name (a field's wins)
requiredbooleanfalsethe form cannot submit with nothing selected
itemSizenumber | (index: number) => number36a row's size in px before it is measured (every row's with measure off): the typical row height
measurebooleantruemeasure the rendered rows; false: fixed sizes
overscannumber4the rows rendered beyond each end of the view
loadingbooleanfalsethe list is loading: data-loading, aria-busy="true"
onLoadMore() => voidthe last row came into view while not loading: load the next page (Vue @load-more, Angular (loadMore))

React's children and Vue's default slot render each row (an Angular template renders rows()).

State and part attributes

Everything of the Listbox's Root and Items, plus:

partattributes
Rootdata-loading and aria-busy="true" while loading; aria-orientation="vertical"
each rendered optionaria-setsize (the number of items), aria-posinset (its index + 1), data-index (its index); inline margin-block-start / margin-block-end when unrendered rows come before / after it, flex-shrink: 0, overflow-anchor: none

Keyboard

The Listbox's keys, over every item: PageUp and PageDown move a page by the rows' sizes, and the target of a key is scrolled into view and focused.

Exports

entryexports
@fragiola/primitives-react/listbox-virtualChangeDetails, ChangeReason, Listbox, ListboxVirtual, ListboxVirtualItem, ListboxVirtualRootMultipleProps, ListboxVirtualRootProps, ListboxVirtualRootSingleProps, RenderProp, SelectionBehavior, SelectionMode, VirtualListProps
@fragiola/primitives-vue/listbox-virtualChangeDetails, ChangeReason, Listbox, ListboxVirtual, ListboxVirtualItem, ListboxVirtualRootEmits, ListboxVirtualRootProps, ListboxVirtualRootSlots, RenderSlotProps, SelectionBehavior, SelectionMode, VirtualListEmits, VirtualListProps
@fragiola/primitives-angular/listbox-virtual* (every export of the Listbox entry), ListboxVirtualItem, ListboxVirtualRoot, ListboxVirtualRow, VirtualItemSize, VirtualRowData