Dialog
npx shadcn add @klarhimmel/dialog @klarhimmel/buttonCopy for AIExamples
Basic
Destructive
With a validation error
Busy: cannot be dismissed while saving
Scroll
DetailsAbout, usage, props and more
A modal window that traps focus, closes on Escape and gives focus back. Built on Radix Dialog.
When to use
Use for
A short task or a decision that needs full attention: rename, confirm, delete.
Not for
Long forms or reading. Use a page. Not for hints. Use a Popover.
Pairs with
Button, Field, DialogClose.
Preview
Import
Import
- Files
- dialog.tsx, button.tsx, in components/klarhimmel/ui/. With it: editing, icon-button, theme, utils. npm: lucide-react, radix-ui.
- Render in
- Render it inside <Theme>, once near the root of your app. Tokens and fonts are scoped to it, so outside it the component is unstyled.
- Limit
- Overlays render into the Theme root with position: fixed. A transform on an ancestor re-parents fixed (a dialog centres in the wrapper) and overflow: hidden clips menus. Render <Theme> at the app root, outside any transformed wrapper.
Anatomy
The parts and how they nest.
DialogOwns open state and the overlay. Controlled with open, or opened by trigger.title / descriptionProps, not parts. The title names the dialog for assistive tech.footerButtons pinned under the scrolling body.DialogCloseWraps a footer Button so it closes the dialog.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
Dialog.open | boolean | - | Controlled or uncontrolled state. |
Dialog.defaultOpen | boolean | - | Controlled or uncontrolled state. |
Dialog.onOpenChange | (open: boolean) => void | - | Controlled or uncontrolled state. |
Dialog.trigger | ReactNode | - | The element that opens it (a Button). Focus returns to it on close. Omit for a controlled dialog. |
Dialog.title | string | required | Required: names the dialog. |
Dialog.description | ReactNode | - | A line under the title: text, or a small row of parts (a key with its copy button). |
Dialog.children | ReactNode | - | |
Dialog.footer | ReactNode | - | Buttons, pinned under the body. |
Dialog.alert | boolean | - | An alert dialog (role=alertdialog) for a decision that cannot be missed, like a delete: clicking outside does not close it, Escape still does. |
Dialog.closeLabel | string | - | The close button's name for screen readers, in the page's language ('Stäng'). Default 'Close'. |
Dialog.className | string | - | Extra classes, merged after the component's own. |
DialogClose.children | ReactNode | required |
Accessibility
| Key | Does |
|---|---|
Tab / Shift+Tab | Cycles inside the dialog; the page behind is unreachable |
Escape | Closes and returns focus to the trigger |
Enter / Space | Activates the focused button |
With alert set, a click outside does not close it; Escape still does.
Accessibility