@kennycorrea/ui 1.8.1 → 1.8.2

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.8.2 — 2026-10-30
4
+
5
+ A live validation status on `FormField`.
6
+
7
+ - **`status` and `statusMessage` on `FormField`**, for checks run as the user types, such as a username being looked up. `checking` shows a spinner and muted text; `valid` shows a check icon and text in `--color-good-ink` (jade-11, at least 4.5:1 on the panel in light and dark); `invalid` looks and behaves like `error`. The icon and the words carry the meaning, not the colour. While shown, the message replaces the hint and is linked through `aria-describedby`. `error` wins when both are set.
8
+ - **`announce`** repeats the message in a polite `role="status"` region. The region starts empty, so nothing is announced on first render, only changes. Off by default.
9
+ - Tests in `test/FormField-status.test.tsx`; demo in `examples/UsernameField.tsx`.
10
+
3
11
  ## 1.8.1 — 2026-10-30
4
12
 
5
13
  TabBar label contrast.
@@ -12,6 +12,19 @@ export interface FormFieldProps {
12
12
  hint?: ReactNode;
13
13
  /** The validation message. Marks the control invalid and is linked with `aria-describedby`. */
14
14
  error?: ReactNode;
15
+ /**
16
+ * A live check's state — a username looked up as it is typed. `checking` shows a spinner,
17
+ * `valid` a check, `invalid` the same red state as `error`. `error` wins when both are set.
18
+ * While shown, `statusMessage` replaces the hint.
19
+ */
20
+ status?: FormFieldStatus;
21
+ /** The words for `status`. The icon and these carry the meaning, never the colour alone. */
22
+ statusMessage?: ReactNode;
23
+ /**
24
+ * Repeat `statusMessage` in a polite live region (`role="status"`). Only changes are
25
+ * announced, never what was there on first render.
26
+ */
27
+ announce?: boolean;
15
28
  required?: boolean;
16
29
  /** Defaults to a generated id. */
17
30
  id?: string;
@@ -28,6 +41,7 @@ export interface FormFieldProps {
28
41
  children: ReactElement | ((control: FormFieldControlProps) => ReactNode);
29
42
  className?: string;
30
43
  }
44
+ export type FormFieldStatus = 'checking' | 'valid' | 'invalid';
31
45
  /**
32
46
  * A label, a control, a hint and an error, wired together: the label names the control, the
33
47
  * hint and error describe it, and the error marks it invalid.
@@ -35,7 +49,7 @@ export interface FormFieldProps {
35
49
  * The error is text with an icon, never colour alone. It is not a live region: announcing each
36
50
  * field's error as it appears is noisy, and `ErrorSummary` says it once, on submit.
37
51
  */
38
- export declare function FormField({ label, hint, error, required, id: idProp, layout, children, className, }: FormFieldProps): import("react").JSX.Element;
52
+ export declare function FormField({ label, hint, error, status, statusMessage, announce, required, id: idProp, layout, children, className, }: FormFieldProps): import("react").JSX.Element;
39
53
  export interface FormFieldsetProps {
40
54
  /** Names the group: "Notifications", "Plan". */
41
55
  legend: ReactNode;
@@ -1,6 +1,7 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
- import { ExclamationTriangleIcon } from '@radix-ui/react-icons';
3
- import { cloneElement, isValidElement, useId } from 'react';
2
+ import { CheckCircledIcon, ExclamationTriangleIcon } from '@radix-ui/react-icons';
3
+ import { Spinner as ThemedSpinner } from '@radix-ui/themes';
4
+ import { cloneElement, isValidElement, useEffect, useId, useRef, useState, } from 'react';
4
5
  import { Input } from '../../atoms/Input/Input.js';
5
6
  import { Label } from '../../atoms/Label/Label.js';
6
7
  import { Select } from '../../atoms/Select/Select.js';
@@ -14,16 +15,29 @@ const INVALID_PROP = new Set([Input, Textarea, Select, PasswordInput]);
14
15
  * The error is text with an icon, never colour alone. It is not a live region: announcing each
15
16
  * field's error as it appears is noisy, and `ErrorSummary` says it once, on submit.
16
17
  */
17
- export function FormField({ label, hint, error, required, id: idProp, layout = 'stacked', children, className, }) {
18
+ export function FormField({ label, hint, error, status, statusMessage, announce, required, id: idProp, layout = 'stacked', children, className, }) {
18
19
  const auto = useId();
19
20
  const id = idProp ?? auto;
20
21
  const hintId = `${id}-hint`;
21
22
  const errorId = `${id}-error`;
22
- const describedBy = [hint ? hintId : null, error ? errorId : null].filter(Boolean).join(' ');
23
+ const statusId = `${id}-status`;
24
+ // The error wins; a status with words stands in for the hint while it is shown.
25
+ const shownStatus = !error && status ? status : undefined;
26
+ const showStatus = !!shownStatus && statusMessage != null && statusMessage !== false;
27
+ const showHint = !!hint && !showStatus;
28
+ const invalid = !!error || shownStatus === 'invalid';
29
+ const describedBy = [
30
+ showHint ? hintId : null,
31
+ showStatus ? statusId : null,
32
+ error ? errorId : null,
33
+ ]
34
+ .filter(Boolean)
35
+ .join(' ');
36
+ const announced = useAnnouncement(showStatus ? statusMessage : null, shownStatus, statusMessage);
23
37
  const control = {
24
38
  id,
25
39
  'aria-describedby': describedBy || undefined,
26
- 'aria-invalid': error ? true : undefined,
40
+ 'aria-invalid': invalid ? true : undefined,
27
41
  required: required || undefined,
28
42
  };
29
43
  let rendered;
@@ -35,7 +49,7 @@ export function FormField({ label, hint, error, required, id: idProp, layout = '
35
49
  // Passing the key at all, even as undefined, would override theirs, so it is left out.
36
50
  const { 'aria-invalid': ariaInvalid, ...rest } = control;
37
51
  const props = INVALID_PROP.has(children.type)
38
- ? { ...rest, invalid: !!error }
52
+ ? { ...rest, invalid }
39
53
  : { ...rest, ...(ariaInvalid ? { 'aria-invalid': ariaInvalid } : {}) };
40
54
  rendered = cloneElement(children, props);
41
55
  }
@@ -43,13 +57,40 @@ export function FormField({ label, hint, error, required, id: idProp, layout = '
43
57
  rendered = children;
44
58
  }
45
59
  const labelEl = (_jsx(Label, { htmlFor: id, required: required, children: label }));
46
- const hintEl = hint ? (_jsx("p", { id: hintId, className: "ui-field__hint", children: hint })) : null;
47
- const errorEl = error ? _jsx(FieldError, { id: errorId, children: error }) : null;
48
- return (_jsx("div", { className: ['ui-field', className].filter(Boolean).join(' '), "data-layout": layout, children: layout === 'inline' ? (_jsxs(_Fragment, { children: [_jsx("span", { className: "ui-field__control", children: rendered }), _jsxs("span", { className: "ui-field__text", children: [labelEl, hintEl, errorEl] })] })) : (_jsxs(_Fragment, { children: [labelEl, hintEl, rendered, errorEl] })) }));
60
+ const hintEl = showHint ? (_jsx("p", { id: hintId, className: "ui-field__hint", children: hint })) : null;
61
+ const errorEl = error ? (_jsx(FieldError, { id: errorId, children: error })) : showStatus && shownStatus ? (_jsx(FieldStatus, { id: statusId, status: shownStatus, children: statusMessage })) : null;
62
+ // Always mounted while opted in, so a change is a change to an existing region.
63
+ const liveEl = announce ? (_jsx("span", { role: "status", className: "ui-sr-only", children: announced })) : null;
64
+ return (_jsx("div", { className: ['ui-field', className].filter(Boolean).join(' '), "data-layout": layout, children: layout === 'inline' ? (_jsxs(_Fragment, { children: [_jsx("span", { className: "ui-field__control", children: rendered }), _jsxs("span", { className: "ui-field__text", children: [labelEl, hintEl, errorEl] }), liveEl] })) : (_jsxs(_Fragment, { children: [labelEl, hintEl, rendered, errorEl, liveEl] })) }));
49
65
  }
50
66
  function FieldError({ id, children }) {
51
67
  return (_jsxs("p", { id: id, className: "ui-field__error", children: [_jsx(ExclamationTriangleIcon, { "aria-hidden": "true" }), _jsx("span", { children: children })] }));
52
68
  }
69
+ function FieldStatus({ id, status, children, }) {
70
+ if (status === 'invalid')
71
+ return _jsx(FieldError, { id: id, children: children });
72
+ return (_jsxs("p", { id: id, className: "ui-field__status", "data-status": status, children: [status === 'checking' ? (_jsx("span", { "aria-hidden": "true", className: "ui-field__status-icon", children: _jsx(ThemedSpinner, { size: "1" }) })) : (_jsx(CheckCircledIcon, { "aria-hidden": "true" })), _jsx("span", { children: children })] }));
73
+ }
74
+ /**
75
+ * What the live region holds: empty on first render, then the message whenever the status or
76
+ * message changes. A region that mounts with text is announced by some screen readers and not
77
+ * others, so it mounts empty and only ever receives changes.
78
+ */
79
+ function useAnnouncement(message, status, source) {
80
+ const [text, setText] = useState(null);
81
+ const first = useRef(true);
82
+ const latest = useRef(message);
83
+ latest.current = message;
84
+ // biome-ignore lint/correctness/useExhaustiveDependencies: keyed on what changed, read through the ref.
85
+ useEffect(() => {
86
+ if (first.current) {
87
+ first.current = false;
88
+ return;
89
+ }
90
+ setText(latest.current);
91
+ }, [status, source]);
92
+ return text;
93
+ }
53
94
  /**
54
95
  * A `fieldset` for controls that answer one question together. The legend names the group;
55
96
  * hint and error describe it.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kennycorrea/ui",
3
- "version": "1.8.1",
3
+ "version": "1.8.2",
4
4
  "description": "A React + TypeScript component library built on Radix Themes: atoms, a Tailwind token bridge and theming.",
5
5
  "private": false,
6
6
  "type": "module",
package/tokens.css CHANGED
@@ -1274,6 +1274,10 @@
1274
1274
  .ui-field__hint { margin: 0; font-size: 13px; color: var(--color-ink-muted); }
1275
1275
  .ui-field__error { display: flex; align-items: flex-start; gap: 6px; margin: 0; font-size: 13px; font-weight: 500; color: var(--red-11); }
1276
1276
  .ui-field__error svg { flex: none; margin-block-start: 2px; }
1277
+ .ui-field__status { display: flex; align-items: flex-start; gap: 6px; margin: 0; font-size: 13px; color: var(--color-ink-muted); }
1278
+ .ui-field__status[data-status="valid"] { font-weight: 500; color: var(--color-good-ink); }
1279
+ .ui-field__status svg, .ui-field__status-icon { flex: none; margin-block-start: 2px; }
1280
+ .ui-field__status-icon { display: inline-flex; }
1277
1281
  .ui-fieldset { display: flex; flex-direction: column; gap: 8px; min-inline-size: 0; margin: 0; padding: 0; border: 0; }
1278
1282
  .ui-fieldset__legend { padding: 0; margin-block-end: 2px; font-size: 14px; font-weight: 500; color: var(--color-ink); }
1279
1283