@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,846 @@
1
+ # Radio
2
+
3
+ Choose one option, move the selection with arrow keys, and submit it with a form.
4
+
5
+ Use radios for a short list of choices where only one can be picked, like a shipping speed or a plan. The group keeps one value and gives the whole list a single tab stop.
6
+
7
+ ```tsx title="basic.tsx"
8
+ import { Field, FieldLabel } from "@ui-sdk/react/field"
9
+ import { Radio, RadioGroup, RadioIndicator } from "@ui-sdk/react/radio"
10
+
11
+ const speeds = [
12
+ { value: "standard", label: "Standard" },
13
+ { value: "express", label: "Express" },
14
+ { value: "overnight", label: "Overnight" },
15
+ ]
16
+
17
+ function Example() {
18
+ return (
19
+ <Field>
20
+ <FieldLabel>Shipping speed</FieldLabel>
21
+ <RadioGroup defaultValue="standard">
22
+ {speeds.map((speed) => (
23
+ <label data-slot="label" key={speed.value}>
24
+ <Radio value={speed.value}>
25
+ <RadioIndicator />
26
+ </Radio>
27
+ {speed.label}
28
+ </label>
29
+ ))}
30
+ </RadioGroup>
31
+ </Field>
32
+ )
33
+ }
34
+
35
+ export { Example }
36
+ ```
37
+
38
+ Choose Express, then use the arrow keys to move the selection between speeds.
39
+
40
+ ## Installation
41
+
42
+ ```bash
43
+ npm install @ui-sdk/react @ui-sdk/core
44
+ ```
45
+
46
+ ## Composition
47
+
48
+ Use the following composition to build a `RadioGroup`:
49
+
50
+ ```text
51
+ RadioGroup
52
+ ├── Radio
53
+ │ └── RadioIndicator
54
+ └── Radio
55
+ └── RadioIndicator
56
+ ```
57
+
58
+ Give each radio a `value` and wrap it with its text in a label. The group's value is the checked radio's value, and the indicator shows while its radio is checked.
59
+
60
+ ## Usage
61
+
62
+ Import the parts and put them together:
63
+
64
+ ```tsx
65
+ import { Field, FieldLabel } from "@ui-sdk/react/field"
66
+ import { Radio, RadioGroup, RadioIndicator } from "@ui-sdk/react/radio"
67
+
68
+ function Example() {
69
+ return (
70
+ <Field>
71
+ <FieldLabel>Shipping speed</FieldLabel>
72
+ <RadioGroup defaultValue="standard">
73
+ <label>
74
+ <Radio value="standard">
75
+ <RadioIndicator />
76
+ </Radio>
77
+ Standard
78
+ </label>
79
+ <label>
80
+ <Radio value="express">
81
+ <RadioIndicator />
82
+ </Radio>
83
+ Express
84
+ </label>
85
+ </RadioGroup>
86
+ </Field>
87
+ )
88
+ }
89
+
90
+ export { Example }
91
+ ```
92
+
93
+ The field's label names the group. Without a field, name it with `aria-label` or `aria-labelledby`. Without `defaultValue`, nothing starts checked and Tab enters on the first enabled radio.
94
+
95
+ ## Description
96
+
97
+ Put each radio in its own `Field` with a `FieldLabel` and `FieldDescription` when an option needs more explanation. That field connects both to its radio, while the outer field still names the group.
98
+
99
+ ```tsx title="description.tsx"
100
+ import { Field, FieldDescription, FieldLabel } from "@ui-sdk/react/field"
101
+ import { Radio, RadioGroup, RadioIndicator } from "@ui-sdk/react/radio"
102
+
103
+ const frequencies = [
104
+ { value: "instant", label: "Instantly", description: "An email right away." },
105
+ {
106
+ value: "daily",
107
+ label: "Daily digest",
108
+ description: "One email each morning.",
109
+ },
110
+ {
111
+ value: "weekly",
112
+ label: "Weekly digest",
113
+ description: "A summary every Monday.",
114
+ },
115
+ ]
116
+
117
+ function Example() {
118
+ return (
119
+ <Field>
120
+ <FieldLabel>Email notifications</FieldLabel>
121
+ <RadioGroup defaultValue="daily">
122
+ {frequencies.map((frequency) => (
123
+ <Field key={frequency.value}>
124
+ <Radio value={frequency.value}>
125
+ <RadioIndicator />
126
+ </Radio>
127
+ <div data-slot="stack">
128
+ <FieldLabel>{frequency.label}</FieldLabel>
129
+ <FieldDescription>{frequency.description}</FieldDescription>
130
+ </div>
131
+ </Field>
132
+ ))}
133
+ </RadioGroup>
134
+ </Field>
135
+ )
136
+ }
137
+
138
+ export { Example }
139
+ ```
140
+
141
+ Choose Weekly digest by its label. Each radio is also described by the line under it.
142
+
143
+ ## Choice Card
144
+
145
+ Wrap an option's content in its label to make the whole card clickable. `:has([data-checked])` lets the card reflect the radio inside it.
146
+
147
+ ```tsx title="choice-card.tsx"
148
+ import { Field, FieldLabel } from "@ui-sdk/react/field"
149
+ import { Radio, RadioGroup, RadioIndicator } from "@ui-sdk/react/radio"
150
+
151
+ const plans = [
152
+ {
153
+ value: "hobby",
154
+ label: "Hobby",
155
+ description: "Free for personal projects.",
156
+ },
157
+ {
158
+ value: "pro",
159
+ label: "Pro",
160
+ description: "$20 per month for growing teams.",
161
+ },
162
+ {
163
+ value: "enterprise",
164
+ label: "Enterprise",
165
+ description: "Custom pricing and support.",
166
+ },
167
+ ]
168
+
169
+ function Example() {
170
+ return (
171
+ <Field>
172
+ <FieldLabel>Plan</FieldLabel>
173
+ <RadioGroup defaultValue="hobby">
174
+ {plans.map((plan) => (
175
+ <label data-slot="card" key={plan.value}>
176
+ <span data-slot="stack">
177
+ <span>{plan.label}</span>
178
+ <span data-slot="hint">{plan.description}</span>
179
+ </span>
180
+ <Radio value={plan.value}>
181
+ <RadioIndicator />
182
+ </Radio>
183
+ </label>
184
+ ))}
185
+ </RadioGroup>
186
+ </Field>
187
+ )
188
+ }
189
+
190
+ export { Example }
191
+ ```
192
+
193
+ Click anywhere on the Pro card to choose it. Arrow keys move the selection between cards too.
194
+
195
+ ## Fieldset and Legend
196
+
197
+ Use `FieldSet` and `FieldLegend` to describe a set of choices together. Connect the group to the legend with `aria-labelledby`, and to the description with `aria-describedby`.
198
+
199
+ ```tsx title="legend.tsx"
200
+ "use client"
201
+
202
+ import { FieldDescription, FieldLegend, FieldSet } from "@ui-sdk/react/field"
203
+ import { Radio, RadioGroup, RadioIndicator } from "@ui-sdk/react/radio"
204
+ import * as React from "react"
205
+
206
+ const periods = [
207
+ { value: "monthly", label: "Monthly", price: "$12 per month" },
208
+ { value: "yearly", label: "Yearly", price: "$120 per year" },
209
+ ]
210
+
211
+ function Example() {
212
+ const id = React.useId()
213
+ return (
214
+ <FieldSet>
215
+ <FieldLegend id={`${id}-legend`}>Billing period</FieldLegend>
216
+ <FieldDescription id={`${id}-description`}>
217
+ Yearly billing gets you two months free.
218
+ </FieldDescription>
219
+ <RadioGroup
220
+ defaultValue="monthly"
221
+ aria-labelledby={`${id}-legend`}
222
+ aria-describedby={`${id}-description`}
223
+ >
224
+ {periods.map((period) => (
225
+ <label data-slot="label" key={period.value}>
226
+ <Radio value={period.value}>
227
+ <RadioIndicator />
228
+ </Radio>
229
+ {period.label}
230
+ <span data-slot="hint">({period.price})</span>
231
+ </label>
232
+ ))}
233
+ </RadioGroup>
234
+ </FieldSet>
235
+ )
236
+ }
237
+
238
+ export { Example }
239
+ ```
240
+
241
+ Choose Yearly. The legend and the line under it describe both billing periods.
242
+
243
+ ## Disabled
244
+
245
+ Set `disabled` on the group or on one radio. Disabled radios can't be focused or checked, and arrow keys skip them. A disabled fieldset disables the group too, and a disabled group submits nothing.
246
+
247
+ ```tsx title="disabled.tsx"
248
+ import { Field, FieldDescription, FieldLabel } from "@ui-sdk/react/field"
249
+ import { Radio, RadioGroup, RadioIndicator } from "@ui-sdk/react/radio"
250
+
251
+ const themes = [
252
+ { value: "light", label: "Light" },
253
+ { value: "dark", label: "Dark" },
254
+ { value: "system", label: "System" },
255
+ ]
256
+
257
+ const backups = [
258
+ { value: "hourly", label: "Hourly", disabled: true },
259
+ { value: "daily", label: "Daily", disabled: false },
260
+ { value: "weekly", label: "Weekly", disabled: false },
261
+ ]
262
+
263
+ function Example() {
264
+ return (
265
+ <div data-slot="stack">
266
+ <Field>
267
+ <FieldLabel>Theme</FieldLabel>
268
+ <FieldDescription>Set by your organization.</FieldDescription>
269
+ <RadioGroup defaultValue="light" disabled>
270
+ {themes.map((theme) => (
271
+ <label data-slot="label" key={theme.value}>
272
+ <Radio value={theme.value}>
273
+ <RadioIndicator />
274
+ </Radio>
275
+ {theme.label}
276
+ </label>
277
+ ))}
278
+ </RadioGroup>
279
+ </Field>
280
+ <Field>
281
+ <FieldLabel>Backups</FieldLabel>
282
+ <FieldDescription>Hourly backups need the Pro plan.</FieldDescription>
283
+ <RadioGroup defaultValue="daily">
284
+ {backups.map((backup) => (
285
+ <label data-slot="label" key={backup.value}>
286
+ <Radio value={backup.value} disabled={backup.disabled}>
287
+ <RadioIndicator />
288
+ </Radio>
289
+ {backup.label}
290
+ </label>
291
+ ))}
292
+ </RadioGroup>
293
+ </Field>
294
+ </div>
295
+ )
296
+ }
297
+
298
+ export { Example }
299
+ ```
300
+
301
+ The Theme group is disabled. In Backups, Hourly is disabled, so the arrow keys move between Daily and Weekly.
302
+
303
+ ## Read-Only
304
+
305
+ Set `readOnly` on the group to keep its value while still letting people focus and read the options. Arrow keys move focus without checking anything, and the value still submits with a form.
306
+
307
+ ```tsx title="read-only.tsx"
308
+ import { Field, FieldDescription, FieldLabel } from "@ui-sdk/react/field"
309
+ import { Radio, RadioGroup, RadioIndicator } from "@ui-sdk/react/radio"
310
+
311
+ const plans = [
312
+ { value: "free", label: "Free" },
313
+ { value: "pro", label: "Pro" },
314
+ { value: "team", label: "Team" },
315
+ ]
316
+
317
+ function Example() {
318
+ return (
319
+ <Field>
320
+ <FieldLabel>Your plan</FieldLabel>
321
+ <FieldDescription>Ask an admin to change plans.</FieldDescription>
322
+ <RadioGroup defaultValue="pro" readOnly>
323
+ {plans.map((plan) => (
324
+ <label data-slot="label" key={plan.value}>
325
+ <Radio value={plan.value}>
326
+ <RadioIndicator />
327
+ </Radio>
328
+ {plan.label}
329
+ </label>
330
+ ))}
331
+ </RadioGroup>
332
+ </Field>
333
+ )
334
+ }
335
+
336
+ export { Example }
337
+ ```
338
+
339
+ Click Team, or focus Pro and press the Down arrow. Pro stays checked.
340
+
341
+ ## Controlled
342
+
343
+ Pass `value` and update it in `onValueChange`. Call `details.cancel()` in the callback to keep the current value. A [controller](#controller) from `createRadioGroupController()` checks a radio from anywhere, like the Choose Team button below. Create one controller per mounted group.
344
+
345
+ ```tsx title="controlled.tsx"
346
+ "use client"
347
+
348
+ import { Field, FieldLabel } from "@ui-sdk/react/field"
349
+ import {
350
+ createRadioGroupController,
351
+ Radio,
352
+ RadioGroup,
353
+ RadioIndicator,
354
+ } from "@ui-sdk/react/radio"
355
+ import * as React from "react"
356
+ import { useLog } from "./log"
357
+
358
+ type Plan = "starter" | "pro" | "team"
359
+
360
+ const plans: { value: Plan; label: string }[] = [
361
+ { value: "starter", label: "Starter" },
362
+ { value: "pro", label: "Pro" },
363
+ { value: "team", label: "Team" },
364
+ ]
365
+
366
+ function Example() {
367
+ const log = useLog()
368
+ const [planGroup] = React.useState(() => createRadioGroupController<Plan>())
369
+ const [value, setValue] = React.useState<Plan | null>("pro")
370
+
371
+ return (
372
+ <div data-slot="stack">
373
+ <div data-slot="controls">
374
+ <button type="button" onClick={() => planGroup.setValue("team")}>
375
+ Choose Team
376
+ </button>
377
+ </div>
378
+ <Field>
379
+ <FieldLabel>Plan</FieldLabel>
380
+ <RadioGroup
381
+ value={value}
382
+ onValueChange={(next, details) => {
383
+ setValue(next)
384
+ log(
385
+ `${plans.find((plan) => plan.value === next)?.label ?? "None"} (${details.reason})`
386
+ )
387
+ }}
388
+ controller={planGroup}
389
+ >
390
+ {plans.map((plan) => (
391
+ <label data-slot="label" key={plan.value}>
392
+ <Radio value={plan.value}>
393
+ <RadioIndicator />
394
+ </Radio>
395
+ {plan.label}
396
+ </label>
397
+ ))}
398
+ </RadioGroup>
399
+ </Field>
400
+ </div>
401
+ )
402
+ }
403
+
404
+ export { Example }
405
+ ```
406
+
407
+ Choose Starter, then press Choose Team. The console logs each change and its reason.
408
+
409
+ See [State and Events](../state-and-events.md) for controlled state, cancellation and debugging.
410
+
411
+ ## Form
412
+
413
+ Each radio renders a button with the `radio` role and a hidden native radio, so the group takes part in forms. Set `name` to submit the checked value; the group submits nothing without a name or a checked radio. `required` blocks submission until there's a choice, and a reset returns to `defaultValue`.
414
+
415
+ Set `form` to a form's id to join it from elsewhere on the page, as this group does.
416
+
417
+ ```tsx title="form.tsx"
418
+ "use client"
419
+
420
+ import { Field, FieldLabel } from "@ui-sdk/react/field"
421
+ import { Radio, RadioGroup, RadioIndicator } from "@ui-sdk/react/radio"
422
+ import * as React from "react"
423
+ import { useLog } from "./log"
424
+
425
+ const speeds = [
426
+ { value: "standard", label: "Standard", detail: "Free, 5 to 7 days" },
427
+ { value: "express", label: "Express", detail: "$9, 2 days" },
428
+ { value: "overnight", label: "Overnight", detail: "$24, next day" },
429
+ ]
430
+
431
+ function Example() {
432
+ const log = useLog()
433
+ const id = React.useId()
434
+
435
+ return (
436
+ <div data-slot="stack">
437
+ <Field>
438
+ <FieldLabel>Shipping speed</FieldLabel>
439
+ <RadioGroup name="shipping" form={`${id}-checkout`} required defaultValue="standard">
440
+ {speeds.map((speed) => (
441
+ <label data-slot="label" key={speed.value}>
442
+ <Radio value={speed.value}>
443
+ <RadioIndicator />
444
+ </Radio>
445
+ {speed.label}
446
+ <span data-slot="hint">({speed.detail})</span>
447
+ </label>
448
+ ))}
449
+ </RadioGroup>
450
+ </Field>
451
+ <form
452
+ data-slot="row"
453
+ id={`${id}-checkout`}
454
+ onSubmit={(event) => {
455
+ event.preventDefault()
456
+ log(
457
+ `Submitted shipping=${new FormData(event.currentTarget).get("shipping")}`
458
+ )
459
+ }}
460
+ >
461
+ <button type="submit" data-variant="solid">
462
+ Continue
463
+ </button>
464
+ <button type="reset">Reset</button>
465
+ </form>
466
+ </div>
467
+ )
468
+ }
469
+
470
+ export { Example }
471
+ ```
472
+
473
+ Choose Express and press Continue. The console logs the submitted value, and Reset returns the choice to Standard.
474
+
475
+ ## Validation
476
+
477
+ Put a required group in a [Field](./field.md) with a `FieldError`. Submitting without a choice shows the error under the group and moves focus to it. The [Forms guide](../forms.md) covers the shared field behavior.
478
+
479
+ ```tsx title="invalid.tsx"
480
+ "use client"
481
+
482
+ import {
483
+ Field,
484
+ FieldDescription,
485
+ FieldError,
486
+ FieldLabel,
487
+ } from "@ui-sdk/react/field"
488
+ import { Radio, RadioGroup, RadioIndicator } from "@ui-sdk/react/radio"
489
+ import { useLog } from "./log"
490
+
491
+ const channels = [
492
+ { value: "email", label: "Email only" },
493
+ { value: "sms", label: "SMS only" },
494
+ { value: "both", label: "Email and SMS" },
495
+ ]
496
+
497
+ function Example() {
498
+ const log = useLog()
499
+
500
+ return (
501
+ <form
502
+ data-slot="stack"
503
+ onSubmit={(event) => {
504
+ event.preventDefault()
505
+ log(`Saved updates=${new FormData(event.currentTarget).get("updates")}`)
506
+ }}
507
+ >
508
+ <Field>
509
+ <FieldLabel>Order updates</FieldLabel>
510
+ <FieldDescription>
511
+ Choose how we tell you about your orders.
512
+ </FieldDescription>
513
+ <RadioGroup name="updates" required>
514
+ {channels.map((channel) => (
515
+ <label data-slot="label" key={channel.value}>
516
+ <Radio value={channel.value}>
517
+ <RadioIndicator />
518
+ </Radio>
519
+ {channel.label}
520
+ </label>
521
+ ))}
522
+ </RadioGroup>
523
+ <FieldError>Choose how to get order updates.</FieldError>
524
+ </Field>
525
+ <div data-slot="row">
526
+ <button type="submit" data-variant="solid">
527
+ Save
528
+ </button>
529
+ </div>
530
+ </form>
531
+ )
532
+ }
533
+
534
+ export { Example }
535
+ ```
536
+
537
+ Press Save without a choice to see the error. Choose SMS only and save again; the console logs the saved value.
538
+
539
+ ## Animation
540
+
541
+ The indicator has `data-starting-style` as it appears and `data-ending-style` while it leaves, and stays mounted until its exit finishes. Transition from those states:
542
+
543
+ ```css
544
+ [data-slot="radio-indicator"] {
545
+ transition:
546
+ opacity 0.15s,
547
+ scale 0.15s;
548
+
549
+ &:is([data-starting-style], [data-ending-style]) {
550
+ opacity: 0;
551
+ scale: 0.5;
552
+ }
553
+ }
554
+ ```
555
+
556
+ `keepMounted` keeps the indicator in the page while unchecked, with `data-unchecked`. Here it lets a hovered option preview its dot.
557
+
558
+ ```tsx title="indicator.tsx"
559
+ import { Field, FieldLabel } from "@ui-sdk/react/field"
560
+ import { Radio, RadioGroup, RadioIndicator } from "@ui-sdk/react/radio"
561
+
562
+ const themes = [
563
+ { value: "light", label: "Light" },
564
+ { value: "dark", label: "Dark" },
565
+ { value: "system", label: "System" },
566
+ ]
567
+
568
+ function Example() {
569
+ return (
570
+ <Field>
571
+ <FieldLabel>Theme</FieldLabel>
572
+ <RadioGroup defaultValue="system">
573
+ {themes.map((theme) => (
574
+ <label data-slot="label" key={theme.value}>
575
+ <Radio value={theme.value}>
576
+ <RadioIndicator keepMounted />
577
+ </Radio>
578
+ {theme.label}
579
+ </label>
580
+ ))}
581
+ </RadioGroup>
582
+ </Field>
583
+ )
584
+ }
585
+
586
+ export { Example }
587
+ ```
588
+
589
+ Hover Light to preview its dot, then choose it. The dot scales in on Light and out of System.
590
+
591
+ ## Loop
592
+
593
+ Arrow keys wrap from the last radio to the first. Set `loop` to `false` to stop at the ends.
594
+
595
+ ```tsx
596
+ <RadioGroup defaultValue="low" loop={false}>
597
+ ...
598
+ </RadioGroup>
599
+ ```
600
+
601
+ ## Custom Rendering
602
+
603
+ The group, radio and indicator 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 interactive element so its role, label and keyboard behavior stay connected. See [Parts and Composition](../parts-and-composition.md) for the rendering contract.
604
+
605
+ ```tsx title="render.tsx"
606
+ "use client"
607
+
608
+ import { Field, FieldLabel } from "@ui-sdk/react/field"
609
+ import { Radio, RadioGroup } from "@ui-sdk/react/radio"
610
+ import { CheckIcon } from "lucide-react"
611
+ import * as React from "react"
612
+
613
+ const options = [
614
+ {
615
+ value: "all",
616
+ label: "Everything",
617
+ description: "Every message and update.",
618
+ },
619
+ {
620
+ value: "mentions",
621
+ label: "Mentions",
622
+ description: "Only when someone mentions you.",
623
+ },
624
+ { value: "none", label: "Nothing", description: "Turn notifications off." },
625
+ ]
626
+
627
+ function Example() {
628
+ const id = React.useId()
629
+ return (
630
+ <Field>
631
+ <FieldLabel>Notify me about</FieldLabel>
632
+ <RadioGroup defaultValue="mentions">
633
+ {options.map((option) => (
634
+ <Radio
635
+ key={option.value}
636
+ value={option.value}
637
+ aria-labelledby={`${id}-${option.value}-label`}
638
+ aria-describedby={`${id}-${option.value}-description`}
639
+ render={(props, state) => (
640
+ <button {...props}>
641
+ <span data-slot="stack">
642
+ <span id={`${id}-${option.value}-label`}>{option.label}</span>
643
+ <span data-slot="hint" id={`${id}-${option.value}-description`}>
644
+ {option.description}
645
+ </span>
646
+ </span>
647
+ <CheckIcon aria-hidden="true" data-checked={state.checked ? "" : undefined} />
648
+ </button>
649
+ )}
650
+ />
651
+ ))}
652
+ </RadioGroup>
653
+ </Field>
654
+ )
655
+ }
656
+
657
+ export { Example }
658
+ ```
659
+
660
+ Choose Nothing. Each radio renders a full-width button that shows a check from its state.
661
+
662
+ ## Styling
663
+
664
+ Use `data-checked` and `data-unchecked` for the checked state, and `data-disabled`, `data-readonly` and `data-required` for the rest. The indicator gets the same state attributes as its radio, and the group gets `data-disabled`, `data-readonly` and `data-required`.
665
+
666
+ Each part has a `data-slot`, such as `radio`, and its state attributes are listed under [Data Attributes](#data-attributes).
667
+
668
+ ## Data Attributes
669
+
670
+ ### RadioGroup
671
+ `[data-slot="radio-group"]`
672
+
673
+ - `data-readonly`: Present when the group is read-only.
674
+ - `data-required`: Present when the group is required.
675
+ - `data-disabled`: Present when disabled, by its own prop, the group or a `<fieldset>`.
676
+
677
+ ### Radio
678
+ `[data-slot="radio"]`
679
+
680
+ - `data-checked`: Present on the checked radio.
681
+ - `data-readonly`: Present when the group is read-only.
682
+ - `data-required`: Present when the group is required.
683
+ - `data-unchecked`: Present on radios that aren't checked.
684
+ - `data-disabled`: Present when disabled, by its own prop, the group or a `<fieldset>`.
685
+
686
+ ### RadioIndicator
687
+ `[data-slot="radio-indicator"]`
688
+
689
+ - `data-checked`: Present on the checked radio.
690
+ - `data-readonly`: Present when the group is read-only.
691
+ - `data-required`: Present when the group is required.
692
+ - `data-unchecked`: Present on radios that aren't checked.
693
+ - `data-disabled`: Present when disabled, by its own prop, the group or a `<fieldset>`.
694
+ - `data-starting-style`: Present for the indicator's first frame. Style the state you animate from.
695
+ - `data-ending-style`: Present while the indicator animates out. Style the state you animate to.
696
+
697
+ ## API Reference
698
+
699
+ Props and callbacks are the same as below, as React props.
700
+
701
+ ### RadioGroup
702
+
703
+ - `value?` (`T | null`): The checked radio's value. Pass it to control the group. `null` checks nothing.
704
+ - `defaultValue?` (`T | null`): The radio checked at first, when uncontrolled. A form reset returns to it.
705
+ - `onValueChange?` (`((value: T | null, details: ChangeDetails<RadioGroupValueChangeReason>) => void)`): Runs when a user or a form reset wants to change the value. `details.reason` says why, and `details.cancel()` stops it.
706
+ - `disabled?` (`boolean`): Disables every radio like native ones: they can't be focused or changed, and the value isn't submitted.
707
+ - `readOnly?` (`boolean`): Keeps the items focusable and the value submitted, but stops changes.
708
+ - `required?` (`boolean`): Blocks form submission until an item is checked, with the browser's own message.
709
+ - `name?` (`string`): The name the value submits under. Without one, the group submits nothing.
710
+ - `form?` (`string`): The id of a form to belong to, when the group isn't inside that form.
711
+ - `loop?` (`boolean`): Whether arrow keys wrap from the last item to the first.
712
+ - `controller?` (`{ getValue: () => T | null; setValue: (value: T | null, details?: Details) => boolean; focus: () => void; }`): Checks radios from anywhere. Create one with `createRadioGroupController()`.
713
+ - `debug?` (`boolean`): Logs every state change with its reason to the console, in development builds.
714
+
715
+ ### Radio
716
+
717
+ - `value` (`string`): What the group's value becomes when this item is checked.
718
+ - `disabled?` (`boolean`): Skips the item, like a disabled native radio: arrow keys pass it and it can't be focused or checked.
719
+
720
+ ### RadioIndicator
721
+
722
+ - `keepMounted?` (`boolean`): Whether the indicator stays in the page while its item is unchecked.
723
+
724
+ ### State
725
+
726
+ What render functions, state-based class names and slots receive.
727
+
728
+ - `value` (`string | null`): The checked item's value.
729
+ - `disabled` (`boolean`): Whether the group is disabled, by itself or by a `<fieldset>`.
730
+ - `readOnly` (`boolean`): Whether the group is read-only: arrow keys still move focus, but nothing changes the checked item.
731
+ - `required` (`boolean`): Whether a form won't submit until an item is checked.
732
+
733
+ - `checked` (`boolean`): Whether this item is checked.
734
+ - `disabled` (`boolean`): Whether this item is disabled, by itself, its group or a `<fieldset>`.
735
+ - `readOnly` (`boolean`): Whether the group is read-only.
736
+ - `required` (`boolean`): Whether the group is required.
737
+
738
+ - `checked` (`boolean`): Whether this item is checked.
739
+ - `disabled` (`boolean`): Whether this item is disabled, by itself, its group or a `<fieldset>`.
740
+ - `readOnly` (`boolean`): Whether the group is read-only.
741
+ - `required` (`boolean`): Whether the group is required.
742
+
743
+ ### Reasons
744
+
745
+ `details.reason` says why the checked value changed.
746
+
747
+ - `item-press` (`MouseEvent | KeyboardEvent`): A member of a collection was chosen: a menu item, a tab, a radio or a checkbox item.
748
+ - `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.
749
+ - `reset` (`none`): A form reset.
750
+ - `restore` (`none`): The browser restored state, like after going back, or a saved value was read back from storage.
751
+ - `imperative` (`none`): A controller or `api` call, or a property set on a web component.
752
+
753
+ ### Controller
754
+
755
+ Create one with `createRadioGroupController()` and pass it as `controller`.
756
+
757
+ Every write method takes an optional `{ reason, event }`, runs the change callback like a press, and returns `false` when the change is canceled.
758
+
759
+ - `getValue()` (`() => T | null`): The checked radio's value.
760
+ - `setValue(value, details?)` (`(value: T | null, details?: RadioGroupChangeOptions | undefined) => boolean`): Checks a radio, as a user would. Disabled radios and read-only groups ignore it.
761
+ - `focus()` (`() => void`): Moves focus to the checked radio, or the first one.
762
+
763
+ ### Events
764
+
765
+ Callbacks get the new value first, then the details.
766
+
767
+ | Prop | On | Receives |
768
+ | --------------- | ------------ | ----------------------------------------------------------------- |
769
+ | `onValueChange` | `RadioGroup` | `(value, details)`: the checked value, and the [reason](#reasons) |
770
+
771
+ ## Accessibility
772
+
773
+ Radio groups follow the [WAI-ARIA radio group pattern](https://www.w3.org/WAI/ARIA/apg/patterns/radio/), and behave like native radios that share a name. `Radio` renders a button with the `radio` role, and a hidden native radio that takes part in forms.
774
+
775
+ ### Keyboard
776
+
777
+ | Key | Does |
778
+ | ---------- | ----------------------------------------------------------------------- |
779
+ | Tab | Moves into the group, onto the checked radio or the first enabled one |
780
+ | Arrow keys | Move to the next or previous radio and check it, skipping disabled ones |
781
+ | Space | Checks the focused radio |
782
+ | Enter | Submits its form |
783
+
784
+ ### Tested Behavior
785
+
786
+ Every line below is an automated test.
787
+
788
+ **Selection**
789
+
790
+ - Nothing is checked at first without a default value.
791
+ - Clicking an item checks it and unchecks the others.
792
+ - Clicking an item's label checks it.
793
+ - Clicking a disabled item does nothing.
794
+ - A change can be cancelled.
795
+
796
+ **Keyboard**
797
+
798
+ - Tab lands on the first item when nothing is checked.
799
+ - Tab lands on the checked item.
800
+ - Tab leaves the group after one stop.
801
+ - Space checks the focused item.
802
+ - ArrowDown and ArrowRight move to the next item and check it.
803
+ - ArrowUp and ArrowLeft move to the previous item and check it.
804
+ - Arrow keys skip disabled items.
805
+ - Arrow keys wrap at both ends.
806
+ - Enter never checks an item.
807
+ - Keys pressed in quick succession are never lost.
808
+ - Enter on an item submits the form.
809
+
810
+ **Read-only and disabled**
811
+
812
+ - A read-only group moves focus with arrow keys but checks nothing.
813
+ - A disabled group can't be focused or changed.
814
+ - Clicking a radio in a disabled group doesn't focus it.
815
+ - Clicking the label of a radio in a disabled group neither focuses nor checks it.
816
+
817
+ **Animation**
818
+
819
+ - The indicator animates in from its starting style.
820
+
821
+ Runs in React, Vue and web components.
822
+
823
+ In a field:
824
+
825
+ **With every radio enabled**
826
+
827
+ - Each radio is named by the label of its own field.
828
+ - The field around the group still names the group.
829
+ - Clicking a radio's label checks it and unchecks the others.
830
+ - The group keeps one tab stop.
831
+ - Arrow keys move between the radios and check them.
832
+
833
+ **A disabled radio**
834
+
835
+ - A disabled field around the first radio moves the tab stop to the next.
836
+ - Disabling the first radio after it renders moves the tab stop to the next.
837
+ - Enabling the first radio again gives it back the tab stop.
838
+
839
+ Runs in React, Vue and web components.
840
+
841
+ In right-to-left pages:
842
+
843
+ - ArrowLeft moves to the next radio and ArrowRight to the previous one.
844
+ - ArrowDown and ArrowUp keep their direction.
845
+
846
+ Runs in React, Vue and web components.