Components
Dialog
Modal window with a fixed header and footer, a scrolling body, three widths and detached triggers.
import { Button, Dialog } from "@openperf/deft";
<Dialog.Root>
<Dialog.Trigger render={<Button.Root />}>Rename</Dialog.Trigger>
<Dialog.Content>
<Dialog.Header>
<Dialog.Title>Rename project</Dialog.Title>
</Dialog.Header>
<Dialog.Footer>
<Dialog.Close render={<Button.Root variant="ghost" />}>Cancel</Dialog.Close>
<Button.Root variant="primary">Save</Button.Root>
</Dialog.Footer>
</Dialog.Content>
</Dialog.Root>;When to use
| Need | Component |
|---|---|
| Short task or form that blocks the page | Dialog |
| Confirm an action before it runs | Confirm dialog |
| Longer supporting content in a full-height side panel | Drawer |
| Small panel next to its trigger, page stays usable | Popover |
| Hint on hover or focus | Tooltip |
| List of actions on a trigger | Menu |
Anatomy
<Dialog.Root>
<Dialog.Trigger />
{/* portal, backdrop, popup and close button */}
<Dialog.Content>
<Dialog.Header>
<Dialog.Title />
<Dialog.Description />
</Dialog.Header>
<Dialog.Body /> {/* scrolls; header and footer stay in place */}
<Dialog.Footer>
<Dialog.Close />
</Dialog.Footer>
</Dialog.Content>
</Dialog.Root>Always render Dialog.Title: it names the dialog. Dialog.Body and Dialog.Footer are optional. Dialog.Trigger and Dialog.Close render a bare <button>, so pass a Button through render.
Sizes
| Size | Max width |
|---|---|
sm | 400px |
md | 480px |
lg | 640px |
The window keeps a 16px margin on each side of a narrow screen, starts at 12% of the viewport height and grows to at most 76% of it.
Dismissal
The dialog is modal: focus stays inside, the page does not scroll and ignores the pointer. Escape, a press outside and the built-in close button close it. Set disablePointerDismissal on Dialog.Root to ignore outside presses. With showClose={false}, keep a Dialog.Close in the footer.
Detached triggers
Create a handle once with Dialog.createHandle(), in useState when it lives in a component, and pass it to the root and to triggers anywhere in the tree. A trigger can send a payload, which the root hands to its function child, so one dialog serves a whole list.
const [handle] = useState(() => Dialog.createHandle<Project>());
<>
{projects.map((project) => (
<Dialog.Trigger handle={handle} key={project.id} payload={project} render={<Button.Root />}>
{project.name}
</Dialog.Trigger>
))}
<Dialog.Root handle={handle}>
{({ payload }) => (
<Dialog.Content size="sm">
<Dialog.Header>
<Dialog.Title>{payload?.name}</Dialog.Title>
</Dialog.Header>
</Dialog.Content>
)}
</Dialog.Root>
</>;The handle also drives the dialog from code, for example handle.close() after a successful submit.
API
Dialog.Root, Dialog.Trigger, Dialog.Close and Dialog.createHandle are Base UI exports, unchanged. On the root, the props used most are open, onOpenChange, handle and disablePointerDismissal.
Dialog.Content
The close button label is a Lingui message, so render the dialog under the root providers.
Prop
Type
DialogHandle
DialogHandle<Payload = unknown> types a handle passed through props. Its methods work only while a Dialog.Root with this handle is mounted.
Prop
Type