Fragiola

API: components

Select

One value chosen from a list popped up by a button, with native form support, following the APG's select-only combobox, in each framework.

Choose one value from a list that pops up from a button (WAI-ARIA APG select-only combobox: a combobox that is not editable, with a listbox popup). It works in a native <form>: it submits, resets and validates as a <select> would. A list the user filters by typing is a Combobox.

Fruit picker
Gallery

Behaviour

  • Placement: the list opens below the trigger (flipping above when there is no room), aligned with its start; there is no alignItemWithTrigger (the behaviour rule: the APG's listbox popup, not Base UI's item alignment).
  • Focus: DOM focus stays on the trigger while the list is open; the highlighted option is announced with aria-activedescendant.
  • Typing while closed: a printable character on the closed trigger opens the list on the first option whose label starts with the typed text (typeahead, reason list-navigation), without choosing it; quick typing extends the search. With no match, nothing happens.
  • Choosing: Enter, Space or a click chooses the highlighted option and closes (reason item-press). Tab chooses the highlighted option and lets focus move on (reason focus-out). A disabled option can be highlighted, never chosen.
  • Close: Escape (escape-key), the trigger (trigger-press) or a press outside (outside-press) close without choosing; focus stays on the trigger.
  • modal (off by default): while open, the page does not scroll and a press outside reaches no element: it only closes the list. Without it, the page keeps its pointer and its scroll.
  • Items: with the Root's items, every label is known while the list is closed. Without it, the Items register while they are rendered, in DOM order, and an item's label is its label, else its ItemText's text, else its text; the Value shows the selected item's label once that item has rendered, else the raw value. Give such a Select the Portal's keepMounted, so the labels are known while it is closed. Groups, their labels and separators are skipped by the keyboard.
  • Lifecycle: onOpenChange is called before the change (details.cancel() keeps it as it is), onOpenChangeComplete(open) reports the end of each transition, the actions close() and unmount() act from code.
  • Forms: a visually hidden input carries name and the value ("" with none). required blocks the submission and focuses the trigger; a form reset returns an uncontrolled Select to defaultValue. In a Field, the trigger takes the field's ids and states.
  • Long and loaded lists: for thousands of items, or items loaded page by page, use SelectVirtual: SelectVirtual.List (/select-virtual) replaces the List and renders only the Root's items in view, with the same keys and typeahead 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
Rootnoneholds the state; renders the hidden input
Triggerbuttonselect-triggerthe combobox; keeps DOM focus
Valuespanselect-valueinside the Trigger: the selected label, or the placeholder
Portalnonemoves the Positioner to the end of body
Positionerdivselect-positionerplaces the popup below the trigger
Popupdivselect-popupthe floating container
Listdivselect-listthe listbox
Itemdivselect-iteman option
ItemTextspanselect-item-textinside an Item: its label, without items
ItemIndicatorspanselect-item-indicatorrendered only while its item is selected
Groupdivselect-groupgroups items under a label
GroupLabeldivselect-group-labelnames its group
Separatordivselect-separatorseparates items or groups
(hidden input)inputselect-inputwhat the form submits and validates

Usage

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

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

<>
    <label htmlFor="fruit">Fruit</label>
    <Select.Root items={FRUITS} name="fruit" required>
        <Select.Trigger id="fruit">
            <Select.Value placeholder="Choose a fruit" />
        </Select.Trigger>
        <Select.Portal>
            <Select.Positioner>
                <Select.Popup>
                    <Select.List>
                        {FRUITS.map((fruit) => (
                            <Select.Item key={fruit.value} value={fruit.value}>
                                {fruit.label}
                                <Select.ItemIndicator>✓</Select.ItemIndicator>
                            </Select.Item>
                        ))}
                    </Select.List>
                </Select.Popup>
            </Select.Positioner>
        </Select.Portal>
    </Select.Root>
</>;

With items, the Root knows every label while the list is closed: for the Value, the typeahead on the closed trigger and the hidden input. Without it, keep the list mounted:

<Select.Root defaultValue="carrot">
    <Select.Trigger>
        <Select.Value placeholder="Choose a food" />
    </Select.Trigger>
    <Select.Portal keepMounted>
        <Select.Positioner>
            <Select.Popup>
                <Select.List>
                    <Select.Group>
                        <Select.GroupLabel>Fruits</Select.GroupLabel>
                        <Select.Item value="apple">
                            <Select.ItemText>Apple</Select.ItemText>
                            <Select.ItemIndicator>✓</Select.ItemIndicator>
                        </Select.Item>
                    </Select.Group>
                    <Select.Separator />
                    <Select.Group>
                        <Select.GroupLabel>Vegetables</Select.GroupLabel>
                        <Select.Item value="carrot">
                            <Select.ItemText>Carrot</Select.ItemText>
                        </Select.Item>
                    </Select.Group>
                </Select.List>
            </Select.Popup>
        </Select.Positioner>
    </Select.Portal>
</Select.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)], (valueChangeDetails)).

Root

proptypedefaultdescription
valuestring | nullthe selected item's value, controlled
defaultValuestring | nullnullthe initial value, uncontrolled
onValueChange(value: string | null, details: { reason }) => voidevery change (item-press, focus-out, none on a form reset)
items{ value, label, disabled? }[]every item (optional: without it, the rendered Items register themselves)
namestringthe hidden input's name (a field's wins)
requiredbooleanfalsethe form cannot submit with no value
disabledbooleanfalsethe trigger opens nothing
modalbooleanfalsewhile open: scroll lock and a blocked outside pointer
openbooleanthe open state, controlled
defaultOpenbooleanfalsethe initial open state
onOpenChange(open: boolean, details: OpenChangeDetails) => voidevery open change, with its reason; 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

Trigger

No props of its own.

Value

proptypedefaultdescription
placeholderstringshown while nothing is selected

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: *selectPortal="{ keepMounted: true }", or *selectPortal="element" for a container alone

Positioner

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

--anchor-width lets the popup be at least as wide as the trigger.

No props of its own.

List

No props of its own.

Item

proptypedefaultdescription
valuestringrequired: its items entry's value
labelstringits ItemText's text, else its textthe label the Value shows and typeahead matches, without the Root's items
disabledbooleanits entry'shighlighted, never chosen

ItemText

No props of its own.

ItemIndicator

proptypedefaultdescription
keepMountedbooleanfalserendered in every state

Group

No props of its own.

GroupLabel

proptypedefaultdescription
id (angular)stringgeneratedthe id its group's aria-labelledby points at; React and Vue take it as a plain attribute

Separator

No props of its own.

State and part attributes

partattributes
Triggerrole="combobox", type="button", aria-haspopup="listbox", aria-expanded, aria-controls and aria-activedescendant (while open), aria-required, disabled, data-popup-open, data-placeholder, data-disabled; in a Field, the field's states
Valuedata-placeholder
Positionerrole="presentation", data-side, data-align, data-open / data-closed; the CSS variables of the Popover's; hidden while kept mounted and closed
Popupdata-side, data-align, data-open / data-closed, data-starting-style, data-ending-style
Listrole="listbox", aria-labelledby
Itemrole="option", aria-selected, aria-disabled, data-highlighted, data-selected, data-disabled
ItemTextnone beyond the part attribute
ItemIndicatoraria-hidden="true"
Grouprole="group", aria-labelledby (its GroupLabel)
GroupLabelan id
Separatorrole="presentation", data-orientation="horizontal" (a separator role is not allowed inside a listbox)

Keyboard

keyclosedopen
ArrowDown, ArrowUp, Enter, Spaceopens on the selected item, else the first enabled oneArrowDown / ArrowUp move the highlight; Enter and Space choose
Home / Endopens on the first / last itemthe first / last item
a printable characteropens on the matching itemmoves the highlight by typeahead
Escapecloses without choosing
Tabmoves onchooses the highlighted item, closes and moves on

Exports

entryexports
@fragiola/primitives-react/selectAlign, ChangeDetails, ChangeReason, PositionerProps, RenderProp, Select, SelectItemData, SelectItemIndicatorProps, SelectItemProps, SelectItemState, SelectPortalProps, SelectPositionerProps, SelectPositionerState, SelectRootProps, SelectTriggerState, SelectValueProps, Side, OpenChangeDetails, OverlayActions, OverlayPortalProps, OverlayRootProps
@fragiola/primitives-vue/selectAlign, ChangeDetails, ChangeReason, PositionerProps, RenderSlotProps, Select, SelectItemData, SelectItemIndicatorProps, SelectItemIndicatorState, SelectItemProps, SelectItemState, SelectListState, SelectPopupState, SelectPortalProps, SelectPositionerState, SelectRootEmits, SelectRootProps, SelectTriggerState, SelectValueProps, SelectValueState, Side, OpenChangeDetails, OverlayActions, OverlayPortalProps, OverlayRootEmits, OverlayRootProps
@fragiola/primitives-angular/selectAlign, ChangeDetails, ChangeReason, OpenChangeEvent, SelectItem, SelectItemData, SelectItemIndicator, SelectItemState, SelectList, SelectPopup, SelectPortal, SelectPositioner, SelectRoot, SelectState, SelectTrigger, SelectTriggerState, SelectValue, SelectItemText, SelectGroup, SelectGroupLabel, SelectSeparator, Side, ValueChangeEvent, OpenChangeDetails, OverlayActions, OverlayPortalOptions, RegisteredItem