Field wires a label, a description and an error message to a form control; Form runs the form's action. There is no validation engine: the browser's own constraint validation (required, type, pattern, minLength, …) is the truth, the platform's :user-invalid heuristic decides when an error is shown, and the browser's localised message is the default text. What the library adds is the wiring the platform does not do (ids, aria-describedby, an explicit aria-invalid), a styleable error element instead of the native bubble, the first-invalid focus, and the asynchronous half: the submission runs in a transition with data-pending, server errors land on their fields and go away on the next edit, and the form is reset only when the action reported no errors.
"use client";
import { useActionState } from "react";
import { Field } from "@funstack/ui/field";
import { Form } from "@funstack/ui/form";
import type { FormErrors } from "@funstack/ui/form";
import { sleep } from "./fake-api.js";
interface SignUpState {
readonly errors?: FormErrors;
readonly created?: string;
}
// A pretend Server Function. It validates on the server and returns field errors keyed by
// name; it never throws for bad input, which is what `useActionState` is for.
async function signUp(_prev: SignUpState, formData: FormData): Promise<SignUpState> {
await sleep(900);
const email = String(formData.get("email"));
const handle = String(formData.get("handle"));
const errors: Record<string, string> = {};
if (email.toLowerCase() === "taken@example.com") {
errors["email"] = "This email is already registered.";
}
if (handle.toLowerCase().includes("admin")) errors["handle"] = "That handle is reserved.";
if (Object.keys(errors).length > 0) return { errors };
return { created: handle };
}
// Try: submit empty (the browser's messages, the first field focused); a short password; a
// mismatched confirmation (re-checked when either field changes); "taken@example.com" or a
// handle containing "admin" (server errors, focused, gone on the next keystroke). A successful
// sign-up resets the form; a failed one keeps every value.
export function FieldSignUp() {
const [state, formAction] = useActionState(signUp, {});
return (
<Form.Root action={formAction} errors={state.errors} className="demo-form">
<Field.Root name="email" required className="demo-fieldset">
<Field.Label className="demo-label">Email</Field.Label>
<Field.Control type="email" autoComplete="off" className="demo-input" />
<Field.Description className="demo-description">We never share it.</Field.Description>
<Field.Error className="demo-field-error" />
</Field.Root>
<Field.Root name="handle" required className="demo-fieldset">
<Field.Label className="demo-label">Handle</Field.Label>
<Field.Control
pattern="[a-z0-9_]+"
autoComplete="off"
className="demo-input"
aria-describedby="handle-hint"
/>
<p id="handle-hint" className="demo-description">
Lowercase letters, digits and underscores.
</p>
<Field.Error className="demo-field-error">
{({ validity, messages }) =>
validity.patternMismatch
? "Use lowercase letters, digits and underscores."
: messages.join(" ")
}
</Field.Error>
</Field.Root>
<Field.Root name="password" required className="demo-fieldset">
<Field.Label className="demo-label">Password</Field.Label>
<Field.Control
type="password"
minLength={8}
autoComplete="new-password"
className="demo-input"
/>
<Field.Error className="demo-field-error">
{({ validity, messages }) =>
validity.tooShort ? "Use at least 8 characters." : messages.join(" ")
}
</Field.Error>
</Field.Root>
<Field.Root
name="confirm"
required
className="demo-fieldset"
validate={(value, formData) =>
value === formData.get("password") ? null : "Passwords do not match."
}
>
<Field.Label className="demo-label">Confirm password</Field.Label>
<Field.Control type="password" autoComplete="new-password" className="demo-input" />
<Field.Error className="demo-field-error" />
</Field.Root>
<div className="demo-row">
<Form.Submit className="demo-button demo-primary demo-submit">
{({ pending }) => (
<>
{pending && <span className="demo-spinner" aria-hidden />}
{pending ? "Creating…" : "Create account"}
</>
)}
</Form.Submit>
<button type="reset" className="demo-button">
Reset
</button>
</div>
{state.created !== undefined && (
<output className="demo-readout">
Created <code>@{state.created}</code> (the form was reset).
</output>
)}
</Form.Root>
);
}/* Field and Form. Every Field part carries data-invalid / data-required / data-disabled /
data-pending; the control also matches the platform's :user-invalid and :user-valid, which
work before hydration too. */
.demo-form {
display: grid;
gap: 1rem;
max-inline-size: 26rem;
}
.demo-fieldset {
display: grid;
gap: 0.3rem;
}
/* A checkbox or switch first, then its label. */
.demo-fieldset-inline {
grid-template-columns: auto 1fr;
align-items: center;
column-gap: 0.6em;
}
.demo-fieldset-inline .demo-field-error {
grid-column: 1 / -1;
}
.demo-label {
font-weight: 600;
color: var(--color-text);
}
/* `/ ""`: the asterisk stays out of the control's accessible name; the control exposes
`required` itself. */
.demo-fieldset[data-required] > .demo-label::after {
content: " *" / "";
color: #dc2626;
}
.demo-input {
padding: 0.4em 0.6em;
border: 1px solid var(--color-border);
border-radius: 6px;
background: var(--color-bg);
color: var(--color-text);
font: inherit;
}
.demo-input:user-valid {
border-color: #16a34a;
}
.demo-input[data-invalid] {
border-color: #dc2626;
}
/* No JS auto-resize: the textarea grows with its content. */
.demo-grow {
field-sizing: content;
min-block-size: 2lh;
max-block-size: 8lh;
resize: none;
}
.demo-description {
margin: 0;
font-size: 0.85rem;
color: var(--color-text-secondary);
}
/* Rendered only while an error is shown: enter with @starting-style, leave instantly. */
.demo-field-error {
font-size: 0.85rem;
color: #dc2626;
opacity: 1;
transition: opacity 150ms;
@starting-style {
opacity: 0;
}
}
/* Form.Error: the form-level error (a failed submission), rendered only while there is one.
Dimmed while the retry is in flight (`data-pending`); the message stays until the result. */
.demo-form-error {
padding: 0.5rem 0.75rem;
border: 1px solid #fca5a5;
border-radius: 6px;
background: #fef2f2;
color: #b91c1c;
font-size: 0.9rem;
opacity: 1;
transition: opacity 150ms;
@starting-style {
opacity: 0;
}
}
.demo-form-error[data-pending] {
opacity: 0.6;
}
.demo-radio-row {
display: flex;
gap: 1.2em;
margin: 0;
padding: 0;
border: 0;
}
/* A native fieldset around several Fields (no Fieldset part). Its `disabled` is inherited by
every control, which `data-disabled` does not see: select the platform's `:disabled`. */
.demo-address {
display: grid;
gap: 1rem;
margin: 0;
padding: 0.75rem 1rem 1rem;
border: 1px solid var(--color-border);
border-radius: 8px;
min-inline-size: 0;
}
.demo-address > legend {
padding: 0 0.3em;
}
.demo-address .demo-fieldset:has(:disabled) {
opacity: 0.5;
}
/* The form's submission: the whole form dims, the button blocks repeat clicks with CSS. */
.demo-form[data-pending] .demo-fieldset {
opacity: 0.7;
}
.demo-submit[data-pending] {
pointer-events: none;
}
@media (prefers-reduced-motion: reduce) {
.demo-field-error,
.demo-form-error {
transition: none;
}
}import { Field } from "@funstack/ui/field";
import { Form } from "@funstack/ui/form";
<Form.Root action={formAction} errors={state.errors} error={state.error}> {/* form */}
<Form.Error /> {/* div, only while error is set */}
<Field.Root name="email" required> {/* div */}
<Field.Label>Email</Field.Label> {/* label[for] */}
<Field.Control type="email" autoComplete="email" /> {/* input */}
<Field.Description>We never share it.</Field.Description> {/* p[id] */}
<Field.Error /> {/* div[id], only while invalid */}
</Field.Root>
<Field.Root name="newsletter">
<Field.Label>Newsletter</Field.Label>
<Checkbox /> {/* any FUNSTACK form control */}
</Field.Root>
<Form.Submit>Create account</Form.Submit> {/* button[type=submit] */}
</Form.Root>Field.Root is a div that owns the ids and the field's state. name, required and disabled go to the control unless it sets its own.Field.Control is a native <input>, or a <textarea> / <select> through render. Every other input prop passes through (type, defaultValue, a controlled value / onChange, pattern, …). Outside a Field.Root it is the plain control (on its own). There is no Input, Textarea or Fieldset part: the native elements, with the textarea and fieldset recipes below.Field.Label through aria-labelledby instead of for. A RadioGroup carries aria-invalid on its radiogroup; a CheckboxGroup and a Slider on every item, because ARIA 1.2 does not support it on a group.Field.Error is rendered only while an error is shown. Write it once, unconditionally; its text is the browser's message, a server error, or what your function children return. An error that appears on blur can shift the layout, so a click right after typing (the click's press is the blur) may miss a button that moved (see Reserve space for the error).Field.Description id while one is mounted, the Field.Error id while one is mounted and shown, a group's aria-labelledby to Field.Label while one is mounted. A part rendered later (behind a condition or a Suspense boundary) is added when it mounts, so no reference points at a missing element. The server HTML references every part, rendered or not; hydration drops the ones that are not there.Form.Error is rendered only while the form has a form-level error (Form.Root error): a failed submission that is not about one field. See Form-level errors.A field shows an error when its control matches :user-invalid or the Form has a server error for it:
:user-invalid starts matching after the user changed the value and left the field, or after a submission attempt; it then updates live on every keystroke and stops after a form reset. There is no validationMode: this is the heuristic every native form already uses.Field.Error is mounted), renders the message, and the first invalid control in document order is focused. Its description, error included, is read at focus. When focus is already there (Enter pressed in that field), the message is announced with ariaNotify() instead, where the browser has it. A field without Field.Error keeps the browser's bubble; so does every field before hydration.form.checkValidity() shows nothing and reportValidity() shows the browser's bubble, as without the library.onChange on a controlled control runs first; the browser's validity follows the new value at once. So no Field part takes a transition prop, Field.Error animates its entry with CSS @starting-style, and content rendered inside it must not suspend.<Field.Root
name="confirm"
required
validate={(value, formData) => (value === formData.get("password") ? null : "Passwords do not match.")}
>validate is synchronous and receives the field's value (formData.get(name) as a string) and the whole form's FormData. Its message is written into the control with setCustomValidity, so it blocks submission, matches :invalid / :user-invalid and is shown like a native constraint. It runs at mount, on every input / change anywhere in the form and on a FUNSTACK control's value change (cross-field rules stay current), on blur, after a reset, on a submission attempt, and whenever the prop changes, so a rule that reads outside state (a list that loads, a value in React state) updates on render. An inline function is a new prop on every render and so re-runs each time; wrap it in useCallback to re-run it only when its inputs change. A field with validate is rendered from a client component (a function is not serialisable).
details.valid<Field.Root name="title" required>
<Field.Label>Title</Field.Label>
<TextField
defaultValue={task.title}
valueCommitAction={async (next, details) => {
if (!details.valid) return; // nothing saved; the error stays next to the text
await saveTitle(next);
}}
/>
<Field.Error />
</Field.Root>Every value action of a form control (Checkbox, Switch, RadioGroup, CheckboxGroup, Select, MultiSelect, Combobox, TextField, NumberField, Slider, and OTPField's completeAction) gets details.valid: the control would be valid holding the value the action is called with. It covers the control's native constraints (required, pattern, minLength, min / max / step, a group's "at least one") and, inside a Field, the Field's validate for that value. It does not cover a server error (that is about a value already submitted, and the change is the edit that dismisses it).
It is information, not a gate: the action is always called, also for an invalid value, and the Field shows the error. An action that saves checks it; the control keeps showing what the user entered, and nothing is saved. In an uncontrolled TextField or NumberField, typed text that is invalid stays a draft: it is not written, so the saved value stays what a later failed save rolls back to (and the browser keeps flagging minLength, which it checks only for edited text). The other controls have no draft and keep an invalid value as they keep any other (a required checkbox can be unchecked). A controlled parent that does not adopt the value rolls back instead. valid is an ordinary boolean field, so a Server Function passed directly receives it. Handlers (onValueChange, onInputValueChange) and the actions of components that are not form controls (Toggle, Tabs, menus) have none.
With a function action (a Server Function, a useActionState dispatch, or a client function), Form.Root renders it on the <form> unwrapped, so before hydration the form posts to the Server Function. After hydration it runs the action itself, inside its own transition:
data-pending is on the form, on Form.Submit and on every Field.Root (and its parts) while the submission is in flight, including the Suspense wait of what the result re-renders. useFormStatus() works as usual in your own components, also through a portal.errors (keyed by field name, usually state.errors from useActionState) reach each field. A server error is shown at once, written into the control's custom validity (a resubmission of the same value is blocked, with the usual focus), and dismissed by the user's next edit. The next settled submission shows its errors again, even identical strings.error (Form-level errors). An entry of errors whose key matches no Field is shown by nothing and focuses nothing, but still counts as an error for autoReset and a dialog's close (an error summary of your own can rely on that); it is not the form-level channel, and a misspelt field name ends up there silently.ariaNotify() when focus is already on it). Errors present at mount (a progressive-enhancement post-back, a Server Component prop) are shown without moving focus.form.reset()) only when the submission settles with no error: no entry in errors and an empty error; pass autoReset={false} to keep the values (a settings form).Form.Root; nothing is reset. Return errors from the action to keep the form. The rejection of a submission a later one superseded, or of one that settles after the form unmounted or was hidden, goes to reportError instead, like every other action (see Action props).useActionState dispatch queues it. Block repeat clicks with CSS on Form.Submit[data-pending] if you need to; the button is never disabled.error and Form.Errorconst [state, formAction] = useActionState(createTask, {});
// state: { errors?: { title?: string }, error?: string }
<Form.Root action={formAction} errors={state.errors} error={state.error}>
<Form.Error className="form-error" />
<Field.Root name="title" required>…</Field.Root>
<Form.Submit>Create</Form.Submit>
</Form.Root>A failure that is not about one field ("the server is unavailable") is the error prop, with the type of one entry of errors (a string, several strings, or nothing). The two props are the two kinds of failure:
| errors[name] | error | |
|---|---|---|
| Says | this field's value is wrong | the submission failed |
| Shown by | the Field.Error of the Field with that name | Form.Error |
| Custom validity | written on the control while shown | never |
| Submitting the same values again | blocked until the field is edited | allowed: that is the retry |
| Dismissed by | the field's next edit | nothing; replaced by the next result |
| At the settle | the first such field is focused | announced, no focus move |
autoReset, Dialog.Form close | prevented | prevented |
Form.Error mirrors the prop: it renders its <div> exactly while error has a message, at mount (a post-back), after a settle, and while the next submission is in flight (with data-pending, to dim it).error, the Form speaks the messages through ariaNotify() on the form, once per settle, also when the text is the same as last time (a second failed retry is news). Only while a Form.Error is mounted: if you render the message yourself, you own its announcement. When a field error is announced in the same settle (focus was already on that field), the two are one announcement, the form-level text first. Nothing is announced at mount.role="alert" and no aria-live: a live region would read the message twice.Form.Error parts may be mounted (top and bottom of a long form); the announcement is still one. error is serialisable, so a Server Component may pass it, and the part is plain markup in the server HTML.error is for a failure the action caught and returned.Dialog.Form rendered as Form.Root"use client";
import { useActionState } from "react";
import { Dialog } from "@funstack/ui/dialog";
import { Field } from "@funstack/ui/field";
import { Form } from "@funstack/ui/form";
import type { FieldErrorValue, FormErrors } from "@funstack/ui/form";
import { sleep } from "./fake-api.js";
interface TaskState {
readonly errors?: FormErrors;
readonly error?: FieldErrorValue;
}
let attempts = 0;
// A pretend Server Function with two kinds of failure. "Groceries" already exists: a field
// error, about that value, which blocks the same title until it is edited. Every other first
// attempt "cannot reach the server": a form-level error, which keeps the dialog open and lets
// the same values be submitted again (the retry succeeds).
async function createTask(_prev: TaskState, formData: FormData): Promise<TaskState> {
await sleep(700);
const title = String(formData.get("title"));
if (title.toLowerCase() === "groceries")
return { errors: { title: "That task already exists." } };
attempts += 1;
if (attempts % 2 === 1) return { error: "Could not reach the server. Try again." };
return {};
}
// `useActionState` lives inside the Popup: its content is unmounted while the dialog is closed,
// so the next open starts with no error. Held by the component that renders `Dialog.Root`, the
// state would outlive the close and the next open would show the last errors on an empty form.
function NewTaskForm() {
const [state, formAction] = useActionState(createTask, {});
return (
<Dialog.Form
action={formAction}
render={<Form.Root errors={state.errors} error={state.error} />}
className="demo-dialog-form"
>
<Form.Error className="demo-form-error" />
<Field.Root name="title" required className="demo-fieldset">
<Field.Label className="demo-label">Title</Field.Label>
<Field.Control autoComplete="off" className="demo-input" />
<Field.Error className="demo-field-error" />
</Field.Root>
<div className="demo-dialog-actions">
<Dialog.Close className="demo-button">Cancel</Dialog.Close>
<Form.Submit className="demo-button demo-primary">
{({ pending }) => (
<>
{pending && <span className="demo-spinner" aria-hidden />}
Create
</>
)}
</Form.Submit>
</div>
</Dialog.Form>
);
}
export function FieldNewTask() {
return (
// `defaultOpen={false}` selects library mode, in which the Popup's content is unmounted
// while closed (in JS-free mode it is always mounted).
<Dialog.Root defaultOpen={false}>
<Dialog.Trigger className="demo-button">New task</Dialog.Trigger>
<Dialog.Popup className="demo-dialog">
<Dialog.Title>New task</Dialog.Title>
<Dialog.Description>
The first save fails as if offline; Create again retries. "Groceries" is taken.
</Dialog.Description>
<NewTaskForm />
</Dialog.Popup>
</Dialog.Root>
);
}/* Field and Form. Every Field part carries data-invalid / data-required / data-disabled /
data-pending; the control also matches the platform's :user-invalid and :user-valid, which
work before hydration too. */
.demo-form {
display: grid;
gap: 1rem;
max-inline-size: 26rem;
}
.demo-fieldset {
display: grid;
gap: 0.3rem;
}
/* A checkbox or switch first, then its label. */
.demo-fieldset-inline {
grid-template-columns: auto 1fr;
align-items: center;
column-gap: 0.6em;
}
.demo-fieldset-inline .demo-field-error {
grid-column: 1 / -1;
}
.demo-label {
font-weight: 600;
color: var(--color-text);
}
/* `/ ""`: the asterisk stays out of the control's accessible name; the control exposes
`required` itself. */
.demo-fieldset[data-required] > .demo-label::after {
content: " *" / "";
color: #dc2626;
}
.demo-input {
padding: 0.4em 0.6em;
border: 1px solid var(--color-border);
border-radius: 6px;
background: var(--color-bg);
color: var(--color-text);
font: inherit;
}
.demo-input:user-valid {
border-color: #16a34a;
}
.demo-input[data-invalid] {
border-color: #dc2626;
}
/* No JS auto-resize: the textarea grows with its content. */
.demo-grow {
field-sizing: content;
min-block-size: 2lh;
max-block-size: 8lh;
resize: none;
}
.demo-description {
margin: 0;
font-size: 0.85rem;
color: var(--color-text-secondary);
}
/* Rendered only while an error is shown: enter with @starting-style, leave instantly. */
.demo-field-error {
font-size: 0.85rem;
color: #dc2626;
opacity: 1;
transition: opacity 150ms;
@starting-style {
opacity: 0;
}
}
/* Form.Error: the form-level error (a failed submission), rendered only while there is one.
Dimmed while the retry is in flight (`data-pending`); the message stays until the result. */
.demo-form-error {
padding: 0.5rem 0.75rem;
border: 1px solid #fca5a5;
border-radius: 6px;
background: #fef2f2;
color: #b91c1c;
font-size: 0.9rem;
opacity: 1;
transition: opacity 150ms;
@starting-style {
opacity: 0;
}
}
.demo-form-error[data-pending] {
opacity: 0.6;
}
.demo-radio-row {
display: flex;
gap: 1.2em;
margin: 0;
padding: 0;
border: 0;
}
/* A native fieldset around several Fields (no Fieldset part). Its `disabled` is inherited by
every control, which `data-disabled` does not see: select the platform's `:disabled`. */
.demo-address {
display: grid;
gap: 1rem;
margin: 0;
padding: 0.75rem 1rem 1rem;
border: 1px solid var(--color-border);
border-radius: 8px;
min-inline-size: 0;
}
.demo-address > legend {
padding: 0 0.3em;
}
.demo-address .demo-fieldset:has(:disabled) {
opacity: 0.5;
}
/* The form's submission: the whole form dims, the button blocks repeat clicks with CSS. */
.demo-form[data-pending] .demo-fieldset {
opacity: 0.7;
}
.demo-submit[data-pending] {
pointer-events: none;
}
@media (prefers-reduced-motion: reduce) {
.demo-field-error,
.demo-form-error {
transition: none;
}
}"use client";
async function createTask(_: State, formData: FormData): Promise<State> {
try {
const result = await api.createTask(formData);
return result.ok ? {} : { errors: result.fieldErrors }; // "title": "Already exists"
} catch {
return { error: "Could not reach the server. Try again." }; // nothing to fix in a field
}
}
function NewTaskForm() {
// Inside the Popup: the state starts fresh at every open.
const [state, formAction] = useActionState(createTask, {});
return (
<Dialog.Form action={formAction} render={<Form.Root errors={state.errors} error={state.error} />}>
<Form.Error />
<Field.Root name="title" required>
<Field.Label>Title</Field.Label>
<Field.Control />
<Field.Error />
</Field.Root>
<Dialog.Close value="cancel">Cancel</Dialog.Close>
<Form.Submit value="save">Create</Form.Submit>
</Dialog.Form>
);
}
<Dialog.Root defaultOpen={false}>
<Dialog.Trigger>New task</Dialog.Trigger>
<Dialog.Popup>
<NewTaskForm />
</Dialog.Popup>
</Dialog.Root>Dialog.Form renders a plain React form, which takes React's path: the form is reset after every successful action, also one that returned field errors. Rendered as Form.Root (render={<Form.Root errors={state.errors} error={state.error} />}), Form.Root receives the action and runs it, so its policy applies: server errors are routed to the fields and focused, and the form is reset only when the result has no error (autoReset, which you can pass on the Form.Root element). Dialog.Form's closeOnSettle follows the same rule: a result with a field error or a form-level error keeps the dialog open (the typed values stay; the first errored field is focused, or the form-level error is announced), a result with no error resets the form and closes the dialog with the Form.Submit's value as returnValue. An action that throws keeps the dialog open and reaches the nearest Error Boundary. An error hidden because the user changed that value while the action ran still counts: the dialog stays open and the user submits the new value.
autoReset={false} on the Form.Root element if your exit is visible. With keepMounted keep the default, since the reset is what makes the next open clean.action. Without one, Form.Root owns method and the form would navigate instead of closing the dialog (a development warning says so); render a plain Dialog.Form for a method="dialog" form.Field.Root| Prop | Type | Default | Description |
|---|---|---|---|
name | string | – | The control's default name, the key in Form.Root errors, and the entry validate reads. |
required | boolean | false | Makes the control natively required and marks every part data-required. |
disabled | boolean | false | Disables the control, marks every part data-disabled, and keeps the control out of the submission: a native control is natively disabled, and Combobox and MultiSelect disable their hidden inputs too. |
readOnly | boolean | false | Makes the control read-only and marks every part data-readonly: the value is focusable and submitted, the user cannot change it, and it never blocks a submission (validate is not run and a server error stays out of the control's validity, though it is still shown). Every FUNSTACK form control takes it; Field.Control renders native readonly. See Disabled or read-only. |
validate | (value: string, formData: FormData) => FieldErrorValue | – | A synchronous custom rule, written with setCustomValidity. Return a message (or several) for an error, null / undefined / "" for none. |
className | string | ((state: FieldState) => string | undefined) | – | A class name, or a function of the part's state. |
style | CSSProperties | ((state: FieldState) => CSSProperties | undefined) | – | Inline style, or a function of the part's state. |
children | ReactNode | ((state: FieldState) => ReactNode) | – | Content, or a function of the part's state. |
render | ReactElement | ((props, state: FieldState) => ReactNode) | – | Replace the default <div> (element form) or take over rendering (function form). |
FieldState, shared by every Field part, is { invalid, messages, validity, disabled, required, readOnly, pending }; invalid: an error is shown; messages: the shown messages (the browser's validationMessage, validate's strings, or the server's), [] while valid; validity: a FieldValidity snapshot of the control's ValidityState (valueMissing, typeMismatch, tooShort, …, customError, valid); disabled / required / readOnly: the Field's or the control's own; pending: the enclosing form's submission is in flight.data-invalid, data-disabled, data-required, data-readonly, data-pending.Field.Label, Field.Description| Prop | Type | Default | Description |
|---|---|---|---|
className | string | ((state: FieldState) => string | undefined) | – | A class name, or a function of the part's state. |
style | CSSProperties | ((state: FieldState) => CSSProperties | undefined) | – | Inline style, or a function of the part's state. |
children | ReactNode | ((state: FieldState) => ReactNode) | – | Content, or a function of the part's state. |
render | ReactElement | ((props, state: FieldState) => ReactNode) | – | Replace the default <label> (Label; it must stay a <label>) or <p> (Description) (element form), or take over rendering (function form). |
id, htmlFor (absent for a group control), onClick (for a button control the label activation is cancelled and the button focused; a render element's own handler runs first); Description: id; both also the Field's five data-* attributes. Each registers with the Field when it mounts, so the control references its id only while it is mounted.Field.Control| Prop | Type | Default | Description |
|---|---|---|---|
id | string | – | Native id, outside a Field.Root only. Inside one the Field owns the id (so Field.Label can point at it) and this prop is ignored with a development warning. |
name | string | the Field's | Native name. |
required, disabled, readOnly | boolean | the Field's | Native; the Field's value also applies (either one is enough). readOnly locks text-like inputs and a <textarea> only: on a <select> or a checkbox it does nothing and logs a development warning (use Select, Checkbox and the other library controls). |
className | string | ((state: FieldState) => string | undefined) | – | A class name, or a function of the part's state. |
style | CSSProperties | ((state: FieldState) => CSSProperties | undefined) | – | Inline style, or a function of the part's state. |
render | ReactElement | ((props, state: FieldState) => ReactNode) | – | Replace the default <input> (element form) or take over rendering (function form). It must render an <input>, <textarea> or <select>: render={<textarea rows={3} />}. |
… | native | – | Every other <input> prop passes through; the Field's listeners are native, so your onInput / onBlur run independently. |
id, name, required, disabled, readOnly, aria-invalid (always "true" or "false": it stops Chromium announcing a typeMismatch before it is shown, and exposes valueMissing, which it otherwise never does), aria-describedby (your tokens first, then the description while one is mounted, then the error while one is mounted and shown; absent with neither), and the Field's five data-* attributes.Outside a Field.Root it is the plain native control: every prop passes through (id included), the library adds no attribute, and the function props receive a FieldState with the control's own disabled / required / readOnly and a constant valid, not-pending rest. The other Field parts still throw outside a Field. See Field.Control on its own.
Field.Error| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | ((state: FieldState) => ReactNode) | messages.join(" ") | Rendered only while invalid. A function for per-constraint wording: ({ validity, messages }) => validity.valueMissing ? "Enter your email" : messages.join(" "). Must not suspend. |
className | string | ((state: FieldState) => string | undefined) | – | A class name, or a function of the part's state. |
style | CSSProperties | ((state: FieldState) => CSSProperties | undefined) | – | Inline style, or a function of the part's state. |
render | ReactElement | ((props, state: FieldState) => ReactNode) | – | Replace the default <div> (element form) or take over rendering (function form). |
id and the Field's five data-* attributes.No role and no live region: after a submission focus moves to the field and reads the error with the description (a live region would read it twice). Add aria-live="polite" yourself if you want blur-time errors announced. One per Field.
Form.Root| Prop | Type | Default | Description |
|---|---|---|---|
action | string | ((formData: FormData) => unknown) | – | A Server Function, a useActionState dispatch or a client function, rendered unwrapped (progressive enhancement) and run by the Form in its own transition after hydration. A string is a navigating form. |
errors | FormErrors | – | Record<string, string | readonly string[] | null | undefined> keyed by field name. Shown on the matching Field, written into its control's validity (the same value cannot be resubmitted), dismissed by its next edit, re-armed by the next settled submission. |
error | FieldErrorValue | – | A form-level error: a failure that is not about one field (usually state.error). Shown by Form.Error and announced when a submission settles. Never written into a control's validity, so the same values can be submitted again. While it is not empty the form is not reset and a Dialog.Form stays open. See Form-level errors. |
autoReset | boolean | true | Reset the form when a submission the Form ran settles with no error: no non-empty entry in errors (one hidden because the user replaced the value still counts) and an empty error. false: never. |
method | string | – | Only with a string action. |
className | string | ((state: FormRootState) => string | undefined) | – | A class name, or a function of the part's state. |
style | CSSProperties | ((state: FormRootState) => CSSProperties | undefined) | – | Inline style, or a function of the part's state. |
children | ReactNode | ((state: FormRootState) => ReactNode) | – | Content, or a function of the part's state. |
render | ReactElement | ((props, state: FormRootState) => ReactNode) | – | Replace the default <form> (element form) or take over rendering (function form). |
FormRootState is { pending }.action, method (except with a string action), onSubmit, data-pending.noValidate and onReset pass through. A render element's own onSubmit runs first; calling preventDefault() in it cancels the submission.
Form.Submit| Prop | Type | Default | Description |
|---|---|---|---|
disabled | boolean | false | Native disabled; owned, so a render element cannot re-enable it. Never set by the library while pending. Native also with a component render (assumed to render a submit button: only native disabled stops a submitter before hydration). With a host element of another tag (render={<a />}), aria-disabled="true" instead and the click is cancelled. A click runs no onClick, a render element's included, and is cancelled; a render element's disabled disables the part too (unavailable parts). |
className | string | ((state: FormSubmitState) => string | undefined) | – | A class name, or a function of the part's state. |
style | CSSProperties | ((state: FormSubmitState) => CSSProperties | undefined) | – | Inline style, or a function of the part's state. |
children | ReactNode | ((state: FormSubmitState) => ReactNode) | – | Content, or a function of the part's state. |
render | ReactElement | ((props, state: FormSubmitState) => ReactNode) | – | Replace the default <button> (element form) or take over rendering (function form). |
FormSubmitState is { pending, disabled }; pending is useFormStatus().pending of the enclosing form, so it works in any React form (Dialog.Form, a plain <form action>).type ("submit", on a <button>), disabled, aria-disabled (a host element other than <button> / <input>), onClick (while disabled), data-pending, data-disabled. To tell submit buttons apart, read the submitter's name / value in the FormData.name, value and formAction pass through.
Form.Error| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | ((state: FormErrorState) => ReactNode) | messages.join(" ") | Rendered only while Form.Root error has a message. A function gets the state. Must not suspend. |
className | string | ((state: FormErrorState) => string | undefined) | – | A class name, or a function of the part's state. |
style | CSSProperties | ((state: FormErrorState) => CSSProperties | undefined) | – | Inline style, or a function of the part's state. |
render | ReactElement | ((props, state: FormErrorState) => ReactNode) | – | Replace the default <div> (element form) or take over rendering (function form). |
FormErrorState is { messages, pending }; messages: Form.Root error, normalised (never empty while the part renders); pending: the enclosing form's submission is in flight.data-pending.Throws outside a Form.Root. No role and no live region: while it is mounted the Form announces the messages with ariaNotify() at each settle. id is yours (nothing references the part).
| Attribute | On | Meaning |
|---|---|---|
data-invalid | every Field part | An error is shown (FieldState.invalid). |
data-required | every Field part | Field.Root required or the control's own required. |
data-disabled | every Field part, Form.Submit | Disabled by prop. |
data-readonly | every Field part | Field.Root readOnly or the control's own readOnly. |
aria-disabled | Form.Submit | disabled with a render element that cannot be natively disabled (<a>). |
data-pending | every Field part, Form.Root, Form.Submit, Form.Error | The form's submission is in flight. A FUNSTACK control inside keeps its own data-pending for its own action. |
:user-invalid, :user-valid | the control | The platform's; work before hydration. data-invalid adds server errors and the parts that are not the control. |
Nothing beyond the platform: Tab order is the DOM's, Enter in a text field submits, a click on the label focuses or activates the control. After a failed attempt, assistive technology hears the control, "invalid entry", the description and the error, in that order. aria-errormessage is not used: VoiceOver does not announce it, and aria-describedby works in every screen reader.
Concept: Transitions & ViewTransition
None: no part adds transition types or takes a transition prop.
Concept: Pending & optimistic state
A Form with a function action runs it in its own transition: data-pending is on the form, on Form.Submit and on every Field part while the submission is in flight. A Field's display state (whether its error shows) is updated urgently from the DOM event (after an edit, once the event has been dispatched), not in a transition.
Concept: Suspense
The submission's pending state includes the Suspense wait of what the result re-renders. Content inside Field.Error must not suspend.
Concept: Browser-owned state
Validity is the browser's: native constraint validation, :user-invalid and the browser's validation message; data-invalid adds server errors.
Concept: Server Components
The browser validates and shows its bubble, and the form posts to the Server Function (React's progressive enhancement). A group control's label carries for until hydration.
// app/subscribe.tsx: a Server Component, no client code of yours.
import { Field } from "@funstack/ui/field";
import { Form } from "@funstack/ui/form";
import { subscribe } from "./actions"; // "use server"
export function Subscribe() {
return (
<Form.Root action={subscribe}>
<Field.Root name="email" required>
<Field.Label>Email</Field.Label>
<Field.Control type="email" autoComplete="email" />
<Field.Error />
</Field.Root>
<Field.Root name="body" required>
<Field.Label>Comment</Field.Label>
<Field.Control render={<textarea rows={2} maxLength={2000} />} className="comment" />
<Field.Error />
</Field.Root>
<Form.Submit>Subscribe</Form.Submit>
</Form.Root>
);
}Every part may be rendered from a Server Component with serialisable props. Before hydration the browser validates and shows its bubble, and the form posts to the Server Function; the ids, aria-describedby and aria-invalid="false" are in the server HTML. A group control's label carries for until hydration replaces it with the group's aria-labelledby. The server HTML references the label, description and error ids whether or not those parts are rendered (the server cannot know about a part rendered after the control); hydration removes a reference whose part is not there.
.field[data-invalid] .control { border-color: var(--danger); }
.field:has(.control:user-valid) .control { border-color: var(--ok); } /* no JS needed */
.field[data-disabled] { opacity: .5; }
.field[data-required] .label::after { content: " *" / ""; }
.error { color: var(--danger); opacity: 1; transition: opacity 150ms; @starting-style { opacity: 0; } }
/* The control grows with its content: no auto-resize hook. */
textarea.control { field-sizing: content; min-height: 3lh; max-height: 12lh; }
select.control { field-sizing: content; }
form[data-pending] { cursor: progress; }
.submit[data-pending] { pointer-events: none; } /* your "no double submit" */field-sizing: content (Chrome 123, Safari 26.2, Firefox 152) makes a textarea or a text input size to its content. Tailwind: data-invalid:border-red-500 data-pending:opacity-70 field-sizing-content.
Field.Error is rendered only while an error is shown, and an error can appear when the field loses focus. Losing focus is often the first half of a click on the submit button: the press moves focus to the button, the error appears above it and pushes it down, and the release lands on something else. The browser then fires click on the nearest element that contains both the press and the release (MDN), not on the button, so nothing is submitted. This happens with any validation shown on blur. Keep the error from moving what follows it: reserve its line, and place the error in it.
.field { position: relative; padding-block-end: 1.5em; } /* room for one line */
.error { position: absolute; inset-inline: 0; inset-block-end: 0; }A message longer than one line overflows the reserved line and covers what follows: size the padding for your longest message.
"use client";
import { startTransition, useState } from "react";
import { Checkbox } from "@funstack/ui/checkbox";
import { Field } from "@funstack/ui/field";
import { Form } from "@funstack/ui/form";
import type { FormErrors } from "@funstack/ui/form";
import { RadioGroup } from "@funstack/ui/radio-group";
import { Switch } from "@funstack/ui/switch";
import { fakeSave } from "./fake-api.js";
// FUNSTACK controls are a Field's control as they are: the Field labels and describes them,
// gives them its `name` / `required` / `disabled`, and shows their native validation. The Switch
// saves on its own (its own `data-pending`); the form's submission is the Field's `data-pending`.
// `autoReset={false}`: a settings form keeps its values after a successful save.
export function FieldControls() {
const [errors, setErrors] = useState<FormErrors>({});
const [saved, setSaved] = useState<string | null>(null);
return (
<Form.Root
className="demo-form"
autoReset={false}
errors={errors}
action={async (formData) => {
await fakeSave("the profile");
const bio = String(formData.get("bio"));
startTransition(() => {
setErrors(bio.includes("http") ? { bio: "Links are not allowed in the bio." } : {});
setSaved(bio.includes("http") ? null : `plan=${String(formData.get("plan"))}`);
});
}}
>
<Field.Root name="plan" required className="demo-fieldset">
<Field.Label className="demo-label">Plan</Field.Label>
<RadioGroup.Root className="demo-radio-row">
<label className="demo-field">
<RadioGroup.Item value="free" className="demo-radio" /> Free
</label>
<label className="demo-field">
<RadioGroup.Item value="pro" className="demo-radio" /> Pro
</label>
</RadioGroup.Root>
<Field.Description className="demo-description">You can change it later.</Field.Description>
<Field.Error className="demo-field-error" />
</Field.Root>
<Field.Root name="bio" className="demo-fieldset">
<Field.Label className="demo-label">Bio</Field.Label>
<Field.Control
render={<textarea rows={1} maxLength={280} />}
className="demo-input demo-grow"
/>
<Field.Description className="demo-description">
Grows as you type (<code>field-sizing: content</code>). Links are rejected by the server.
</Field.Description>
<Field.Error className="demo-field-error" />
</Field.Root>
<Field.Root name="news" className="demo-fieldset demo-fieldset-inline">
<Checkbox className="demo-checkbox" />
<Field.Label className="demo-label">Email me product news</Field.Label>
</Field.Root>
<Field.Root name="terms" required className="demo-fieldset demo-fieldset-inline">
<Checkbox className="demo-checkbox" />
<Field.Label className="demo-label">I accept the terms</Field.Label>
<Field.Error className="demo-field-error" />
</Field.Root>
<Field.Root name="beta" className="demo-fieldset demo-fieldset-inline">
<Switch
className="demo-switch"
checkedChangeAction={() => fakeSave("the beta setting", { ms: 1200 })}
/>
<Field.Label className="demo-label">Beta features (saves on its own)</Field.Label>
</Field.Root>
<div className="demo-row">
<Form.Submit className="demo-button demo-primary demo-submit">
{({ pending }) => (
<>
{pending && <span className="demo-spinner" aria-hidden />}
Save
</>
)}
</Form.Submit>
</div>
{saved !== null && (
<output className="demo-readout">
Saved <code>{saved}</code>; the values stay (<code>autoReset={false}</code>).
</output>
)}
</Form.Root>
);
}/* Field and Form. Every Field part carries data-invalid / data-required / data-disabled /
data-pending; the control also matches the platform's :user-invalid and :user-valid, which
work before hydration too. */
.demo-form {
display: grid;
gap: 1rem;
max-inline-size: 26rem;
}
.demo-fieldset {
display: grid;
gap: 0.3rem;
}
/* A checkbox or switch first, then its label. */
.demo-fieldset-inline {
grid-template-columns: auto 1fr;
align-items: center;
column-gap: 0.6em;
}
.demo-fieldset-inline .demo-field-error {
grid-column: 1 / -1;
}
.demo-label {
font-weight: 600;
color: var(--color-text);
}
/* `/ ""`: the asterisk stays out of the control's accessible name; the control exposes
`required` itself. */
.demo-fieldset[data-required] > .demo-label::after {
content: " *" / "";
color: #dc2626;
}
.demo-input {
padding: 0.4em 0.6em;
border: 1px solid var(--color-border);
border-radius: 6px;
background: var(--color-bg);
color: var(--color-text);
font: inherit;
}
.demo-input:user-valid {
border-color: #16a34a;
}
.demo-input[data-invalid] {
border-color: #dc2626;
}
/* No JS auto-resize: the textarea grows with its content. */
.demo-grow {
field-sizing: content;
min-block-size: 2lh;
max-block-size: 8lh;
resize: none;
}
.demo-description {
margin: 0;
font-size: 0.85rem;
color: var(--color-text-secondary);
}
/* Rendered only while an error is shown: enter with @starting-style, leave instantly. */
.demo-field-error {
font-size: 0.85rem;
color: #dc2626;
opacity: 1;
transition: opacity 150ms;
@starting-style {
opacity: 0;
}
}
/* Form.Error: the form-level error (a failed submission), rendered only while there is one.
Dimmed while the retry is in flight (`data-pending`); the message stays until the result. */
.demo-form-error {
padding: 0.5rem 0.75rem;
border: 1px solid #fca5a5;
border-radius: 6px;
background: #fef2f2;
color: #b91c1c;
font-size: 0.9rem;
opacity: 1;
transition: opacity 150ms;
@starting-style {
opacity: 0;
}
}
.demo-form-error[data-pending] {
opacity: 0.6;
}
.demo-radio-row {
display: flex;
gap: 1.2em;
margin: 0;
padding: 0;
border: 0;
}
/* A native fieldset around several Fields (no Fieldset part). Its `disabled` is inherited by
every control, which `data-disabled` does not see: select the platform's `:disabled`. */
.demo-address {
display: grid;
gap: 1rem;
margin: 0;
padding: 0.75rem 1rem 1rem;
border: 1px solid var(--color-border);
border-radius: 8px;
min-inline-size: 0;
}
.demo-address > legend {
padding: 0 0.3em;
}
.demo-address .demo-fieldset:has(:disabled) {
opacity: 0.5;
}
/* The form's submission: the whole form dims, the button blocks repeat clicks with CSS. */
.demo-form[data-pending] .demo-fieldset {
opacity: 0.7;
}
.demo-submit[data-pending] {
pointer-events: none;
}
@media (prefers-reduced-motion: reduce) {
.demo-field-error,
.demo-form-error {
transition: none;
}
}Field.Root the control's id belongs to the Field (so Field.Label can point at it); name, required and disabled default from the Field.data-pending; the form's submission gives the Field data-pending, never the control.Combobox.Input is the control, the hidden input takes the Field's name, and the listbox is named by Field.Label when no Combobox.Label is rendered. validate reads the selection (the hidden input), and picking an option counts as an edit: it re-runs validate and dismisses a server error, like typing.<button>) is the control; Field.Label focuses it instead of clicking it, as a label does for a <select>. Its validity is a hidden <select>'s, which shows no browser bubble, so Field.Error is where a required message appears (MultiSelect forms).<input type="number">'s, so Field.Error's function children can branch on validity.stepMismatch, rangeUnderflow or rangeOverflow: {({ validity, messages }) => validity.stepMismatch ? "Enter a multiple of six." : messages.join(" ")}. validate reads the committed number, never a half-typed draft.checkedChangeAction={() => formRef.current?.requestSubmit()} goes through the Form like a click. Never call form.submit(), which skips validation and the Form.There is no Fieldset part: a set of Fields with a common caption is the native <fieldset>, which already names the group (<legend>), describes it (aria-describedby) and disables every native control inside it. Every FUNSTACK form control has a native element, so <fieldset disabled> reaches them all: they leave the FormData, skip validation and the Tab order, and the pointer surfaces (a Slider's Track, NumberField's buttons) ignore presses too.
"use client";
import { useRef, useState } from "react";
import { Field } from "@funstack/ui/field";
import { Form } from "@funstack/ui/form";
import { NumberField } from "@funstack/ui/number-field";
import { fakeSave } from "./fake-api.js";
// A native <fieldset> captions a set of Fields and disables all of them: every FUNSTACK control
// has a native element, so `disabled` on the fieldset reaches each one (and the NumberField's
// buttons ignore the pointer too). The library's `data-disabled` does not see an inherited
// `disabled`, so the CSS selects `:disabled` instead. The fieldset is never disabled because a
// submission is pending: that would drop focus; after a save, focus moves to the checkbox before
// the fieldset is disabled, for the same reason. The notes are a textarea that grows with its
// content (`field-sizing: content`).
export function FieldAddress() {
const [editing, setEditing] = useState(false);
const [saved, setSaved] = useState<string | null>(null);
const editRef = useRef<HTMLInputElement>(null);
return (
<Form.Root
className="demo-form"
autoReset={false}
action={async (formData) => {
await fakeSave("the address");
setSaved(`${String(formData.get("street"))}, ${String(formData.get("city"))}`);
// Disabling the fieldset would drop focus from the Save button inside it: move focus to
// the checkbox (outside the fieldset) first.
editRef.current?.focus();
setEditing(false);
}}
>
<label className="demo-field">
<input
ref={editRef}
type="checkbox"
checked={editing}
onChange={(e) => setEditing(e.target.checked)}
/>{" "}
Edit the address
</label>
<fieldset className="demo-address" disabled={!editing} aria-describedby="address-hint">
<legend className="demo-label">Shipping address</legend>
<p id="address-hint" className="demo-description">
Used for this order only.
</p>
<Field.Root name="street" required className="demo-fieldset">
<Field.Label className="demo-label">Street</Field.Label>
<Field.Control
className="demo-input"
autoComplete="address-line1"
defaultValue="1 Main St"
/>
<Field.Error className="demo-field-error" />
</Field.Root>
<Field.Root name="city" required className="demo-fieldset">
<Field.Label className="demo-label">City</Field.Label>
<Field.Control
className="demo-input"
autoComplete="address-level2"
defaultValue="Springfield"
/>
<Field.Error className="demo-field-error" />
</Field.Root>
<Field.Root name="floor" className="demo-fieldset">
<Field.Label className="demo-label">Floor</Field.Label>
<NumberField.Root
defaultValue={1}
min={0}
max={99}
locale="en-US"
className="demo-number-field"
>
<div className="demo-number-group">
<NumberField.Decrement className="demo-number-step">−</NumberField.Decrement>
<NumberField.Input className="demo-number-input" />
<NumberField.Increment className="demo-number-step">+</NumberField.Increment>
</div>
</NumberField.Root>
</Field.Root>
<Field.Root name="notes" className="demo-fieldset">
<Field.Label className="demo-label">Delivery notes</Field.Label>
<Field.Control
render={<textarea rows={1} maxLength={500} />}
className="demo-input demo-grow"
/>
<Field.Description className="demo-description">
Grows as you type (<code>field-sizing: content</code>).
</Field.Description>
</Field.Root>
<div className="demo-row">
<Form.Submit className="demo-button demo-primary demo-submit">Save address</Form.Submit>
</div>
</fieldset>
{saved !== null && <output className="demo-readout">Saved {saved}.</output>}
</Form.Root>
);
}/* Field and Form. Every Field part carries data-invalid / data-required / data-disabled /
data-pending; the control also matches the platform's :user-invalid and :user-valid, which
work before hydration too. */
.demo-form {
display: grid;
gap: 1rem;
max-inline-size: 26rem;
}
.demo-fieldset {
display: grid;
gap: 0.3rem;
}
/* A checkbox or switch first, then its label. */
.demo-fieldset-inline {
grid-template-columns: auto 1fr;
align-items: center;
column-gap: 0.6em;
}
.demo-fieldset-inline .demo-field-error {
grid-column: 1 / -1;
}
.demo-label {
font-weight: 600;
color: var(--color-text);
}
/* `/ ""`: the asterisk stays out of the control's accessible name; the control exposes
`required` itself. */
.demo-fieldset[data-required] > .demo-label::after {
content: " *" / "";
color: #dc2626;
}
.demo-input {
padding: 0.4em 0.6em;
border: 1px solid var(--color-border);
border-radius: 6px;
background: var(--color-bg);
color: var(--color-text);
font: inherit;
}
.demo-input:user-valid {
border-color: #16a34a;
}
.demo-input[data-invalid] {
border-color: #dc2626;
}
/* No JS auto-resize: the textarea grows with its content. */
.demo-grow {
field-sizing: content;
min-block-size: 2lh;
max-block-size: 8lh;
resize: none;
}
.demo-description {
margin: 0;
font-size: 0.85rem;
color: var(--color-text-secondary);
}
/* Rendered only while an error is shown: enter with @starting-style, leave instantly. */
.demo-field-error {
font-size: 0.85rem;
color: #dc2626;
opacity: 1;
transition: opacity 150ms;
@starting-style {
opacity: 0;
}
}
/* Form.Error: the form-level error (a failed submission), rendered only while there is one.
Dimmed while the retry is in flight (`data-pending`); the message stays until the result. */
.demo-form-error {
padding: 0.5rem 0.75rem;
border: 1px solid #fca5a5;
border-radius: 6px;
background: #fef2f2;
color: #b91c1c;
font-size: 0.9rem;
opacity: 1;
transition: opacity 150ms;
@starting-style {
opacity: 0;
}
}
.demo-form-error[data-pending] {
opacity: 0.6;
}
.demo-radio-row {
display: flex;
gap: 1.2em;
margin: 0;
padding: 0;
border: 0;
}
/* A native fieldset around several Fields (no Fieldset part). Its `disabled` is inherited by
every control, which `data-disabled` does not see: select the platform's `:disabled`. */
.demo-address {
display: grid;
gap: 1rem;
margin: 0;
padding: 0.75rem 1rem 1rem;
border: 1px solid var(--color-border);
border-radius: 8px;
min-inline-size: 0;
}
.demo-address > legend {
padding: 0 0.3em;
}
.demo-address .demo-fieldset:has(:disabled) {
opacity: 0.5;
}
/* The form's submission: the whole form dims, the button blocks repeat clicks with CSS. */
.demo-form[data-pending] .demo-fieldset {
opacity: 0.7;
}
.demo-submit[data-pending] {
pointer-events: none;
}
@media (prefers-reduced-motion: reduce) {
.demo-field-error,
.demo-form-error {
transition: none;
}
}<fieldset disabled={!canEdit} aria-describedby="address-hint">
<legend>Shipping address</legend>
<p id="address-hint">Used for this order only.</p>
<Field.Root name="street"><Field.Label>Street</Field.Label><Field.Control required /><Field.Error /></Field.Root>
<Field.Root name="city"><Field.Label>City</Field.Label><Field.Control required /><Field.Error /></Field.Root>
</fieldset>disabled is the platform's: data-disabled and state.disabled stay the props'. Style it with the platform's selector, which covers both sources: .field:has(.control:disabled) { opacity: .5; }.validate (it receives the whole FormData); a message that belongs to no control is your own element, referenced from the fieldset's aria-describedby.Label is the <legend>): do not wrap one in another fieldset to caption it.<fieldset disabled={pending}> drops focus from the control the user is on. Mark pending with form[data-pending].Both lock a value; they differ in everything else. A disabled control is not focusable and not submitted: the value does not count. A read-only control is focusable, readable (its text selectable, its radios and chips still reachable with the arrows) and submitted, and the user cannot change it. Neither dispatches for user interaction, both are barred from validation, and both follow a form reset.
readOnly is on every form value control (TextField, NumberField, OTPField, Checkbox, Switch, RadioGroup, CheckboxGroup, Select, Combobox, MultiSelect, Slider) and on Field.Root, which hands it to its control as it hands disabled. It is the owner's prop: a single radio or box that cannot be picked is disabled.<select>, a range), the control refuses the change itself and marks it with aria-readonly: a click is cancelled, a Select disables its other options, a Slider cancels its keys, a Combobox or MultiSelect does not open its popup.[data-readonly], on every control and every Field part. :read-only matches every element that is not editable (a checkbox, a div), so it tells nothing outside a text input.required is not rendered, and validate or a server error do not enter its validity. A server error for it is still shown.disabled wins when both are set. A <fieldset> has no readonly: lock a group of Fields with readOnly on each.A textarea is Field.Control render={<textarea />}; textarea-only props (rows, wrap) go on the render element. It grows with its content through CSS, so there is no autoSize prop (the notes in the example above):
<Field.Root name="notes">
<Field.Label>Delivery notes</Field.Label>
<Field.Control render={<textarea rows={1} maxLength={500} />} className="control grow" />
</Field.Root>.grow { field-sizing: content; min-block-size: 2lh; max-block-size: 8lh; resize: none; }Outside a Field.Root, Field.Control renders the plain native control: id, name, disabled and required pass through, and nothing is added. For a direct call site write the native element (<input type="search" aria-label="Search">); the standalone form is for your own styled input built on Field.Control, which then works in a form field and in a search box or a table cell alike:
function TextInput(props: FieldControlProps) {
return <Field.Control {...props} className={(s) => (s.invalid ? "input invalid" : "input")} />;
}
<Field.Root name="email" required>
<Field.Label>Email</Field.Label>
<TextInput type="email" /> {/* wired to the Field */}
</Field.Root>
<label htmlFor="q">Search</label>
<TextInput id="q" type="search" /> {/* the plain <input>, state never invalid */}Outside a Field the function props receive a FieldState whose disabled and required are the control's own props (as inside a Field), so className={(s) => s.disabled ? …} works in both places; invalid, validity and pending are constant (valid, not pending: there is no validation without a Field), and no data-* attribute is rendered. Select a plain control's validity with :user-invalid. A search box has no Field to wire; let the query leave the urgent lane with useDeferredValue, or use a Combobox when the result is a list of options. The text of an input is a fact, not a request, so there is no valueChangeAction on a text control.