@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,549 @@
1
+ # Field
2
+
3
+ Connect a form control to its label, description, and validation errors.
4
+
5
+ A field connects a form control to its label, its description and the errors it shows, and wires up `id`, `for` and `aria-describedby` for you.
6
+
7
+ ```tsx title="input.tsx"
8
+ import { Field, FieldDescription, FieldLabel } from "@ui-sdk/react/field"
9
+ import { Input } from "@ui-sdk/react/input"
10
+
11
+ function Example() {
12
+ return (
13
+ <div data-slot="stack">
14
+ <Field>
15
+ <FieldLabel>Username</FieldLabel>
16
+ <Input name="username" placeholder="hollis" autoComplete="username" />
17
+ <FieldDescription>
18
+ Choose a unique username for your account.
19
+ </FieldDescription>
20
+ </Field>
21
+ </div>
22
+ )
23
+ }
24
+
25
+ export { Example }
26
+ ```
27
+
28
+ Press Username to focus the input, then type a name.
29
+
30
+ ## Installation
31
+
32
+ ```bash
33
+ npm install @ui-sdk/react @ui-sdk/core
34
+ ```
35
+
36
+ ## Composition
37
+
38
+ Use the following composition to build a `Field`:
39
+
40
+ ```text
41
+ Field
42
+ ├── FieldLabel
43
+ ├── Input
44
+ ├── FieldDescription
45
+ └── FieldError
46
+ ```
47
+
48
+ ## Usage
49
+
50
+ ```tsx
51
+ import {
52
+ Field,
53
+ FieldDescription,
54
+ FieldError,
55
+ FieldLabel,
56
+ } from "@ui-sdk/react/field"
57
+ import { Input } from "@ui-sdk/react/input"
58
+
59
+ function UsernameField() {
60
+ return (
61
+ <Field name="username">
62
+ <FieldLabel>Username</FieldLabel>
63
+ <Input autoComplete="username" required />
64
+ <FieldDescription>This is your public name.</FieldDescription>
65
+ <FieldError />
66
+ </Field>
67
+ )
68
+ }
69
+
70
+ export { UsernameField }
71
+ ```
72
+
73
+ The control is one of ours, like an [Input](./input.md), a [Textarea](./textarea.md), a [Select](./select.md) or a [Checkbox](./checkbox.md), and it wires itself to the field.
74
+
75
+ The label names the control, and the description and any error that shows describe it.
76
+
77
+ Group related fields in a [field set](#field-sets):
78
+
79
+ ```tsx
80
+ <FieldSet>
81
+ <FieldLegend />
82
+ <FieldDescription />
83
+ <FieldGroup>
84
+ <Field />
85
+ <Field />
86
+ </FieldGroup>
87
+ </FieldSet>
88
+ ```
89
+
90
+ ## Controls
91
+
92
+ Use an [Input](./input.md) for a line of text and a [Textarea](./textarea.md) for more:
93
+
94
+ ```tsx
95
+ <Field>
96
+ <FieldLabel>Feedback</FieldLabel>
97
+ <Textarea name="feedback" />
98
+ </Field>
99
+ ```
100
+
101
+ For a control we don't ship, like a native `<select>`, use `FieldControl`. It renders an `<input>`, or another element with `render`, and connects it to the field the same way. An [agent](../agents.md) sees only our components, so it can't fill a `FieldControl`: use ours where there is one.
102
+
103
+ ```tsx
104
+ <Field>
105
+ <FieldLabel>Department</FieldLabel>
106
+ <FieldControl render={<select />} name="department">
107
+ <option value="design">Design</option>
108
+ <option value="engineering">Engineering</option>
109
+ </FieldControl>
110
+ </Field>
111
+ ```
112
+
113
+ A [Checkbox](./checkbox.md), [Switch](./switch.md) or [Radio Group](./radio.md) inside a field uses the field's label and description. Pressing the label toggles a checkbox or switch. A radio group takes the field's label as its name, and each radio keeps a label of its own:
114
+
115
+ ```tsx
116
+ <Field>
117
+ <Switch name="mfa" />
118
+ <FieldLabel>Two-factor authentication</FieldLabel>
119
+ <FieldDescription>Ask for a code when you sign in.</FieldDescription>
120
+ </Field>
121
+
122
+ <Field>
123
+ <FieldLabel>Plan</FieldLabel>
124
+ <RadioGroup name="plan" defaultValue="monthly">
125
+ <label>
126
+ <Radio value="monthly" />
127
+ Monthly
128
+ </label>
129
+ <label>
130
+ <Radio value="yearly" />
131
+ Yearly
132
+ </label>
133
+ </RadioGroup>
134
+ </Field>
135
+ ```
136
+
137
+ ## Validation
138
+
139
+ Use native constraints such as `required`, `type="email"`, and `pattern`. Errors appear after a submit attempt and update as the value changes. The first invalid control takes focus.
140
+
141
+ Leave an error empty to show the browser's message, or use `match="valueMissing"` and `match="typeMismatch"` for your own messages. Adding an error part replaces the browser's validation bubble with that message in the page.
142
+
143
+ ```tsx title="validation.tsx"
144
+ import {
145
+ Field,
146
+ FieldDescription,
147
+ FieldError,
148
+ FieldLabel,
149
+ } from "@ui-sdk/react/field"
150
+ import { Input } from "@ui-sdk/react/input"
151
+ import { useLog } from "./log"
152
+
153
+ function Example() {
154
+ const log = useLog()
155
+
156
+ return (
157
+ <form
158
+ data-slot="stack"
159
+ onSubmit={(event) => {
160
+ event.preventDefault()
161
+ log(`Subscribed ${new FormData(event.currentTarget).get("email")}`)
162
+ }}
163
+ >
164
+ <Field name="email">
165
+ <FieldLabel>Email</FieldLabel>
166
+ <Input type="email" required placeholder="hollis@example.com" />
167
+ <FieldDescription
168
+ render={(props, state) => (
169
+ <p {...props}>
170
+ {state.valid
171
+ ? "Looks good."
172
+ : "We'll send you a confirmation link."}
173
+ </p>
174
+ )}
175
+ />
176
+ <FieldError match="valueMissing">Enter your email address.</FieldError>
177
+ <FieldError match="typeMismatch">
178
+ Enter an email address like hollis@example.com.
179
+ </FieldError>
180
+ </Field>
181
+ <Field name="website">
182
+ <FieldLabel>Website</FieldLabel>
183
+ <Input type="url" required placeholder="https://example.com" />
184
+ <FieldError />
185
+ </Field>
186
+ <button type="submit" data-variant="solid">
187
+ Subscribe
188
+ </button>
189
+ </form>
190
+ )
191
+ }
192
+
193
+ export { Example }
194
+ ```
195
+
196
+ Press Subscribe with an empty or invalid email, then correct it to clear the error.
197
+
198
+ ## Errors from Your Code
199
+
200
+ Pass `invalid` when your server or form library supplies an error. `true` shows it immediately. Clear the value to return to the browser's validation.
201
+
202
+ Give this error its own text and set `match` to `true`. Named matches such as `valueMissing` still refer to native validation. `invalid={false}` hides the field's invalid state, but does not make an invalid native control pass validation.
203
+
204
+ ```tsx
205
+ <Field name="email" invalid={serverError ? true : undefined}>
206
+ <FieldLabel>Email</FieldLabel>
207
+ <Input type="email" required onValueChange={() => setServerError(null)} />
208
+ <FieldError match="valueMissing">Enter your email address.</FieldError>
209
+ {serverError && <FieldError match>{serverError}</FieldError>}
210
+ </Field>
211
+ ```
212
+
213
+ ## Required and Disabled
214
+
215
+ A required control marks every part with `data-required`, and the parts' state has `required`, so a label can show a mark with CSS alone.
216
+
217
+ A disabled field disables its control and marks every part with `data-disabled`, the way a [disabled field set](#disabled-field-sets) does for every field inside it.
218
+
219
+ ## Character Count
220
+
221
+ Keep the text in state and show its length in the description. Set `maxLength` on the control to enforce the limit.
222
+
223
+ ```tsx
224
+ <Field>
225
+ <FieldLabel>Bio</FieldLabel>
226
+ <Textarea
227
+ maxLength={280}
228
+ value={bio}
229
+ onValueChange={(value) => setBio(value)}
230
+ />
231
+ <FieldDescription>{bio.length} / 280</FieldDescription>
232
+ </Field>
233
+ ```
234
+
235
+ ## Forms
236
+
237
+ The control submits its value under its own `name`, or the field's name if it has none. Reset restores the control's default and hides validation errors. Disabled controls are left out of the submitted values. See [Forms](../forms.md) for the shared form behavior.
238
+
239
+ ## Field Sets
240
+
241
+ A field set groups related fields under a legend, and disables them together.
242
+
243
+ ```tsx title="field-set.tsx"
244
+ import {
245
+ Field,
246
+ FieldDescription,
247
+ FieldGroup,
248
+ FieldLabel,
249
+ FieldLegend,
250
+ FieldSet,
251
+ } from "@ui-sdk/react/field"
252
+ import { Input } from "@ui-sdk/react/input"
253
+
254
+ function Example() {
255
+ return (
256
+ <FieldSet>
257
+ <FieldLegend>Address</FieldLegend>
258
+ <FieldDescription>
259
+ We need your address to deliver your order.
260
+ </FieldDescription>
261
+ <FieldGroup>
262
+ <Field name="street">
263
+ <FieldLabel>Street address</FieldLabel>
264
+ <Input autoComplete="street-address" />
265
+ </Field>
266
+ <div data-slot="grid">
267
+ <Field name="city">
268
+ <FieldLabel>City</FieldLabel>
269
+ <Input autoComplete="address-level2" />
270
+ </Field>
271
+ <Field name="postal-code">
272
+ <FieldLabel>Postal code</FieldLabel>
273
+ <Input autoComplete="postal-code" />
274
+ </Field>
275
+ </div>
276
+ </FieldGroup>
277
+ </FieldSet>
278
+ )
279
+ }
280
+
281
+ export { Example }
282
+ ```
283
+
284
+ Move through the address fields. Their legend and description name the group.
285
+
286
+ `FieldSet` is a native `<fieldset>` and `FieldLegend` a native `<legend>`, so the set is named, and disabled, before scripts run. Keep the legend the first child. A legend rendered as another element, like `render={<div />}`, still names the set through `aria-labelledby`.
287
+
288
+ A `FieldDescription` right inside the set, outside any `Field`, describes the set, so screen readers read it as focus enters. `FieldGroup` is a `<div>` that stacks the fields, with no role and no state: the field set is what names and disables a group.
289
+
290
+ ## Disabled Field Sets
291
+
292
+ Disabling the set disables its controls and nested field sets. The form leaves their values out of submissions.
293
+
294
+ ```tsx
295
+ <FieldSet disabled={sameAsShipping}>
296
+ <FieldLegend>Billing address</FieldLegend>
297
+ <Field name="billing-street">
298
+ <FieldLabel>Street address</FieldLabel>
299
+ <Input autoComplete="billing street-address" />
300
+ </Field>
301
+ </FieldSet>
302
+ ```
303
+
304
+ ## Groups in a Field Set
305
+
306
+ The legend names a [checkbox group](./checkbox.md#group) or a [radio group](./radio.md) inside the set, so the group needs no label of its own.
307
+
308
+ ```tsx
309
+ <FieldSet>
310
+ <FieldLegend>Notifications</FieldLegend>
311
+ <CheckboxGroup name="notify" defaultValue={["mentions"]}>
312
+ <label>
313
+ <Checkbox value="mentions" />
314
+ Mentions
315
+ </label>
316
+ <label>
317
+ <Checkbox value="follows" />
318
+ New followers
319
+ </label>
320
+ </CheckboxGroup>
321
+ </FieldSet>
322
+ ```
323
+
324
+ ## Custom Rendering
325
+
326
+ Every part takes `render`, to render a different element or component in its place, like `render={<select />}` on `FieldControl`. As a function, it receives the props to spread and the field's state:
327
+
328
+ ```tsx
329
+ <FieldDescription
330
+ render={(props, state) => (
331
+ <p {...props}>
332
+ {state.invalid ? "Use letters and numbers only." : "Letters and numbers."}
333
+ </p>
334
+ )}
335
+ />
336
+ ```
337
+
338
+ `className` and `style` can be functions of the state too. `FieldError`'s state also has the `message` for the current problem.
339
+
340
+ State is read lazily. A part re-renders for a change only if its render function or slot read that value. Each part's state is listed under [State](#state).
341
+
342
+ ## Custom Controls
343
+
344
+ `useFieldControl` lets a control of your own join the field around it, the way ours do. Outside a field it returns `null`. Inside one, it returns:
345
+
346
+ - `name`: the field's `name`, for a control that doesn't set its own.
347
+ - `disabled`: whether the field is disabled.
348
+ - `props(props)`: the control's props with the field's merged in: the `id`, `aria-labelledby`, `aria-describedby`, `aria-invalid`, `disabled` and the field's data attributes, and a ref that tells the field which element is its control. The control keeps its own `data-slot`.
349
+
350
+ The control re-renders when the field changes, like when its label or errors appear.
351
+
352
+ ```tsx
353
+ import * as React from "react"
354
+ import { useFieldControl } from "@ui-sdk/react/field"
355
+
356
+ export function PhoneInput(props: React.ComponentProps<"input">) {
357
+ const field = useFieldControl()
358
+ const input = {
359
+ type: "tel",
360
+ autoComplete: "tel",
361
+ name: field?.name,
362
+ ...props,
363
+ "data-slot": "phone-input",
364
+ }
365
+ return <input {...(field ? field.props(input) : input)} />
366
+ }
367
+ ```
368
+
369
+ ```tsx
370
+ <Field>
371
+ <FieldLabel>Phone</FieldLabel>
372
+ <PhoneInput required />
373
+ <FieldError />
374
+ </Field>
375
+ ```
376
+
377
+ The field reads validity from the native inputs, selects and textareas inside it, so `required` and `pattern` work on the input above. A control that isn't a native input, like a row of buttons, takes part through a native input of its own: ours render one, visually hidden, beside the control.
378
+
379
+ To build a control from scratch on the primitives ours use, see [Primitives](../core.md#define-a-primitive).
380
+
381
+ ## Styling
382
+
383
+ Use `data-invalid` and `data-valid` for validation, and `data-focused`, `data-required` and `data-disabled` for the rest. Every part of a field gets the same attributes, so a label can turn red with its control.
384
+
385
+ Each part has a `data-slot`, such as `field-label`, and its state attributes are listed under [Data Attributes](#data-attributes).
386
+
387
+ ## Data Attributes
388
+
389
+ ### Field
390
+ `[data-slot="field"]`
391
+
392
+ - `data-disabled`: Present when the field is disabled.
393
+ - `data-invalid`: Present while the field shows as invalid: with `invalid`, or once a submit finds the control invalid.
394
+
395
+ ### FieldLabel
396
+ `[data-slot="field-label"]`
397
+
398
+ - `data-disabled`: Present when the field is disabled.
399
+ - `data-invalid`: Present while the field shows as invalid: with `invalid`, or once a submit finds the control invalid.
400
+
401
+ ### FieldControl
402
+ `[data-slot="field-control"]`
403
+
404
+ - `data-disabled`: Present when the field is disabled.
405
+ - `data-invalid`: Present while the field shows as invalid: with `invalid`, or once a submit finds the control invalid.
406
+
407
+ ### FieldDescription
408
+ `[data-slot="field-description"]`
409
+
410
+ - `data-disabled`: Present when the field is disabled.
411
+ - `data-invalid`: Present while the field shows as invalid: with `invalid`, or once a submit finds the control invalid.
412
+
413
+ ### FieldError
414
+ `[data-slot="field-error"]`
415
+
416
+ - `data-disabled`: Present when the field is disabled.
417
+ - `data-invalid`: Present while the field shows as invalid: with `invalid`, or once a submit finds the control invalid.
418
+
419
+ ### FieldSet
420
+ `FieldGroup` has `data-slot="field-group"` and no state attributes.
421
+
422
+ `[data-slot="field-set"]`
423
+
424
+ - `data-disabled`: Present when the field set is disabled, by its own prop or a `<fieldset>` around it.
425
+
426
+ ### FieldLegend
427
+ `[data-slot="field-legend"]`
428
+
429
+ - `data-disabled`: Present when the field set is disabled, by its own prop or a `<fieldset>` around it.
430
+
431
+ ### FieldDescription in a FieldSet
432
+ `[data-slot="field-description"]`
433
+
434
+ - `data-disabled`: Present when the field set is disabled, by its own prop or a `<fieldset>` around it.
435
+
436
+ ## API Reference
437
+
438
+ Props are the same as below, as React props.
439
+
440
+ A field has no change events. Read its state from the parts' data attributes, or from render functions. Set `debug` to log every state change and its reason.
441
+
442
+ ### Field
443
+
444
+ - `name?` (`string`): The name the control submits under, when the control doesn't set its own.
445
+ - `disabled?` (`boolean`): Disables the control, and marks every part.
446
+ - `invalid?` (`boolean`): Whether the field shows as invalid, when your code decides, like a form library or an error from the server. `true` shows its errors now, `false` shows none whatever the browser thinks. Leave it out to show the browser's own validation once a submit finds the field invalid.
447
+ - `debug?` (`boolean`): Logs every state change with its reason to the console, in development builds.
448
+
449
+ ### FieldError
450
+
451
+ - `match?` (`boolean | ValidityKey`): When this error shows. `true` shows it always and `false` never. A validity flag, like `"valueMissing"`, shows it only for that problem of the browser's own validation. Without it, any problem shows it.
452
+
453
+ ### FieldSet
454
+
455
+ - `disabled?` (`boolean`): Disables every control inside it, except those in its legend, and leaves them out of the form.
456
+ - `debug?` (`boolean`): Logs every state change with its reason to the console, in development builds.
457
+
458
+ ### State
459
+
460
+ - `disabled` (`boolean`): Whether the field is disabled, by itself or by a `<fieldset>`.
461
+ - `required` (`boolean`): Whether the control is required, read from its `required` or `aria-required`.
462
+ - `focused` (`boolean`): Whether focus is inside the control.
463
+ - `invalid` (`boolean`): Whether the field shows as invalid: `invalid` when you pass it, else the browser's validation once shown.
464
+ - `valid` (`boolean`): Whether the browser's validation passes, once shown. Always false while you pass `invalid`.
465
+
466
+ - `disabled` (`boolean`): Whether the field is disabled, by itself or by a `<fieldset>`.
467
+ - `required` (`boolean`): Whether the control is required, read from its `required` or `aria-required`.
468
+ - `focused` (`boolean`): Whether focus is inside the control.
469
+ - `invalid` (`boolean`): Whether the field shows as invalid: `invalid` when you pass it, else the browser's validation once shown.
470
+ - `valid` (`boolean`): Whether the browser's validation passes, once shown. Always false while you pass `invalid`.
471
+ - `message` (`string`): The browser's message for the current problem. Children you pass show instead.
472
+
473
+ A `FieldDescription` receives the field's state, or only `disabled` when it describes a field set.
474
+
475
+ - `disabled` (`boolean`): Whether the field set is disabled, by itself or by a field set around it. Whether the field is disabled, by itself or by a `<fieldset>`.
476
+ - `required` (`boolean | undefined`): Whether the control is required, read from its `required` or `aria-required`.
477
+ - `focused` (`boolean | undefined`): Whether focus is inside the control.
478
+ - `invalid` (`boolean | undefined`): Whether the field shows as invalid: `invalid` when you pass it, else the browser's validation once shown.
479
+ - `valid` (`boolean | undefined`): Whether the browser's validation passes, once shown. Always false while you pass `invalid`.
480
+
481
+ Every part of a field set receives the same state.
482
+
483
+ - `disabled` (`boolean`): Whether the field set is disabled, by itself or by a field set around it.
484
+
485
+ ## Accessibility
486
+
487
+ The label names the control with `aria-labelledby`, beside a native `<label for>` where the control can take one. The description and any error that shows describe it with `aria-describedby`, and an invalid control has `aria-invalid`. The tests below cover these connections and keyboard behavior.
488
+
489
+ **Labelling**
490
+
491
+ - The label names the control.
492
+ - The description describes the control.
493
+ - Clicking the label of a text field focuses it.
494
+
495
+ **A label wrapping a field**
496
+
497
+ - Names the control inside it.
498
+ - Checks its control when clicked anywhere.
499
+ - Keeps the field's description on its control.
500
+ - Names a control that renders after it.
501
+ - Keeps the part's data-slot.
502
+ - Warns in development only when a label outside a field labels nothing.
503
+ - A description and an error outside a field keep their data-slot. Web components only.
504
+ - A hidden error outside a field stays hidden whatever display its class gives. Web components only.
505
+ - An error outside a field follows its match. Web components only.
506
+ - An error outside a field shows a child that arrives after it. Web components only.
507
+
508
+ **Errors**
509
+
510
+ - Stay hidden until the form tries to submit.
511
+ - Show the browser's message when they have no text of their own.
512
+ - Count another library's form-associated element inside the field.
513
+ - Describe the control while they show.
514
+ - Update as the value changes once shown.
515
+ - Update as state set from script changes the value once shown. Web components only.
516
+ - A field marked invalid shows its error right away.
517
+ - A field marked valid never shows the browser's errors.
518
+ - A form reset hides them again.
519
+ - Errors in the page replace the browser's message, and the first invalid control takes focus.
520
+
521
+ Runs in React, Vue and web components.
522
+
523
+ In a field set:
524
+
525
+ - The legend names the group.
526
+ - A disabled field set disables the controls inside and leaves them out of the form.
527
+ - Enabling it again restores the controls.
528
+ - A field set disabled from the start disables its required fields.
529
+ - Fields in a disabled field set mark their parts disabled.
530
+
531
+ **Enhancements**
532
+
533
+ - The description describes the group.
534
+
535
+ Runs in React, Vue and web components.
536
+
537
+ In a form:
538
+
539
+ - The form submits every checked control's name and value.
540
+ - Unchecked controls submit nothing.
541
+ - A checkbox outside the form joins it with the form attribute.
542
+ - Required controls block submission.
543
+ - A form reset returns every control to its default.
544
+ - A reset returns to the default value, not to nothing.
545
+ - A disabled fieldset disables the controls inside it.
546
+ - A read-only control still submits.
547
+ - A user's change fires native input and change events on the form.
548
+
549
+ Runs in React, Vue and web components.