Segmented control
One choice among two to five, drawn as pills on a well: the unit figures are shown in, the view of a list. Native radios underneath.
- Native HTML
- 1.9 KB gzipped · script and styles
@orbex/sonar-components/segmented-control
Examples
Code
import { SegmentedControl } from '@orbex/sonar-components/segmented-control'
export default function Units() {
return (
<SegmentedControl
legend="Unit"
defaultValue="plt"
options={[
{ value: 'plt', label: 'Pallets' },
{ value: 'kg', label: 'Kilograms' },
{ value: 'items', label: 'Items' },
]}
/>
)
}
Code
import { SegmentedControl } from '@orbex/sonar-components/segmented-control'
import type { ComponentProps } from 'react'
import calendarSymbol from '@material-symbols/svg-400/rounded/calendar_month.svg?raw'
import gridSymbol from '@material-symbols/svg-400/rounded/grid_view.svg?raw'
import listSymbol from '@material-symbols/svg-400/rounded/view_list.svg?raw'
/* Material Symbols Rounded, weight 400, as icon components (Foundations › Icons) */
const symbol = (svg: string) => {
const path = svg.match(/ d="([^"]+)"/)?.[1]
return (props: ComponentProps<'svg'>) => (
<svg viewBox="0 -960 960 960" fill="currentColor" aria-hidden="true" focusable="false" {...props}>
<path d={path} />
</svg>
)
}
const CalendarIcon = symbol(calendarSymbol)
const GridIcon = symbol(gridSymbol)
const ListIcon = symbol(listSymbol)
export default function Views() {
return (
<SegmentedControl
legend="View"
hideLegend
iconOnly
size="small"
defaultValue="list"
options={[
{ value: 'list', label: 'List', icon: <ListIcon /> },
{ value: 'grid', label: 'Grid', icon: <GridIcon /> },
{ value: 'calendar', label: 'Calendar', icon: <CalendarIcon /> },
]}
/>
)
}
Sizes, variants and states
Usage
- The choice applies at once. More than five options is a select.
- Icons only when each icon is unmistakable in its context; every option keeps a label for assistive technology.
Do and don’t
Accessibility
Keyboard
| Key | What it does |
|---|---|
| Tab | Moves focus to the chosen option. |
| Arrow keys | Move to the next or previous option and choose it. |
| Space | Chooses the focused option. |
Roles and attributes
| Element or attribute | What it gives |
|---|---|
<fieldset> and <legend> |
Group the options under one name, read when focus enters. A hidden legend is still read. |
<input type="radio"> |
One per option: announced as “2 of 3”, with the chosen one checked. |
| Icons only | Each option keeps its label as text for assistive technology. |
Notes
- A
<fieldset>named by its legend, with one native radio per option: arrow keys move the choice, Tab leaves the group. - The legend can be hidden visually and still names the group.
- It submits with a form without any script, under its
name.
Use it
Each component is its own entry point with its own stylesheet, so an app loads only what it uses. The tokens and the cascade layers are set once for the app: see Installation.
import { SegmentedControl } from '@orbex/sonar-components/segmented-control'
import '@orbex/sonar-components/segmented-control.css'
API
| Prop | Type | Default |
|---|---|---|
legend
Required
What is being chosen, such as “Unit”. Always present for assistive technology; drawn unless |
string
|
— |
hideLegend
Hides the legend from view while keeping it for assistive technology, when the options explain themselves. |
boolean
|
false |
options
Required
Two to five options. More than that is a select. |
SegmentedOption[]
|
— |
name
Shared by the radios, so the choice is submitted with a form. A unique name is made when it is left out. |
string
|
— |
value
The chosen value, for a controlled control. |
string
|
— |
defaultValue
The value chosen at first, for an uncontrolled control. |
string
|
— |
onValueChange
Called with the new value when the choice changes. |
(value: string) => void
|
— |
iconOnly
Show icons only. Every option still needs a label. |
boolean
|
false |
size
Medium for toolbars and forms; small inside a card header, beside its title. |
'small' | 'medium'
|
'medium' |
Also takes every attribute of the native <fieldset>
, except onChange | defaultValue, and a ref to it.