@dowel-ui/react 0.4.0 → 0.5.0

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 (146) hide show
  1. package/README.md +9 -5
  2. package/dist/components/ai-extraction-review/ai-extraction-review.d.ts +52 -0
  3. package/dist/components/ai-extraction-review/ai-extraction-review.d.ts.map +1 -0
  4. package/dist/components/ai-extraction-review/ai-extraction-review.js +249 -0
  5. package/dist/components/ai-extraction-review/ai-extraction-review.js.map +1 -0
  6. package/dist/components/ai-extraction-review/extraction-model.d.ts +94 -0
  7. package/dist/components/ai-extraction-review/extraction-model.d.ts.map +1 -0
  8. package/dist/components/ai-extraction-review/extraction-model.js +91 -0
  9. package/dist/components/ai-extraction-review/extraction-model.js.map +1 -0
  10. package/dist/components/ai-extraction-review/index.d.ts +3 -0
  11. package/dist/components/ai-extraction-review/index.js +3 -0
  12. package/dist/components/ai-extraction-review/meta.js +17 -0
  13. package/dist/components/ai-extraction-review/meta.js.map +1 -0
  14. package/dist/components/ai-suggested-value/ai-suggested-value.d.ts +72 -0
  15. package/dist/components/ai-suggested-value/ai-suggested-value.d.ts.map +1 -0
  16. package/dist/components/ai-suggested-value/ai-suggested-value.js +126 -0
  17. package/dist/components/ai-suggested-value/ai-suggested-value.js.map +1 -0
  18. package/dist/components/ai-suggested-value/index.d.ts +2 -0
  19. package/dist/components/ai-suggested-value/index.js +2 -0
  20. package/dist/components/ai-suggested-value/meta.js +17 -0
  21. package/dist/components/ai-suggested-value/meta.js.map +1 -0
  22. package/dist/components/alert/alert.d.ts +1 -1
  23. package/dist/components/avatar/avatar.d.ts +1 -1
  24. package/dist/components/badge/badge.d.ts +1 -1
  25. package/dist/components/button/button.d.ts +1 -1
  26. package/dist/components/confirm-typed/confirm-typed.d.ts +47 -0
  27. package/dist/components/confirm-typed/confirm-typed.d.ts.map +1 -0
  28. package/dist/components/confirm-typed/confirm-typed.js +106 -0
  29. package/dist/components/confirm-typed/confirm-typed.js.map +1 -0
  30. package/dist/components/confirm-typed/index.d.ts +2 -0
  31. package/dist/components/confirm-typed/index.js +2 -0
  32. package/dist/components/confirm-typed/meta.js +17 -0
  33. package/dist/components/confirm-typed/meta.js.map +1 -0
  34. package/dist/components/cron-editor/cron-editor.d.ts +36 -0
  35. package/dist/components/cron-editor/cron-editor.d.ts.map +1 -0
  36. package/dist/components/cron-editor/cron-editor.js +493 -0
  37. package/dist/components/cron-editor/cron-editor.js.map +1 -0
  38. package/dist/components/cron-editor/cron-expression.d.ts +82 -0
  39. package/dist/components/cron-editor/cron-expression.d.ts.map +1 -0
  40. package/dist/components/cron-editor/cron-expression.js +352 -0
  41. package/dist/components/cron-editor/cron-expression.js.map +1 -0
  42. package/dist/components/cron-editor/index.d.ts +3 -0
  43. package/dist/components/cron-editor/index.js +3 -0
  44. package/dist/components/cron-editor/meta.js +17 -0
  45. package/dist/components/cron-editor/meta.js.map +1 -0
  46. package/dist/components/dns-record/dns-record.d.ts +61 -0
  47. package/dist/components/dns-record/dns-record.d.ts.map +1 -0
  48. package/dist/components/dns-record/dns-record.js +227 -0
  49. package/dist/components/dns-record/dns-record.js.map +1 -0
  50. package/dist/components/dns-record/index.d.ts +2 -0
  51. package/dist/components/dns-record/index.js +2 -0
  52. package/dist/components/dns-record/meta.js +17 -0
  53. package/dist/components/dns-record/meta.js.map +1 -0
  54. package/dist/components/permission-matrix/index.d.ts +3 -0
  55. package/dist/components/permission-matrix/index.js +3 -0
  56. package/dist/components/permission-matrix/meta.js +17 -0
  57. package/dist/components/permission-matrix/meta.js.map +1 -0
  58. package/dist/components/permission-matrix/permission-matrix.d.ts +46 -0
  59. package/dist/components/permission-matrix/permission-matrix.d.ts.map +1 -0
  60. package/dist/components/permission-matrix/permission-matrix.js +199 -0
  61. package/dist/components/permission-matrix/permission-matrix.js.map +1 -0
  62. package/dist/components/permission-matrix/permission-model.d.ts +53 -0
  63. package/dist/components/permission-matrix/permission-model.d.ts.map +1 -0
  64. package/dist/components/permission-matrix/permission-model.js +64 -0
  65. package/dist/components/permission-matrix/permission-model.js.map +1 -0
  66. package/dist/components/progress/progress.d.ts +1 -1
  67. package/dist/components/secret-field/index.d.ts +2 -0
  68. package/dist/components/secret-field/index.js +2 -0
  69. package/dist/components/secret-field/meta.js +17 -0
  70. package/dist/components/secret-field/meta.js.map +1 -0
  71. package/dist/components/secret-field/secret-field.d.ts +58 -0
  72. package/dist/components/secret-field/secret-field.d.ts.map +1 -0
  73. package/dist/components/secret-field/secret-field.js +168 -0
  74. package/dist/components/secret-field/secret-field.js.map +1 -0
  75. package/dist/components/session-expiry/index.d.ts +2 -0
  76. package/dist/components/session-expiry/index.js +2 -0
  77. package/dist/components/session-expiry/meta.js +17 -0
  78. package/dist/components/session-expiry/meta.js.map +1 -0
  79. package/dist/components/session-expiry/session-expiry.d.ts +64 -0
  80. package/dist/components/session-expiry/session-expiry.d.ts.map +1 -0
  81. package/dist/components/session-expiry/session-expiry.js +185 -0
  82. package/dist/components/session-expiry/session-expiry.js.map +1 -0
  83. package/dist/components/shortcut-recorder/index.d.ts +3 -0
  84. package/dist/components/shortcut-recorder/index.js +3 -0
  85. package/dist/components/shortcut-recorder/meta.js +17 -0
  86. package/dist/components/shortcut-recorder/meta.js.map +1 -0
  87. package/dist/components/shortcut-recorder/shortcut-model.d.ts +67 -0
  88. package/dist/components/shortcut-recorder/shortcut-model.d.ts.map +1 -0
  89. package/dist/components/shortcut-recorder/shortcut-model.js +168 -0
  90. package/dist/components/shortcut-recorder/shortcut-model.js.map +1 -0
  91. package/dist/components/shortcut-recorder/shortcut-recorder.d.ts +47 -0
  92. package/dist/components/shortcut-recorder/shortcut-recorder.d.ts.map +1 -0
  93. package/dist/components/shortcut-recorder/shortcut-recorder.js +212 -0
  94. package/dist/components/shortcut-recorder/shortcut-recorder.js.map +1 -0
  95. package/dist/components/spinner/spinner.d.ts +1 -1
  96. package/dist/components/sync-status/index.d.ts +2 -0
  97. package/dist/components/sync-status/index.js +2 -0
  98. package/dist/components/sync-status/meta.js +17 -0
  99. package/dist/components/sync-status/meta.js.map +1 -0
  100. package/dist/components/sync-status/sync-status.d.ts +65 -0
  101. package/dist/components/sync-status/sync-status.d.ts.map +1 -0
  102. package/dist/components/sync-status/sync-status.js +103 -0
  103. package/dist/components/sync-status/sync-status.js.map +1 -0
  104. package/dist/components/toast/toast.d.ts +1 -1
  105. package/dist/index.d.ts +25 -1
  106. package/dist/index.js +16 -2
  107. package/dist/registry/components.d.ts.map +1 -1
  108. package/dist/registry/components.js +73 -53
  109. package/dist/registry/components.js.map +1 -1
  110. package/package.json +4 -4
  111. package/src/components/ai-extraction-review/ai-extraction-review.tsx +492 -0
  112. package/src/components/ai-extraction-review/extraction-model.ts +159 -0
  113. package/src/components/ai-extraction-review/index.ts +13 -0
  114. package/src/components/ai-extraction-review/meta.ts +24 -0
  115. package/src/components/ai-suggested-value/ai-suggested-value.tsx +295 -0
  116. package/src/components/ai-suggested-value/index.ts +6 -0
  117. package/src/components/ai-suggested-value/meta.ts +24 -0
  118. package/src/components/confirm-typed/confirm-typed.tsx +191 -0
  119. package/src/components/confirm-typed/index.ts +1 -0
  120. package/src/components/confirm-typed/meta.ts +22 -0
  121. package/src/components/cron-editor/cron-editor.tsx +676 -0
  122. package/src/components/cron-editor/cron-expression.ts +552 -0
  123. package/src/components/cron-editor/index.ts +23 -0
  124. package/src/components/cron-editor/meta.ts +23 -0
  125. package/src/components/dns-record/dns-record.tsx +315 -0
  126. package/src/components/dns-record/index.ts +7 -0
  127. package/src/components/dns-record/meta.ts +21 -0
  128. package/src/components/permission-matrix/index.ts +14 -0
  129. package/src/components/permission-matrix/meta.ts +23 -0
  130. package/src/components/permission-matrix/permission-matrix.tsx +368 -0
  131. package/src/components/permission-matrix/permission-model.ts +120 -0
  132. package/src/components/secret-field/index.ts +6 -0
  133. package/src/components/secret-field/meta.ts +21 -0
  134. package/src/components/secret-field/secret-field.tsx +284 -0
  135. package/src/components/session-expiry/index.ts +9 -0
  136. package/src/components/session-expiry/meta.ts +23 -0
  137. package/src/components/session-expiry/session-expiry.tsx +321 -0
  138. package/src/components/shortcut-recorder/index.ts +19 -0
  139. package/src/components/shortcut-recorder/meta.ts +22 -0
  140. package/src/components/shortcut-recorder/shortcut-model.ts +204 -0
  141. package/src/components/shortcut-recorder/shortcut-recorder.tsx +336 -0
  142. package/src/components/sync-status/index.ts +9 -0
  143. package/src/components/sync-status/meta.ts +21 -0
  144. package/src/components/sync-status/sync-status.tsx +217 -0
  145. package/src/index.ts +10 -0
  146. package/src/registry/components.ts +20 -0
@@ -0,0 +1,295 @@
1
+ "use client";
2
+
3
+ import {
4
+ cloneElement,
5
+ isValidElement,
6
+ useId,
7
+ useState,
8
+ type ComponentPropsWithRef,
9
+ type ReactElement,
10
+ type ReactNode,
11
+ } from "react";
12
+
13
+ import { disabledStyles, focusRing } from "@/lib/styles";
14
+ import { cn } from "@/lib/utils";
15
+
16
+ /**
17
+ * An AI-proposed value for a form control, offered beside it rather than
18
+ * written into it.
19
+ *
20
+ * Autofill is the most common shape AI takes inside ordinary software —
21
+ * enrich this contact, fill this form from the upload, guess the category —
22
+ * and nearly every implementation writes the value straight into the field
23
+ * as if the person had typed it. Two things go wrong. A plausible, wrong
24
+ * value rides through on the person's own Submit, because nothing marked it
25
+ * as needing a look. And once submitted, the record cannot tell a value the
26
+ * model supplied from one a human typed, which is the one fact an audit later
27
+ * needs.
28
+ *
29
+ * So the suggestion stays pending until it is accepted, and acceptance is
30
+ * reported rather than performed: the component hands the value to `onAccept`
31
+ * and never touches the control. That is also what lets it wrap any control —
32
+ * a select, a date, a number, a combobox — where ghost text can only complete
33
+ * a string. Afterwards the field says it was filled by AI, says if it was
34
+ * edited since, and offers to put back what was there.
35
+ *
36
+ * Deliberately no "accept all". A button that takes every suggestion at once
37
+ * is the review deleting itself, and every product that ships one watches its
38
+ * users press it without reading.
39
+ */
40
+
41
+ export interface Suggestion<T = string> {
42
+ value: T;
43
+ /** How the value reads — a select's option text, a formatted date. Defaults to the value. */
44
+ label?: string;
45
+ /** Where it came from, in the reader's language: "from the invoice header". */
46
+ source?: string;
47
+ /** 0 to 1. Shown as a number, never only as a colour. */
48
+ confidence?: number;
49
+ }
50
+
51
+ export type SuggestionStatus = "pending" | "accepted" | "dismissed";
52
+
53
+ export interface SuggestedValueProps<T = string> extends Omit<
54
+ ComponentPropsWithRef<"div">,
55
+ "children" | "id" | "aria-describedby" | "aria-invalid"
56
+ > {
57
+ /** The field, for the buttons' names: "Accept suggestion — Company". */
58
+ label: string;
59
+ /** Null or undefined offers nothing. A different suggestion starts pending again. */
60
+ suggestion?: Suggestion<T> | null;
61
+ /**
62
+ * The control's current value. Lets the marker say the fill was edited
63
+ * since, and lets Undo restore what the field held before.
64
+ */
65
+ value?: T;
66
+ /** Supply to own the status; otherwise it is kept here. */
67
+ status?: SuggestionStatus;
68
+ onAccept: (value: T, suggestion: Suggestion<T>) => void;
69
+ onDismiss?: (suggestion: Suggestion<T>) => void;
70
+ /** Called with the value the field held before the fill, so it can be put back. */
71
+ onRevert?: (previous: T | undefined) => void;
72
+ /** How to compare values. Object.is by default. */
73
+ equals?: (a: T, b: T) => boolean;
74
+ /** Confidence below this is called low, in words. */
75
+ lowConfidenceBelow?: number;
76
+ /**
77
+ * Announce a suggestion when it arrives. Off by default: a form that fills
78
+ * twenty fields at once would narrate all twenty, and the description on
79
+ * the control already tells a reader when they reach the field.
80
+ */
81
+ announce?: boolean;
82
+ /** The control. Receives the description, and any id or ARIA a FormControl passes down. */
83
+ children: ReactElement<Record<string, unknown>>;
84
+ /* Passed through to the control, so this can sit inside a FormControl. */
85
+ id?: string;
86
+ "aria-describedby"?: string;
87
+ "aria-invalid"?: ComponentPropsWithRef<"input">["aria-invalid"];
88
+ }
89
+
90
+ function defaultLabel(value: unknown): string {
91
+ if (value === null || value === undefined) return "";
92
+ if (typeof value === "string") return value;
93
+ if (typeof value === "number" || typeof value === "bigint") return String(value);
94
+ if (typeof value === "boolean") return value ? "Yes" : "No";
95
+ if (value instanceof Date) return value.toLocaleDateString();
96
+ return JSON.stringify(value);
97
+ }
98
+
99
+ function sameSuggestion<T>(
100
+ a: Suggestion<T> | null | undefined,
101
+ b: Suggestion<T> | null | undefined,
102
+ equals: (x: T, y: T) => boolean,
103
+ ): boolean {
104
+ if (!a || !b) return a === b;
105
+ return equals(a.value, b.value) && a.label === b.label && a.source === b.source;
106
+ }
107
+
108
+ export function SuggestedValue<T = string>({
109
+ className,
110
+ label,
111
+ suggestion,
112
+ value,
113
+ status: controlledStatus,
114
+ onAccept,
115
+ onDismiss,
116
+ onRevert,
117
+ equals = Object.is,
118
+ lowConfidenceBelow = 0.7,
119
+ announce = false,
120
+ children,
121
+ id,
122
+ "aria-describedby": describedByProp,
123
+ "aria-invalid": ariaInvalid,
124
+ ...props
125
+ }: SuggestedValueProps<T>) {
126
+ const descriptionId = useId();
127
+
128
+ const [internalStatus, setInternalStatus] = useState<SuggestionStatus>("pending");
129
+ const [previous, setPrevious] = useState<T | undefined>(undefined);
130
+
131
+ // A new suggestion starts pending, whatever happened to the last one.
132
+ // Compared by content rather than identity, so a parent that rebuilds the
133
+ // object on every keystroke does not reopen a suggestion already accepted.
134
+ const [seen, setSeen] = useState(suggestion);
135
+ if (!sameSuggestion(seen, suggestion, equals)) {
136
+ setSeen(suggestion);
137
+ setInternalStatus("pending");
138
+ }
139
+
140
+ const status = controlledStatus ?? internalStatus;
141
+ const offered = suggestion ?? null;
142
+ const pending = offered !== null && status === "pending";
143
+ const accepted = offered !== null && status === "accepted";
144
+ const edited = accepted && value !== undefined && !equals(value, offered.value);
145
+
146
+ const low = offered?.confidence !== undefined && offered.confidence < lowConfidenceBelow;
147
+ const percent =
148
+ offered?.confidence === undefined
149
+ ? null
150
+ : new Intl.NumberFormat(undefined, { style: "percent", maximumFractionDigits: 0 }).format(
151
+ offered.confidence,
152
+ );
153
+
154
+ const source = offered?.source ? ` ${offered.source}` : "";
155
+
156
+ // What the control's description says. This is how a reader who lands on
157
+ // the field learns there is a suggestion, without any announcement.
158
+ let description: ReactNode = null;
159
+ if (pending) {
160
+ description = (
161
+ <>
162
+ Suggested:{" "}
163
+ <strong className="font-medium text-foreground">
164
+ {offered.label ?? defaultLabel(offered.value)}
165
+ </strong>
166
+ {source ? `,${source}` : null}
167
+ {percent !== null ? (
168
+ <span className={cn(low && "text-warning")}>
169
+ {low ? `. Low confidence, ${percent} — worth checking` : `. ${percent} confidence`}
170
+ </span>
171
+ ) : null}
172
+ </>
173
+ );
174
+ } else if (accepted) {
175
+ description = edited
176
+ ? `Filled by AI${source ? `,${source}` : ""}, then edited`
177
+ : `Filled by AI${source ? `,${source}` : ""}`;
178
+ }
179
+
180
+ // Merged with whatever already describes the control — a FormControl's
181
+ // description and message, or the child's own — rather than replacing it.
182
+ const childDescribedBy = isValidElement(children)
183
+ ? (children.props["aria-describedby"] as string | undefined)
184
+ : undefined;
185
+ const describedBy =
186
+ [childDescribedBy, describedByProp, description !== null ? descriptionId : null]
187
+ .filter(Boolean)
188
+ .join(" ") || undefined;
189
+
190
+ const control = isValidElement(children)
191
+ ? cloneElement(children, {
192
+ id: id ?? children.props.id,
193
+ "aria-describedby": describedBy,
194
+ "aria-invalid": ariaInvalid ?? children.props["aria-invalid"],
195
+ })
196
+ : children;
197
+
198
+ const accept = () => {
199
+ if (!offered) return;
200
+ setPrevious(value);
201
+ setInternalStatus("accepted");
202
+ onAccept(offered.value, offered);
203
+ };
204
+
205
+ return (
206
+ <div
207
+ data-slot="suggested-value"
208
+ data-status={offered ? status : undefined}
209
+ data-filled-by={accepted ? "ai" : undefined}
210
+ data-edited={edited || undefined}
211
+ className={cn("flex flex-col gap-1.5", className)}
212
+ {...props}
213
+ >
214
+ {control}
215
+
216
+ {/* Present from the start and never display:none, so that with `announce`
217
+ a suggestion arriving after mount is heard — a region that enters the
218
+ accessibility tree at the same moment as its content announces
219
+ nothing. Empty, it has no height. */}
220
+ <div
221
+ data-slot="suggested-value-row"
222
+ aria-live={announce ? "polite" : undefined}
223
+ className={cn(
224
+ "flex flex-wrap items-center gap-x-3 gap-y-1 text-xs",
225
+ pending && "rounded-md border border-primary/40 bg-primary/5 px-2.5 py-1.5",
226
+ )}
227
+ >
228
+ {description !== null ? (
229
+ <p
230
+ id={descriptionId}
231
+ data-slot="suggested-value-description"
232
+ className="m-0 flex-1 text-muted-foreground"
233
+ >
234
+ {description}
235
+ </p>
236
+ ) : null}
237
+
238
+ {pending ? (
239
+ <span className="flex items-center gap-2">
240
+ <RowButton
241
+ variant="primary"
242
+ aria-label={`Accept suggestion — ${label}`}
243
+ onClick={accept}
244
+ >
245
+ Accept
246
+ </RowButton>
247
+ <RowButton
248
+ aria-label={`Dismiss suggestion — ${label}`}
249
+ onClick={() => {
250
+ setInternalStatus("dismissed");
251
+ onDismiss?.(offered);
252
+ }}
253
+ >
254
+ Dismiss
255
+ </RowButton>
256
+ </span>
257
+ ) : null}
258
+
259
+ {accepted && onRevert ? (
260
+ <RowButton
261
+ aria-label={`Undo — ${label}`}
262
+ onClick={() => {
263
+ setInternalStatus("pending");
264
+ onRevert(previous);
265
+ }}
266
+ >
267
+ Undo
268
+ </RowButton>
269
+ ) : null}
270
+ </div>
271
+ </div>
272
+ );
273
+ }
274
+
275
+ function RowButton({
276
+ className,
277
+ variant = "default",
278
+ ...props
279
+ }: ComponentPropsWithRef<"button"> & { variant?: "default" | "primary" }) {
280
+ return (
281
+ <button
282
+ type="button"
283
+ className={cn(
284
+ "rounded-md border px-2 py-0.5 text-xs font-medium transition-colors",
285
+ variant === "primary"
286
+ ? "border-primary bg-primary text-primary-foreground hover:bg-primary-hover"
287
+ : "border-input bg-background hover:bg-accent hover:text-accent-foreground",
288
+ focusRing,
289
+ disabledStyles,
290
+ className,
291
+ )}
292
+ {...props}
293
+ />
294
+ );
295
+ }
@@ -0,0 +1,6 @@
1
+ export {
2
+ SuggestedValue,
3
+ type SuggestedValueProps,
4
+ type Suggestion,
5
+ type SuggestionStatus,
6
+ } from "./ai-suggested-value";
@@ -0,0 +1,24 @@
1
+ import { defineMeta } from "@/registry/schema";
2
+
3
+ export const meta = defineMeta({
4
+ name: "ai-suggested-value",
5
+ title: "AI Suggested Value",
6
+ description:
7
+ "An AI-proposed value for any form control, pending until accepted and marked afterwards.",
8
+ category: "ai",
9
+ status: "stable",
10
+ dependencies: [],
11
+ registryDependencies: [],
12
+ files: ["ai-suggested-value.tsx"],
13
+ a11y:
14
+ "The suggestion is the control's description, so a reader who lands on the field hears it " +
15
+ "without any announcement — and announcing is opt-in, because a form filling twenty fields at " +
16
+ "once would narrate all twenty. The row that holds it is rendered from the start and never " +
17
+ "display:none, so when announcing is on, a suggestion arriving after mount is actually heard. " +
18
+ "Each button's name carries the field it belongs to, so a page of Accepts is a page of " +
19
+ "different buttons. Provenance and confidence are words with a number, never a colour. " +
20
+ "Acceptance is reported, not performed: the control is the consumer's, so its value, its " +
21
+ "label and its own keyboard behaviour are untouched — there is no shortcut hijacked from a " +
22
+ "select or a date field. The id and ARIA a FormControl passes down are forwarded to the " +
23
+ "control, and an existing aria-describedby is merged rather than replaced.",
24
+ });
@@ -0,0 +1,191 @@
1
+ "use client";
2
+
3
+ import {
4
+ useId,
5
+ useRef,
6
+ useState,
7
+ type ComponentPropsWithRef,
8
+ type KeyboardEvent,
9
+ type ReactNode,
10
+ } from "react";
11
+
12
+ import { Button } from "@/components/button";
13
+ import { Input } from "@/components/input";
14
+ import { cn } from "@/lib/utils";
15
+
16
+ /**
17
+ * Type the name to confirm.
18
+ *
19
+ * The GitHub pattern for deleting a repository, and the one every product
20
+ * copies for the action that cannot be undone: the name of the thing has to
21
+ * pass through the reader's fingers before the button works. Trivial to
22
+ * build, absent from every component library, and usually built wrong in
23
+ * the one place it matters — what happens when the text does not match.
24
+ * The common version disables the button and says nothing, so a keyboard
25
+ * or screen reader user presses it, or presses Enter, and nothing happens
26
+ * at all.
27
+ *
28
+ * Here a mismatch is a thing that is said. The button stays reachable and
29
+ * pressing it, or Enter, before the text matches announces what was
30
+ * expected and marks the field invalid. The moment the text matches, that
31
+ * is announced too, once, so a reader knows the action has become
32
+ * available without watching the button change colour.
33
+ *
34
+ * Pasting is allowed. Blocking it is a popular piece of friction, and it
35
+ * punishes exactly the people who cannot type a long name easily — switch
36
+ * users, voice users, anyone with a tremor — while stopping nobody who can
37
+ * select-all and copy. The name is on screen either way; the point is that
38
+ * it passed through the reader's attention, not their keyboard.
39
+ */
40
+
41
+ export interface ConfirmTypedProps extends Omit<ComponentPropsWithRef<"div">, "children"> {
42
+ /** The text that must be typed, exactly. */
43
+ expected: string;
44
+ /** What confirming does, as the button reads: "Delete project". */
45
+ action: string;
46
+ onConfirm: () => void;
47
+ /** While the action runs. The button stays focusable and says so. */
48
+ pending?: boolean;
49
+ /** Match regardless of case. Off by default: a name is a name. */
50
+ caseSensitive?: boolean;
51
+ variant?: "destructive" | "primary";
52
+ /** The typed text. Controlled, if you want it. */
53
+ value?: string;
54
+ onValueChange?: (value: string) => void;
55
+ /** The consequence, in a sentence or two, before the field. */
56
+ children?: ReactNode;
57
+ }
58
+
59
+ export function ConfirmTyped({
60
+ className,
61
+ expected,
62
+ action,
63
+ onConfirm,
64
+ pending = false,
65
+ caseSensitive = true,
66
+ variant = "destructive",
67
+ value: valueProp,
68
+ onValueChange,
69
+ children,
70
+ ...props
71
+ }: ConfirmTypedProps) {
72
+ const id = useId();
73
+ const inputRef = useRef<HTMLInputElement>(null);
74
+
75
+ const [uncontrolled, setUncontrolled] = useState("");
76
+ const value = valueProp ?? uncontrolled;
77
+
78
+ const [attempted, setAttempted] = useState(false);
79
+ const [announcement, setAnnouncement] = useState("");
80
+
81
+ // Surrounding whitespace never decides it: a trailing space is not a
82
+ // different name, and a reader cannot see it to know why it failed.
83
+ const normalise = (text: string) => (caseSensitive ? text.trim() : text.trim().toLowerCase());
84
+ const matched = value.length > 0 && normalise(value) === normalise(expected);
85
+
86
+ // Announce the match once, on the transition. Adjusted during render so the
87
+ // announcement lands in the same commit as the state it describes.
88
+ const [wasMatched, setWasMatched] = useState(matched);
89
+ if (wasMatched !== matched) {
90
+ setWasMatched(matched);
91
+ if (matched) {
92
+ setAnnouncement(`Matches. ${action} is available.`);
93
+ setAttempted(false);
94
+ }
95
+ }
96
+
97
+ const setValue = (next: string) => {
98
+ setUncontrolled(next);
99
+ onValueChange?.(next);
100
+ };
101
+
102
+ const attempt = () => {
103
+ if (pending) return;
104
+ if (matched) {
105
+ onConfirm();
106
+ return;
107
+ }
108
+ // Said, not swallowed. A press that does nothing tells a keyboard user
109
+ // nothing; this one tells them what was expected.
110
+ setAttempted(true);
111
+ setAnnouncement(`Does not match. Type ${expected} exactly.`);
112
+ inputRef.current?.focus();
113
+ };
114
+
115
+ const onKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
116
+ if (event.key !== "Enter" || event.nativeEvent.isComposing) return;
117
+ event.preventDefault();
118
+ attempt();
119
+ };
120
+
121
+ const mismatch = attempted && !matched;
122
+ const hint = matched
123
+ ? "Matches."
124
+ : mismatch
125
+ ? `Does not match. Type ${expected} exactly.`
126
+ : caseSensitive
127
+ ? "Type it exactly as shown."
128
+ : "Type it as shown. Case does not matter.";
129
+
130
+ return (
131
+ <div
132
+ data-slot="confirm-typed"
133
+ data-state={matched ? "matched" : mismatch ? "mismatched" : "idle"}
134
+ className={cn("flex flex-col gap-2", className)}
135
+ {...props}
136
+ >
137
+ {children}
138
+
139
+ <label htmlFor={`${id}-input`} className="text-sm">
140
+ To confirm, type{" "}
141
+ <strong className="rounded bg-muted px-1 py-0.5 font-mono text-xs font-medium select-all">
142
+ {expected}
143
+ </strong>
144
+ </label>
145
+
146
+ <Input
147
+ ref={inputRef}
148
+ id={`${id}-input`}
149
+ value={value}
150
+ autoComplete="off"
151
+ autoCapitalize="off"
152
+ spellCheck={false}
153
+ aria-describedby={`${id}-hint`}
154
+ aria-invalid={mismatch || undefined}
155
+ onChange={(event) => {
156
+ setValue(event.target.value);
157
+ }}
158
+ onKeyDown={onKeyDown}
159
+ />
160
+
161
+ <p
162
+ id={`${id}-hint`}
163
+ data-slot="confirm-typed-hint"
164
+ className={cn(
165
+ "text-xs",
166
+ mismatch ? "text-destructive" : matched ? "text-success" : "text-muted-foreground",
167
+ )}
168
+ >
169
+ {hint}
170
+ </p>
171
+
172
+ {/* Present from the first render, so the first announcement is heard. */}
173
+ <span role="status" aria-live="polite" className="sr-only">
174
+ {announcement}
175
+ </span>
176
+
177
+ <Button
178
+ variant={variant}
179
+ loading={pending}
180
+ data-ready={matched || undefined}
181
+ aria-describedby={matched ? undefined : `${id}-hint`}
182
+ // Dimmed rather than disabled while the text does not match: it stays
183
+ // reachable, and pressing it says why nothing happened.
184
+ className={cn("self-start", !matched && !pending && "opacity-55")}
185
+ onClick={attempt}
186
+ >
187
+ {pending ? `${action}…` : action}
188
+ </Button>
189
+ </div>
190
+ );
191
+ }
@@ -0,0 +1 @@
1
+ export { ConfirmTyped, type ConfirmTypedProps } from "./confirm-typed";
@@ -0,0 +1,22 @@
1
+ import { defineMeta } from "@/registry/schema";
2
+
3
+ export const meta = defineMeta({
4
+ name: "confirm-typed",
5
+ title: "Confirm Typed",
6
+ description: "Type the name to confirm — and be told, not ignored, when it does not match.",
7
+ category: "form",
8
+ status: "stable",
9
+ dependencies: [],
10
+ registryDependencies: ["button", "input"],
11
+ files: ["confirm-typed.tsx"],
12
+ a11y:
13
+ "The button stays reachable while the text does not match, dimmed rather than disabled, and " +
14
+ "pressing it or Enter announces what was expected, marks the field invalid and returns focus " +
15
+ "to it — the common version disables the button and says nothing, so a keyboard user cannot " +
16
+ "tell whether anything happened. The match is announced once, on the transition, naming the " +
17
+ "action that became available, so nobody has to watch the button change colour. Typing " +
18
+ "itself stays silent, because a verdict on every keystroke is noise. The hint is the field's " +
19
+ "description and the button's, and the status region exists from the first render so the " +
20
+ "first announcement is heard. Pasting is allowed: blocking it punishes people who cannot " +
21
+ "type a long name easily and stops nobody who can select-all and copy.",
22
+ });