Fragiola

API: components

CascadeSelect

Choose one path through a tree of options from a button whose popup shows one listbox per level, with Menu's submenu keys and hover, and form support.

Choose one path through a tree of options (a region, then a country, then a city) from a button whose popup shows one list per level, side by side. It works in a native <form> as a Select does: it submits, resets and validates.

The APG has no cascading pattern and Base UI has no CascadeSelect. The APG-closest shape is taken: the APG select-only combobox (a Select) whose popup holds one listbox per open level; the levels open and close as the APG menu's submenus do (Right opens, Left closes, mirrored in RTL), with Menu's hover delay and safe area. Zag's and Ark's cascade select and Ant Design's Cascader were read for the behaviour, never copied.

Region and city picker
Gallery

Behaviour

  • The value is a path: the values from the first level down (["europe", "france", "paris"]), [] when nothing is chosen.
  • The tree is items (required): { value, label, disabled?, children? }[], the toolkit's CascadeItem. An item with children is a parent, else a leaf.
  • One Level per depth: render a Level for each depth of your tree; a Level hands its items (the first level's, or the children of the item open at the depth above) to its content, which renders an Item per entry. A Level whose depth is not open is hidden.
  • The levels and the highlight: the highlight moves within one level; moving it closes the levels below. A keyboard highlight never opens a level by itself: Right (or Enter, Space) does, as in the APG menu. Hovering a parent for 100 ms opens its level, the highlight staying on it.
  • Safe area: while the pointer moves from a parent whose level is open toward that level, crossing sibling items does not highlight them nor close the level (Menu's).
  • Choosing: a leaf is chosen by Enter, Space, Tab or a click, and the popup closes. A parent opens its level and is not chosen; with changeOnSelect, Enter, Space and a click also choose the path up to it and the popup stays open on it, so the user can go on. A disabled item can be highlighted but is never chosen, and a disabled parent never opens its level.
  • Focus: DOM focus always stays on the Trigger; the highlighted item is announced with aria-activedescendant.
  • A parent option says it opened a level with aria-controls (an option may not carry aria-expanded nor aria-haspopup), and the deeper Level is named by its parent (aria-labelledby). Style it from data-has-children and data-expanded.
  • Forms: a hidden input carries name and the path joined by valueSeparator (/ by default: location=europe/france/paris); required blocks a submission with no path; a form reset returns an uncontrolled CascadeSelect to defaultValue. In a Field, the Trigger is the field's labelable control.
  • Value shows the last chosen item's label, or, with separator, the labels of the whole path joined by it ("Europe / France / Paris" with " / "). The labels and the separator are the app's text. On the server, the Value shows a defaultValue's labels, read from items.

Anatomy

partelementdata-primitives-partrole
Rootnoneholds the state; renders the hidden input
Triggerbuttoncascade-select-triggerthe combobox; keeps DOM focus while open
Valuespancascade-select-valuethe chosen label (or the path's), or the placeholder
Portalnonemoves the Positioner to the end of body
Positionerdivcascade-select-positionerplaces the popup below the trigger
Popupdivcascade-select-popupthe floating container of the levels; the app lays them out
Leveldivcascade-select-levelthe listbox of one depth; hidden while not open
Itemdivcascade-select-iteman option: a parent or a leaf
ItemIndicatorspancascade-select-item-indicatorrendered only while its Item is on the chosen path
(hidden input)inputcascade-select-inputrendered by the Root; what the form submits and validates

Usage

import { CascadeSelect, type CascadeItem } from "@fragiola/primitives-react/cascade-select";

<CascadeSelect.Root items={LOCATIONS} name="location">
    <CascadeSelect.Trigger aria-label="Location">
        <CascadeSelect.Value placeholder="Choose a place" separator=" / " />
    </CascadeSelect.Trigger>
    <CascadeSelect.Portal>
        <CascadeSelect.Positioner>
            <CascadeSelect.Popup className="flex">
                {[0, 1, 2].map((depth) => (
                    <CascadeSelect.Level key={depth} depth={depth}>
                        {(items: readonly CascadeItem[]) =>
                            items.map((item) => (
                                <CascadeSelect.Item key={item.value} value={item.value}>
                                    {item.label}
                                    <CascadeSelect.ItemIndicator>✓</CascadeSelect.ItemIndicator>
                                </CascadeSelect.Item>
                            ))
                        }
                    </CascadeSelect.Level>
                ))}
            </CascadeSelect.Popup>
        </CascadeSelect.Positioner>
    </CascadeSelect.Portal>
</CascadeSelect.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
items{ value, label, disabled?, children? }[]required: the tree
valuestring[]the chosen path, controlled
defaultValuestring[][]the initial path, uncontrolled
onValueChange(value: string[], details: { reason }) => voidevery change (item-press, focus-out for Tab, none on a form reset)
changeOnSelectbooleanfalsea parent can be chosen too (Enter, Space, a click), the popup staying open
namestringthe hidden input's name (a field's wins)
requiredbooleanfalsethe form cannot submit with no path
disabledbooleanfalsethe trigger opens nothing
valueSeparatorstring"/"joins the path in the hidden input
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
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

Trigger

No props of its own.

Value

proptypedefaultdescription
placeholderReactNodeshown while nothing is chosen
separatorstringjoins the labels of the whole path; without it, the last chosen label

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: *cascadeSelectPortal="{ keepMounted: true }"

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.

Level

proptypedefaultdescription
depthnumberrequired: the level's depth, 0 for the first

React's children may be a function of the Level's items; Vue's default slot gets { items }; Angular reads items() through exportAs.

Item

proptypedefaultdescription
valuestringrequired: the entry's value among its Level's items (its label, disabled and children are the entry's)

ItemIndicator

proptypedefaultdescription
keepMountedbooleanfalsekeep it rendered while its Item is not on the chosen path (Angular: *cascadeSelectItemIndicator="true")

State and part attributes

partattributes
Triggerrole="combobox", type="button", aria-haspopup="listbox", aria-expanded, aria-controls (the open Levels, while open), aria-activedescendant (the highlighted item, while open), aria-required, disabled, data-popup-open, data-placeholder, data-disabled; in a Field, the field's states
Valuedata-placeholder
Positioner, Popupas the Select's
Levelrole="listbox", data-depth; the first level aria-labelledby the Trigger's labels, a deeper one its parent option; hidden while not open
Itemrole="option", aria-selected (on the chosen path at its depth), aria-disabled, data-highlighted, data-selected, data-disabled, data-has-children (a parent), data-expanded and aria-controls (its children's Level) while its level is open
ItemIndicatoraria-hidden="true"
hidden inputname, value (the path joined by valueSeparator), required, disabled, aria-hidden="true", tabindex="-1"

Keyboard

DOM focus stays on the Trigger. "Inline-end" is Right in LTR and Left in RTL.

keyclosedopen
ArrowDown, ArrowUp, Enter, Spaceopens on the chosen path (every level of it open), else on the first enabled itemArrowDown / ArrowUp: the next / previous item of the level; Enter / Space: chooses a leaf, opens a parent (and chooses it with changeOnSelect)
Home / Endopens on the first / last item of the first levelthe first / last item of the level
inline-end (Right in LTR)on an enabled parent: opens its level on its first enabled item
inline-start (Left in LTR)in a deeper level: closes it and highlights its parent
a printable characteropens on the first item of the first level whose label starts with the texttypeahead within the level
Escapecloses without choosing
Tabmoves onchooses the highlighted item when it can be chosen, closes and moves on

Exports

entryexports
@fragiola/primitives-react/cascade-selectAlign, CascadeItem, CascadeSelect, CascadeSelectItemIndicatorProps, CascadeSelectItemProps, CascadeSelectItemState, CascadeSelectLevelProps, CascadeSelectLevelState, CascadeSelectPortalProps, CascadeSelectPositionerProps, CascadeSelectPositionerState, CascadeSelectRootProps, CascadeSelectTriggerState, CascadeSelectValueProps, ChangeDetails, ChangeReason, OpenChangeDetails, OverlayActions, OverlayPortalProps, OverlayRootProps, PositionerProps, RenderProp, Side
@fragiola/primitives-vue/cascade-selectAlign, CascadeItem, CascadeSelect, CascadeSelectItemIndicatorProps, CascadeSelectItemIndicatorState, CascadeSelectItemProps, CascadeSelectItemState, CascadeSelectLevelProps, CascadeSelectLevelState, CascadeSelectPopupState, CascadeSelectPortalProps, CascadeSelectPositionerState, CascadeSelectRootEmits, CascadeSelectRootProps, CascadeSelectTriggerState, CascadeSelectValueProps, CascadeSelectValueState, ChangeDetails, ChangeReason, OpenChangeDetails, OverlayActions, OverlayPortalProps, OverlayRootEmits, OverlayRootProps, PositionerProps, RenderSlotProps, Side
@fragiola/primitives-angular/cascade-selectAlign, CascadeItem, CascadeSelectItem, CascadeSelectItemIndicator, CascadeSelectItemState, CascadeSelectLevel, CascadeSelectLevelState, CascadeSelectPopup, CascadeSelectPortal, CascadeSelectPositioner, CascadeSelectRoot, CascadeSelectState, CascadeSelectTrigger, CascadeSelectTriggerState, CascadeSelectValue, ChangeDetails, ChangeReason, OpenChangeDetails, OpenChangeEvent, OverlayActions, OverlayPortalOptions, Side, ValueChangeEvent