Fragiola

API: components

ComboboxVirtual

A Combobox List over thousands of items that renders only the matches in view, the keys reaching every match, with async pages filtered by the text.

A Combobox for a long or loaded list: ComboboxVirtual.List replaces the Combobox's List and renders only the matching items in view (with 10 000 items, a few dozen options in the DOM), while the input's keys and aria-activedescendant reach every match. The items can come from a loader, filtered by the input's text, through useAsyncList. It is opt-in, in an entry of its own, /combobox-virtual: a Combobox 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 Combobox's (the APG combobox pattern): a virtual list behaves as the same list rendered whole.

Async user search
Gallery

Behaviour

  • The Combobox's part: the List is a Combobox List (data-primitives-part="combobox-list", the same role and state attributes, data-empty included), so a Combobox recipe and its selectors apply. Its options are Combobox.Items that you render for each row in view.
  • It needs the Root's items, flat (no groups); the List renders the matching ones (the filtered items, as the List's own render function).
  • Only the rows in view are rendered, plus overscan rows beyond each end and the pinned rows: the first and last matches, the highlighted one with its neighbours and the selected one. So the arrows, the wrap through the input and opening on the first, last or selected item behave as with every item rendered.
  • The place in the set: each rendered option has aria-setsize (the number of matching items), aria-posinset (its index + 1) and data-index.
  • Sizes: each row starts at itemSize (36 px) and is measured once rendered (measure); measure={false} keeps every row at itemSize (fixed sizes).
  • Loading: loading sets data-loading and aria-busy="true" on the List; onLoadMore runs when the last row comes into view while not loading (load more on scroll end). A Status can say "Loading…" meanwhile.
  • Async filtering: with filter={null}, send the input's text to the async list (setFilterText): a new text aborts the page in flight, and a stale page never shows.

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, make the List the popup's scroll container (a height and overflow-y: auto on it), and set itemSize to the typical row height.

Anatomy

The Combobox's parts, the List replaced:

partelementdata-primitives-partrole
Listdivcombobox-listthe listbox, a Combobox List that renders the matching items in view
(options)divcombobox-itemyour Combobox.Item for each row in view

Usage

import { Combobox, ComboboxVirtual } from "@fragiola/primitives-react/combobox-virtual";

<Combobox.Root items={ITEMS} name="item">
    <label htmlFor="item">Item</label>
    <Combobox.InputGroup>
        <Combobox.Input id="item" />
    </Combobox.InputGroup>
    <Combobox.Portal>
        <Combobox.Positioner sideOffset={4}>
            <Combobox.Popup>
                <Combobox.Empty>No item found.</Combobox.Empty>
                <ComboboxVirtual.List className="flex max-h-80 flex-col overflow-y-auto">
                    {(item: Item) => <Combobox.Item value={item}>{item.label}</Combobox.Item>}
                </ComboboxVirtual.List>
            </Combobox.Popup>
        </Combobox.Positioner>
    </Combobox.Portal>
</Combobox.Root>;

Loaded and filtered by a server, with useAsyncList:

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

<Combobox.Root
    items={list.items}
    filter={null}
    onInputValueChange={(text) => list.setFilterText(text)}
>
    {/* the Input, then in the Popup: */}
    <Combobox.Status>{list.loading ? "Loading…" : ""}</Combobox.Status>
    <ComboboxVirtual.List loading={list.loading} onLoadMore={list.loadMore}>
        {(item: Item) => <Combobox.Item value={item}>{item.label}</Combobox.Item>}
    </ComboboxVirtual.List>
</Combobox.Root>;

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 (@load-more), and Angular as inputs and outputs ((loadMore)). The other parts are the Combobox's.

List

proptypedefaultdescription
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 matching item in view, with its index (an Angular template renders rows()).

State and part attributes

Everything of the Combobox's List and Items, plus:

partattributes
Listdata-loading and aria-busy="true" while loading
each rendered optionaria-setsize (the number of matching 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 Combobox's keys, over every matching item: the highlighted item is rendered and scrolled into view.

Exports

entryexports
@fragiola/primitives-react/combobox-virtualCombobox, ComboboxVirtual, ComboboxVirtualListProps, VirtualListProps
@fragiola/primitives-vue/combobox-virtualCombobox, ComboboxVirtual, ComboboxVirtualListProps, ComboboxVirtualListSlots, VirtualListEmits, VirtualListProps
@fragiola/primitives-angular/combobox-virtual* (every export of the Combobox entry), ComboboxVirtualList, ComboboxVirtualRow, VirtualItemSize, VirtualRowData