Fragiola

API: components

AutocompleteVirtual

An Autocomplete List over thousands of suggestions that renders only the matches in view, the keys reaching every match, with async pages.

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

Behaviour

  • The Autocomplete's part: the List is an Autocomplete List (data-primitives-part="autocomplete-list", the same role and state attributes, data-empty included), so an Autocomplete recipe and its selectors apply. Its options are Autocomplete.Items that you render for each row in view.
  • It needs the Root's items, flat (no groups); the List renders the matching suggestions.
  • Only the rows in view are rendered, plus overscan rows beyond each end and the pinned rows: the first and last matches and the highlighted one with its neighbours. So the arrows, the wrap through the input and the inline completion behave as with every suggestion rendered.
  • The place in the set: each rendered option has aria-setsize (the number of matching suggestions), 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 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 Autocomplete's parts, the List replaced:

partelementdata-primitives-partrole
Listdivautocomplete-listthe listbox, an Autocomplete List that renders the matching suggestions in view
(options)divautocomplete-itemyour Autocomplete.Item for each row in view

Usage

import { Autocomplete, AutocompleteVirtual } from "@fragiola/primitives-react/autocomplete-virtual";

<Autocomplete.Root items={LABELS} name="search">
    <label htmlFor="search">Search</label>
    <Autocomplete.InputGroup>
        <Autocomplete.Input id="search" />
    </Autocomplete.InputGroup>
    <Autocomplete.Portal>
        <Autocomplete.Positioner sideOffset={4}>
            <Autocomplete.Popup>
                <AutocompleteVirtual.List className="flex max-h-80 flex-col overflow-y-auto">
                    {(label: string) => <Autocomplete.Item value={label}>{label}</Autocomplete.Item>}
                </AutocompleteVirtual.List>
            </Autocomplete.Popup>
        </Autocomplete.Positioner>
    </Autocomplete.Portal>
</Autocomplete.Root>;

Loaded and filtered by a server, with useAsyncList:

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

<Autocomplete.Root
    items={list.items}
    filter={null}
    onValueChange={(text) => list.setFilterText(text)}
>
    {/* the Input, then in the Popup: */}
    <Autocomplete.Status>{list.loading ? "Loading…" : ""}</Autocomplete.Status>
    <AutocompleteVirtual.List loading={list.loading} onLoadMore={list.loadMore}>
        {(label: string) => <Autocomplete.Item value={label}>{label}</Autocomplete.Item>}
    </AutocompleteVirtual.List>
</Autocomplete.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 Autocomplete'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 suggestion in view, with its index (an Angular template renders rows()).

State and part attributes

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

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

Exports

entryexports
@fragiola/primitives-react/autocomplete-virtualAutocomplete, AutocompleteVirtual, AutocompleteVirtualListProps, VirtualListProps
@fragiola/primitives-vue/autocomplete-virtualAutocomplete, AutocompleteVirtual, AutocompleteVirtualListProps, AutocompleteVirtualListSlots, VirtualListEmits, VirtualListProps
@fragiola/primitives-angular/autocomplete-virtual* (every export of the Autocomplete entry), AutocompleteVirtualList, AutocompleteVirtualRow, VirtualItemSize, VirtualRowData