@funstack/ui 0.1.0-alpha.20 → 0.1.0-alpha.21

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 (105) hide show
  1. package/dist/alert-dialog/alert-dialog-root.mjs +2 -1
  2. package/dist/alert-dialog/alert-dialog-root.mjs.map +1 -1
  3. package/dist/avatar/avatar-context.mjs +24 -0
  4. package/dist/avatar/avatar-context.mjs.map +1 -0
  5. package/dist/avatar/avatar-fallback.d.mts +32 -0
  6. package/dist/avatar/avatar-fallback.d.mts.map +1 -0
  7. package/dist/avatar/avatar-fallback.mjs +31 -0
  8. package/dist/avatar/avatar-fallback.mjs.map +1 -0
  9. package/dist/avatar/avatar-image.d.mts +37 -0
  10. package/dist/avatar/avatar-image.d.mts.map +1 -0
  11. package/dist/avatar/avatar-image.mjs +91 -0
  12. package/dist/avatar/avatar-image.mjs.map +1 -0
  13. package/dist/avatar/avatar-root.d.mts +48 -0
  14. package/dist/avatar/avatar-root.d.mts.map +1 -0
  15. package/dist/avatar/avatar-root.mjs +81 -0
  16. package/dist/avatar/avatar-root.mjs.map +1 -0
  17. package/dist/avatar/index.d.mts +24 -0
  18. package/dist/avatar/index.d.mts.map +1 -0
  19. package/dist/avatar/index.mjs +24 -0
  20. package/dist/avatar/index.mjs.map +1 -0
  21. package/dist/dialog/dialog-close.d.mts.map +1 -1
  22. package/dist/dialog/dialog-close.mjs +14 -3
  23. package/dist/dialog/dialog-close.mjs.map +1 -1
  24. package/dist/dialog/dialog-context.mjs +7 -1
  25. package/dist/dialog/dialog-context.mjs.map +1 -1
  26. package/dist/dialog/dialog-popup.d.mts +1 -0
  27. package/dist/dialog/dialog-popup.d.mts.map +1 -1
  28. package/dist/dialog/dialog-popup.mjs +20 -2
  29. package/dist/dialog/dialog-popup.mjs.map +1 -1
  30. package/dist/dialog/dialog-root.mjs +6 -4
  31. package/dist/dialog/dialog-root.mjs.map +1 -1
  32. package/dist/docs/components/avatar.md +354 -0
  33. package/dist/docs/components/drawer.md +622 -0
  34. package/dist/docs/components/otp-field.md +501 -0
  35. package/dist/docs/components/scroll-area.md +294 -0
  36. package/dist/docs/index.md +5 -1
  37. package/dist/docs/roadmap.md +6 -0
  38. package/dist/drawer/drawer-close.d.mts +26 -0
  39. package/dist/drawer/drawer-close.d.mts.map +1 -0
  40. package/dist/drawer/drawer-close.mjs +19 -0
  41. package/dist/drawer/drawer-close.mjs.map +1 -0
  42. package/dist/drawer/drawer-description.d.mts +23 -0
  43. package/dist/drawer/drawer-description.d.mts.map +1 -0
  44. package/dist/drawer/drawer-description.mjs +18 -0
  45. package/dist/drawer/drawer-description.mjs.map +1 -0
  46. package/dist/drawer/drawer-form.d.mts +24 -0
  47. package/dist/drawer/drawer-form.d.mts.map +1 -0
  48. package/dist/drawer/drawer-form.mjs +19 -0
  49. package/dist/drawer/drawer-form.mjs.map +1 -0
  50. package/dist/drawer/drawer-popup.d.mts +77 -0
  51. package/dist/drawer/drawer-popup.d.mts.map +1 -0
  52. package/dist/drawer/drawer-popup.mjs +124 -0
  53. package/dist/drawer/drawer-popup.mjs.map +1 -0
  54. package/dist/drawer/drawer-root.d.mts +31 -0
  55. package/dist/drawer/drawer-root.d.mts.map +1 -0
  56. package/dist/drawer/drawer-root.mjs +23 -0
  57. package/dist/drawer/drawer-root.mjs.map +1 -0
  58. package/dist/drawer/drawer-swipe.d.mts +5 -0
  59. package/dist/drawer/drawer-swipe.d.mts.map +1 -0
  60. package/dist/drawer/drawer-swipe.mjs +193 -0
  61. package/dist/drawer/drawer-swipe.mjs.map +1 -0
  62. package/dist/drawer/drawer-title.d.mts +23 -0
  63. package/dist/drawer/drawer-title.d.mts.map +1 -0
  64. package/dist/drawer/drawer-title.mjs +18 -0
  65. package/dist/drawer/drawer-title.mjs.map +1 -0
  66. package/dist/drawer/drawer-trigger.d.mts +23 -0
  67. package/dist/drawer/drawer-trigger.d.mts.map +1 -0
  68. package/dist/drawer/drawer-trigger.mjs +18 -0
  69. package/dist/drawer/drawer-trigger.mjs.map +1 -0
  70. package/dist/drawer/index.d.mts +34 -0
  71. package/dist/drawer/index.d.mts.map +1 -0
  72. package/dist/drawer/index.mjs +33 -0
  73. package/dist/drawer/index.mjs.map +1 -0
  74. package/dist/index.d.mts +19 -1
  75. package/dist/index.mjs +4 -1
  76. package/dist/internal/swipe-arithmetic.mjs +75 -0
  77. package/dist/internal/swipe-arithmetic.mjs.map +1 -0
  78. package/dist/internal/swipe.mjs +169 -0
  79. package/dist/internal/swipe.mjs.map +1 -0
  80. package/dist/menu/menu-context-trigger.mjs +1 -1
  81. package/dist/menu/menu-popup.mjs +1 -1
  82. package/dist/otp-field/index.d.mts +23 -0
  83. package/dist/otp-field/index.d.mts.map +1 -0
  84. package/dist/otp-field/index.mjs +23 -0
  85. package/dist/otp-field/index.mjs.map +1 -0
  86. package/dist/otp-field/otp-field-context.mjs +13 -0
  87. package/dist/otp-field/otp-field-context.mjs.map +1 -0
  88. package/dist/otp-field/otp-field-filter.d.mts +5 -0
  89. package/dist/otp-field/otp-field-filter.d.mts.map +1 -0
  90. package/dist/otp-field/otp-field-filter.mjs +135 -0
  91. package/dist/otp-field/otp-field-filter.mjs.map +1 -0
  92. package/dist/otp-field/otp-field-input.d.mts +41 -0
  93. package/dist/otp-field/otp-field-input.d.mts.map +1 -0
  94. package/dist/otp-field/otp-field-input.mjs +177 -0
  95. package/dist/otp-field/otp-field-input.mjs.map +1 -0
  96. package/dist/otp-field/otp-field-root.d.mts +143 -0
  97. package/dist/otp-field/otp-field-root.d.mts.map +1 -0
  98. package/dist/otp-field/otp-field-root.mjs +397 -0
  99. package/dist/otp-field/otp-field-root.mjs.map +1 -0
  100. package/dist/otp-field/otp-field-slot.d.mts +52 -0
  101. package/dist/otp-field/otp-field-slot.d.mts.map +1 -0
  102. package/dist/otp-field/otp-field-slot.mjs +67 -0
  103. package/dist/otp-field/otp-field-slot.mjs.map +1 -0
  104. package/package.json +4 -1
  105. package/skills/funstack-ui-knowledge/SKILL.md +5 -1
@@ -0,0 +1,501 @@
1
+ # OTPField
2
+
3
+ A field for a short code the user received elsewhere: an SMS or email verification code, an authenticator's six digits, a PIN. It looks like a row of boxes and is **one text input** with `autocomplete="one-time-code"`, so typing, paste, the keyboard's SMS suggestion and a password manager all fill it as they fill any input. The library adds the boxes, a filter that keeps the text a code, and the asynchronous half: **the moment the code is complete is a request**, `completeAction`, run in a transition with `data-pending`.
4
+
5
+ ```tsx title="OTPFieldVerify.tsx"
6
+ "use client";
7
+ import { startTransition, useRef, useState } from "react";
8
+ import { OTPField } from "@funstack/ui/otp-field";
9
+ import { sleep } from "./fake-api.js";
10
+
11
+ const SLOTS = [0, 1, 2, 3, 4, 5];
12
+
13
+ // A pretend server check. A wrong code is not an error: it resolves with a message.
14
+ async function checkCode(code: string): Promise<{ ok: true } | { ok: false; message: string }> {
15
+ await sleep(900);
16
+ return code === "123456"
17
+ ? { ok: true }
18
+ : { ok: false, message: "That code is not right. Check the message and try again." };
19
+ }
20
+
21
+ // Try a wrong code: the field pends while the server checks, then clears with a message. Type
22
+ // 123456: verified. Paste "123 456" or "123-456" too; the filter keeps the digits. The check runs
23
+ // once per completed code, and again when the same code is entered again.
24
+ export function OTPFieldVerify() {
25
+ const [code, setCode] = useState("");
26
+ const [message, setMessage] = useState<string | null>(null);
27
+ const [verified, setVerified] = useState(false);
28
+ // The latest text, for the check to compare against after its await.
29
+ const current = useRef("");
30
+
31
+ if (verified) {
32
+ return (
33
+ <div className="demo-otp-done">
34
+ <p className="demo-readout">Verified. Welcome back.</p>
35
+ <button
36
+ type="button"
37
+ className="demo-button"
38
+ onClick={() => {
39
+ setVerified(false);
40
+ current.current = "";
41
+ setCode("");
42
+ }}
43
+ >
44
+ Start over
45
+ </button>
46
+ </div>
47
+ );
48
+ }
49
+
50
+ return (
51
+ <div className="demo-otp-form">
52
+ <label htmlFor="verify-code" className="demo-label">
53
+ Verification code
54
+ </label>
55
+ <p id="verify-hint" className="demo-description">
56
+ Enter the 6-digit code we sent to your phone (try 123456).
57
+ </p>
58
+ <OTPField.Root
59
+ length={6}
60
+ value={code}
61
+ onValueChange={(next) => {
62
+ current.current = next;
63
+ setCode(next);
64
+ setMessage(null);
65
+ }}
66
+ completeAction={async (entered) => {
67
+ const result = await checkCode(entered);
68
+ // The user may have edited the code meanwhile: act on the current one only.
69
+ if (entered !== current.current) return;
70
+ if (result.ok) {
71
+ startTransition(() => setVerified(true));
72
+ return;
73
+ }
74
+ setMessage(result.message);
75
+ current.current = "";
76
+ setCode("");
77
+ }}
78
+ className="demo-otp"
79
+ >
80
+ <OTPField.Input
81
+ id="verify-code"
82
+ aria-describedby="verify-hint verify-message"
83
+ className="demo-otp-input"
84
+ />
85
+ {SLOTS.map((i) => (
86
+ <OTPField.Slot key={i} index={i} className="demo-otp-slot" />
87
+ ))}
88
+ </OTPField.Root>
89
+ <p id="verify-message" role="status" className="demo-field-error">
90
+ {message}
91
+ </p>
92
+ </div>
93
+ );
94
+ }
95
+ ```
96
+
97
+ ```css title="otp-field.css"
98
+ /* OTPField: the Root is the box, the Input covers it with transparent text (the library's inline
99
+ style), and the Slots draw the characters, the caret and the focus. */
100
+ .demo-otp-form {
101
+ display: grid;
102
+ gap: 0.35rem;
103
+ justify-items: start;
104
+ }
105
+
106
+ .demo-otp {
107
+ display: inline-flex;
108
+ gap: 0.4rem;
109
+ border-radius: 8px;
110
+ }
111
+
112
+ .demo-otp-input {
113
+ border-radius: 8px;
114
+ /* 16px or more: iOS does not zoom on focus. */
115
+ font-size: 1rem;
116
+ outline: none;
117
+ }
118
+
119
+ .demo-otp-input::selection {
120
+ background: transparent;
121
+ }
122
+
123
+ /* The focus ring is drawn on the row while the input has keyboard or pointer focus. */
124
+ .demo-otp:has(.demo-otp-input:focus-visible) .demo-otp-slot[data-active] {
125
+ outline: 2px solid var(--color-primary);
126
+ outline-offset: 1px;
127
+ }
128
+
129
+ .demo-otp-slot {
130
+ display: grid;
131
+ place-items: center;
132
+ inline-size: 2.5rem;
133
+ block-size: 3rem;
134
+ border: 1px solid var(--color-border);
135
+ border-radius: 8px;
136
+ background: var(--color-bg);
137
+ color: var(--color-text);
138
+ font:
139
+ 600 1.25rem / 1 ui-monospace,
140
+ monospace;
141
+ font-variant-numeric: tabular-nums;
142
+ }
143
+
144
+ .demo-otp-slot[data-active] {
145
+ border-color: var(--color-primary);
146
+ }
147
+
148
+ /* The drawn caret in an empty active slot. */
149
+ .demo-otp-slot[data-active]:not([data-filled])::after {
150
+ content: "";
151
+ inline-size: 1px;
152
+ block-size: 1.25em;
153
+ background: currentColor;
154
+ animation: demo-otp-caret 1s steps(1) infinite;
155
+ }
156
+
157
+ @keyframes demo-otp-caret {
158
+ 50% {
159
+ opacity: 0;
160
+ }
161
+ }
162
+
163
+ .demo-otp[data-pending] .demo-otp-slot {
164
+ opacity: 0.6;
165
+ }
166
+
167
+ .demo-otp[data-pending] .demo-otp-input {
168
+ cursor: progress;
169
+ }
170
+
171
+ .demo-otp[data-invalid] .demo-otp-slot {
172
+ border-color: #dc2626;
173
+ }
174
+
175
+ .demo-otp-done {
176
+ display: grid;
177
+ gap: 0.5rem;
178
+ justify-items: start;
179
+ }
180
+
181
+ @media (prefers-reduced-motion: reduce) {
182
+ .demo-otp-slot::after {
183
+ animation: none;
184
+ }
185
+ }
186
+
187
+ /* Without JavaScript the Slots never fill: show the plain input instead. */
188
+ @media (scripting: none) {
189
+ .demo-otp-slot {
190
+ display: none;
191
+ }
192
+ .demo-otp-input {
193
+ position: static !important;
194
+ color: inherit !important;
195
+ caret-color: auto !important;
196
+ border: 1px solid var(--color-border) !important;
197
+ padding: 0.5rem !important;
198
+ }
199
+ }
200
+ ```
201
+
202
+ ## Anatomy
203
+
204
+ ```tsx
205
+ import { OTPField } from "@funstack/ui/otp-field";
206
+ import { Separator } from "@funstack/ui/separator";
207
+
208
+ <label htmlFor="code">Verification code</label>
209
+ <OTPField.Root length={6} completeAction={verifyCode}> {/* div, position: relative */}
210
+ <OTPField.Input id="code" /> {/* input autocomplete=one-time-code, covers the Root */}
211
+ <OTPField.Slot index={0} /> {/* span aria-hidden: one character */}
212
+ <OTPField.Slot index={1} />
213
+ <OTPField.Slot index={2} />
214
+ <Separator decorative orientation="vertical" />
215
+ <OTPField.Slot index={3} />
216
+ <OTPField.Slot index={4} />
217
+ <OTPField.Slot index={5} />
218
+ </OTPField.Root>
219
+ ```
220
+
221
+ - **The Input is the field.** It lies over the whole Root with transparent text and caret (an inline style the library sets), so a click anywhere focuses it and the browser's own long-press, paste menu and autofill work on it. It is the only focusable element and the only thing assistive technology sees.
222
+ - **The Slots are drawings.** Each shows the character at its `index` and is `aria-hidden`. You choose the grouping with ordinary elements between them. A missing or repeated index is a development warning.
223
+ - There is no `Label` part: name the Input with a `<label for>`, `Field.Label`, or `aria-label` (an unnamed Input is a development warning). No hidden input either: the Input is the form control.
224
+
225
+ ## How it works
226
+
227
+ ### The text is a fact, the completion is a request
228
+
229
+ | | onValueChange | completeAction |
230
+ | - | - | - |
231
+ | Called | For every change of the filtered text: an edit (`"activate"`), a form reset, text found at hydration, a WebOTP fill (`"platform"`). | Each time an edit (or a WebOTP fill) leaves the code complete. Before it, `onValueChange` for the same edit. |
232
+ | Lane | Urgent: synchronously in the `input` event, outside any transition. A controlled parent's `setCode(next)` keeps the input in step per keystroke. | The Root's transition, awaited. |
233
+ | Pending | Never. | `data-pending` on the Root and the Input until every run has settled. The input stays enabled and editable. |
234
+ | Errors | A throw or a rejected promise goes to `reportError`; the edit stands. | A rejection reaches the nearest Error Boundary. A wrong code is not an error: resolve and show the message. |
235
+
236
+ - `completeAction` runs for **every** completed code: a second one while the first is pending runs too (it is a different code), and so does the same code pasted again (the way to retry). A character the filter drops does not re-run it. There is no `whilePending`: ignoring a completion would drop what the user typed. An action that must not act on a stale code compares it with the current text after its `await`, as the example does.
237
+ - Not run at mount, for text found at hydration (a code the browser restored on reload is not a fresh one), for a controlled `value` you changed, for a form reset, or while `disabled` / `readOnly`.
238
+ - Clearing after a wrong code is the controlled form: `setCode("")` in the action, after its `await`. That is an urgent update, which is what a text input needs.
239
+
240
+ ### The filter
241
+
242
+ The browser performs every edit; in each `input` event the library reads the result and, where the filtered text differs, writes it back with the caret. Nothing is `preventDefault()`ed.
243
+
244
+ - Characters outside `charset` are dropped, so a pasted `123 456` or `123-456` becomes `123456`. Full-width digits (from a Japanese or Chinese IME) are converted for `"numeric"`. Case is kept.
245
+ - **A full code replaces everything**: pasting six digits into a half-typed field does not append to the half.
246
+ - **Typing over a filled box replaces it** and moves on; at the end of a complete code a character replaces the last one. A selected range is replaced.
247
+ - A dropped character over a selection leaves the text and the selection as they were.
248
+ - No `maxlength`: it would cut `123 456` to `123 45` before the filter sees it. The length is the filter's, and the derived `pattern`'s without JavaScript.
249
+ - While an IME composes, the library does not write to the input (that would end the composition): the Slots follow the filtered text and the write-back happens at `compositionend`. Numeric keyboards do not compose.
250
+
251
+ ## Props
252
+
253
+ ### `OTPField.Root`
254
+
255
+ | Prop | Type | Default | Description |
256
+ | - | - | - | - |
257
+ | `length` | `number` | – | Required. The number of characters in a complete code; also the `pattern`'s count. A non-positive or fractional value logs a development warning. |
258
+ | `charset` | `"numeric" \| "alphanumeric" \| "alphabetic"` | `"numeric"` | Which characters the filter keeps (ASCII digits, letters and digits, letters), and so the Input's `inputmode` (`numeric` / `text`) and `pattern` (`[0-9]{6}`, `[A-Za-z0-9]{6}`, `[A-Za-z]{6}`). |
259
+ | `value` | `string` | – | Controlled text. It follows the input and cannot hold an edit back; a value the filter would change is rendered filtered, with a development warning. |
260
+ | `defaultValue` | `string` | `""` | Initial text in uncontrolled mode (filtered); also the form-reset target. |
261
+ | `onValueChange` | `UrgentChangeHandler<string, OTPFieldValueChangeDetails>` | – | The text changed. A fact: called synchronously, outside any transition, not awaited. Called before `completeAction` for the same edit. |
262
+ | `completeAction` | `Action<[value: string, details: OTPFieldCompleteDetails]>` | – | An edit left the code complete. Runs in the Root's transition and is awaited, with `data-pending`. May be a Server Function. |
263
+ | `submitOnComplete` | `boolean` | `false` | Submit the Input's form (`requestSubmit()`) when an edit leaves the code complete, right after dispatching `completeAction`; inside a Field after re-validating the filtered text; during an IME composition at `compositionend`. See [Auto-submit](#auto-submit) for the notice it needs. |
264
+ | `name, form` | `string` | – | On the Input, which posts the text. `name` defaults to the Field's inside a `Field.Root`. |
265
+ | `required` | `boolean` | `false` | On the Input: an empty field is `valueMissing`. Or the Field's. |
266
+ | `disabled` | `boolean` | `false` | On the Input (not submitted). Or the Field's. |
267
+ | `readOnly` | `boolean` | `false` | Native `readonly` on the Input: no edits and no completion. |
268
+ | `experimental_webOTP` | `boolean` | `false` | Ask the browser for the code of an origin-bound SMS while the Input is mounted ([WebOTP](#webotp)). Chromium and Safari; does nothing in Firefox. |
269
+ | `className` | `string \| ((state: OTPFieldState) => string \| undefined)` | – | A class name, or a function of the part's state. |
270
+ | `style` | `CSSProperties \| ((state: OTPFieldState) => CSSProperties \| undefined)` | – | Inline style, or a function of the part's state. |
271
+ | `children` | `ReactNode \| ((state: OTPFieldState) => ReactNode)` | – | The parts. The function form receives the state. |
272
+ | `render` | `ReactElement \| ((props, state: OTPFieldState) => ReactNode)` | – | Replace the default \<div> (element form) or take over rendering (function form). |
273
+
274
+ - **State**: `OTPFieldState` is `{ value, length, complete, pending, disabled, readOnly, invalid }`: `value` is the filtered text, `complete` is `value.length === length`, `pending` is a `completeAction` in flight, `invalid` an error shown by an enclosing Field (`false` outside one).
275
+ - **Owned**: `data-complete`, `data-pending`, `data-disabled`, `data-invalid` (inside a Field), and `position` in `style` (`relative`) unless your style sets it.
276
+
277
+ Both details objects are `{ reason, event }`. `onValueChange`: `"activate"` for an edit (`event` is the `input` event, or the `compositionend` that ended a composition), `"platform"` for a form reset (the `reset` event), text found at hydration and a WebOTP fill (`undefined`). `completeAction`: `"activate"` for an edit, `"platform"` for a WebOTP fill. Serialisable: a Server Function receives `{ reason }`.
278
+
279
+ ### `OTPField.Input`
280
+
281
+ | Prop | Type | Default | Description |
282
+ | - | - | - | - |
283
+ | `autoComplete, inputMode, autoCapitalize, autoCorrect, spellCheck` | `string, string, string, string, boolean` | `"one-time-code", from charset, "off", "off", false` | Defaults your own prop replaces. `autoComplete="off"` is the spelling for a PIN that is not a one-time code. |
284
+ | `className` | `string \| ((state: OTPFieldState) => string \| undefined)` | – | A class name, or a function of the part's state. |
285
+ | `style` | `CSSProperties \| ((state: OTPFieldState) => CSSProperties \| undefined)` | – | Inline style, or a function of the part's state. |
286
+ | `render` | `ReactElement \| ((props, state: OTPFieldState) => ReactNode)` | – | Replace the default \<input> (element form) or take over rendering (function form). |
287
+
288
+ - **State**: `OTPFieldState`.
289
+ - **Owned**: `type` (`"text"`), `value`, `pattern`, `name`, `form`, `required`, `disabled`, `readOnly`, `onChange` (and native `beforeinput`, composition, selection and `click` listeners), the inline style (`position: absolute; inset: 0; …; color: transparent; caret-color: transparent; forced-color-adjust: none`; your style cannot override those keys), inside a Field `id`, `aria-describedby` (token-merged) and `aria-invalid`, and `data-complete`, `data-pending`, `data-disabled`, `data-invalid`.
290
+
291
+ `id`, `aria-label`, `aria-describedby`, `autoFocus`, `title` and the rest go on the Input. A `title` is appended to the browser's message for a partial code. The outline is not touched: the Input's focus ring surrounds the whole field unless you move it.
292
+
293
+ ### `OTPField.Slot`
294
+
295
+ | Prop | Type | Default | Description |
296
+ | - | - | - | - |
297
+ | `index` | `number` | – | Required. Which character position this slot shows, from 0. |
298
+ | `className` | `string \| ((state: OTPFieldSlotState) => string \| undefined)` | – | A class name, or a function of the part's state. |
299
+ | `style` | `CSSProperties \| ((state: OTPFieldSlotState) => CSSProperties \| undefined)` | – | Inline style, or a function of the part's state. |
300
+ | `children` | `ReactNode \| ((state: OTPFieldSlotState) => ReactNode)` | – | Default: the character, or nothing. A function draws a placeholder, a dot, a caret element. |
301
+ | `render` | `ReactElement \| ((props, state: OTPFieldSlotState) => ReactNode)` | – | Replace the default \<span> (element form) or take over rendering (function form). |
302
+
303
+ - **State**: `OTPFieldSlotState` is `OTPFieldState` plus `{ index, char, filled, active }`: `char` is the character at `index` or `null`, `active` is the caret or part of the selection at this position while the input is focused.
304
+ - **Owned**: `aria-hidden`, `data-index`, `data-filled`, `data-active`, and `pointer-events: none` in the inline style.
305
+
306
+ ## Data attributes
307
+
308
+ | Hook | On | Meaning |
309
+ | - | - | - |
310
+ | `data-complete` | Root, Input | The text has `length` characters. |
311
+ | `data-pending` | Root, Input | A `completeAction` is in flight. |
312
+ | `data-disabled` | Root, Input | Disabled (own prop or the Field's). |
313
+ | `data-invalid` | Root, Input | Inside a Field that shows an error. |
314
+ | `data-index` | Slot | The slot's `index`. |
315
+ | `data-filled` | Slot | A character is at this position. |
316
+ | `data-active` | Slot | The input is focused and the caret (the next character goes here) or part of the selection is at this position; at the end of a complete code, the last slot. |
317
+ | `:focus-visible`, `:user-invalid`, `:disabled` | the Input | The platform's own states (`.root:has(:focus-visible)`). |
318
+
319
+ ```css
320
+ .otp { display: inline-flex; gap: .5rem; }
321
+ .otp-input { border-radius: .5rem; font-size: 1rem; } /* 16px or more: no zoom on focus in iOS */
322
+ .otp-input::selection { background: transparent; }
323
+ .otp-slot { display: grid; place-items: center; inline-size: 2.5rem; block-size: 3rem;
324
+ border: 1px solid var(--line); border-radius: .5rem; font: 600 1.25rem / 1 var(--mono); }
325
+ .otp-slot[data-active] { border-color: var(--accent); }
326
+ .otp-slot[data-active]:not([data-filled])::after { /* the drawn caret */
327
+ content: ""; inline-size: 1px; block-size: 1.25em; background: currentColor;
328
+ animation: otp-caret 1s steps(1) infinite;
329
+ }
330
+ @keyframes otp-caret { 50% { opacity: 0; } }
331
+ .otp[data-pending] .otp-slot { opacity: .6; }
332
+ .otp[data-invalid] .otp-slot { border-color: var(--danger); }
333
+ .otp-slot:not([data-filled])::before { content: "·"; color: var(--muted); } /* placeholder */
334
+ @media (prefers-reduced-motion: reduce) { .otp-slot::after { animation: none; } }
335
+ ```
336
+
337
+ - Style the Slots freely: they carry an owned inline `pointer-events: none`, so a dimmed, positioned or animated Slot never takes the click from the Input.
338
+ - A design that moves the focus ring from the Input to the active Slot must keep one visible:`.otp:has(.otp-input:focus-visible) .otp-slot[data-active]`.
339
+ - Under forced colours the Input keeps its text transparent (`forced-color-adjust: none`); the Slots are ordinary text and follow the system colours.
340
+
341
+ ## Keyboard and ARIA
342
+
343
+ | Input | Effect |
344
+ | - | - |
345
+ | A character of `charset` | Inserted, or typed over the character at the caret. Others are dropped. |
346
+ | Backspace, Delete | Native: the character before / after the caret; the rest closes up. |
347
+ | ArrowLeft / ArrowRight, Home / End (Shift: select) | Native caret movement: one character is one slot. |
348
+ | Ctrl / Cmd + A, C, X, V, Z | Native. |
349
+ | Enter | Native implicit submission of the form. |
350
+ | A click or tap | Focuses the Input and puts the caret at the slot under the pointer. |
351
+
352
+ - One field, one name, one value: a screen reader reads typed characters as in any text field. The library ships no built-in string; there is no per-box label to invent.
353
+ - Put the expected length in the visible label or description ("the 6-digit code we sent to …"); the `pattern` alone tells a screen-reader user nothing until it fails.
354
+ - Pending is visual (`data-pending`). The answer is your content: a Field error through `aria-describedby` and the Form's focus move, or your own `role="status"` message, as in the example.
355
+ - A programmatic write clears the browser's undo history for the input.
356
+
357
+ ## Transition types
358
+
359
+ Concept: [Transitions & ViewTransition](../concepts/transitions.md#transition-types)
360
+
361
+ Each `completeAction` run: `fsui-otp-field` plus `fsui-otp-field-complete`. Typing is urgent and carries none. No part takes a `transition` prop; use the types in your own boundary (the next step of a sign-in).
362
+
363
+ ```css
364
+ ::view-transition-old(.step):active-view-transition-type(fsui-otp-field-complete) { animation: slide-out-left .2s; }
365
+ ```
366
+
367
+ ## Optimistic and pending
368
+
369
+ Concept: [Pending & optimistic state](../concepts/pending-and-optimistic.md)
370
+
371
+ Nothing is optimistic: the text is urgent library state written from the `input` event. `data-pending` is on the Root and the Input while a `completeAction` runs; the field is never disabled meanwhile.
372
+
373
+ ## Suspense
374
+
375
+ Concept: [Suspense](../concepts/suspense.md)
376
+
377
+ Nothing specific: the OTPField renders no content region.
378
+
379
+ ## Browser-owned state
380
+
381
+ Concept: [Browser-owned state](../concepts/browser-owned-state.md)
382
+
383
+ The text is the input's: the browser edits it, the library filters it. A form reset is the browser's; the library adopts the reset target with `onValueChange` (`"platform"`) and runs no completion. The selection is the browser's too; the Slots mirror it (`data-active`).
384
+
385
+ ## Before hydration
386
+
387
+ Concept: [Server Components](../concepts/server-components.md#before-hydration)
388
+
389
+ The server HTML is the input and the Slots with the default value. Before hydration the input takes typing, paste and autofill natively, but the text is transparent and the Slots are static, so **what is typed is not visible until hydration**. At hydration the text is filtered and adopted (`"platform"`), with no completion: the user submits with Enter or the button. A form submitted before hydration posts the text, checked by `pattern`.
390
+
391
+ ### Without JavaScript
392
+
393
+ The Slots never fill without script. A page that must work without it shows the plain input instead (the inline styles need `!important` to undo):
394
+
395
+ ```css
396
+ @media (scripting: none) {
397
+ .otp-slot { display: none; }
398
+ .otp-input { position: static !important; color: inherit !important; caret-color: auto !important;
399
+ border: 1px solid var(--line) !important; padding: .5rem !important; }
400
+ }
401
+ ```
402
+
403
+ ## Forms
404
+
405
+ - The Input posts its text under `name`; a partial code posts as typed, and `pattern` blocks the submission (`patternMismatch`). `required` makes an empty one `valueMissing`.
406
+ - **Form reset** restores the reset target (the latest `defaultValue` uncontrolled, the mount-time value controlled) with `onValueChange` `"platform"`, and runs no completion.
407
+ - Enter submits the form natively.
408
+
409
+ ## Recipes
410
+
411
+ ### <a id="auto-submit"></a>Auto-submit inside a Form
412
+
413
+ ```tsx title="OTPFieldAutoSubmit.tsx"
414
+ "use client";
415
+ import { useActionState } from "react";
416
+ import { Field } from "@funstack/ui/field";
417
+ import { Form } from "@funstack/ui/form";
418
+ import type { FormErrors } from "@funstack/ui/form";
419
+ import { OTPField } from "@funstack/ui/otp-field";
420
+ import { sleep } from "./fake-api.js";
421
+
422
+ const SLOTS = [0, 1, 2, 3, 4, 5];
423
+
424
+ interface VerifyState {
425
+ readonly errors?: FormErrors;
426
+ readonly signedIn?: boolean;
427
+ }
428
+
429
+ // A pretend Server Function: it checks the code and returns a field error for a wrong one.
430
+ async function verify(_prev: VerifyState, formData: FormData): Promise<VerifyState> {
431
+ await sleep(900);
432
+ if (formData.get("code") !== "123456") {
433
+ return { errors: { code: "That code has expired. We sent you a new one." } };
434
+ }
435
+ return { signedIn: true };
436
+ }
437
+
438
+ // The code is submitted as soon as it is complete (`submitOnComplete`): the form's pending
439
+ // shows on the button, a wrong code comes back as the Field's error with focus on the input,
440
+ // and the next keystroke dismisses it. The description says so before the field (WCAG 3.2.2),
441
+ // and the button stays for a user who wants to check the code first. Try 111111, then 123456.
442
+ export function OTPFieldAutoSubmit() {
443
+ const [state, formAction] = useActionState(verify, {});
444
+ if (state.signedIn === true) {
445
+ return <p className="demo-readout">Signed in.</p>;
446
+ }
447
+ return (
448
+ <Form.Root action={formAction} errors={state.errors} className="demo-form">
449
+ <Field.Root name="code" required className="demo-fieldset">
450
+ <Field.Label className="demo-label">Sign-in code</Field.Label>
451
+ <Field.Description className="demo-description">
452
+ The 6-digit code from your authenticator app. It is submitted when complete.
453
+ </Field.Description>
454
+ <OTPField.Root length={6} submitOnComplete className="demo-otp">
455
+ <OTPField.Input className="demo-otp-input" title="6 digits" />
456
+ {SLOTS.map((i) => (
457
+ <OTPField.Slot key={i} index={i} className="demo-otp-slot" />
458
+ ))}
459
+ </OTPField.Root>
460
+ <Field.Error className="demo-field-error">
461
+ {({ validity, messages }) =>
462
+ validity.patternMismatch ? "Enter all 6 digits." : messages.join(" ")
463
+ }
464
+ </Field.Error>
465
+ </Field.Root>
466
+ <div className="demo-row">
467
+ <Form.Submit className="demo-button demo-primary demo-submit">
468
+ {({ pending }) => (
469
+ <>
470
+ {pending && <span className="demo-spinner" aria-hidden />}
471
+ Verify
472
+ </>
473
+ )}
474
+ </Form.Submit>
475
+ </div>
476
+ </Form.Root>
477
+ );
478
+ }
479
+ ```
480
+
481
+ `otp-field.css`: the same file as above.
482
+
483
+ > **Tell the user before the field (WCAG 3.2.2)**
484
+ >
485
+ > `submitOnComplete` changes the context on input: the page submits while the user is still typing. Say so before or at the field ("It is submitted when complete"), and keep a submit button, for the path without JavaScript and for a user who wants to check the code first. A `completeAction` that navigates is the same change of context and needs the same notice.
486
+
487
+ - Inside a `Field.Root` the Input is the Field's control, as `Field.Control` is: `Field.Label`'s `for` names it, the description and error describe it, its validity (`required`, the derived `pattern`, `validate`, server errors) is the input's own. There is no hidden input: the text shown is the value posted.
488
+ - The request is the form's here: `Form.Submit` and the Field parts show `data-pending`, a wrong code comes back in `errors` and is shown by `Field.Error` with focus on the Input, and the next keystroke dismisses it. `completeAction` is for a field with no form around it.
489
+ - The filter's write-back counts as an edit for the Field (it re-runs `validate` on the filtered text and dismisses a server error); a reset or a controlled change re-runs `validate` only.
490
+
491
+ ### WebOTP
492
+
493
+ `autocomplete="one-time-code"` is what iOS, Android keyboards and password managers key on, and it needs nothing from the library. WebOTP is the API form of the same thing for SMS: the page asks for the code of a message whose last line names its origin (`@example.com #123456`), the browser asks the user, and a promise resolves with the code. It is in Chromium and Safari, not in Firefox, so it is behind `experimental_webOTP`.
494
+
495
+ - With the flag on and `OTPCredential` present, the Input's mount requests a code. A resolved code of exactly `length` characters (after the filter) fills the field (`onValueChange` `"platform"`) and completes it (`completeAction` `"platform"`, `submitOnComplete`). Anything else, a refusal, and an abort are ignored.
496
+ - One request for the life of the Input. It is aborted at unmount, when the Input's form is submitted and when the user completes the code by hand; after a completion no new request starts and a late code is ignored.
497
+
498
+ ```tsx
499
+ // A Server Function as the check, straight from a Server Component:
500
+ <OTPField.Root length={6} completeAction={verifyCode} experimental_webOTP>…</OTPField.Root>
501
+ ```