Dialog
A modal window for a short task or a confirmation.
import { Dialog } from '@/design-system';Examples
Render it to open it, and stop rendering it to close it.
'use client';
import { useState } from 'react';
import { Button, Dialog, Field, Input } from '@/design-system';
export default function DialogBasic() {
const [open, setOpen] = useState(false);
const close = () => setOpen(false);
return (
<>
<Button onClick={() => setOpen(true)}>Add customer</Button>
{open && (
<Dialog title="Add customer" onClose={close}>
<Field label="Company name">
<Input data-autofocus />
</Field>
<div className="mt-6 flex justify-end gap-3">
<Button variant="secondary" onClick={close}>
Cancel
</Button>
<Button onClick={close}>Add</Button>
</div>
</Dialog>
)}
</>
);
}How it works
- It uses the native
dialogelement as a modal, so the rest of the page cannot be used while it is open. - Focus goes to the element marked
data-autofocus. Without one, the browser chooses, usually the close button. - When it closes, focus returns to the element that had it before.
- Clicking outside the dialog does not close it.
Props
Dialog takes only these props.
| Prop | Type | Default | Description |
|---|---|---|---|
title | string | required | The heading, which also names the dialog. |
onClose | () => void | required | Called by the close button and the Escape key. Stop rendering the dialog here. |
children | ReactNode | required | The content, including any action buttons. |
Tokens
| Token | Default | Sets |
|---|---|---|
--dialog-title-size | 1.125rem | Title text size |
--dialog-radius | --radius-control | Corner radius |
--dialog-width | 28rem | Widest it gets |
--dialog-padding | 1.5rem | Padding |
--dialog-background | --surface | Fill |
--dialog-backdrop | half-transparent black | Shade behind it |