Molecules · Segmented control
Segmented control
Two to five options in one framed row, with one selected. It changes what's on screen in place. Under the hood it's a radio group: Tab stops on it once, the arrow keys move and select, and the selected option takes the neutral selected look: a selected surface, primary text and a 2px signal underline.
- Best for
- Switching the view of the same content in place: Board or Table, 24 h or 7 d.
- Not for
- Panels of different content: use Tabs. A choice in a form: use Radio.
This page as Markdown, a plain-text copy for a coding agent (an AI tool that writes code).
Examples
A time range
Showing the last 24 h.
Code
tsx
const RANGES = [ { value: '1h', label: '1 h' }, { value: '24h', label: '24 h' }, { value: '7d', label: '7 d' }, { value: '30d', label: '30 d' },]function RangeExample() { const [range, setRange] = useState('24h') return ( <div className="flex flex-col items-center gap-3"> <SegmentedControl label="Range" options={RANGES} value={range} onValueChange={setRange} /> <p className="font-body text-sm text-[var(--color-text-secondary)]">Showing the last {RANGES.find(option => option.value === range)?.label}.</p> </div> )}<RangeExample />Small, with icons, lined up with a small Button
Code
tsx
function ToolbarExample() { const [view, setView] = useState('board') return ( <div className="flex flex-wrap items-center gap-3"> <SegmentedControl label="View" size="sm" options={[ { value: 'board', label: 'Board', icon: <Columns3 /> }, { value: 'table', label: 'Table', icon: <Table /> }, ]} value={view} onValueChange={setView} /> <Button size="sm" variant="secondary"> Export </Button> </div> )}<ToolbarExample />Anatomy
| Part | What it is |
|---|---|
| Group | A radiogroup named by label, framed with the control hairline. It scrolls sideways if it's too wide. |
| Option | A radio with a monospace label and an optional icon, the same height as a Button at sm or md. |
| Selected option | The neutral selected surface with primary text in semibold, and a 2px signal underline. Its focus ring is the default one. |
Props
- labelstring
- Required. Names the group: "View", "Range".
- optionsreadonly SegmentedControlOption[]
- Required. Two to five.
- valuestring
- Required. The selected option.
- onValueChange(value: string) => void
- Required. Called with the new value.
- size'sm' | 'md'Default 'md'
- The same height as Button's sm or md.
- classNamestring
- Added to the group.
- refRef<HTMLDivElement>
- Passed through to the group.
- value (SegmentedControlOption)string
- Required. What onValueChange receives.
- label (SegmentedControlOption)string
- Required. Always shown.
- icon (SegmentedControlOption)ReactNode
- A lucide icon before the label, aria-hidden so screen readers skip it.
Keyboard
| Keys | Result |
|---|---|
| Tab | Moves into the group, onto the selected option, and out again. |
| ArrowLeftArrowRight | Moves to the previous or next option and selects it. Wraps round at the ends. |
| ArrowUpArrowDown | The same as Left and Right. |
| Space | Selects the focused option, if it isn't selected already. |
Do and don't
Do
- Use it where the change happens straight away and can be undone: the view, a time range, a unit.
- Keep labels to a word or two; they're set in uppercase monospace.
- Match the size to the buttons beside it: sm in a busy toolbar, md otherwise.
Don't
- Don't use it for panels of different content; that's Tabs, which links each tab to its panel.
- Don't use it for a choice that's sent with a form; that's a RadioGroup inside the form.
- Don't drop the label. Every option keeps its words, even with an icon.