Fragiola

API: components

SegmentedControl

Mutually exclusive segments with an Indicator that moves to the checked one, the APG radio group, as a form control, in each framework.

A row (or a column) of mutually exclusive segments, one of them checked, with an Indicator that moves to the checked segment: a view switcher, a density or a unit picker. The APG has no segmented control and Base UI none: a segmented control is a set of radios with a moving mark, so it takes the WAI-ARIA APG radio group pattern whole, as RadioGroup does, and its Indicator is Tabs' Indicator applied to the checked segment. It is a choice of one value: switching panels is the app's business (Tabs own panels), and pressing several or none is a ToggleGroup.

View switcher
Gallery

Behaviour

  • Value: the checked Item's value (a string), or null while none is checked (the default). Reasons: item-press (a press on an Item, or on its <label>), keyboard, none (a form reset). A press on the checked Item changes nothing: there is no deselecting.
  • Focus: one tab stop, the checked Item when it is enabled, else the first enabled Item; every other Item has tabindex="-1". Tab enters the control there, and Tab again leaves it.
  • Arrows: every arrow moves the focus to the next or previous enabled Item and checks it (the APG radio group), whatever the orientation; disabled Items are skipped. With loopFocus (the default) they wrap; without it, an arrow at an end does nothing. Home and End do nothing (the APG radio group has neither).
  • RTL: ArrowLeft is the next Item and ArrowRight the previous, in the direction of a DirectionProvider, else the Root's computed one. ArrowDown and ArrowUp do not change.
  • Indicator: it exposes the checked Item's box, measured from the Root's padding box (where an absolute Indicator is placed from), as ten structural CSS variables, logical and physical (see Props). It is measured on mount, when the value changes, when the Root or the checked Item resizes and when a provider's direction changes; a CSS transition on the variables animates it. It is not rendered while no Item is checked. Place it after the Items, and paint it under them (isolation: isolate on the Root, a negative z-index on the Indicator).
  • Server rendering: with renderBeforeHydration, a server render writes the Indicator with a small inline script that measures the checked Item as the page parses, so it is in place before hydration (Server rendering).
  • Forms: the Root renders, as its last child, one visually hidden input with the name (only while an Item is checked: none checked submits nothing, as a radio set) and the value; required fails the validation while none is checked and focuses the tab stop. A form reset returns an uncontrolled Root to defaultValue; a controlled one reports the default through onValueChange.
  • Name: the Root is a radiogroup; name it with aria-labelledby (a heading or a label) or aria-label, or put it in a Field, whose Label names it. An Item's name is its content.
  • In a Field: the Root is the field's control, as a RadioGroup: named and described by the field, aria-invalid and the field's states on it, validate gets the value (or null); it takes the field's name and disabled.
  • The mechanisms: the toolkit's /collection (radioKeyTarget, segmentIndicatorBox, placeSegmentIndicator) and /form; it shares no code with RadioGroup's entries.

Anatomy

partelementdata-primitives-partrole
Rootdivsegmented-control-rootthe radiogroup; holds the value; the Indicator's containing block
Itembuttonsegmented-control-itema radio
ItemTextspansegmented-control-item-textthe Item's text (optional)
Indicatorspansegmented-control-indicatorfollows the checked Item (optional)
(hidden input)inputsegmented-control-inputthe Root's last child: what the form submits and validates

Usage

import { SegmentedControl } from "@fragiola/primitives-react/segmented-control";

<span id="view">View</span>
<SegmentedControl.Root aria-labelledby="view" name="view" defaultValue="list">
    <SegmentedControl.Item value="list">
        <SegmentedControl.ItemText>List</SegmentedControl.ItemText>
    </SegmentedControl.Item>
    <SegmentedControl.Item value="grid">
        <SegmentedControl.ItemText>Grid</SegmentedControl.ItemText>
    </SegmentedControl.Item>
    <SegmentedControl.Indicator />
</SegmentedControl.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, @value-change), and Angular as inputs and outputs ([(value)], (valueChangeDetails) with { value, details }).

Root

proptypedefaultdescription
valuestring | nullthe checked Item's value, controlled; null for none
defaultValuestring | nullnullthe initial value, uncontrolled
onValueChange(value: string | null, details: { reason }) => voidevery change (item-press, keyboard, none); null when a reset returns it to none
disabledbooleanfalsedisables every Item
requiredbooleanfalsethe form's validation fails while no Item is checked
namestringthe hidden input's name (a Field's wins)
orientation"horizontal" | "vertical""horizontal"aria-orientation and data-orientation on every part; it changes no key
loopFocusbooleantruethe arrows wrap from the last Item to the first and back

Item

proptypedefaultdescription
valuestringrequired: its value
disabledbooleanfalseskipped by the arrows, never checked
idstringgeneratedits id, for a <label for>

ItemText

No props of its own: it reads its Item's state.

Indicator

It sets the logical --segment-indicator-inset-inline-start (from the Root's inline-start padding edge: its right one in RTL), --segment-indicator-inset-block-start, --segment-indicator-inline-size and --segment-indicator-block-size, and the physical --segment-indicator-left, -right, -top, -bottom, --segment-indicator-width and --segment-indicator-height, in pixels, as its inline style.

proptypedefaultdescription
renderBeforeHydrationbooleanfalsein a server render, places it before hydration: a small inline script inside it measures the checked Item as the page parses, then removes itself (Angular: *segmentedControlIndicator="true")
noncestringthe nonce of that script, for a Content Security Policy that allows inline scripts by nonce (Angular: *segmentedControlIndicator="true; nonce: cspNonce", else Angular's CSP_NONCE provider)

State and part attributes

partattributes
Rootrole="radiogroup", aria-orientation and data-orientation, aria-required="true" and data-required when required, aria-disabled="true" and data-disabled when disabled; in a Field, aria-labelledby, aria-describedby, aria-invalid and the field's states
Itemrole="radio", type="button", aria-checked ("true" or "false"), tabindex (0 on the tab stop, -1 on the others), disabled and data-disabled when disabled (itself or the Root), data-checked / data-unchecked (exactly one), data-orientation
ItemTextits Item's data-checked / data-unchecked, data-disabled
Indicatoraria-hidden="true", data-orientation, data-disabled when the Root is disabled; the ten variables
hidden inputname (while an Item is checked), value ("" for none), required, disabled, aria-hidden="true", tabindex="-1", visually hidden

Keyboard

keyaction
Tabenters the control at the checked Item, else its first enabled one; Tab again leaves it
ArrowRight, ArrowDownthe next enabled Item, focused and checked (ArrowLeft in RTL); wraps with loopFocus
ArrowLeft, ArrowUpthe previous enabled Item, focused and checked (ArrowRight in RTL); wraps with loopFocus
Spacechecks the focused Item
Enternothing (a native radio does not check on Enter)

Exports

entryexports
@fragiola/primitives-react/segmented-controlChangeDetails, ChangeReason, RenderProp, SegmentedControl, SegmentedControlIndicatorProps, SegmentedControlIndicatorState, SegmentedControlItemProps, SegmentedControlItemState, SegmentedControlRootProps, SegmentedControlRootState
@fragiola/primitives-vue/segmented-controlChangeDetails, ChangeReason, RenderSlotProps, SegmentedControl, SegmentedControlIndicatorProps, SegmentedControlIndicatorState, SegmentedControlItemProps, SegmentedControlItemState, SegmentedControlRootEmits, SegmentedControlRootProps, SegmentedControlRootState
@fragiola/primitives-angular/segmented-controlChangeDetails, ChangeReason, SegmentedControlIndicator, SegmentedControlIndicatorState, SegmentedControlItem, SegmentedControlItemEntry, SegmentedControlItemState, SegmentedControlItemText, SegmentedControlOrientation, SegmentedControlRoot, SegmentedControlRootState, ValueChangeEvent