Fragiola

API: components

Listbox

A list of options on the page, for one value or several, with the APG's two selection models, ranges and select all, as a form control.

A list of options that stays on the page, for choosing one value or several (the APG listbox pattern). It is a form control: it submits, resets and validates, alone or in a Field. A list that pops up from a button is a Select; a list filtered by a text input, a Combobox or an Autocomplete.

Transfer list
Gallery

Behaviour

Base UI has no Listbox: the APG decides the roles, the keys and the selection models; Select's parts and names are the API pattern.

  • Focus on the options: DOM focus moves to the options (one tab stop, a roving tabindex). Tab enters on the first selected option, else the first one; while the focus stays inside, the tab stop is the focused option (data-highlighted).
  • The keys: the arrows along orientation (mirrored in RTL when horizontal), Home, End, PageUp and PageDown (a page of the scroll container), and typeahead on the options' text. loopFocus wraps at the ends.
  • Two selection models (selectionBehavior): "toggle", the APG's recommended one, never selects on a move (Space and Enter select or toggle); "replace", the alternative one, makes the selection follow the focus (Ctrl moves without selecting, Ctrl+Space toggles).
  • Ranges and select all (multiple): Shift with an arrow moves and toggles, Shift+Space selects from the anchor (the last option toggled), Ctrl+Shift+Home / End select to an end, Ctrl+A selects every option (or none, when they all are); Shift and a press select a range, Ctrl and a press toggle (the replace model).
  • Disabled options take focus, never the selection; ranges and select all skip them.
  • The value is a string or null, or with multiple an array of strings in the list's order.
  • Forms: a visually hidden input carries the value and required (with multiple, one hidden input per value). A form reset returns an uncontrolled list to defaultValue. In a Field, the list is the field's control: named and described by it, aria-invalid on the Root.
  • The collections mechanism: the list model, the selection rules and the key map are the toolkit's /selection entry, which Menu, Tabs or Dialog never import.
  • Long and loaded lists: for thousands of items, or items loaded page by page, use ListboxVirtual: ListboxVirtual.Root (/listbox-virtual) takes every option as items and renders only the ones in view, with the same keys, typeahead, ranges and select all over every item. Its pages can come from a loader through the async list (loading, onLoadMore). It is opt-in: this entry ships no virtualizer.

Anatomy

partelementdata-primitives-partrole
Rootdivlistbox-rootthe listbox; holds the state
Itemdivlistbox-iteman option
ItemIndicatorspanlistbox-item-indicatorrendered only while its item is selected
Groupdivlistbox-groupa group of items
GroupLabeldivlistbox-group-labelnames its Group
Separatordivlistbox-separatorbetween items or groups
(hidden inputs)inputlistbox-inputthe Root's last children: what the form submits and validates

Usage

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

<span id="toppings">Toppings</span>
<Listbox.Root aria-labelledby="toppings" multiple name="toppings" defaultValue={["cheese"]}>
    <Listbox.Item value="cheese">
        Cheese
        <Listbox.ItemIndicator>✓</Listbox.ItemIndicator>
    </Listbox.Item>
    <Listbox.Item value="olives">
        Olives
        <Listbox.ItemIndicator>✓</Listbox.ItemIndicator>
    </Listbox.Item>
</Listbox.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 (v-model:value, @value-change), and Angular as inputs and outputs ([(value)], (valueChangeDetails) with { value, details }).

Root

proptypedefaultdescription
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
orientation"horizontal" | "vertical""vertical"aria-orientation; a horizontal list moves with ArrowLeft and ArrowRight
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

Item

proptypedefaultdescription
valuestringrequired: the value it selects
disabledbooleanfalsetakes focus, never the selection
labelstringits text contentthe text typeahead matches

ItemIndicator

proptypedefaultdescription
keepMountedbooleanfalserendered in every state (Angular: *listboxItemIndicator="true")

Group

No props of its own.

GroupLabel

proptypedefaultdescription
id (angular)stringgeneratedthe id its Group's aria-labelledby points at

Separator

No props of its own.

State and part attributes

partattributes
Rootrole="listbox", aria-orientation, aria-multiselectable, aria-required, aria-disabled, data-orientation, data-multiple, data-disabled; in a Field, the field's states
Itemrole="option", tabindex (0 on the tab stop), aria-selected ("true" or "false"), aria-disabled, data-selected, data-highlighted (the focused option), data-disabled
ItemIndicatoraria-hidden="true"
Grouprole="group", aria-labelledby
Separatorrole="presentation", data-orientation (across the list)

Keyboard

keytoggle modelreplace model
arrows, Home, End, PageUp, PageDown, typeaheadmove the focusmove the focus and select the option alone
Ctrl + a movemove the focus only
Space, Enterselect (single) or toggle (multiple)select the option alone
Ctrl + Spacetoggle (multiple)
Shift + an arrowmove and toggle (multiple)move and toggle (multiple)
Shift + Spaceselect from the anchor, addedselect from the anchor, replacing
Ctrl + Shift + Home / Endselect to the first / last, addedselect to the first / last, replacing
Ctrl + Aselect all, or none when all arethe same

Exports

entryexports
@fragiola/primitives-react/listboxChangeDetails, ChangeReason, Listbox, ListboxItemIndicatorProps, ListboxItemProps, ListboxItemState, ListboxRootMultipleProps, ListboxRootProps, ListboxRootSingleProps, ListboxRootState, RenderProp, SelectionBehavior, SelectionMode
@fragiola/primitives-vue/listboxChangeDetails, ChangeReason, Listbox, ListboxItemIndicatorProps, ListboxItemIndicatorState, ListboxItemProps, ListboxItemState, ListboxRootEmits, ListboxRootProps, ListboxRootState, RenderSlotProps, SelectionBehavior, SelectionMode
@fragiola/primitives-angular/listboxChangeDetails, ChangeReason, ListboxGroup, ListboxGroupLabel, ListboxItem, ListboxItemEntry, ListboxItemIndicator, ListboxItemState, ListboxOrientation, ListboxRoot, ListboxRootState, ListboxSeparator, ListboxValue, SelectionBehavior, SelectionMode, ValueChangeEvent