Appearance
Modal Form
When to Use
- Default for most Create and Edit forms — keeps user in context on parent screen
- Inline add of a related entity from another screen (add address from order detail)
- Quick configuration changes and single-purpose forms (rename, change status)
- Any form the design hasn't explicitly called out as a full-page routed screen
Page Implementation
tsx
function ModalForm() {
return (
<Dialog.Root>
<Dialog.Trigger render={<Button />}>Add address</Dialog.Trigger>
<Dialog.Content>
<Dialog.Header>
<Dialog.Title>Add address</Dialog.Title>
<Dialog.Description>Add a shipping address to this order.</Dialog.Description>
</Dialog.Header>
{/*
* `onFormSubmit` fires only after validation passes and receives the
* registered field values — no `<form onSubmit>` + `new FormData(...)`.
*/}
<Form<AddressDraft>
noValidate
onFormSubmit={(values) => window.alert(`Saving ${values.label}`)}
>
<div className="flex flex-col gap-4 py-4">
<Field.Root name="label">
<Field.Label>Label</Field.Label>
<Field.Control required />
<Field.Error match="valueMissing">Label is required.</Field.Error>
</Field.Root>
<Field.Root name="street">
<Field.Label>Street</Field.Label>
<Field.Control required />
<Field.Error match="valueMissing">Street is required.</Field.Error>
</Field.Root>
<Field.Root name="city">
<Field.Label>City</Field.Label>
<Field.Control required />
<Field.Error match="valueMissing">City is required.</Field.Error>
</Field.Root>
</div>
<Dialog.Footer>
<Dialog.Close render={<Button type="button" variant="ghost" />}>Cancel</Dialog.Close>
<Button type="submit">Save</Button>
</Dialog.Footer>
</Form>
</Dialog.Content>
</Dialog.Root>
);
}Route-driven Variant
The route-driven variant renders the same component at both the parent path and the create / edit path, with onOpenChange navigating back so the URL stays in sync.
tsx
function ModalFormRouted() {
const [isCreateOpen, setCreateOpen] = useState(false);
return (
<Layout>
<Layout.Header
title="Products"
actions={[
<Button key="create" onClick={() => setCreateOpen(true)}>
Create
</Button>,
]}
/>
<Layout.Column>{/* products list — see page/document-collection */}</Layout.Column>
<Dialog.Root open={isCreateOpen} onOpenChange={setCreateOpen}>
<Dialog.Content>
<Dialog.Header>
<Dialog.Title>Create product</Dialog.Title>
</Dialog.Header>
<Form<ProductDraft>
noValidate
onFormSubmit={(values) => {
window.alert(`Saving ${values.name}`);
setCreateOpen(false);
}}
>
<div className="flex flex-col gap-4 py-4">
<Field.Root name="name">
<Field.Label>Name</Field.Label>
<Field.Control required />
<Field.Error match="valueMissing">Name is required.</Field.Error>
</Field.Root>
</div>
<Dialog.Footer>
{/* Inside a `<Form>` an untyped button submits — Cancel must be `type="button"`. */}
<Button type="button" variant="ghost" onClick={() => setCreateOpen(false)}>
Cancel
</Button>
<Button type="submit">Save</Button>
</Dialog.Footer>
</Form>
</Dialog.Content>
</Dialog.Root>
</Layout>
);
}Second Level: Confirm or Pick
A confirmation or a picker may open on top of the form modal: a "Discard changes?" confirm when the user cancels a dirty form, or a picker that fills one field. This is the second and last level. Render it inside the form dialog's content, so Escape and an outside press close only the top dialog. When it closes, focus returns to the element that opened it. See Dialog → Stacking.
tsx
function ModalFormConfirm() {
const [open, setOpen] = useState(false);
const [dirty, setDirty] = useState(false);
const [confirmOpen, setConfirmOpen] = useState(false);
const close = () => {
setConfirmOpen(false);
setDirty(false);
setOpen(false);
};
const requestClose = () => (dirty ? setConfirmOpen(true) : close());
return (
// Escape, the close button and a press on the scrim all arrive as `onOpenChange(false)`.
<Dialog.Root open={open} onOpenChange={(next) => (next ? setOpen(true) : requestClose())}>
<Dialog.Trigger render={<Button />}>Add contact</Dialog.Trigger>
<Dialog.Content>
<Dialog.Header>
<Dialog.Title>Add contact</Dialog.Title>
</Dialog.Header>
<Form<ContactDraft>
noValidate
onFormSubmit={(values) => {
window.alert(`Saving ${values.email}`);
close();
}}
>
<div className="flex flex-col gap-4 py-4" onInput={() => setDirty(true)}>
<Field.Root name="email">
<Field.Label>Email</Field.Label>
<Field.Control type="email" required />
<Field.Error match="valueMissing">Email is required.</Field.Error>
</Field.Root>
</div>
<Dialog.Footer>
<Button type="button" variant="ghost" onClick={requestClose}>
Cancel
</Button>
<Button type="submit">Save</Button>
</Dialog.Footer>
</Form>
<Dialog.Root open={confirmOpen} onOpenChange={setConfirmOpen}>
<Dialog.Content>
<Dialog.Header>
<Dialog.Title>Discard changes?</Dialog.Title>
<Dialog.Description>The contact you typed will be lost.</Dialog.Description>
</Dialog.Header>
<Dialog.Footer>
<Dialog.Close render={<Button type="button" variant="outline" />}>
Keep editing
</Dialog.Close>
<Button type="button" variant="destructive" onClick={close}>
Discard
</Button>
</Dialog.Footer>
</Dialog.Content>
</Dialog.Root>
</Dialog.Content>
</Dialog.Root>
);
}Constraints
- Dialog renders full-screen sheet below 1024px; centered max-w-md at 1024–1280px
- Route-driven variant requires both parent path and create/edit path to render the same component
onOpenChangemust navigate back — just callingsetOpen(false)leaves the URL broken- Use
<Form onFormSubmit>, never a bare<form onSubmit>— see Form state below - Cancel must be
type="button"; inside a<Form>an untyped<button>defaults tosubmit - Two levels of modal at most; the second is a confirmation or a picker, rendered inside the form dialog's content
Form state
Applies to every form/* pattern. Full detail in components.md → Forms.
form/composer follows all of this too, with one documented exception noted below: its body is controlled, because a composer reads the value during render to gate its submit and swap its placeholder.
Form+Fieldis the default stack. They wrap Base UI and ship with AppShell — no extra dependency.- Submit via
onFormSubmit(values). It fires only after validation passes. Do not hand-roll<form onSubmit>+new FormData(...)— that bypassesForm's field validation and server-error routing. onFormSubmitreads registeredField.Roots, not the DOM. So every control — includingSelect,Combobox, andAutocomplete— just needs wrapping in aField.Root name="…". They need nonameof their own and nouseState. Mirroring field values into React state is the most common thing to get wrong here. The exception is a value the component must read during render — a submit gate, a dependent placeholder, a live character count. That state is load-bearing, not mirrored; seeform/composer.Field.Controlis already a styled input. Write<Field.Control />, not<Field.Control render={<Input />} />. Every other control —Select,Combobox,Autocomplete,Checkbox,Textarea,DateField,DatePicker— goes directly underField.Root. Passing one toField.Control'srenderdraws a second frame around it.- Object items need
itemToStringValue. Items shaped{ value, label }submitvalueautomatically; any other object submits as a JSON string unless you supply it. - Server errors go through
Form'serrorsprop, keyed by fieldname— not a toast or a banner. They clear when the user edits the field. - React Hook Form is optional, consumer-installed, and only warranted for cross-field validation, field arrays, or a Zod resolver. It composes with
Field: drive the control from aControllerand spreadfieldStateontoField.Root.
Anti-patterns
- A second modal that is more than a confirmation or a picker (a form inside the form modal), or a third level — use a step inside the dialog instead
- Modal containing a wizard — promote to a routed
form/wizard - Save closes the dialog but parent state is stale — wire refetch or optimistic update
- Building a routed Create/Edit page when the design didn't explicitly call for one — modal is the default
- Registering the create path as a separate top-level route — that unmounts the parent list
- Reaching for React Hook Form on a form this size —
onFormSubmitalready covers it - Holding a
Select/Comboboxvalue inuseStatejust to submit it —Field.Rootalready does - Wrapping a
Select/ComboboxinField.Control(render={<Select />}) — a double frame - Surfacing API validation failures in a toast or banner instead of routing them via
errors