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.
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 (reasonfocus-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 itslabel, 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'skeepMounted, so the labels are known while it is closed. Groups, their labels and separators are skipped by the keyboard. - Lifecycle:
onOpenChangeis called before the change (details.cancel()keeps it as it is),onOpenChangeComplete(open)reports the end of each transition, the actionsclose()andunmount()act from code. - Forms: a visually hidden input carries
nameand the value (""with none).requiredblocks the submission and focuses the trigger; a form reset returns an uncontrolled Select todefaultValue. 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'sitemsin 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
| part | element | data-primitives-part | role |
|---|---|---|---|
| Root | none | holds the state; renders the hidden input | |
| Trigger | button | select-trigger | the combobox; keeps DOM focus |
| Value | span | select-value | inside the Trigger: the selected label, or the placeholder |
| Portal | none | moves the Positioner to the end of body | |
| Positioner | div | select-positioner | places the popup below the trigger |
| Popup | div | select-popup | the floating container |
| List | div | select-list | the listbox |
| Item | div | select-item | an option |
| ItemText | span | select-item-text | inside an Item: its label, without items |
| ItemIndicator | span | select-item-indicator | rendered only while its item is selected |
| Group | div | select-group | groups items under a label |
| GroupLabel | div | select-group-label | names its group |
| Separator | div | select-separator | separates items or groups |
| (hidden input) | input | select-input | what 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
| prop | type | default | description |
|---|---|---|---|
value | string | null | the selected item's value, controlled | |
defaultValue | string | null | null | the initial value, uncontrolled |
onValueChange | (value: string | null, details: { reason }) => void | every change (item-press, focus-out, none on a form reset) | |
items | { value, label, disabled? }[] | every item (optional: without it, the rendered Items register themselves) | |
name | string | the hidden input's name (a field's wins) | |
required | boolean | false | the form cannot submit with no value |
disabled | boolean | false | the trigger opens nothing |
modal | boolean | false | while open: scroll lock and a blocked outside pointer |
open | boolean | the open state, controlled | |
defaultOpen | boolean | false | the initial open state |
onOpenChange | (open: boolean, details: OpenChangeDetails) => void | every open change, with its reason; called before the change, details.cancel() stops it | |
onOpenChangeComplete | (open: boolean) => void | once 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
| prop | type | default | description |
|---|---|---|---|
placeholder | string | shown while nothing is selected |
Portal
| prop | type | default | description |
|---|---|---|---|
container (react, vue) | Element | document.body | where the Positioner goes |
keepMounted (react, vue) | boolean | false | keep 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
| prop | type | default | description |
|---|---|---|---|
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 |
sideOffset | number | 4 | the distance from the trigger, in px |
alignOffset | number | 0 | the shift along the trigger's edge, in px |
collisionPadding | number | 8 | the space kept from the viewport's edges, in px |
anchor | Element | the trigger | the element to place the popup next to |
--anchor-width lets the popup be at least as wide as the trigger.
Popup
No props of its own.
List
No props of its own.
Item
| prop | type | default | description |
|---|---|---|---|
value | string | required: its items entry's value | |
label | string | its ItemText's text, else its text | the label the Value shows and typeahead matches, without the Root's items |
disabled | boolean | its entry's | highlighted, never chosen |
ItemText
No props of its own.
ItemIndicator
| prop | type | default | description |
|---|---|---|---|
keepMounted | boolean | false | rendered in every state |
Group
No props of its own.
GroupLabel
| prop | type | default | description |
|---|---|---|---|
id (angular) | string | generated | the 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
| part | attributes |
|---|---|
| Trigger | role="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 |
| Value | data-placeholder |
| Positioner | role="presentation", data-side, data-align, data-open / data-closed; the CSS variables of the Popover's; hidden while kept mounted and closed |
| Popup | data-side, data-align, data-open / data-closed, data-starting-style, data-ending-style |
| List | role="listbox", aria-labelledby |
| Item | role="option", aria-selected, aria-disabled, data-highlighted, data-selected, data-disabled |
| ItemText | none beyond the part attribute |
| ItemIndicator | aria-hidden="true" |
| Group | role="group", aria-labelledby (its GroupLabel) |
| GroupLabel | an id |
| Separator | role="presentation", data-orientation="horizontal" (a separator role is not allowed inside a listbox) |
Keyboard
| key | closed | open |
|---|---|---|
| ArrowDown, ArrowUp, Enter, Space | opens on the selected item, else the first enabled one | ArrowDown / ArrowUp move the highlight; Enter and Space choose |
| Home / End | opens on the first / last item | the first / last item |
| a printable character | opens on the matching item | moves the highlight by typeahead |
| Escape | closes without choosing | |
| Tab | moves on | chooses the highlighted item, closes and moves on |
Exports
| entry | exports |
|---|---|
@fragiola/primitives-react/select | Align, ChangeDetails, ChangeReason, PositionerProps, RenderProp, Select, SelectItemData, SelectItemIndicatorProps, SelectItemProps, SelectItemState, SelectPortalProps, SelectPositionerProps, SelectPositionerState, SelectRootProps, SelectTriggerState, SelectValueProps, Side, OpenChangeDetails, OverlayActions, OverlayPortalProps, OverlayRootProps |
@fragiola/primitives-vue/select | Align, 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/select | Align, 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 |