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-emptyincluded), so an Autocomplete recipe and its selectors apply. Its options areAutocomplete.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
overscanrows 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) anddata-index. - Sizes: each row starts at
itemSize(36 px) and is measured once rendered (measure);measure={false}keeps every row atitemSize(fixed sizes). - Loading:
loadingsetsdata-loadingandaria-busy="true"on the List;onLoadMoreruns when the last row comes into view while notloading(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:
| part | element | data-primitives-part | role |
|---|---|---|---|
| List | div | autocomplete-list | the listbox, an Autocomplete List that renders the matching suggestions in view |
| (options) | div | autocomplete-item | your 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
| prop | type | default | description |
|---|---|---|---|
itemSize | number | (index: number) => number | 36 | a row's size in px before it is measured (every row's with measure off): the typical row height |
measure | boolean | true | measure the rendered rows; false: fixed sizes |
overscan | number | 4 | the rows rendered beyond each end of the view |
loading | boolean | false | the list is loading: data-loading, aria-busy="true" |
onLoadMore | () => void | the 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:
| part | attributes |
|---|---|
| List | data-loading and aria-busy="true" while loading |
| each rendered option | aria-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
| entry | exports |
|---|---|
@fragiola/primitives-react/autocomplete-virtual | Autocomplete, AutocompleteVirtual, AutocompleteVirtualListProps, VirtualListProps |
@fragiola/primitives-vue/autocomplete-virtual | Autocomplete, AutocompleteVirtual, AutocompleteVirtualListProps, AutocompleteVirtualListSlots, VirtualListEmits, VirtualListProps |
@fragiola/primitives-angular/autocomplete-virtual | * (every export of the Autocomplete entry), AutocompleteVirtualList, AutocompleteVirtualRow, VirtualItemSize, VirtualRowData |