@ui-sdk/react 0.0.0-stage → 0.1.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +10 -2
- package/dist/accordion.d.ts +331 -0
- package/dist/accordion.js +25 -0
- package/dist/agent.d.ts +2 -0
- package/dist/agent.js +8 -0
- package/dist/alert-dialog.d.ts +56 -0
- package/dist/alert-dialog.js +45 -0
- package/dist/async-options.d.ts +22 -0
- package/dist/async-options.js +56 -0
- package/dist/autocomplete.d.ts +102 -0
- package/dist/autocomplete.js +195 -0
- package/dist/avatar.d.ts +301 -0
- package/dist/avatar.js +17 -0
- package/dist/button.d.ts +21 -0
- package/dist/button.js +45 -0
- package/dist/calendar-context.d.ts +10 -0
- package/dist/calendar-context.js +9 -0
- package/dist/calendar.d.ts +80 -0
- package/dist/calendar.js +257 -0
- package/dist/carousel.d.ts +52 -0
- package/dist/carousel.js +151 -0
- package/dist/checkbox.d.ts +24 -0
- package/dist/checkbox.js +98 -0
- package/dist/children.d.ts +38 -0
- package/dist/children.js +63 -0
- package/dist/collapsible.d.ts +302 -0
- package/dist/collapsible.js +19 -0
- package/dist/color-picker.d.ts +72 -0
- package/dist/color-picker.js +133 -0
- package/dist/color.d.ts +2 -0
- package/dist/color.js +22 -0
- package/dist/combobox.d.ts +152 -0
- package/dist/combobox.js +338 -0
- package/dist/command.d.ts +88 -0
- package/dist/command.js +233 -0
- package/dist/controller-target.d.ts +3 -0
- package/dist/controller-target.js +59 -0
- package/dist/date-input.d.ts +19 -0
- package/dist/date-input.js +58 -0
- package/dist/date-picker.d.ts +47 -0
- package/dist/date-picker.js +310 -0
- package/dist/date.d.ts +2 -0
- package/dist/date.js +60 -0
- package/dist/dialog-parts.d.ts +23 -0
- package/dist/dialog-parts.js +142 -0
- package/dist/dialog.d.ts +57 -0
- package/dist/dialog.js +32 -0
- package/dist/drawer.d.ts +70 -0
- package/dist/drawer.js +158 -0
- package/dist/editor-context.d.ts +4 -0
- package/dist/editor-context.js +9 -0
- package/dist/field-context.d.ts +47 -0
- package/dist/field-context.js +87 -0
- package/dist/field.d.ts +49 -0
- package/dist/field.js +169 -0
- package/dist/file-input.d.ts +46 -0
- package/dist/file-input.js +170 -0
- package/dist/form-inputs.d.ts +5 -0
- package/dist/form-inputs.js +22 -0
- package/dist/gesture.d.ts +1 -0
- package/dist/gesture.js +16 -0
- package/dist/history.d.ts +2 -0
- package/dist/history.js +12 -0
- package/dist/hover-card.d.ts +33 -0
- package/dist/hover-card.js +84 -0
- package/dist/image-cropper.d.ts +333 -0
- package/dist/image-cropper.js +37 -0
- package/dist/image.d.ts +2 -0
- package/dist/image.js +6 -0
- package/dist/input.d.ts +22 -0
- package/dist/input.js +51 -0
- package/dist/label.d.ts +294 -0
- package/dist/label.js +9 -0
- package/dist/list-items.d.ts +15 -0
- package/dist/list-items.js +39 -0
- package/dist/list-text.d.ts +4 -0
- package/dist/list-text.js +15 -0
- package/dist/listbox.d.ts +56 -0
- package/dist/listbox.js +122 -0
- package/dist/menu.d.ts +116 -0
- package/dist/menu.js +426 -0
- package/dist/menubar-context.d.ts +4 -0
- package/dist/menubar-context.js +7 -0
- package/dist/menubar.d.ts +11 -0
- package/dist/menubar.js +32 -0
- package/dist/meter.d.ts +312 -0
- package/dist/meter.js +30 -0
- package/dist/navigation-menu.d.ts +61 -0
- package/dist/navigation-menu.js +169 -0
- package/dist/number-input.d.ts +44 -0
- package/dist/number-input.js +185 -0
- package/dist/number.d.ts +2 -0
- package/dist/number.js +40 -0
- package/dist/observe.d.ts +2 -0
- package/dist/observe.js +8 -0
- package/dist/otp-input.d.ts +29 -0
- package/dist/otp-input.js +94 -0
- package/dist/overlays.d.ts +26 -0
- package/dist/overlays.js +82 -0
- package/dist/pagination.d.ts +336 -0
- package/dist/pagination.js +71 -0
- package/dist/password-input.d.ts +23 -0
- package/dist/password-input.js +65 -0
- package/dist/popover.d.ts +54 -0
- package/dist/popover.js +151 -0
- package/dist/popup.d.ts +41 -0
- package/dist/popup.js +180 -0
- package/dist/preferences.d.ts +35 -0
- package/dist/preferences.js +93 -0
- package/dist/primitive.d.ts +2 -0
- package/dist/primitive.js +14 -0
- package/dist/progress.d.ts +312 -0
- package/dist/progress.js +30 -0
- package/dist/radio.d.ts +22 -0
- package/dist/radio.js +86 -0
- package/dist/rating.d.ts +20 -0
- package/dist/rating.js +94 -0
- package/dist/render-element.d.ts +35 -0
- package/dist/render-element.js +103 -0
- package/dist/resizable.d.ts +302 -0
- package/dist/resizable.js +23 -0
- package/dist/scroll-area.d.ts +324 -0
- package/dist/scroll-area.js +49 -0
- package/dist/segmented-control.d.ts +24 -0
- package/dist/segmented-control.js +78 -0
- package/dist/segmented-input.d.ts +15 -0
- package/dist/segmented-input.js +48 -0
- package/dist/select.d.ts +125 -0
- package/dist/select.js +330 -0
- package/dist/separator.d.ts +290 -0
- package/dist/separator.js +9 -0
- package/dist/shortcuts.d.ts +32 -0
- package/dist/shortcuts.js +61 -0
- package/dist/slider.d.ts +38 -0
- package/dist/slider.js +163 -0
- package/dist/switch.d.ts +15 -0
- package/dist/switch.js +39 -0
- package/dist/tabs.d.ts +331 -0
- package/dist/tabs.js +28 -0
- package/dist/text-control.d.ts +22 -0
- package/dist/text-control.js +24 -0
- package/dist/text-match.d.ts +2 -0
- package/dist/text-match.js +8 -0
- package/dist/textarea.d.ts +10 -0
- package/dist/textarea.js +48 -0
- package/dist/time-input.d.ts +19 -0
- package/dist/time-input.js +63 -0
- package/dist/timeline.d.ts +327 -0
- package/dist/timeline.js +31 -0
- package/dist/toast-parts.d.ts +63 -0
- package/dist/toast-parts.js +144 -0
- package/dist/toast.d.ts +2 -0
- package/dist/toast.js +22 -0
- package/dist/toggle.d.ts +23 -0
- package/dist/toggle.js +76 -0
- package/dist/token-input.d.ts +35 -0
- package/dist/token-input.js +100 -0
- package/dist/toolbar-context.d.ts +6 -0
- package/dist/toolbar-context.js +16 -0
- package/dist/toolbar.d.ts +38 -0
- package/dist/toolbar.js +63 -0
- package/dist/tooltip.d.ts +39 -0
- package/dist/tooltip.js +108 -0
- package/dist/tree.d.ts +51 -0
- package/dist/tree.js +150 -0
- package/dist/trigger-menu.d.ts +83 -0
- package/dist/trigger-menu.js +167 -0
- package/dist/use-primitive.d.ts +178 -0
- package/dist/use-primitive.js +674 -0
- package/dist/waterfall.d.ts +80 -0
- package/dist/waterfall.js +108 -0
- package/docs/README.md +89 -0
- package/docs/agents.md +1523 -0
- package/docs/components/accordion.md +1391 -0
- package/docs/components/alert-dialog.md +850 -0
- package/docs/components/autocomplete.md +1963 -0
- package/docs/components/avatar.md +252 -0
- package/docs/components/button.md +444 -0
- package/docs/components/calendar.md +1687 -0
- package/docs/components/carousel.md +1244 -0
- package/docs/components/checkbox.md +1069 -0
- package/docs/components/collapsible.md +567 -0
- package/docs/components/color-picker.md +736 -0
- package/docs/components/combobox.md +3647 -0
- package/docs/components/command.md +1573 -0
- package/docs/components/date-input.md +608 -0
- package/docs/components/date-picker.md +1266 -0
- package/docs/components/dialog.md +1469 -0
- package/docs/components/drawer.md +1197 -0
- package/docs/components/field.md +549 -0
- package/docs/components/file-input.md +609 -0
- package/docs/components/hover-card.md +514 -0
- package/docs/components/image-cropper.md +579 -0
- package/docs/components/input.md +301 -0
- package/docs/components/label.md +217 -0
- package/docs/components/listbox.md +1130 -0
- package/docs/components/menu.md +1286 -0
- package/docs/components/menubar.md +455 -0
- package/docs/components/meter.md +304 -0
- package/docs/components/navigation-menu.md +1581 -0
- package/docs/components/number-input.md +1086 -0
- package/docs/components/otp-input.md +565 -0
- package/docs/components/pagination.md +648 -0
- package/docs/components/password-input.md +737 -0
- package/docs/components/popover.md +1552 -0
- package/docs/components/preferences.md +220 -0
- package/docs/components/progress.md +443 -0
- package/docs/components/radio.md +846 -0
- package/docs/components/rating.md +458 -0
- package/docs/components/resizable.md +982 -0
- package/docs/components/scroll-area.md +980 -0
- package/docs/components/segmented-control.md +465 -0
- package/docs/components/select.md +2133 -0
- package/docs/components/separator.md +243 -0
- package/docs/components/slider.md +1169 -0
- package/docs/components/switch.md +613 -0
- package/docs/components/tabs.md +1014 -0
- package/docs/components/textarea.md +318 -0
- package/docs/components/time-input.md +231 -0
- package/docs/components/timeline.md +677 -0
- package/docs/components/toast.md +1395 -0
- package/docs/components/toggle.md +541 -0
- package/docs/components/token-input.md +1007 -0
- package/docs/components/toolbar.md +787 -0
- package/docs/components/tooltip.md +884 -0
- package/docs/components/tree.md +1346 -0
- package/docs/components/trigger-menu.md +1257 -0
- package/docs/components/waterfall.md +754 -0
- package/docs/controllers.md +89 -0
- package/docs/core.md +8027 -0
- package/docs/custom-parts.md +120 -0
- package/docs/first-component.md +83 -0
- package/docs/forms.md +148 -0
- package/docs/hooks/async-options.md +366 -0
- package/docs/hooks/shortcuts.md +221 -0
- package/docs/installation.md +45 -0
- package/docs/introduction.md +32 -0
- package/docs/lists-and-menus.md +170 -0
- package/docs/llms.txt +1844 -0
- package/docs/parts-and-composition.md +93 -0
- package/docs/popups.md +117 -0
- package/docs/rendering.md +121 -0
- package/docs/server-rendering.md +253 -0
- package/docs/state-and-events.md +105 -0
- package/docs/styling-and-animation.md +176 -0
- package/package.json +333 -3
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
# Segmented Control
|
|
2
|
+
|
|
3
|
+
Pick one of a few options from a row of buttons, with an indicator that slides to the choice.
|
|
4
|
+
|
|
5
|
+
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.
|
|
6
|
+
|
|
7
|
+
```tsx title="basic.tsx"
|
|
8
|
+
"use client"
|
|
9
|
+
|
|
10
|
+
import { Field, FieldLabel } from "@ui-sdk/react/field"
|
|
11
|
+
import {
|
|
12
|
+
SegmentedControl,
|
|
13
|
+
SegmentedControlIndicator,
|
|
14
|
+
SegmentedControlItem,
|
|
15
|
+
} from "@ui-sdk/react/segmented-control"
|
|
16
|
+
|
|
17
|
+
function Example() {
|
|
18
|
+
return (
|
|
19
|
+
<Field>
|
|
20
|
+
<FieldLabel>View</FieldLabel>
|
|
21
|
+
<SegmentedControl defaultValue="list">
|
|
22
|
+
<SegmentedControlIndicator />
|
|
23
|
+
<SegmentedControlItem value="list">List</SegmentedControlItem>
|
|
24
|
+
<SegmentedControlItem value="grid">Grid</SegmentedControlItem>
|
|
25
|
+
<SegmentedControlItem value="board">Board</SegmentedControlItem>
|
|
26
|
+
</SegmentedControl>
|
|
27
|
+
</Field>
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { Example }
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Choose Grid, then use the arrow keys to move between views. The label above names the control, as in any field.
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
npm install @ui-sdk/react @ui-sdk/core
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Composition
|
|
43
|
+
|
|
44
|
+
Use the following composition to build a `SegmentedControl`:
|
|
45
|
+
|
|
46
|
+
```text
|
|
47
|
+
SegmentedControl
|
|
48
|
+
├── SegmentedControlIndicator
|
|
49
|
+
├── SegmentedControlItem
|
|
50
|
+
└── SegmentedControlItem
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Give each item a `value`. The control's value is the checked item's value, and the indicator sits behind that item.
|
|
54
|
+
|
|
55
|
+
## Usage
|
|
56
|
+
|
|
57
|
+
Import the parts and put them together:
|
|
58
|
+
|
|
59
|
+
```tsx
|
|
60
|
+
import {
|
|
61
|
+
SegmentedControl,
|
|
62
|
+
SegmentedControlIndicator,
|
|
63
|
+
SegmentedControlItem,
|
|
64
|
+
} from "@ui-sdk/react/segmented-control"
|
|
65
|
+
|
|
66
|
+
function Example() {
|
|
67
|
+
return (
|
|
68
|
+
<SegmentedControl aria-label="View" defaultValue="list">
|
|
69
|
+
<SegmentedControlIndicator />
|
|
70
|
+
<SegmentedControlItem value="list">List</SegmentedControlItem>
|
|
71
|
+
<SegmentedControlItem value="grid">Grid</SegmentedControlItem>
|
|
72
|
+
</SegmentedControl>
|
|
73
|
+
)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export { Example }
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Name the control with `aria-label`, or put it in a [Field](./field.md) whose label names it. Without `defaultValue`, nothing starts checked and Tab enters on the first enabled item.
|
|
80
|
+
|
|
81
|
+
## Indicator
|
|
82
|
+
|
|
83
|
+
`SegmentedControlIndicator` measures the checked item. Position it inside the control and use its [CSS variables](#styling-indicator) 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.
|
|
84
|
+
|
|
85
|
+
```css
|
|
86
|
+
[data-slot="segmented-control"] {
|
|
87
|
+
position: relative;
|
|
88
|
+
display: inline-flex;
|
|
89
|
+
isolation: isolate;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
[data-slot="segmented-control-indicator"] {
|
|
93
|
+
position: absolute;
|
|
94
|
+
z-index: -1;
|
|
95
|
+
top: var(--segmented-control-indicator-top);
|
|
96
|
+
left: var(--segmented-control-indicator-left);
|
|
97
|
+
width: var(--segmented-control-indicator-width);
|
|
98
|
+
height: var(--segmented-control-indicator-height);
|
|
99
|
+
transition-property: top, left, width, height;
|
|
100
|
+
transition-duration: 200ms;
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Vertical
|
|
105
|
+
|
|
106
|
+
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.
|
|
107
|
+
|
|
108
|
+
```tsx title="vertical.tsx"
|
|
109
|
+
"use client"
|
|
110
|
+
|
|
111
|
+
import { Field, FieldLabel } from "@ui-sdk/react/field"
|
|
112
|
+
import {
|
|
113
|
+
SegmentedControl,
|
|
114
|
+
SegmentedControlIndicator,
|
|
115
|
+
SegmentedControlItem,
|
|
116
|
+
} from "@ui-sdk/react/segmented-control"
|
|
117
|
+
|
|
118
|
+
function Example() {
|
|
119
|
+
return (
|
|
120
|
+
<Field>
|
|
121
|
+
<FieldLabel>Vertical alignment</FieldLabel>
|
|
122
|
+
<SegmentedControl orientation="vertical" defaultValue="middle">
|
|
123
|
+
<SegmentedControlIndicator />
|
|
124
|
+
<SegmentedControlItem value="top">Top</SegmentedControlItem>
|
|
125
|
+
<SegmentedControlItem value="middle">Middle</SegmentedControlItem>
|
|
126
|
+
<SegmentedControlItem value="bottom">Bottom</SegmentedControlItem>
|
|
127
|
+
</SegmentedControl>
|
|
128
|
+
</Field>
|
|
129
|
+
)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export { Example }
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Choose Bottom, then press the Up arrow. The indicator follows down the column.
|
|
136
|
+
|
|
137
|
+
## Disabled
|
|
138
|
+
|
|
139
|
+
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.
|
|
140
|
+
|
|
141
|
+
```tsx title="disabled.tsx"
|
|
142
|
+
"use client"
|
|
143
|
+
|
|
144
|
+
import {
|
|
145
|
+
SegmentedControl,
|
|
146
|
+
SegmentedControlIndicator,
|
|
147
|
+
SegmentedControlItem,
|
|
148
|
+
} from "@ui-sdk/react/segmented-control"
|
|
149
|
+
|
|
150
|
+
function Example() {
|
|
151
|
+
return (
|
|
152
|
+
<div data-slot="stack">
|
|
153
|
+
<SegmentedControl aria-label="Theme" defaultValue="system" disabled>
|
|
154
|
+
<SegmentedControlIndicator />
|
|
155
|
+
<SegmentedControlItem value="light">Light</SegmentedControlItem>
|
|
156
|
+
<SegmentedControlItem value="dark">Dark</SegmentedControlItem>
|
|
157
|
+
<SegmentedControlItem value="system">System</SegmentedControlItem>
|
|
158
|
+
</SegmentedControl>
|
|
159
|
+
<SegmentedControl aria-label="Billing period" defaultValue="monthly">
|
|
160
|
+
<SegmentedControlIndicator />
|
|
161
|
+
<SegmentedControlItem value="monthly">Monthly</SegmentedControlItem>
|
|
162
|
+
<SegmentedControlItem value="yearly">Yearly</SegmentedControlItem>
|
|
163
|
+
<SegmentedControlItem value="lifetime" disabled>
|
|
164
|
+
Lifetime
|
|
165
|
+
</SegmentedControlItem>
|
|
166
|
+
</SegmentedControl>
|
|
167
|
+
</div>
|
|
168
|
+
)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export { Example }
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
The Theme control is disabled. In Billing period, Lifetime is disabled, so the arrow keys move between Monthly and Yearly.
|
|
175
|
+
|
|
176
|
+
## Controlled
|
|
177
|
+
|
|
178
|
+
Pass `value` and update it in `onValueChange`. Call `details.cancel()` in the callback to keep the current value. A [controller](#controller) from `createSegmentedControlController()` checks an item from anywhere, like the Show Month button below. Create one controller per mounted control.
|
|
179
|
+
|
|
180
|
+
```tsx title="controlled.tsx"
|
|
181
|
+
"use client"
|
|
182
|
+
|
|
183
|
+
import {
|
|
184
|
+
createSegmentedControlController,
|
|
185
|
+
SegmentedControl,
|
|
186
|
+
SegmentedControlIndicator,
|
|
187
|
+
SegmentedControlItem,
|
|
188
|
+
} from "@ui-sdk/react/segmented-control"
|
|
189
|
+
import * as React from "react"
|
|
190
|
+
import { useLog } from "./log"
|
|
191
|
+
|
|
192
|
+
function Example() {
|
|
193
|
+
const log = useLog()
|
|
194
|
+
const [range] = React.useState(() => createSegmentedControlController())
|
|
195
|
+
const [value, setValue] = React.useState<string | null>("week")
|
|
196
|
+
|
|
197
|
+
return (
|
|
198
|
+
<div>
|
|
199
|
+
<div data-slot="controls">
|
|
200
|
+
<button type="button" onClick={() => range.setValue("month")}>
|
|
201
|
+
Show Month
|
|
202
|
+
</button>
|
|
203
|
+
</div>
|
|
204
|
+
<SegmentedControl
|
|
205
|
+
aria-label="Range"
|
|
206
|
+
value={value}
|
|
207
|
+
onValueChange={(next, details) => {
|
|
208
|
+
setValue(next)
|
|
209
|
+
log(`value: ${next} (${details.reason})`)
|
|
210
|
+
}}
|
|
211
|
+
controller={range}
|
|
212
|
+
>
|
|
213
|
+
<SegmentedControlIndicator />
|
|
214
|
+
<SegmentedControlItem value="day">Day</SegmentedControlItem>
|
|
215
|
+
<SegmentedControlItem value="week">Week</SegmentedControlItem>
|
|
216
|
+
<SegmentedControlItem value="month">Month</SegmentedControlItem>
|
|
217
|
+
</SegmentedControl>
|
|
218
|
+
</div>
|
|
219
|
+
)
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export { Example }
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
Choose Day, then press Show Month. The console logs each change and its reason.
|
|
226
|
+
|
|
227
|
+
See [State and Events](../state-and-events.md) for controlled state, cancellation and debugging.
|
|
228
|
+
|
|
229
|
+
## Form
|
|
230
|
+
|
|
231
|
+
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`.
|
|
232
|
+
|
|
233
|
+
```tsx title="form.tsx"
|
|
234
|
+
"use client"
|
|
235
|
+
|
|
236
|
+
import { Field, FieldDescription, FieldLabel } from "@ui-sdk/react/field"
|
|
237
|
+
import {
|
|
238
|
+
SegmentedControl,
|
|
239
|
+
SegmentedControlIndicator,
|
|
240
|
+
SegmentedControlItem,
|
|
241
|
+
} from "@ui-sdk/react/segmented-control"
|
|
242
|
+
import { useLog } from "./log"
|
|
243
|
+
|
|
244
|
+
function Example() {
|
|
245
|
+
const log = useLog()
|
|
246
|
+
|
|
247
|
+
return (
|
|
248
|
+
<form
|
|
249
|
+
data-slot="stack"
|
|
250
|
+
onSubmit={(event) => {
|
|
251
|
+
event.preventDefault()
|
|
252
|
+
const data = new FormData(event.currentTarget)
|
|
253
|
+
log(`Saved theme=${data.get("theme")}, density=${data.get("density")}`)
|
|
254
|
+
}}
|
|
255
|
+
>
|
|
256
|
+
<Field>
|
|
257
|
+
<FieldLabel>Theme</FieldLabel>
|
|
258
|
+
<SegmentedControl name="theme" required defaultValue="system">
|
|
259
|
+
<SegmentedControlIndicator />
|
|
260
|
+
<SegmentedControlItem value="light">Light</SegmentedControlItem>
|
|
261
|
+
<SegmentedControlItem value="dark">Dark</SegmentedControlItem>
|
|
262
|
+
<SegmentedControlItem value="system">System</SegmentedControlItem>
|
|
263
|
+
</SegmentedControl>
|
|
264
|
+
<FieldDescription>
|
|
265
|
+
System follows your device's setting.
|
|
266
|
+
</FieldDescription>
|
|
267
|
+
</Field>
|
|
268
|
+
<Field>
|
|
269
|
+
<FieldLabel>Density</FieldLabel>
|
|
270
|
+
<SegmentedControl name="density" defaultValue="comfortable">
|
|
271
|
+
<SegmentedControlIndicator />
|
|
272
|
+
<SegmentedControlItem value="compact">Compact</SegmentedControlItem>
|
|
273
|
+
<SegmentedControlItem value="comfortable">
|
|
274
|
+
Comfortable
|
|
275
|
+
</SegmentedControlItem>
|
|
276
|
+
</SegmentedControl>
|
|
277
|
+
</Field>
|
|
278
|
+
<div data-slot="row">
|
|
279
|
+
<button type="submit" data-variant="solid">
|
|
280
|
+
Save
|
|
281
|
+
</button>
|
|
282
|
+
<button type="reset">Reset</button>
|
|
283
|
+
</div>
|
|
284
|
+
</form>
|
|
285
|
+
)
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export { Example }
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
Choose Dark and Compact, then press Save. The console logs both values, and Reset returns each control to its default.
|
|
292
|
+
|
|
293
|
+
## Read-Only
|
|
294
|
+
|
|
295
|
+
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.
|
|
296
|
+
|
|
297
|
+
## Loop
|
|
298
|
+
|
|
299
|
+
Arrow keys wrap from the last item to the first. Set `loop` to `false` to stop at the ends.
|
|
300
|
+
|
|
301
|
+
```tsx
|
|
302
|
+
<SegmentedControl aria-label="View" defaultValue="list" loop={false}>
|
|
303
|
+
...
|
|
304
|
+
</SegmentedControl>
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
## Styling
|
|
308
|
+
|
|
309
|
+
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.
|
|
310
|
+
|
|
311
|
+
Each part has a `data-slot`, such as `segmented-control-item`, and its state attributes are listed under [Data Attributes](#data-attributes).
|
|
312
|
+
|
|
313
|
+
### SegmentedControlIndicator
|
|
314
|
+
- `--segmented-control-indicator-left`: Distance from the control's left edge to the checked item.
|
|
315
|
+
- `--segmented-control-indicator-right`: Distance from the control's right edge to the checked item.
|
|
316
|
+
- `--segmented-control-indicator-top`: Distance from the control's top edge to the checked item.
|
|
317
|
+
- `--segmented-control-indicator-bottom`: Distance from the control's bottom edge to the checked item.
|
|
318
|
+
- `--segmented-control-indicator-width`: Width of the checked item.
|
|
319
|
+
- `--segmented-control-indicator-height`: Height of the checked item.
|
|
320
|
+
|
|
321
|
+
## Data Attributes
|
|
322
|
+
|
|
323
|
+
### SegmentedControl
|
|
324
|
+
`[data-slot="segmented-control"]`
|
|
325
|
+
|
|
326
|
+
- `data-orientation` (`horizontal`, `vertical`): Which way the items run.
|
|
327
|
+
- `data-readonly`: Present when the control is read-only.
|
|
328
|
+
- `data-required`: Present when the control is required.
|
|
329
|
+
- `data-disabled`: Present when disabled, by its own prop, the control or a `<fieldset>`.
|
|
330
|
+
|
|
331
|
+
### SegmentedControlItem
|
|
332
|
+
`[data-slot="segmented-control-item"]`
|
|
333
|
+
|
|
334
|
+
- `data-checked`: Present on the checked item.
|
|
335
|
+
- `data-readonly`: Present when the control is read-only.
|
|
336
|
+
- `data-required`: Present when the control is required.
|
|
337
|
+
- `data-unchecked`: Present on items that aren't checked.
|
|
338
|
+
- `data-disabled`: Present when disabled, by its own prop, the control or a `<fieldset>`.
|
|
339
|
+
|
|
340
|
+
### SegmentedControlIndicator
|
|
341
|
+
`[data-slot="segmented-control-indicator"]`
|
|
342
|
+
|
|
343
|
+
- `data-orientation` (`horizontal`, `vertical`): Which way the items run.
|
|
344
|
+
|
|
345
|
+
## API Reference
|
|
346
|
+
|
|
347
|
+
Props and callbacks are the same as below, as React props.
|
|
348
|
+
|
|
349
|
+
### SegmentedControl
|
|
350
|
+
|
|
351
|
+
- `value?` (`T | null`): The checked item's value. Pass it to control the segmented control. `null` checks nothing.
|
|
352
|
+
- `defaultValue?` (`T | null`): The item checked at first, when uncontrolled. A form reset returns to it.
|
|
353
|
+
- `onValueChange?` (`((value: T | null, details: ChangeDetails<SegmentedControlValueChangeReason>) => void)`): Runs when a user or a form reset wants to change the value. `details.reason` says why, and `details.cancel()` stops it.
|
|
354
|
+
- `disabled?` (`boolean`): Disables every item: they can't be focused or changed, and the value isn't submitted.
|
|
355
|
+
- `readOnly?` (`boolean`): Keeps the items focusable and the value submitted, but stops changes.
|
|
356
|
+
- `required?` (`boolean`): Blocks form submission until an item is checked, with the browser's own message.
|
|
357
|
+
- `name?` (`string`): The name the value submits under. Without one, the segmented control submits nothing.
|
|
358
|
+
- `form?` (`string`): The id of a form to belong to, when the segmented control isn't inside that form.
|
|
359
|
+
- `loop?` (`boolean`): Whether arrow keys wrap from the last item to the first.
|
|
360
|
+
- `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.
|
|
361
|
+
- `controller?` (`{ getValue: () => T | null; setValue: (value: T | null, details?: SegmentedControlChangeOptions) => boolean; focus: () => void; }`): Checks items from anywhere. Create one with `createSegmentedControlController()`.
|
|
362
|
+
- `debug?` (`boolean`): Logs every state change with its reason to the console, in development builds.
|
|
363
|
+
|
|
364
|
+
### SegmentedControlItem
|
|
365
|
+
|
|
366
|
+
- `value` (`string`): What the value becomes when this item is checked.
|
|
367
|
+
- `disabled?` (`boolean`): Skips the item, like a disabled native radio: arrow keys pass it and it can't be focused or checked.
|
|
368
|
+
|
|
369
|
+
### State
|
|
370
|
+
|
|
371
|
+
What render functions, state-based class names and slots receive.
|
|
372
|
+
|
|
373
|
+
- `value` (`string | null`): The checked item's value.
|
|
374
|
+
- `disabled` (`boolean`): Whether the segmented control is disabled, by itself or by a `<fieldset>`.
|
|
375
|
+
- `readOnly` (`boolean`): Whether it's read-only: arrow keys still move focus, but nothing changes the checked item.
|
|
376
|
+
- `required` (`boolean`): Whether a form won't submit until an item is checked.
|
|
377
|
+
- `orientation` (`SegmentedControlOrientation`): Which way the items run.
|
|
378
|
+
|
|
379
|
+
- `checked` (`boolean`): Whether this item is the control's value, which the indicator sits behind.
|
|
380
|
+
- `disabled` (`boolean`): Whether this item is disabled, by itself, the segmented control or a `<fieldset>`.
|
|
381
|
+
- `readOnly` (`boolean`): Whether the control is read-only, so pressing this item changes nothing.
|
|
382
|
+
- `required` (`boolean`): Whether a form won't submit until an item is checked.
|
|
383
|
+
|
|
384
|
+
- `value` (`string | null`): The checked item's value, which the indicator sits behind.
|
|
385
|
+
- `orientation` (`SegmentedControlOrientation`): Which way the items run.
|
|
386
|
+
|
|
387
|
+
### Reasons
|
|
388
|
+
|
|
389
|
+
`details.reason` says why the checked value changed.
|
|
390
|
+
|
|
391
|
+
- `item-press` (`MouseEvent | KeyboardEvent`): A member of a collection was chosen: a menu item, a tab, a radio or a checkbox item.
|
|
392
|
+
- `list-navigation` (`KeyboardEvent`): Arrow, Home or End keys moved the selection, typing picked an item, or Ctrl or ⌘ with A selected every item of a listbox.
|
|
393
|
+
- `reset` (`none`): A form reset.
|
|
394
|
+
- `restore` (`none`): The browser restored state, like after going back, or a saved value was read back from storage.
|
|
395
|
+
- `imperative` (`none`): A controller or `api` call, or a property set on a web component.
|
|
396
|
+
|
|
397
|
+
### Controller
|
|
398
|
+
|
|
399
|
+
Create one with `createSegmentedControlController()` and pass it as `controller`.
|
|
400
|
+
|
|
401
|
+
Every write method takes an optional `{ reason, event }`, runs the change callback like a press, and returns `false` when the change is canceled.
|
|
402
|
+
|
|
403
|
+
- `getValue()` (`() => T | null`): The checked item's value.
|
|
404
|
+
- `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.
|
|
405
|
+
- `focus()` (`() => void`): Moves focus to the checked item, or the first one.
|
|
406
|
+
|
|
407
|
+
### Events
|
|
408
|
+
|
|
409
|
+
Callbacks get the new value first, then the details.
|
|
410
|
+
|
|
411
|
+
| Prop | On | Receives |
|
|
412
|
+
| --------------- | ------------------ | ----------------------------------------------------------------- |
|
|
413
|
+
| `onValueChange` | `SegmentedControl` | `(value, details)`: the checked value, and the [reason](#reasons) |
|
|
414
|
+
|
|
415
|
+
## Accessibility
|
|
416
|
+
|
|
417
|
+
A segmented control is a radio group, so it follows the [WAI-ARIA radio group pattern](https://www.w3.org/WAI/ARIA/apg/patterns/radio/) and behaves like native radios that share a name. Each `SegmentedControlItem` 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.
|
|
418
|
+
|
|
419
|
+
### Keyboard
|
|
420
|
+
|
|
421
|
+
| Key | Does |
|
|
422
|
+
| ---------- | ---------------------------------------------------------------------- |
|
|
423
|
+
| Tab | Moves into the control, onto the checked item or the first enabled one |
|
|
424
|
+
| Arrow keys | Move to the next or previous item and check it, skipping disabled ones |
|
|
425
|
+
| Space | Checks the focused item |
|
|
426
|
+
| Enter | Submits its form |
|
|
427
|
+
|
|
428
|
+
### Tested Behavior
|
|
429
|
+
|
|
430
|
+
Every line below is an automated test.
|
|
431
|
+
|
|
432
|
+
**Selection**
|
|
433
|
+
|
|
434
|
+
- The default item is checked, and only that one.
|
|
435
|
+
- Clicking an item checks it and says why.
|
|
436
|
+
- Clicking a disabled item does nothing.
|
|
437
|
+
- A change can be cancelled.
|
|
438
|
+
- A controller checks an item.
|
|
439
|
+
|
|
440
|
+
**Keyboard**
|
|
441
|
+
|
|
442
|
+
- Tab lands on the checked item, and the next Tab leaves.
|
|
443
|
+
- Arrow keys on both axes check the next item, skip disabled ones and wrap.
|
|
444
|
+
|
|
445
|
+
**Orientation**
|
|
446
|
+
|
|
447
|
+
- A segmented control is horizontal unless it says otherwise.
|
|
448
|
+
- A vertical segmented control reports it.
|
|
449
|
+
|
|
450
|
+
**Indicator**
|
|
451
|
+
|
|
452
|
+
- The indicator covers the checked item.
|
|
453
|
+
- The indicator follows the checked item.
|
|
454
|
+
- The indicator covers the checked item in a vertical control.
|
|
455
|
+
- The indicator matches items with fractional widths exactly.
|
|
456
|
+
- A scaled ancestor doesn't move the indicator off fractional items.
|
|
457
|
+
- With nothing checked, the indicator has no box.
|
|
458
|
+
|
|
459
|
+
**Forms**
|
|
460
|
+
|
|
461
|
+
- The checked value submits under the control's name.
|
|
462
|
+
- A form reset checks the default item again.
|
|
463
|
+
- A disabled control submits nothing, and its items can't be focused or checked.
|
|
464
|
+
|
|
465
|
+
Runs in React, Vue and web components.
|