@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.
Files changed (247) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +10 -2
  3. package/dist/accordion.d.ts +331 -0
  4. package/dist/accordion.js +25 -0
  5. package/dist/agent.d.ts +2 -0
  6. package/dist/agent.js +8 -0
  7. package/dist/alert-dialog.d.ts +56 -0
  8. package/dist/alert-dialog.js +45 -0
  9. package/dist/async-options.d.ts +22 -0
  10. package/dist/async-options.js +56 -0
  11. package/dist/autocomplete.d.ts +102 -0
  12. package/dist/autocomplete.js +195 -0
  13. package/dist/avatar.d.ts +301 -0
  14. package/dist/avatar.js +17 -0
  15. package/dist/button.d.ts +21 -0
  16. package/dist/button.js +45 -0
  17. package/dist/calendar-context.d.ts +10 -0
  18. package/dist/calendar-context.js +9 -0
  19. package/dist/calendar.d.ts +80 -0
  20. package/dist/calendar.js +257 -0
  21. package/dist/carousel.d.ts +52 -0
  22. package/dist/carousel.js +151 -0
  23. package/dist/checkbox.d.ts +24 -0
  24. package/dist/checkbox.js +98 -0
  25. package/dist/children.d.ts +38 -0
  26. package/dist/children.js +63 -0
  27. package/dist/collapsible.d.ts +302 -0
  28. package/dist/collapsible.js +19 -0
  29. package/dist/color-picker.d.ts +72 -0
  30. package/dist/color-picker.js +133 -0
  31. package/dist/color.d.ts +2 -0
  32. package/dist/color.js +22 -0
  33. package/dist/combobox.d.ts +152 -0
  34. package/dist/combobox.js +338 -0
  35. package/dist/command.d.ts +88 -0
  36. package/dist/command.js +233 -0
  37. package/dist/controller-target.d.ts +3 -0
  38. package/dist/controller-target.js +59 -0
  39. package/dist/date-input.d.ts +19 -0
  40. package/dist/date-input.js +58 -0
  41. package/dist/date-picker.d.ts +47 -0
  42. package/dist/date-picker.js +310 -0
  43. package/dist/date.d.ts +2 -0
  44. package/dist/date.js +60 -0
  45. package/dist/dialog-parts.d.ts +23 -0
  46. package/dist/dialog-parts.js +142 -0
  47. package/dist/dialog.d.ts +57 -0
  48. package/dist/dialog.js +32 -0
  49. package/dist/drawer.d.ts +70 -0
  50. package/dist/drawer.js +158 -0
  51. package/dist/editor-context.d.ts +4 -0
  52. package/dist/editor-context.js +9 -0
  53. package/dist/field-context.d.ts +47 -0
  54. package/dist/field-context.js +87 -0
  55. package/dist/field.d.ts +49 -0
  56. package/dist/field.js +169 -0
  57. package/dist/file-input.d.ts +46 -0
  58. package/dist/file-input.js +170 -0
  59. package/dist/form-inputs.d.ts +5 -0
  60. package/dist/form-inputs.js +22 -0
  61. package/dist/gesture.d.ts +1 -0
  62. package/dist/gesture.js +16 -0
  63. package/dist/history.d.ts +2 -0
  64. package/dist/history.js +12 -0
  65. package/dist/hover-card.d.ts +33 -0
  66. package/dist/hover-card.js +84 -0
  67. package/dist/image-cropper.d.ts +333 -0
  68. package/dist/image-cropper.js +37 -0
  69. package/dist/image.d.ts +2 -0
  70. package/dist/image.js +6 -0
  71. package/dist/input.d.ts +22 -0
  72. package/dist/input.js +51 -0
  73. package/dist/label.d.ts +294 -0
  74. package/dist/label.js +9 -0
  75. package/dist/list-items.d.ts +15 -0
  76. package/dist/list-items.js +39 -0
  77. package/dist/list-text.d.ts +4 -0
  78. package/dist/list-text.js +15 -0
  79. package/dist/listbox.d.ts +56 -0
  80. package/dist/listbox.js +122 -0
  81. package/dist/menu.d.ts +116 -0
  82. package/dist/menu.js +426 -0
  83. package/dist/menubar-context.d.ts +4 -0
  84. package/dist/menubar-context.js +7 -0
  85. package/dist/menubar.d.ts +11 -0
  86. package/dist/menubar.js +32 -0
  87. package/dist/meter.d.ts +312 -0
  88. package/dist/meter.js +30 -0
  89. package/dist/navigation-menu.d.ts +61 -0
  90. package/dist/navigation-menu.js +169 -0
  91. package/dist/number-input.d.ts +44 -0
  92. package/dist/number-input.js +185 -0
  93. package/dist/number.d.ts +2 -0
  94. package/dist/number.js +40 -0
  95. package/dist/observe.d.ts +2 -0
  96. package/dist/observe.js +8 -0
  97. package/dist/otp-input.d.ts +29 -0
  98. package/dist/otp-input.js +94 -0
  99. package/dist/overlays.d.ts +26 -0
  100. package/dist/overlays.js +82 -0
  101. package/dist/pagination.d.ts +336 -0
  102. package/dist/pagination.js +71 -0
  103. package/dist/password-input.d.ts +23 -0
  104. package/dist/password-input.js +65 -0
  105. package/dist/popover.d.ts +54 -0
  106. package/dist/popover.js +151 -0
  107. package/dist/popup.d.ts +41 -0
  108. package/dist/popup.js +180 -0
  109. package/dist/preferences.d.ts +35 -0
  110. package/dist/preferences.js +93 -0
  111. package/dist/primitive.d.ts +2 -0
  112. package/dist/primitive.js +14 -0
  113. package/dist/progress.d.ts +312 -0
  114. package/dist/progress.js +30 -0
  115. package/dist/radio.d.ts +22 -0
  116. package/dist/radio.js +86 -0
  117. package/dist/rating.d.ts +20 -0
  118. package/dist/rating.js +94 -0
  119. package/dist/render-element.d.ts +35 -0
  120. package/dist/render-element.js +103 -0
  121. package/dist/resizable.d.ts +302 -0
  122. package/dist/resizable.js +23 -0
  123. package/dist/scroll-area.d.ts +324 -0
  124. package/dist/scroll-area.js +49 -0
  125. package/dist/segmented-control.d.ts +24 -0
  126. package/dist/segmented-control.js +78 -0
  127. package/dist/segmented-input.d.ts +15 -0
  128. package/dist/segmented-input.js +48 -0
  129. package/dist/select.d.ts +125 -0
  130. package/dist/select.js +330 -0
  131. package/dist/separator.d.ts +290 -0
  132. package/dist/separator.js +9 -0
  133. package/dist/shortcuts.d.ts +32 -0
  134. package/dist/shortcuts.js +61 -0
  135. package/dist/slider.d.ts +38 -0
  136. package/dist/slider.js +163 -0
  137. package/dist/switch.d.ts +15 -0
  138. package/dist/switch.js +39 -0
  139. package/dist/tabs.d.ts +331 -0
  140. package/dist/tabs.js +28 -0
  141. package/dist/text-control.d.ts +22 -0
  142. package/dist/text-control.js +24 -0
  143. package/dist/text-match.d.ts +2 -0
  144. package/dist/text-match.js +8 -0
  145. package/dist/textarea.d.ts +10 -0
  146. package/dist/textarea.js +48 -0
  147. package/dist/time-input.d.ts +19 -0
  148. package/dist/time-input.js +63 -0
  149. package/dist/timeline.d.ts +327 -0
  150. package/dist/timeline.js +31 -0
  151. package/dist/toast-parts.d.ts +63 -0
  152. package/dist/toast-parts.js +144 -0
  153. package/dist/toast.d.ts +2 -0
  154. package/dist/toast.js +22 -0
  155. package/dist/toggle.d.ts +23 -0
  156. package/dist/toggle.js +76 -0
  157. package/dist/token-input.d.ts +35 -0
  158. package/dist/token-input.js +100 -0
  159. package/dist/toolbar-context.d.ts +6 -0
  160. package/dist/toolbar-context.js +16 -0
  161. package/dist/toolbar.d.ts +38 -0
  162. package/dist/toolbar.js +63 -0
  163. package/dist/tooltip.d.ts +39 -0
  164. package/dist/tooltip.js +108 -0
  165. package/dist/tree.d.ts +51 -0
  166. package/dist/tree.js +150 -0
  167. package/dist/trigger-menu.d.ts +83 -0
  168. package/dist/trigger-menu.js +167 -0
  169. package/dist/use-primitive.d.ts +178 -0
  170. package/dist/use-primitive.js +674 -0
  171. package/dist/waterfall.d.ts +80 -0
  172. package/dist/waterfall.js +108 -0
  173. package/docs/README.md +89 -0
  174. package/docs/agents.md +1523 -0
  175. package/docs/components/accordion.md +1391 -0
  176. package/docs/components/alert-dialog.md +850 -0
  177. package/docs/components/autocomplete.md +1963 -0
  178. package/docs/components/avatar.md +252 -0
  179. package/docs/components/button.md +444 -0
  180. package/docs/components/calendar.md +1687 -0
  181. package/docs/components/carousel.md +1244 -0
  182. package/docs/components/checkbox.md +1069 -0
  183. package/docs/components/collapsible.md +567 -0
  184. package/docs/components/color-picker.md +736 -0
  185. package/docs/components/combobox.md +3647 -0
  186. package/docs/components/command.md +1573 -0
  187. package/docs/components/date-input.md +608 -0
  188. package/docs/components/date-picker.md +1266 -0
  189. package/docs/components/dialog.md +1469 -0
  190. package/docs/components/drawer.md +1197 -0
  191. package/docs/components/field.md +549 -0
  192. package/docs/components/file-input.md +609 -0
  193. package/docs/components/hover-card.md +514 -0
  194. package/docs/components/image-cropper.md +579 -0
  195. package/docs/components/input.md +301 -0
  196. package/docs/components/label.md +217 -0
  197. package/docs/components/listbox.md +1130 -0
  198. package/docs/components/menu.md +1286 -0
  199. package/docs/components/menubar.md +455 -0
  200. package/docs/components/meter.md +304 -0
  201. package/docs/components/navigation-menu.md +1581 -0
  202. package/docs/components/number-input.md +1086 -0
  203. package/docs/components/otp-input.md +565 -0
  204. package/docs/components/pagination.md +648 -0
  205. package/docs/components/password-input.md +737 -0
  206. package/docs/components/popover.md +1552 -0
  207. package/docs/components/preferences.md +220 -0
  208. package/docs/components/progress.md +443 -0
  209. package/docs/components/radio.md +846 -0
  210. package/docs/components/rating.md +458 -0
  211. package/docs/components/resizable.md +982 -0
  212. package/docs/components/scroll-area.md +980 -0
  213. package/docs/components/segmented-control.md +465 -0
  214. package/docs/components/select.md +2133 -0
  215. package/docs/components/separator.md +243 -0
  216. package/docs/components/slider.md +1169 -0
  217. package/docs/components/switch.md +613 -0
  218. package/docs/components/tabs.md +1014 -0
  219. package/docs/components/textarea.md +318 -0
  220. package/docs/components/time-input.md +231 -0
  221. package/docs/components/timeline.md +677 -0
  222. package/docs/components/toast.md +1395 -0
  223. package/docs/components/toggle.md +541 -0
  224. package/docs/components/token-input.md +1007 -0
  225. package/docs/components/toolbar.md +787 -0
  226. package/docs/components/tooltip.md +884 -0
  227. package/docs/components/tree.md +1346 -0
  228. package/docs/components/trigger-menu.md +1257 -0
  229. package/docs/components/waterfall.md +754 -0
  230. package/docs/controllers.md +89 -0
  231. package/docs/core.md +8027 -0
  232. package/docs/custom-parts.md +120 -0
  233. package/docs/first-component.md +83 -0
  234. package/docs/forms.md +148 -0
  235. package/docs/hooks/async-options.md +366 -0
  236. package/docs/hooks/shortcuts.md +221 -0
  237. package/docs/installation.md +45 -0
  238. package/docs/introduction.md +32 -0
  239. package/docs/lists-and-menus.md +170 -0
  240. package/docs/llms.txt +1844 -0
  241. package/docs/parts-and-composition.md +93 -0
  242. package/docs/popups.md +117 -0
  243. package/docs/rendering.md +121 -0
  244. package/docs/server-rendering.md +253 -0
  245. package/docs/state-and-events.md +105 -0
  246. package/docs/styling-and-animation.md +176 -0
  247. package/package.json +333 -3
@@ -0,0 +1,613 @@
1
+ # Switch
2
+
3
+ Turn a setting on or off.
4
+
5
+ Use a switch for a setting that takes effect as soon as it changes, like Airplane mode or a notification preference. Keep its label the same in both states: the label says what the setting is, and the switch shows whether it's on.
6
+
7
+ ```tsx title="basic.tsx"
8
+ import { Switch, SwitchThumb } from "@ui-sdk/react/switch"
9
+
10
+ function Example() {
11
+ return (
12
+ <label data-slot="label">
13
+ Airplane mode
14
+ <Switch>
15
+ <SwitchThumb />
16
+ </Switch>
17
+ </label>
18
+ )
19
+ }
20
+
21
+ export { Example }
22
+ ```
23
+
24
+ Click Airplane mode, or focus the switch and press Space, to turn it on and off.
25
+
26
+ ## Installation
27
+
28
+ ```bash
29
+ npm install @ui-sdk/react @ui-sdk/core
30
+ ```
31
+
32
+ ## Composition
33
+
34
+ Use the following composition to build a `Switch`:
35
+
36
+ ```text
37
+ Switch
38
+ └── SwitchThumb
39
+ ```
40
+
41
+ Wrap the switch and its text in a label. The thumb stays mounted and gets the switch's state, so CSS can slide it from one end to the other.
42
+
43
+ ## Usage
44
+
45
+ Import the parts and put them together:
46
+
47
+ ```tsx
48
+ import { Switch, SwitchThumb } from "@ui-sdk/react/switch"
49
+
50
+ function Example() {
51
+ return (
52
+ <label>
53
+ Airplane mode
54
+ <Switch>
55
+ <SwitchThumb />
56
+ </Switch>
57
+ </label>
58
+ )
59
+ }
60
+
61
+ export { Example }
62
+ ```
63
+
64
+ ## Description
65
+
66
+ Use `FieldLabel` and `FieldDescription` when a setting needs more explanation. The field connects both to the switch.
67
+
68
+ ```tsx title="description.tsx"
69
+ import { Field, FieldDescription, FieldLabel } from "@ui-sdk/react/field"
70
+ import { Switch, SwitchThumb } from "@ui-sdk/react/switch"
71
+
72
+ function Example() {
73
+ return (
74
+ <Field>
75
+ <div data-slot="stack">
76
+ <FieldLabel>Weekly digest</FieldLabel>
77
+ <FieldDescription>
78
+ A summary of your week, every Monday.
79
+ </FieldDescription>
80
+ </div>
81
+ <Switch>
82
+ <SwitchThumb />
83
+ </Switch>
84
+ </Field>
85
+ )
86
+ }
87
+
88
+ export { Example }
89
+ ```
90
+
91
+ Click Weekly digest. The field connects the label and description to the switch.
92
+
93
+ ## Choice Card
94
+
95
+ A setting can fill a whole card. Here the label's `::after` covers the card, so a click anywhere on it toggles the switch, and `:has([data-checked])` marks the cards that are on.
96
+
97
+ ```tsx title="choice-card.tsx"
98
+ import { Field, FieldDescription, FieldLabel } from "@ui-sdk/react/field"
99
+ import { Switch, SwitchThumb } from "@ui-sdk/react/switch"
100
+
101
+ const settings = [
102
+ {
103
+ label: "Public profile",
104
+ description: "Anyone can see your name and photo.",
105
+ defaultChecked: false,
106
+ },
107
+ {
108
+ label: "Show activity status",
109
+ description: "Teammates can see when you're online.",
110
+ defaultChecked: true,
111
+ },
112
+ ]
113
+
114
+ function Example() {
115
+ return (
116
+ <div data-slot="stack">
117
+ {settings.map((setting) => (
118
+ <Field key={setting.label} data-variant="card">
119
+ <div data-slot="stack">
120
+ <FieldLabel>{setting.label}</FieldLabel>
121
+ <FieldDescription>{setting.description}</FieldDescription>
122
+ </div>
123
+ <Switch defaultChecked={setting.defaultChecked}>
124
+ <SwitchThumb />
125
+ </Switch>
126
+ </Field>
127
+ ))}
128
+ </div>
129
+ )
130
+ }
131
+
132
+ export { Example }
133
+ ```
134
+
135
+ Click anywhere on the Public profile card to turn it on. Each card's switch keeps its own value.
136
+
137
+ ## Disabled
138
+
139
+ `disabled` prevents focus and changes, and leaves the switch out of form submissions. A disabled `fieldset` does the same.
140
+
141
+ ```tsx title="disabled.tsx"
142
+ import { Switch, SwitchThumb } from "@ui-sdk/react/switch"
143
+
144
+ function Example() {
145
+ return (
146
+ <div data-slot="stack">
147
+ <label data-slot="label">
148
+ Product updates
149
+ <Switch>
150
+ <SwitchThumb />
151
+ </Switch>
152
+ </label>
153
+ <label data-slot="label">
154
+ Security alerts
155
+ <Switch defaultChecked disabled>
156
+ <SwitchThumb />
157
+ </Switch>
158
+ </label>
159
+ </div>
160
+ )
161
+ }
162
+
163
+ export { Example }
164
+ ```
165
+
166
+ Security alerts are always on, so that switch is disabled. Clicking it leaves it on, and Tab skips it.
167
+
168
+ ## Read-Only
169
+
170
+ `readOnly` keeps the switch focusable and included in form submissions, while preventing the user from changing it. Use it for a setting managed somewhere else.
171
+
172
+ ```tsx title="read-only.tsx"
173
+ import { Field, FieldDescription, FieldLabel } from "@ui-sdk/react/field"
174
+ import { Switch, SwitchThumb } from "@ui-sdk/react/switch"
175
+
176
+ function Example() {
177
+ return (
178
+ <Field>
179
+ <div data-slot="stack">
180
+ <FieldLabel>Encrypt backups</FieldLabel>
181
+ <FieldDescription>
182
+ Your organization manages this setting.
183
+ </FieldDescription>
184
+ </div>
185
+ <Switch name="encrypt" defaultChecked readOnly>
186
+ <SwitchThumb />
187
+ </Switch>
188
+ </Field>
189
+ )
190
+ }
191
+
192
+ export { Example }
193
+ ```
194
+
195
+ Tab to Encrypt backups. Space and clicks leave it on.
196
+
197
+ ## Controlled
198
+
199
+ Pass `checked` and `onCheckedChange` to own the value. A [controller](#controller) from `createSwitchController()` changes it from anywhere, like the Toggle button below. See [State and Events](../state-and-events.md) for the shared change callback.
200
+
201
+ ```tsx title="controlled.tsx"
202
+ "use client"
203
+
204
+ import * as React from "react"
205
+ import { createSwitchController } from "@ui-sdk/core/switch"
206
+ import { Switch, SwitchThumb } from "@ui-sdk/react/switch"
207
+ import { useLog } from "./log"
208
+
209
+ function Example() {
210
+ const log = useLog()
211
+ const [wifi] = React.useState(() => createSwitchController())
212
+ const [checked, setChecked] = React.useState(true)
213
+
214
+ return (
215
+ <div data-slot="stack">
216
+ <div data-slot="controls">
217
+ <button type="button" onClick={() => wifi.toggle()}>
218
+ Toggle
219
+ </button>
220
+ </div>
221
+ <label data-slot="label">
222
+ Wi-Fi
223
+ <Switch
224
+ checked={checked}
225
+ onCheckedChange={(next, details) => {
226
+ setChecked(next)
227
+ log(`${next ? "On" : "Off"} (${details.reason})`)
228
+ }}
229
+ controller={wifi}
230
+ >
231
+ <SwitchThumb />
232
+ </Switch>
233
+ </label>
234
+ </div>
235
+ )
236
+ }
237
+
238
+ export { Example }
239
+ ```
240
+
241
+ Turn Wi-Fi off, or press Toggle. The console logs each change and its reason.
242
+
243
+ ## Form
244
+
245
+ Set `name` to include the switch in a native form. A switch that's on submits its `value`, which defaults to `"on"`; a switch that's off submits nothing. Reset returns it to `defaultChecked`. Set `form` to a form's id to join it from elsewhere on the page.
246
+
247
+ ```tsx title="form.tsx"
248
+ "use client"
249
+
250
+ import { FieldLegend, FieldSet } from "@ui-sdk/react/field"
251
+ import { Switch, SwitchThumb } from "@ui-sdk/react/switch"
252
+ import { useLog } from "./log"
253
+
254
+ function Example() {
255
+ const log = useLog()
256
+
257
+ return (
258
+ <form
259
+ data-slot="stack"
260
+ onSubmit={(event) => {
261
+ event.preventDefault()
262
+ const entries = [...new FormData(event.currentTarget)].map(
263
+ ([name, value]) => `${name}=${value}`
264
+ )
265
+ log(
266
+ entries.length
267
+ ? `Submitted ${entries.join("&")}`
268
+ : "Submitted nothing"
269
+ )
270
+ }}
271
+ >
272
+ <FieldSet>
273
+ <FieldLegend>Email notifications</FieldLegend>
274
+ <label data-slot="label">
275
+ Product updates
276
+ <Switch name="updates">
277
+ <SwitchThumb />
278
+ </Switch>
279
+ </label>
280
+ <label data-slot="label">
281
+ Security alerts
282
+ <Switch name="security" defaultChecked>
283
+ <SwitchThumb />
284
+ </Switch>
285
+ </label>
286
+ <label data-slot="label">
287
+ Weekly digest
288
+ <Switch name="digest" value="weekly" defaultChecked>
289
+ <SwitchThumb />
290
+ </Switch>
291
+ </label>
292
+ </FieldSet>
293
+ <div data-slot="row">
294
+ <button type="submit" data-variant="solid">
295
+ Save
296
+ </button>
297
+ <button type="reset">Reset</button>
298
+ </div>
299
+ </form>
300
+ )
301
+ }
302
+
303
+ export { Example }
304
+ ```
305
+
306
+ Turn on Product updates and press Save. The console logs the submitted values, and Reset restores the defaults.
307
+
308
+ ## Validation
309
+
310
+ `required` blocks submission until the switch is on. In a [Field](./field.md), `FieldError` shows the message under the switch and connects it to the switch. [Forms and Validation](../forms.md) covers the shared field behavior.
311
+
312
+ ```tsx title="invalid.tsx"
313
+ "use client"
314
+
315
+ import {
316
+ Field,
317
+ FieldDescription,
318
+ FieldError,
319
+ FieldLabel,
320
+ } from "@ui-sdk/react/field"
321
+ import { Switch, SwitchThumb } from "@ui-sdk/react/switch"
322
+ import { useLog } from "./log"
323
+
324
+ function Example() {
325
+ const log = useLog()
326
+
327
+ return (
328
+ <form
329
+ data-slot="stack"
330
+ onSubmit={(event) => {
331
+ event.preventDefault()
332
+ log(
333
+ `Submitted two-factor=${new FormData(event.currentTarget).get("two-factor")}`
334
+ )
335
+ }}
336
+ >
337
+ <Field>
338
+ <div data-slot="row">
339
+ <div data-slot="stack">
340
+ <FieldLabel>Two-factor authentication</FieldLabel>
341
+ <FieldDescription>
342
+ Required for admins of this workspace.
343
+ </FieldDescription>
344
+ </div>
345
+ <Switch name="two-factor" required>
346
+ <SwitchThumb />
347
+ </Switch>
348
+ </div>
349
+ <FieldError match="valueMissing">Turn this on to continue.</FieldError>
350
+ </Field>
351
+ <div data-slot="row">
352
+ <button type="submit" data-variant="solid">
353
+ Continue
354
+ </button>
355
+ </div>
356
+ </form>
357
+ )
358
+ }
359
+
360
+ export { Example }
361
+ ```
362
+
363
+ Press Continue with the switch off to see the error. Turn on Two-factor authentication and continue; the console logs the submitted value.
364
+
365
+ ## Saving on Toggle
366
+
367
+ To save a change before it shows, call `details.cancel()` in the change callback to keep the current value while the save runs. When it finishes, call `controller.setChecked(next, { reason: "imperative" })`, and let that reason through in the callback so the saved value commits.
368
+
369
+ The example ignores presses while a save is running, and shows a spinner in the thumb. `aria-busy` tells assistive technology the switch is busy; the callback is what blocks another change.
370
+
371
+ ```tsx title="async.tsx"
372
+ "use client"
373
+
374
+ import * as React from "react"
375
+ import { createSwitchController } from "@ui-sdk/core/switch"
376
+ import { Switch, SwitchThumb } from "@ui-sdk/react/switch"
377
+ import { LoaderCircleIcon } from "lucide-react"
378
+ import { useLog } from "./log"
379
+
380
+ const save = (_checked: boolean) =>
381
+ new Promise((resolve) => setTimeout(resolve, 800))
382
+
383
+ function Example() {
384
+ const log = useLog()
385
+ const [backups] = React.useState(() => createSwitchController())
386
+ const [saving, setSaving] = React.useState(false)
387
+ const pending = React.useRef(false)
388
+
389
+ return (
390
+ <label data-slot="label">
391
+ Automatic backups
392
+ <Switch
393
+ controller={backups}
394
+ aria-busy={saving}
395
+ onCheckedChange={(next, details) => {
396
+ if (details.reason === "imperative") {
397
+ return
398
+ }
399
+ details.cancel()
400
+ if (pending.current) {
401
+ return
402
+ }
403
+ pending.current = true
404
+ setSaving(true)
405
+ log("Saving…")
406
+ save(next).then(() => {
407
+ pending.current = false
408
+ backups.setChecked(next, { reason: "imperative" })
409
+ setSaving(false)
410
+ log(`Saved: ${next ? "on" : "off"}`)
411
+ })
412
+ }}
413
+ >
414
+ <SwitchThumb>
415
+ {saving && (
416
+ <LoaderCircleIcon data-slot="spinner" aria-hidden="true" />
417
+ )}
418
+ </SwitchThumb>
419
+ </Switch>
420
+ </label>
421
+ )
422
+ }
423
+
424
+ export { Example }
425
+ ```
426
+
427
+ Turn on Automatic backups. The thumb shows a spinner while it saves, then the switch turns on.
428
+
429
+ ## Custom Rendering
430
+
431
+ The switch and thumb take `render`. Pass an element and its props merge with the part's, or a function that receives the props and the part's state. Keep the part's props on the element so its role, label and keyboard behavior stay connected. See [Parts and Composition](../parts-and-composition.md) for the rendering contract.
432
+
433
+ ```tsx title="render.tsx"
434
+ import { Switch, SwitchThumb } from "@ui-sdk/react/switch"
435
+ import { MoonIcon, SunIcon } from "lucide-react"
436
+
437
+ function Example() {
438
+ return (
439
+ <label data-slot="label">
440
+ Dark mode
441
+ <Switch>
442
+ <SwitchThumb
443
+ render={(props, state) => (
444
+ <span {...props}>
445
+ {state.checked ? (
446
+ <MoonIcon aria-hidden="true" />
447
+ ) : (
448
+ <SunIcon aria-hidden="true" />
449
+ )}
450
+ </span>
451
+ )}
452
+ />
453
+ </Switch>
454
+ </label>
455
+ )
456
+ }
457
+
458
+ export { Example }
459
+ ```
460
+
461
+ Turn on Dark mode. The thumb renders a sun or a moon from its state.
462
+
463
+ ## Styling
464
+
465
+ Style the switch and its thumb with `data-checked` and `data-unchecked`, and use `data-disabled`, `data-readonly` and `data-required` for the rest. In an invalid field, the switch also gets `data-invalid` and `aria-invalid`; the thumb doesn't. Slide the thumb with `translate` on `data-checked`, and reverse it under `dir="rtl"`.
466
+
467
+ Each part has a `data-slot`, such as `switch`, and its state attributes are listed under [Data Attributes](#data-attributes).
468
+
469
+ ## Data Attributes
470
+
471
+ ### Switch
472
+ `[data-slot="switch"]`
473
+
474
+ - `data-checked`: Present while on.
475
+ - `data-disabled`: Present when disabled, by its own prop or a `<fieldset>`.
476
+ - `data-readonly`: Present when read-only.
477
+ - `data-required`: Present when required.
478
+ - `data-unchecked`: Present while off.
479
+ - `data-invalid`: Present while a `Field` around it shows as invalid. The thumb doesn't get it.
480
+
481
+ ### SwitchThumb
482
+ `[data-slot="switch-thumb"]`
483
+
484
+ - `data-checked`: Present while on.
485
+ - `data-disabled`: Present when disabled, by its own prop or a `<fieldset>`.
486
+ - `data-readonly`: Present when read-only.
487
+ - `data-required`: Present when required.
488
+ - `data-unchecked`: Present while off.
489
+
490
+ ## API Reference
491
+
492
+ Props and callbacks are the same as below, as React props.
493
+
494
+ ### Switch
495
+
496
+ - `checked?` (`boolean`): Whether it's checked. Pass it to control the checkbox.
497
+ - `defaultChecked?` (`boolean`): Whether it's checked at first, when uncontrolled. A form reset returns to it.
498
+ - `onCheckedChange?` (`((checked: boolean, details: ChangeDetails<CheckboxCheckedChangeReason>) => void)`): Runs when a user or a form reset wants to change it. `details.reason` says why, and `details.cancel()` stops it.
499
+ - `disabled?` (`boolean`): Disables it like a native control: it can't be focused or changed, and isn't submitted.
500
+ - `readOnly?` (`boolean`): Keeps it focusable and submitted, but stops changes.
501
+ - `required?` (`boolean`): Blocks form submission until it's checked, with the browser's own message.
502
+ - `name?` (`string`): The name it submits under. Without one, it submits nothing.
503
+ - `value?` (`string`): What it submits while checked.
504
+ - `form?` (`string`): The id of a form to belong to, when it isn't inside that form.
505
+ - `controller?` (`{ toggle: (details?: Details) => boolean; getChecked: () => boolean; setChecked: (checked: boolean, details?: Details) => boolean; focus: () => void; }`): Toggles it from anywhere. Create one with `createSwitchController()`.
506
+ - `debug?` (`boolean`): Logs every state change with its reason to the console, in development builds.
507
+
508
+ ### State
509
+
510
+ What render functions, state-based class names and slots receive. The thumb's state, `SwitchThumbState`, is the same.
511
+
512
+ - `checked` (`boolean`): Whether it's checked. False while indeterminate.
513
+ - `disabled` (`boolean`): Whether it's disabled, by itself or by a `<fieldset>`.
514
+ - `readOnly` (`boolean`): Whether it's read-only: it stays focusable and submitted, but presses don't change it.
515
+ - `required` (`boolean`): Whether a form won't submit until it's checked. Indeterminate doesn't count.
516
+
517
+ ### Reasons
518
+
519
+ `details.reason` says why the switch turned on or off.
520
+
521
+ - `press` (`MouseEvent | KeyboardEvent`): A single-element control was pressed: a checkbox, a switch or a toggle.
522
+ - `reset` (`none`): A form reset.
523
+ - `restore` (`none`): The browser restored state, like after going back, or a saved value was read back from storage.
524
+ - `imperative` (`none`): A controller or `api` call, or a property set on a web component.
525
+
526
+ ### Controller
527
+
528
+ Create one with `createSwitchController()` and pass it as `controller`.
529
+
530
+ Every write method takes an optional `{ reason, event }`, runs the change callback like a press, and returns `false` when the change is canceled.
531
+
532
+ - `toggle(details?)` (`(details?: CheckboxChangeOptions | undefined) => boolean`): Turns it on, or off.
533
+ - `getChecked()` (`() => boolean`): Whether it's on.
534
+ - `setChecked(checked, details?)` (`(checked: boolean, details?: CheckboxChangeOptions | undefined) => boolean`): Turns it on or off, as a user would.
535
+ - `focus()` (`() => void`): Moves focus to it.
536
+
537
+ ### Events
538
+
539
+ Callbacks get the new state first, then the details.
540
+
541
+ | Prop | On | Receives |
542
+ | ----------------- | -------- | --------------------------------------------------------------- |
543
+ | `onCheckedChange` | `Switch` | `(checked, details)`: the new state, and the [reason](#reasons) |
544
+
545
+ ## Accessibility
546
+
547
+ The switch follows the [WAI-ARIA switch pattern](https://www.w3.org/WAI/ARIA/apg/patterns/switch/). `Switch` renders a button with the `switch` role, and a hidden native checkbox that takes part in forms.
548
+
549
+ ### Keyboard
550
+
551
+ | Key | Does |
552
+ | ----- | -------------------------------------- |
553
+ | Tab | Moves to the next switch |
554
+ | Space | Turns it on or off |
555
+ | Enter | Submits its form, and never toggles it |
556
+
557
+ ### Tested Behavior
558
+
559
+ Every line below is an automated test.
560
+
561
+ - Starts in its default state.
562
+ - Clicking toggles it.
563
+ - Clicking its label toggles it.
564
+ - Space toggles it.
565
+ - Enter submits the form and never toggles it.
566
+ - The thumb follows the state.
567
+
568
+ **The first input on a page**
569
+
570
+ - The first Space reports keyboard.
571
+
572
+ **On a touch screen**
573
+
574
+ - The first tap reports touch.
575
+
576
+ Runs in React, Vue and web components.
577
+
578
+ In forms:
579
+
580
+ - The form submits every checked control's name and value.
581
+ - Unchecked controls submit nothing.
582
+ - A checkbox outside the form joins it with the form attribute.
583
+ - Required controls block submission.
584
+ - A form reset returns every control to its default.
585
+ - A reset returns to the default value, not to nothing.
586
+ - A disabled fieldset disables the controls inside it.
587
+ - A read-only control still submits.
588
+ - A user's change fires native input and change events on the form.
589
+
590
+ Runs in React, Vue and web components.
591
+
592
+ Beyond the pattern's baseline:
593
+
594
+ **Enhancements**
595
+
596
+ - The first invalid control takes focus in document order.
597
+ - A later required field without an error part doesn't take focus from the first.
598
+
599
+ **Form-associated custom elements**
600
+
601
+ - Take part in the form with no hidden input. Web components only.
602
+ - Report validity like native controls. Web components only.
603
+ - Have live checked and value properties. Web components only.
604
+ - Are named by their labels. Web components only.
605
+
606
+ Runs in React, Vue and web components.
607
+
608
+ Screen readers hear the same thing in each tested framework, recorded by a virtual screen reader:
609
+
610
+ - Announcements while checking boxes with a keyboard.
611
+ - Announcements while choosing a radio with a keyboard.
612
+
613
+ Runs in React, Vue and web components.