Fragiola

API: components

SelectVirtual

A Select List over thousands of items that renders only the ones in view, with the keys and the typeahead over every item, and async pages.

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

Country list of thousands
Gallery

Behaviour

  • The Select's part: the List is a Select List (data-primitives-part="select-list", the same role and state attributes), so a Select recipe and its selectors apply. Its options are Select.Items that you render for each row in view.
  • It needs the Root's items: every item, the labels known while the list is closed (the Value, the typeahead, the hidden input).
  • Only the rows in view are rendered, plus overscan rows beyond each end and the pinned rows: the highlighted and the selected item. aria-activedescendant always names a rendered option.
  • The keys over every item: End highlights the last of 10 000 items and the typeahead reaches any label; the highlighted item scrolls into view by its offset, also while the popup's opening scale runs.
  • 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); 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 flat list: no Group, GroupLabel or Separator in a virtual 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, 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 Select's parts, the List replaced:

partelementdata-primitives-partrole
Listdivselect-listthe listbox, a Select List that renders the Root's items in view
(options)divselect-itemyour Select.Item for each row in view

Usage

import { Select, SelectVirtual } from "@fragiola/primitives-react/select-virtual";

<Select.Root items={ITEMS} name="item">
    <Select.Trigger aria-label="Item">
        <Select.Value placeholder="Choose an item" />
    </Select.Trigger>
    <Select.Portal>
        <Select.Positioner>
            <Select.Popup>
                <SelectVirtual.List className="flex max-h-80 flex-col overflow-y-auto">
                    {(item) => <Select.Item value={item.value}>{item.label}</Select.Item>}
                </SelectVirtual.List>
            </Select.Popup>
        </Select.Positioner>
    </Select.Portal>
</Select.Root>;

With useAsyncList: items={list.items} on the Root, and loading={list.loading} and onLoadMore={list.loadMore} on the List.

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 Select'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 item of the Root's items in view, with its index (an Angular template renders rows()).

State and part attributes

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

partattributes
Listdata-loading and aria-busy="true" while loading
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 Select's keys, over every item: the highlighted item is rendered and scrolled into view.

Exports

entryexports
@fragiola/primitives-react/select-virtualSelect, SelectItemData, SelectVirtual, SelectVirtualListProps, VirtualListProps
@fragiola/primitives-vue/select-virtualSelect, SelectItemData, SelectVirtual, SelectVirtualListProps, SelectVirtualListSlots, VirtualListEmits, VirtualListProps
@fragiola/primitives-angular/select-virtual* (every export of the Select entry), SelectVirtualList, SelectVirtualRow, VirtualItemSize, VirtualRowData