Fragiola

API: components

Autocomplete

A free-text input with suggestions whose value is the text, with the APG's four autocomplete behaviours, filtering and native form support.

A free-text input with a list of suggestions (the APG combobox pattern with list autocomplete; Base UI's Autocomplete). The value is the text: a suggestion fills the input, and any other text is a value too. A value that must be one of the items is a Combobox's.

Search suggestions
Gallery

Behaviour

  • mode: the APG's four autocomplete behaviours, named as Base UI does: list (the list follows the text; a highlight leaves the text as it is), both (the list follows the text and the input completes inline), inline (inline completion over a fixed list), none (a fixed list, the text unchanged).
  • Inline completion (both, inline, the APG's): typing a character highlights the first suggestion that starts with the text and shows it in the input, the part not typed selected, so the next character replaces it; a deletion removes it; the arrows show the highlighted suggestion; leaving the input accepts it (reason input-blur); Escape drops it.
  • Filtering is Combobox's: a locale-aware "contains" (filter, null for none), limit, groups; items may change at any time (suggestions loaded as the text changes), and a highlight whose suggestion is gone is dropped.
  • Opening: typing opens the list, an empty text closes it; a press on the input opens it only with openOnInputClick; ArrowDown opens on the first suggestion. autoHighlight highlights the first suggestion after typing (list, none).
  • Escape closes the list, then clears the text. Enter chooses the highlighted suggestion, or, with none, closes the list and lets the form submit.
  • Focus: DOM focus stays on the input; the highlighted suggestion is announced with aria-activedescendant. Disabled suggestions are highlighted, never chosen.
  • Forms: the Input is the form control (name, required, its own validity), with no hidden input. A form reset returns an uncontrolled autocomplete to defaultValue. In a Field, the Input takes the field's ids and states.
  • Shared with Combobox: through each framework's shared code and the toolkit, never through Combobox's entry, so an app with Autocomplete alone ships no Combobox code.
  • Long and loaded lists: for thousands of items, or items loaded page by page, use AutocompleteVirtual: AutocompleteVirtual.List (/autocomplete-virtual) replaces the List and renders only the matching suggestions 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
InputGroupdivautocomplete-input-groupholds the Input and its buttons, and anchors the list
Inputinputautocomplete-inputthe combobox; keeps DOM focus; the form control
Triggerbuttonautocomplete-triggertoggles the list; not in the tab order
Clearbuttonautocomplete-clearempties the text; rendered only while there is text
Valuenonerenders the value: the text, or a render function of it
Statusdivautocomplete-statusa polite live region for the consumer's text
Portalnonemoves the Positioner to the end of body
Positionerdivautocomplete-positionerplaces the popup under the anchor
Popupdivautocomplete-popupthe floating container
Emptydivautocomplete-emptya polite live region; its children show only while the list is empty
Listdivautocomplete-listthe listbox
Groupdivautocomplete-groupa group of suggestions
GroupLabeldivautocomplete-group-labelnames its Group
Separatordivautocomplete-separatorbetween groups
Itemdivautocomplete-iteman option: a suggestion

Usage

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

<Autocomplete.Root items={CITIES} name="city">
    <label htmlFor="city">City</label>
    <Autocomplete.InputGroup>
        <Autocomplete.Input id="city" />
        <Autocomplete.Clear aria-label="Clear city">×</Autocomplete.Clear>
    </Autocomplete.InputGroup>
    <Autocomplete.Portal>
        <Autocomplete.Positioner sideOffset={4}>
            <Autocomplete.Popup>
                <Autocomplete.Empty>No city found.</Autocomplete.Empty>
                <Autocomplete.List>
                    {(city: string) => (
                        <Autocomplete.Item key={city} value={city}>
                            {city}
                        </Autocomplete.Item>
                    )}
                </Autocomplete.List>
            </Autocomplete.Popup>
        </Autocomplete.Positioner>
    </Autocomplete.Portal>
</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 (v-model:value, v-model:open), and Angular as inputs and outputs ([(value)], [(open)], (valueChangeDetails)).

Root

proptypedefaultdescription
valuestringthe text, controlled
defaultValuestring""the initial text, uncontrolled
onValueChange(value: string, details: { reason }) => voidevery change (input-change, item-press, input-blur, clear-press, escape-key, none)
items(Item | { items: Item[] })[]the suggestions, or groups of them
mode"list" | "both" | "inline" | "none""list"the autocomplete behaviour (aria-autocomplete); inline and none never filter
filter(item, query, itemToString) => boolean | nulllocale-aware "contains"whether a suggestion matches the text; null: no filtering
limitnumber-1the most suggestions the List renders
localestringthe document'sthe collator's locale
itemToStringValue(item) => stringan object's label, else its value, else the itema suggestion's text, written into the input
openOnInputClickbooleanfalsea press on the input opens the list
autoHighlightbooleanfalsetyping highlights the first suggestion (list, none)
namestringthe Input's name (a field's wins)
requiredbooleanfalsethe form cannot submit with no text
disabledbooleanfalsedisables the input and its buttons
openbooleanthe open state, controlled
defaultOpenbooleanfalsethe initial open state
onOpenChange(open: boolean, details: OpenChangeDetails) => voidevery open change (input-change, input-clear, input-press, list-navigation, trigger-press, item-press, escape-key, outside-press, focus-out, none); called before the change, details.cancel() stops it
onOpenChangeComplete(open: boolean) => voidonce an opening or a closing has finished its transitions
actionsRef (react){ current: OverlayActions | null }receives close() and unmount(); 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.

Value

No props of its own: its children are the text, or a render function of it (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, hidden and data-closed
options (angular)Element | { container?, keepMounted? }{}the same two options: *autocompletePortal="{ keepMounted: true }"

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 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 (a suggestion, or a group with its matching suggestions).

Group

No props of its own.

GroupLabel

No props of its own.

Separator

No props of its own.

Item

proptypedefaultdescription
valueItemrequired: the suggestion
disabledbooleanfalsehighlighted, never chosen nor completed

State and part attributes

partattributes
InputGrouprole="group", data-popup-open, data-disabled
Inputrole="combobox", aria-autocomplete (the mode), aria-expanded, aria-controls, aria-activedescendant (while a suggestion is highlighted), autocomplete="off", name, required, data-popup-open, data-disabled; in a Field, the field's states
Triggertabindex="-1", aria-expanded, aria-controls, data-popup-open, data-disabled
Cleartype="button", tabindex="-1", data-disabled
Status, Emptyrole="status", aria-live="polite", aria-atomic="true"
Positioner, Popupas the Combobox's
Listrole="listbox", data-empty
Grouprole="group", aria-labelledby
Separatorrole="presentation", data-orientation="horizontal"
Itemrole="option", aria-disabled, data-highlighted, data-disabled; no aria-selected

Keyboard

keyaction
typingchanges the text and filters; opens the list (an empty text closes it)
ArrowDown / ArrowUpclosed: opens on the first (last) suggestion; open: the next (previous), then back to the input
Alt+ArrowDown / Alt+ArrowUpopens without highlighting / closes
Enterchooses the highlighted suggestion; with none, closes the list and lets the form submit
Escapeopen: closes the list, the text kept; closed: clears the text
Tabcloses the list and moves on (both, inline: accepts the completion)

Exports

entryexports
@fragiola/primitives-react/autocompleteAlign, Autocomplete, AutocompleteItemProps, AutocompleteItemState, AutocompleteListProps, AutocompleteMode, AutocompletePortalProps, AutocompletePositionerProps, AutocompletePositionerState, AutocompleteRootProps, AutocompleteValueProps, ChangeDetails, ChangeReason, ItemFilter, ItemGroup, OpenChangeDetails, OverlayActions, OverlayPortalProps, OverlayRootProps, PositionerProps, RenderProp, Side
@fragiola/primitives-vue/autocompleteAlign, Autocomplete, AutocompleteDisabledState, AutocompleteFilter, AutocompleteItemProps, AutocompleteItemState, AutocompleteItemToString, AutocompleteListSlots, AutocompleteListState, AutocompleteMode, AutocompleteOpenState, AutocompletePopupState, AutocompletePortalProps, AutocompletePositionerState, AutocompleteRootEmits, AutocompleteRootProps, AutocompleteValueSlots, ChangeDetails, ChangeReason, ItemFilter, ItemGroup, OpenChangeDetails, OverlayActions, OverlayPortalProps, OverlayRootEmits, OverlayRootProps, PositionerProps, RenderSlotProps, Side
@fragiola/primitives-angular/autocompleteAlign, AutocompleteClear, AutocompleteEmpty, AutocompleteGroup, AutocompleteGroupLabel, AutocompleteInput, AutocompleteInputGroup, AutocompleteItem, AutocompleteItemInfo, AutocompleteItemState, AutocompleteItemValue, AutocompleteList, AutocompleteMode, AutocompletePending, AutocompletePopup, AutocompletePortal, AutocompletePositioner, AutocompleteRoot, AutocompleteSeparator, AutocompleteShown, AutocompleteState, AutocompleteStatus, AutocompleteTrigger, ChangeDetails, ChangeReason, ItemFilter, ItemGroup, OpenChangeDetails, OpenChangeEvent, OverlayActions, OverlayPortalOptions, Side, ValueChangeEvent, injectAutocomplete