Field
One control with its label, its description and its error message, linked to each other so screen readers read them together. It checks the value on submit, on blur or on change. Use it for every control in a form; a bare input with a label is enough for a filter or a search box.
Install
npx shadcn@latest add https://thrumui.dev/r/field.jsonAdds components/ui/field.tsx and installs @base-ui/react@1.9.0. Nothing else in your project changes.
import { Field } from "@/components/ui/field";Props
| Prop | Type | Default | Description |
|---|---|---|---|
Field: name | string | – | The field's name in a form. The form's errors are matched to fields by it. |
Field: validate | (value, formValues) => string | string[] | null | – | A check of your own. Return the message, or null when the value is fine. It runs after the browser's own constraints. |
Field: validationMode | "onSubmit" | "onBlur" | "onChange" | Default "onSubmit" | When the value is checked. After a failed submit the field is checked again on every change. |
Field: validationDebounceTime | number | Default 0 | With onChange: how long to wait after the last keystroke before validate runs, in milliseconds. |
Field: invalid | boolean | – | Marks the field invalid from outside, when another library holds the state. |
Field: disabled | boolean | Default false | Disables the control, dims the label and leaves the value out of validation. |
Field: className | string | – | Extra classes for the wrapper, merged last. It is a one-column grid with a 6 pixel gap that fills its container. |
FieldLabel: required | boolean | Default false | Adds an asterisk and the hidden word required. The control still needs its own required attribute. |
FieldControl | input props | – | The control: the library's Input, with every input attribute. It receives its id, its name, aria-describedby and aria-invalid from the field. |
FieldControl: render | ReactElement | – | Another control to wire instead of the Input, passed as an element: a Textarea, a select, a PasswordInput. |
FieldControl: onValueChange | (value: string) => void | – | Called with the new text on every change. |
FieldDescription | paragraph props | – | A hint under the control in the muted text colour. It is read after the control's name. |
FieldError: match | boolean | keyof ValidityState | – | Show this message for one kind of error only, such as valueMissing or typeMismatch. true shows it always. Without it the message shows for any error. |
FieldError: children | ReactNode | – | The message. Left out, it is the text from validate, from the form's errors, or from the browser. |
Motion and sound
Motion
- The control's border colour changes over 150 ms when the field becomes invalid or valid again.
- The error message appears and leaves at once.
- Reduced motion: the border changes at once.
Sound
Silent. This component makes no sound.
Accessibility
- The label is a native label element linked to the control, so it names the control and a press on it moves the focus there.
- The description and the error message are linked with aria-describedby. A screen reader reads them after the name, and reads the error when it appears on a focused control.
- An invalid control carries aria-invalid and a border in the destructive colour. The message under it says what is wrong, so colour is never the only sign.
- The asterisk of a required field is hidden from screen readers, which hear the word required instead.
- The parts carry data-invalid, data-valid, data-touched, data-dirty, data-filled, data-focused and data-disabled for styling.
- Long labels, descriptions and messages wrap. Nothing is cut off at 160 pixels wide.
| Keys | Action |
|---|---|
| Tab | Focus the control. With validationMode onBlur, leaving it checks the value. |
| Enter | In a form: submit. An invalid field stops the submit and takes the focus. |
Source
"use client";
import { Field as FieldPrimitive } from "@base-ui/react/field";
import { cn } from "@/lib/utils";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
/**
* One control with its label, its description and its error message. The parts find each other: the label names the
* control, the description and the error describe it, and the control is marked invalid while an error shows.
* Give it a `name` inside a form, and a `validate` function or native constraints (`required`, `type`, `pattern`) to check the value.
*/
export function Field({ className, ...props }: FieldPrimitive.Root.Props) {
// `group` lets the label dim when the field is disabled.
return <FieldPrimitive.Root data-slot="field" className={cn("group grid w-full min-w-0 gap-1.5", className)} {...props} />;
}
export type FieldLabelProps = FieldPrimitive.Label.Props & {
/** Adds an asterisk after the text, and the word "required" for screen readers. The control still needs its own `required` attribute. */
required?: boolean;
};
/** The control's name. A press on it moves the focus to the control. */
export function FieldLabel({ required = false, render, ...props }: FieldLabelProps) {
return <FieldPrimitive.Label data-slot="field-label" render={render ?? <Label required={required} />} {...props} />;
}
/**
* The control. With no `render` it is the library's `Input`. Pass another control as an element to wire that one instead:
* `render={<Textarea />}`, `render={<NativeSelect />}`. It receives the `id`, the name, `aria-describedby` and `aria-invalid`.
*/
export function FieldControl({ render, ...props }: FieldPrimitive.Control.Props) {
return <FieldPrimitive.Control render={render ?? <Input data-slot="field-control" />} {...props} />;
}
/** A hint under the control, read after its name. */
export function FieldDescription({ className, ...props }: FieldPrimitive.Description.Props) {
return <FieldPrimitive.Description data-slot="field-description" className={cn("text-sm text-muted-foreground [overflow-wrap:anywhere]", className)} {...props} />;
}
/**
* The error message. It is in the page only while the field is invalid. With no children it prints the message from
* `validate`, from the form's `errors`, or from the browser. `match` ties it to one kind of error, or with `true` shows it always.
*/
export function FieldError({ className, ...props }: FieldPrimitive.Error.Props) {
return <FieldPrimitive.Error data-slot="field-error" className={cn("text-sm text-destructive [overflow-wrap:anywhere]", className)} {...props} />;
}Files and dependencies
Changelog
- 1.0.0 · 2026-12-03First release.