Components
Form
Built on Base UIA form whose fields explain their own errors.
Installation
npx nook-ui@latest add formPrompt
Paste this into your AI coding assistant. It adds this exact component, the same way the CLI does, and tells the assistant how to use it.
Short. For assistants that can run commands in your project.
Add the Nook "Form" component to this project, exactly as Nook ships it.
A form whose fields explain their own errors.
Steps
1. Check for `nook.json` in the project root. If it is missing, run `npx nook-ui@latest init` first (React or Next.js with Tailwind CSS v4). It adds the Nook tokens, the theme and `cn()`.
2. Run `npx nook-ui@latest add form`. It writes `components/ui/form.tsx` together with everything the component needs. Do not write the component from memory.
3. If you cannot run commands, fetch http://localhost:3000/r/form.json, write every entry of `files` to the path it names, install the packages in `dependencies`, and repeat for each name in `registryDependencies`.
4. Use it where I ask. Start from this example:
```tsx
"use client";
import * as React from "react";
import { Button } from "@/components/ui/button";
import { Field, FieldControl, FieldDescription, FieldError, FieldLabel, Form } from "@/components/ui/form";
import { Textarea } from "@/components/ui/textarea";
export default function FormDefault() {
const [sent, setSent] = React.useState(false);
return (
<Form
className="max-w-sm"
onFormSubmit={() => setSent(true)}
>
<Field name="name">
<FieldLabel>Full name</FieldLabel>
<FieldControl required placeholder="Maya Chen" />
<FieldError />
</Field>
<Field name="email">
<FieldLabel>Work email</FieldLabel>
<FieldControl type="email" required placeholder="maya@acme.com" />
<FieldDescription>We only use it to reply to you.</FieldDescription>
<FieldError match="valueMissing">Enter your email so we can reply.</FieldError>
<FieldError match="typeMismatch">That does not look like an email address.</FieldError>
</Field>
<Field name="message" validate={(value) => (String(value ?? "").trim().length < 20 ? "Tell us a little more: at least 20 characters." : null)}>
<FieldLabel>How can we help?</FieldLabel>
<FieldControl render={<Textarea />} required />
<FieldError />
</Field>
<div className="flex items-center gap-3">
<Button type="submit">Send message</Button>
<p role="status" className="text-sm text-muted-foreground">
{sent ? "Sent. We reply within a day." : ""}
</p>
</div>
</Form>
);
}
```
API
- `Form`: The form.
- `onFormSubmit` (`(values) => void`): Called with the values as an object when every field is valid.
- `errors` (`Record<string, string | string[]>`): Errors from the server, by field name.
- `validationMode` (`"onSubmit" | "onBlur" | "onChange"`, default "onSubmit"): When fields are checked.
- `Field`: One labelled control.
- `name` (`string`): Ties the field to the form values and errors.
- `validate` (`(value) => string | null`): A custom rule. Return the error text, or null.
- `disabled` (`boolean`): Disables the control and dims the label.
- `FieldControl`: The control: an Input unless `render` says otherwise.
- `render` (`ReactElement`, default <Input />): Another control, such as `<Textarea />`.
- `FieldLabel, FieldDescription`: The label and the hint.
- `FieldError`: The error text.
- `match` (`keyof ValidityState | boolean`): Show only for one kind of problem, such as `valueMissing`.
Rules
- Colors, radii, shadows and motion come from Nook tokens and classes such as `bg-primary`, `text-muted-foreground`, `border-border`, `rounded-lg` and `h-control`. Never hard-code them.
- Do not restyle or simplify the file. Change how it looks through `className`, its props and the theme.
- It must keep working with the keyboard, in light and dark mode, and in the sharp, soft and bold styles.
- Accessibility: Each label, hint and error is tied to its control. On submit, focus moves to the first field with a problem and its error is announced. Errors are text with an underline, never color alone.
Done when the component renders like the example in light and dark mode and the project type-checks.Usage
"use client";
import * as React from "react";
import { Button } from "@/components/ui/button";
import { Field, FieldControl, FieldDescription, FieldError, FieldLabel, Form } from "@/components/ui/form";
import { Textarea } from "@/components/ui/textarea";
export default function FormDefault() {
const [sent, setSent] = React.useState(false);
return (
<Form
className="max-w-sm"
onFormSubmit={() => setSent(true)}
>
<Field name="name">
<FieldLabel>Full name</FieldLabel>
<FieldControl required placeholder="Maya Chen" />
<FieldError />
</Field>
<Field name="email">
<FieldLabel>Work email</FieldLabel>
<FieldControl type="email" required placeholder="maya@acme.com" />
<FieldDescription>We only use it to reply to you.</FieldDescription>
<FieldError match="valueMissing">Enter your email so we can reply.</FieldError>
<FieldError match="typeMismatch">That does not look like an email address.</FieldError>
</Field>
<Field name="message" validate={(value) => (String(value ?? "").trim().length < 20 ? "Tell us a little more: at least 20 characters." : null)}>
<FieldLabel>How can we help?</FieldLabel>
<FieldControl render={<Textarea />} required />
<FieldError />
</Field>
<div className="flex items-center gap-3">
<Button type="submit">Send message</Button>
<p role="status" className="text-sm text-muted-foreground">
{sent ? "Sent. We reply within a day." : ""}
</p>
</div>
</Form>
);
}Examples
Sign in
Errors from the server
Pass errors by field name after the request fails.
Mixed controls
Selects, switches and text share one layout.
Inline subscribe
A one-field form with the button beside it.
Props
Form
The form.
| Prop | Type | Default | Description |
|---|---|---|---|
| onFormSubmit | (values) => void | – | Called with the values as an object when every field is valid. |
| errors | Record<string, string | string[]> | – | Errors from the server, by field name. |
| validationMode | "onSubmit" | "onBlur" | "onChange" | "onSubmit" | When fields are checked. |
Field
One labelled control.
| Prop | Type | Default | Description |
|---|---|---|---|
| name | string | – | Ties the field to the form values and errors. |
| validate | (value) => string | null | – | A custom rule. Return the error text, or null. |
| disabled | boolean | – | Disables the control and dims the label. |
FieldControl
The control: an Input unless `render` says otherwise.
| Prop | Type | Default | Description |
|---|---|---|---|
| render | ReactElement | <Input /> | Another control, such as `<Textarea />`. |
FieldLabel, FieldDescription
The label and the hint.
FieldError
The error text.
| Prop | Type | Default | Description |
|---|---|---|---|
| match | keyof ValidityState | boolean | – | Show only for one kind of problem, such as `valueMissing`. |
Accessibility
Each label, hint and error is tied to its control. On submit, focus moves to the first field with a problem and its error is announced. Errors are text with an underline, never color alone.
Checked against WCAG 2.1 AA with automated axe tests and keyboard tests on every release.