Fragiola

API: components

Combobox

A text input that filters a popup list, for one value or several with chips, with native form support, following the APG's combobox pattern, in each framework.

A text input that filters a popup list, for choosing one value or several (WAI-ARIA APG combobox pattern with a listbox popup: list autocomplete without automatic selection). It has the parts of Fragiola UI's combobox, on Base UI's behaviour. A list chosen from a button, with no text, is a Select.

Fruit tags
Gallery

Behaviour

These are decided (D10): the APG allows them or says nothing about them.

  • It filters: given items, the combobox filters them with the input's text and gives the List the matching ones, through a render function. The default filter is a locale-aware "contains" that ignores case and accents (sao finds "São Paulo"); filter replaces it, null turns it off, limit caps the list. Without items, the consumer filters and renders the Items.
  • Values are items: a string or any object, compared with isItemEqualToValue and turned into text with itemToStringLabel (the input's text, the filter) and itemToStringValue (the form).
  • multiple keeps a list of values, shown as chips: choosing an item adds it, or removes it when it is already chosen.
  • Escape resets the text: closing the list without a choice resets the input's text to the selected item's label ("" with none, or with multiple); Escape on the closed list clears the text and the value.
  • A press on the input opens the list (reason input-press), on the selected item.
  • Focus: DOM focus stays on the input; the highlighted item is announced with aria-activedescendant. Typing highlights nothing until the user navigates (with autoHighlight, the first item the text keeps, so Enter chooses it); ArrowDown past the last item returns to the input.
  • Forms: hidden inputs carry name and itemToStringValue(value) (one per value with multiple), and a visually hidden input carries required. A form reset returns an uncontrolled combobox to defaultValue. In a Field, the Input takes the field's ids and states.
  • Lifecycle: onOpenChange is called before the change (details.cancel() keeps it as it is, change details), onOpenChangeComplete(open) reports the end of each transition, the actions close() and unmount() act from code, and the Portal's keepMounted keeps the Positioner in the page while closed (hidden, data-closed).
  • Long and loaded lists: for thousands of items, or items loaded page by page, use ComboboxVirtual: ComboboxVirtual.List (/combobox-virtual) replaces the List and renders only the matching items in view, with the same keys over every match. 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
Rootnoneholds the state; renders the hidden inputs
InputGroupdivcombobox-input-groupholds the Input and its buttons, and anchors the list
Inputinputcombobox-inputthe combobox; keeps DOM focus
Triggerbuttoncombobox-triggertoggles the list; not in the tab order
Clearbuttoncombobox-clearclears the value and the text; rendered only while there is a value
Chipsdivcombobox-chipsmultiple: holds the chips and the Input; anchors the list
Chipdivcombobox-chipone per value
ChipRemovebuttoncombobox-chip-removeremoves its chip's value
Valuenonerenders the value: its label, or a render function of it
Statusdivcombobox-statusa polite live region for the consumer's text
Portalnonemoves the Positioner to the end of body
Positionerdivcombobox-positionerplaces the popup under the anchor
Popupdivcombobox-popupthe floating container
Emptydivcombobox-emptya polite live region; its children show only while the list is empty
Listdivcombobox-listthe listbox
Groupdivcombobox-groupa group of items
GroupLabeldivcombobox-group-labelnames its Group
Separatordivcombobox-separatorbetween groups
Itemdivcombobox-iteman option
ItemIndicatorspancombobox-item-indicatorrendered only while its item is selected
(hidden inputs)inputcombobox-hidden-inputwhat the form submits and validates

Usage

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

const FRUITS = [
    { value: "apple", label: "Apple" },
    { value: "banana", label: "Banana" },
];

<Combobox.Root items={FRUITS} name="fruit">
    <label htmlFor="fruit">Fruit</label>
    <Combobox.InputGroup>
        <Combobox.Input id="fruit" />
        <Combobox.Clear aria-label="Clear fruit">×</Combobox.Clear>
        <Combobox.Trigger aria-label="Show fruits">▾</Combobox.Trigger>
    </Combobox.InputGroup>
    <Combobox.Portal>
        <Combobox.Positioner sideOffset={4}>
            <Combobox.Popup>
                <Combobox.Empty>No fruit found.</Combobox.Empty>
                <Combobox.List>
                    {(fruit: (typeof FRUITS)[number]) => (
                        <Combobox.Item key={fruit.value} value={fruit}>
                            {fruit.label}
                        </Combobox.Item>
                    )}
                </Combobox.List>
            </Combobox.Popup>
        </Combobox.Positioner>
    </Combobox.Portal>
</Combobox.Root>;

With multiple, the Chips hold a Combobox.Value whose render function gets the values and renders a Chip per value, then the Input.

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, v-model:input-value, v-model:open), and Angular as inputs and outputs ([(value)], [(inputValue)], [(open)], (valueChangeDetails)).

Root

proptypedefaultdescription
items(Item | { items: Item[] })[]the items, or groups of items: the combobox filters them
filter(item, query, itemToString) => boolean | nulllocale-aware "contains"whether an item matches; null: no filtering. Vue and Angular type it over the consumer's own item (ItemFilter<Country>, containsFilter<Country>() bind with no cast), as isItemEqualToValue and the itemToString* callbacks
limitnumber-1the most items the List renders
localestringthe document'sthe collator's locale
multiplebooleanfalsea list of values, with chips
autoHighlightbooleanfalsetyping highlights the first item the text keeps, so Enter chooses it
valueItem | null, or Item[]the value, controlled
defaultValueItem | null, or Item[]null, or []the initial value, uncontrolled
onValueChange(value, details: { reason }) => voidevery change (item-press, clear-press, chip-remove-press, escape-key, none)
inputValuestringthe input's text, controlled
defaultInputValuestringthe default value's labelthe initial text
onInputValueChange(text: string, details: { reason }) => voidevery text change (input-change, input-clear, item-press, none)
isItemEqualToValue(item, value) => booleanObject.iswhether an item is a value
itemToStringLabel(item) => stringan object's label, else its value, else the iteman item's text
itemToStringValue(item) => stringan object's value, else the iteman item's form value
namestringthe hidden inputs' name (a field's wins)
requiredbooleanfalsethe form cannot submit with no value
disabledbooleanfalsedisables the input and its buttons
openbooleanthe open state, controlled
defaultOpenbooleanfalsethe initial open state
onOpenChange(open: boolean, details: OpenChangeDetails) => voidevery open change (input-press, input-change, list-navigation, trigger-press, item-press, escape-key, outside-press, focus-out); called before the change, details.cancel() stops it
onOpenChangeComplete(open: boolean) => voidonce an opening or a closing has finished its transitions (not on the first render, nor for an exit a reopening interrupted)
actionsRef (react){ current: OverlayActions | null }receives close() (reason imperative-action) and unmount() (ends an exit now); Vue exposes them on the Root's template ref, Angular as the root directive's methods

InputGroup

No props of its own.

Input

No props of its own.

Trigger

No props of its own.

Clear

No props of its own.

Chips

No props of its own.

Chip

No props of its own.

ChipRemove

No props of its own.

Value

No props of its own: its children are the value's label, or a render function of the value (not in Angular).

Status

No props of its own.

Portal

proptypedefaultdescription
container (react, vue)Elementdocument.bodywhere the Positioner goes
keepMounted (react, vue)booleanfalsekeep the content rendered while closed (before the first opening and after an exit), hidden and data-closed; reopening shows the same elements
options (angular)Element | { container?, keepMounted? }{}the same two options: *comboboxPortal="{ keepMounted: true }", or *comboboxPortal="element" for a container alone

Positioner

proptypedefaultdescription
side"top" | "bottom" | "inline-start" | "inline-end" | "left" | "right""bottom"the side of the anchor
align"start" | "center" | "end""start"the alignment along that side
sideOffsetnumber0the distance from the anchor, in px
alignOffsetnumber0the shift along the anchor's edge, in px
collisionPaddingnumber8the space kept from the viewport's edges, in px
anchorElementthe InputGroup, else the Chips, else the Inputthe element to place the popup next to

No props of its own.

Empty

No props of its own.

List

No props of its own: its children are nodes, or, with items, a render function of each matching entry (an item, or a group with its matching items).

Group

No props of its own.

GroupLabel

No props of its own.

Separator

No props of its own.

Item

proptypedefaultdescription
valueItemrequired: the item it chooses
disabledbooleanfalsehighlighted, never chosen

ItemIndicator

proptypedefaultdescription
keepMountedbooleanfalserendered in every state

State and part attributes

partattributes
InputGrouprole="group", data-popup-open, data-disabled
Inputrole="combobox", aria-autocomplete="list", aria-expanded, aria-controls, aria-activedescendant (while an item is highlighted), autocomplete="off", data-popup-open, data-disabled; in a Field, the field's states
Triggertabindex="-1", aria-expanded, aria-controls, data-popup-open, data-disabled
Clear, ChipRemovetype="button", tabindex="-1", data-disabled
Chipsrole="toolbar" while it holds a chip
Chiptabindex="-1", aria-disabled, data-disabled
Status, Emptyrole="status", aria-live="polite", aria-atomic="true"
Positioner, Popupas the Select's
Listrole="listbox", aria-multiselectable with multiple, data-empty
Grouprole="group", aria-labelledby
Separatorrole="presentation", data-orientation="horizontal"
Itemrole="option", aria-selected, aria-disabled, data-highlighted, data-selected, data-disabled
ItemIndicatoraria-hidden="true"

Keyboard

keyaction
typingfilters and opens; nothing is highlighted yet (with autoHighlight, the first match)
ArrowDown / ArrowUpclosed: opens on the selected item, else the first (last); open: the next (previous) item, then back to the input
Alt+ArrowDown / Alt+ArrowUpopens without highlighting / closes
Enterchooses the highlighted item; never submits a form while the list is open
Escapeopen: closes and resets the text; closed: clears the text and the value
Tabcloses without choosing and moves on
Backspacemultiple, empty input: removes the last value
ArrowLeft (ArrowRight in RTL)multiple, caret at the start: focuses the last chip; on a chip, the arrows move between chips and back to the input, Backspace and Delete remove it

Exports

entryexports
@fragiola/primitives-react/comboboxAlign, ChangeDetails, ChangeReason, Combobox, ComboboxItemIndicatorProps, ComboboxItemProps, ComboboxItemState, ComboboxListProps, ComboboxPortalProps, ComboboxPositionerProps, ComboboxPositionerState, ComboboxRootMultipleProps, ComboboxRootProps, ComboboxRootSingleProps, ComboboxValueProps, ItemEquality, ItemFilter, ItemGroup, PositionerProps, RenderProp, Side, OpenChangeDetails, OverlayActions, OverlayPortalProps, OverlayRootProps
@fragiola/primitives-vue/comboboxAlign, ChangeDetails, ChangeReason, Combobox, ComboboxDisabledState, ComboboxFilter, ComboboxItemEquality, ComboboxItemIndicatorProps, ComboboxItemIndicatorState, ComboboxItemProps, ComboboxItemState, ComboboxItemToString, ComboboxListSlots, ComboboxListState, ComboboxOpenState, ComboboxPopupState, ComboboxPortalProps, ComboboxPositionerState, ComboboxRootEmits, ComboboxRootProps, ComboboxValueSlots, ItemEquality, ItemFilter, ItemGroup, PositionerProps, RenderSlotProps, Side, OpenChangeDetails, OverlayActions, OverlayPortalProps, OverlayRootEmits, OverlayRootProps
@fragiola/primitives-angular/comboboxAlign, ChangeDetails, ChangeReason, ComboboxChip, ComboboxChipRemove, ComboboxChips, ComboboxClear, ComboboxEmpty, ComboboxGroup, ComboboxGroupLabel, ComboboxInput, ComboboxInputGroup, ComboboxItem, ComboboxItemIndicator, ComboboxItemInfo, ComboboxItemState, ComboboxItemValue, ComboboxList, ComboboxPopup, ComboboxPortal, ComboboxPositioner, ComboboxRoot, ComboboxSeparator, ComboboxState, ComboboxStatus, ComboboxTrigger, ItemEquality, ItemFilter, ItemGroup, OpenChangeEvent, PendingHighlight, Side, ValueChangeEvent, injectCombobox, OpenChangeDetails, OverlayActions, OverlayPortalOptions