Segmented Control
Pick one of a few options from a row of buttons, with an indicator that slides to the choice.
Use a segmented control to switch between a few views or settings, like List and Grid. It works like a radio group, with one value and a single tab stop, and its indicator slides to the checked item.
import { Component } from "@angular/core"
import { Field, FieldLabel } from "@ui-sdk/angular/field"
import {
SegmentedControl,
SegmentedControlIndicator,
SegmentedControlItem,
} from "@ui-sdk/angular/segmented-control"
@Component({
imports: [
Field,
FieldLabel,
SegmentedControl,
SegmentedControlIndicator,
SegmentedControlItem,
],
template: `
<div uiField>
<label uiFieldLabel>View</label>
<div uiSegmentedControl defaultValue="list">
<span uiSegmentedControlIndicator></span>
<button uiSegmentedControlItem value="list">List</button>
<button uiSegmentedControlItem value="grid">Grid</button>
<button uiSegmentedControlItem value="board">Board</button>
</div>
</div>
`,
})
class Example {}
export { Example }Installation
npm install @ui-sdk/angular @ui-sdk/coreComposition
Use the following composition to build SegmentedControl:
uiSegmentedControl
โโโ uiSegmentedControlIndicator
โโโ uiSegmentedControlItem
โโโ uiSegmentedControlItemGive each item a value. The control's value is the checked item's value, and the indicator sits behind that item.
Usage
Import the parts and put them together:
import { Component } from "@angular/core"
import {
SegmentedControl,
SegmentedControlIndicator,
SegmentedControlItem,
} from "@ui-sdk/angular/segmented-control"
@Component({
selector: "app-example",
imports: [SegmentedControl, SegmentedControlIndicator, SegmentedControlItem],
template: `
<div uiSegmentedControl aria-label="View" defaultValue="list">
<span uiSegmentedControlIndicator></span>
<button uiSegmentedControlItem value="list">List</button>
<button uiSegmentedControlItem value="grid">Grid</button>
</div>
`,
})
class Example {}
export { Example }Name the control with aria-label, or put it in a Field whose label names it. Without defaultValue, nothing starts checked and Tab enters on the first enabled item.
Indicator
uiSegmentedControlIndicator measures the checked item. Position it inside the control and use its CSS variables to draw the background that slides to the choice. It measures again when an item or the control changes size, and has no box while nothing is checked. It gets data-visible once it has a position. Until then, like on a page rendered on the server before its script runs, its variables are unset, so hide it while it's :not([data-visible]).
[data-slot="segmented-control"] {
position: relative;
display: inline-flex;
isolation: isolate;
}
[data-slot="segmented-control-indicator"] {
position: absolute;
z-index: -1;
top: var(--segmented-control-indicator-top);
left: var(--segmented-control-indicator-left);
width: var(--segmented-control-indicator-width);
height: var(--segmented-control-indicator-height);
transition-property: top, left, width, height;
transition-duration: 200ms;
&[data-activation-direction="none"] {
transition: none;
}
}Skip the transitions while data-activation-direction is "none", so the indicator doesn't slide in on its first placement. After that it says which way the checked item moved: left, right, up or down.
Give it followHover and it follows the item under the pointer instead, then goes back to the checked item when the pointer leaves the control. It passes over disabled items.
Vertical
Set orientation="vertical" to stack the items. Arrow keys on both axes still move between them, as in any radio group, and data-orientation on every part lets one stylesheet lay out both directions.
import { Component } from "@angular/core"
import { Field, FieldLabel } from "@ui-sdk/angular/field"
import {
SegmentedControl,
SegmentedControlIndicator,
SegmentedControlItem,
} from "@ui-sdk/angular/segmented-control"
@Component({
imports: [
Field,
FieldLabel,
SegmentedControl,
SegmentedControlIndicator,
SegmentedControlItem,
],
template: `
<div uiField>
<label uiFieldLabel>Vertical alignment</label>
<div uiSegmentedControl orientation="vertical" defaultValue="middle">
<span uiSegmentedControlIndicator></span>
<button uiSegmentedControlItem value="top">Top</button>
<button uiSegmentedControlItem value="middle">Middle</button>
<button uiSegmentedControlItem value="bottom">Bottom</button>
</div>
</div>
`,
})
class Example {}
export { Example }Disabled
Set disabled on the control or on one item. Disabled items can't be focused or checked, and arrow keys skip them. A disabled control submits nothing.
import { Component } from "@angular/core"
import {
SegmentedControl,
SegmentedControlIndicator,
SegmentedControlItem,
} from "@ui-sdk/angular/segmented-control"
@Component({
imports: [SegmentedControl, SegmentedControlIndicator, SegmentedControlItem],
template: `
<div data-slot="stack">
<div uiSegmentedControl aria-label="Theme" defaultValue="system" disabled>
<span uiSegmentedControlIndicator></span>
<button uiSegmentedControlItem value="light">Light</button>
<button uiSegmentedControlItem value="dark">Dark</button>
<button uiSegmentedControlItem value="system">System</button>
</div>
<div
uiSegmentedControl
aria-label="Billing period"
defaultValue="monthly"
>
<span uiSegmentedControlIndicator></span>
<button uiSegmentedControlItem value="monthly">Monthly</button>
<button uiSegmentedControlItem value="yearly">Yearly</button>
<button uiSegmentedControlItem value="lifetime" disabled>
Lifetime
</button>
</div>
</div>
`,
})
class Example {}
export { Example }Controlled
Bind value with [(value)], and pass (beforeValueChange) for the reason. Call $event.cancel() in the callback to keep the current value. A controller from createSegmentedControlController() checks an item from anywhere, like the Show Month button below. Create one controller per mounted control.
import { Component, signal } from "@angular/core"
import { useLog } from "./log"
import {
createSegmentedControlController,
SegmentedControl,
SegmentedControlIndicator,
SegmentedControlItem,
} from "@ui-sdk/angular/segmented-control"
@Component({
imports: [SegmentedControl, SegmentedControlIndicator, SegmentedControlItem],
template: `
<div>
<div data-slot="controls">
<button type="button" (click)="range.setValue('month')">
Show Month
</button>
</div>
<div
uiSegmentedControl
aria-label="Range"
[(value)]="value"
(beforeValueChange)="
log('value: ' + $event.value + ' (' + $event.reason + ')')
"
[controller]="range"
>
<span uiSegmentedControlIndicator></span>
<button uiSegmentedControlItem value="day">Day</button>
<button uiSegmentedControlItem value="week">Week</button>
<button uiSegmentedControlItem value="month">Month</button>
</div>
</div>
`,
})
class Example {
readonly log = useLog()
readonly range = createSegmentedControlController()
readonly value = signal<string | null>("week")
}
export { Example }See State and Events for controlled state, cancellation and debugging.
Form
Each item renders a hidden native radio, so the control takes part in forms. Set name to submit the checked value; nothing submits without a name or a checked item. required blocks submission until there's a choice, form joins a form by its id from elsewhere on the page, and a reset returns to defaultValue.
import { Component } from "@angular/core"
import { useLog } from "./log"
import { Field, FieldDescription, FieldLabel } from "@ui-sdk/angular/field"
import {
SegmentedControl,
SegmentedControlIndicator,
SegmentedControlItem,
} from "@ui-sdk/angular/segmented-control"
@Component({
imports: [
Field,
FieldDescription,
FieldLabel,
SegmentedControl,
SegmentedControlIndicator,
SegmentedControlItem,
],
template: `
<form data-slot="stack" (submit)="onSubmit($event)">
<div uiField>
<label uiFieldLabel>Theme</label>
<div uiSegmentedControl name="theme" required defaultValue="system">
<span uiSegmentedControlIndicator></span>
<button uiSegmentedControlItem value="light">Light</button>
<button uiSegmentedControlItem value="dark">Dark</button>
<button uiSegmentedControlItem value="system">System</button>
</div>
<p uiFieldDescription>System follows your device's setting.</p>
</div>
<div uiField>
<label uiFieldLabel>Density</label>
<div uiSegmentedControl name="density" defaultValue="comfortable">
<span uiSegmentedControlIndicator></span>
<button uiSegmentedControlItem value="compact">Compact</button>
<button uiSegmentedControlItem value="comfortable">
Comfortable
</button>
</div>
</div>
<div data-slot="row">
<button type="submit" data-variant="solid">Save</button>
<button type="reset">Reset</button>
</div>
</form>
`,
})
class Example {
readonly log = useLog()
onSubmit(event: SubmitEvent) {
event.preventDefault()
const data = new FormData(event.currentTarget as HTMLFormElement)
this.log(`Saved theme=${data.get("theme")}, density=${data.get("density")}`)
}
}
export { Example }Read-Only
Set readOnly to keep the value while still letting people focus the items. Arrow keys move focus without checking anything, and the value still submits with a form.
Loop
Arrow keys wrap from the last item to the first. Set loop to false to stop at the ends.
<div uiSegmentedControl aria-label="View" defaultValue="list" [loop]="false">
...
</div>Styling
Parts are unstyled. Use data-checked and data-unchecked for the checked item, and data-disabled, data-readonly and data-required for the rest. data-orientation is on the control and the indicator.
Each part has a data-slot, such as segmented-control-item, and its state attributes are listed under Data Attributes.
SegmentedControlIndicator
| Variable | What it holds |
|---|---|
--segmented-control-indicator-left | Distance from the control's left edge to the checked item. |
--segmented-control-indicator-right | Distance from the control's right edge to the checked item. |
--segmented-control-indicator-top | Distance from the control's top edge to the checked item. |
--segmented-control-indicator-bottom | Distance from the control's bottom edge to the checked item. |
--segmented-control-indicator-width | Width of the checked item. |
--segmented-control-indicator-height | Height of the checked item. |
Data Attributes
SegmentedControl
[data-slot="segmented-control"]
| Attribute | What it means |
|---|---|
data-orientation | horizontalverticalWhich way the items run. |
data-readonly | Present when the control is read-only. |
data-required | Present when the control is required. |
data-disabled | Present when disabled, by its own prop, the control or a <fieldset>. |
SegmentedControlItem
[data-slot="segmented-control-item"]
| Attribute | What it means |
|---|---|
data-checked | Present on the checked item. |
data-readonly | Present when the control is read-only. |
data-required | Present when the control is required. |
data-unchecked | Present on items that aren't checked. |
data-disabled | Present when disabled, by its own prop, the control or a <fieldset>. |
SegmentedControlIndicator
[data-slot="segmented-control-indicator"]
| Attribute | What it means |
|---|---|
data-orientation | horizontalverticalWhich way the items run. |
data-activation-direction | none |
data-visible | Present once the indicator has a position. Its variables are unset until then. |
API Reference
Each option below is an input on the directive, and each callback an output. A change callback is two outputs: (beforeValueChange), with the details and cancel(), then (valueChange) with the new value alone. Bind value with [(value)].
SegmentedControl
value?T | null- The checked item's value. Pass it to control the segmented control.
nullchecks nothing. defaultValue?T | null- The item checked at first, when uncontrolled. A form reset returns to it.
(beforeValueChange)- Runs when a user or a form reset wants to change the value.
details.reasonsays why, anddetails.cancel()stops it. disabled?boolean- Disables every item: they can't be focused or changed, and the value isn't submitted.
readOnly?boolean- Keeps the items focusable and the value submitted, but stops changes.
required?boolean- Blocks form submission until an item is checked, with the browser's own message.
name?string- The name the value submits under. Without one, the segmented control submits nothing.
form?string- The id of a form to belong to, when the segmented control isn't inside that form.
loop?boolean- Whether arrow keys wrap from the last item to the first.
orientation?SegmentedControlOrientation- Which way the items run, for styling and for screen readers. Arrow keys on both axes move between items either way, as in any radio group.
controller?{ getValue: () => T | null; setValue: (value: T | null, details?: SegmentedControlChangeOptions) => boolean; focus: () => void; }- Checks items from anywhere. Create one with
createSegmentedControlController(). debug?boolean- Logs every state change with its reason to the console, in development builds.
SegmentedControlItem
valuestring- What the value becomes when this item is checked.
disabled?boolean- Skips the item, like a disabled native radio: arrow keys pass it and it can't be focused or checked.
SegmentedControlIndicator
followHover?boolean- Whether it follows the item under the pointer, and goes back to the checked one when the pointer leaves the control.
State
What each part's state() signal holds.
valuestring | null- The checked item's value.
disabledboolean- Whether the segmented control is disabled, by itself or by a
<fieldset>. readOnlyboolean- Whether it's read-only: arrow keys still move focus, but nothing changes the checked item.
requiredboolean- Whether a form won't submit until an item is checked.
orientationSegmentedControlOrientation- Which way the items run.
checkedboolean- Whether this item is the control's value, which the indicator sits behind.
disabledboolean- Whether this item is disabled, by itself, the segmented control or a
<fieldset>. readOnlyboolean- Whether the control is read-only, so pressing this item changes nothing.
requiredboolean- Whether a form won't submit until an item is checked.
valuestring | null- The checked item's value, which the indicator sits behind.
orientationSegmentedControlOrientation- Which way the items run.
directionDirection- Which way the checked item last moved on screen,
"none"before it first moves.
Reasons
$event.reason says why the checked value changed.
item-pressMouseEvent | KeyboardEvent- A member of a collection was chosen: a menu item, a tab, a radio or a checkbox item.
list-navigationKeyboardEvent- Arrow, Home or End keys moved the selection, typing picked an item, or Ctrl or โ with A selected every item of a listbox.
resetnone- A form reset.
restorenone- The browser restored state, like after going back, or a saved value was read back from storage.
imperativenone- A controller or
apicall.
Controller
Create one with createSegmentedControlController() and pass it as controller.
Every write method takes an optional { reason, event }, runs the change callback like a press, and returns false when the change is canceled.
getValue()() => T | null- The checked item's value.
setValue(value, details?)(value: T | null, details?: SegmentedControlChangeOptions | undefined) => boolean- Checks an item, as a user would. Disabled items and read-only controls ignore it.
focus()() => void- Moves focus to the checked item, or the first one.
Events
A before output emits { value, reason, event, cancel() } before the change, and its plain output follows with the value alone. A bound value only updates when (beforeValueChange) wasn't canceled.
| Binding | On | Receives |
|---|---|---|
[(value)] | uiSegmentedControl | The checked value |
(beforeValueChange) | uiSegmentedControl | { value, reason, cancel() }: the checked value, and the reason |
Accessibility
A segmented control is a radio group, so it follows the WAI-ARIA radio group pattern and behaves like native radios that share a name. Each uiSegmentedControlItem renders a button with the radio role, and a hidden native radio that takes part in forms. The indicator is presentational, so screen readers skip it.
Keyboard
| Key | Does |
|---|---|
| Tab | Moves into the control, onto the checked item or the first enabled one |
| Arrow keys | Move to the next or previous item and check it, skipping disabled ones |
| Space | Checks the focused item |
| Enter | Submits its form |
Tested Behavior
Every line below is an automated test.
- The default item is checked, and only that one.
- Clicking an item checks it and says why.
- Clicking a disabled item does nothing.
- A change can be cancelled.
- A controller checks an item.
- Tab lands on the checked item, and the next Tab leaves.
- Arrow keys on both axes check the next item, skip disabled ones and wrap.
- A segmented control is horizontal unless it says otherwise.
- A vertical segmented control reports it.
- With followHover, the indicator follows the hovered item and goes back when the pointer leaves.
- The indicator covers the checked item.
- The indicator follows the checked item.
- The indicator says which way the checked item moved, and none at first.
- The indicator covers the checked item in a vertical control.
- The indicator matches items with fractional widths exactly.
- A scaled ancestor doesn't move the indicator off fractional items.
- With nothing checked, the indicator has no box.
- The checked value submits under the control's name.
- A form reset checks the default item again.
- A disabled control submits nothing, and its items can't be focused or checked.
Runs in React, Vue and web components.