Dialog

A modal dialog overlay with customizable header, content, and footer.

Installation

$
npx shadcn@latest add https://ui.mattdowney.com/r/dialog.json

Usage

import { Dialog, DialogTrigger, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter, DialogClose } from "@/components/ui/dialog"

Examples

Default

A form dialog. DialogFooter showCancelButton adds a Cancel that closes the dialog, placed left of your primary action. initialFocus focuses the first field with the caret at the end instead of selecting its text.

Scrollable

Long content scrolls in a ScrollFade region while the header and footer stay put. gap-0 lets the dividers own their spacing, and every band around one is the dialog's own 24px padding. The max-height goes on the viewport, not the root.

Share

A utility dialog: a read-only field with an in-field CopyButton (Input actionEnd) and a Done action. initialFocus selects the link, so a manual copy is one keystroke.

Confirm a delete

A delete from an open dialog is an AlertDialog mounted inside DialogContent. The second window forces its own backdrop, so it dims the first. DialogTitle truncate keeps a long title on one line with its descenders.

API Reference

PropTypeDefaultDescription
openDialogbooleanNoneControlled open state. Pair with onOpenChange.
onOpenChangeDialog(open: boolean) => voidNoneCalled when the open state changes.
defaultOpenDialogbooleanfalseUncontrolled initial open state.
modalDialogbooleantrueWhen true, blocks interaction with the rest of the page and traps focus.
showCloseButtonDialogContentbooleantrueShows the X close button in the top-right corner.
truncateDialogTitle, SheetTitle, DrawerTitlebooleanfalseOne line with an ellipsis, cut sideways only so the descenders of a cap-trimmed title stay visible. truncate as a class cannot do this: its overflow: hidden clips the descenders.
showCancelButtonDialogFooterbooleanfalseAdds a ghost Cancel button that closes the dialog, placed left of your actions.
initialFocusDialogContentboolean | React.RefObject<HTMLElement | null> | ((openType: InteractionType) => boolean | HTMLElement | null | void)trueThe element to focus when the dialog opens. Pass a ref to focus a specific element, or a function returning an element, true for default behavior, or false/null/void to leave focus alone.
renderDialogTrigger, DialogCloseReact.ReactElementNoneReplaces the default trigger or close button with your own element, e.g. render={<Button>Done</Button>}. Base UI merges props onto it.
openDialogbooleanControlled open state. Pair with onOpenChange.
onOpenChangeDialog(open: boolean) => voidCalled when the open state changes.
defaultOpenDialogboolean = falseUncontrolled initial open state.
modalDialogboolean = trueWhen true, blocks interaction with the rest of the page and traps focus.
showCloseButtonDialogContentboolean = trueShows the X close button in the top-right corner.
truncateDialogTitle, SheetTitle, DrawerTitleboolean = falseOne line with an ellipsis, cut sideways only so the descenders of a cap-trimmed title stay visible. truncate as a class cannot do this: its overflow: hidden clips the descenders.
showCancelButtonDialogFooterboolean = falseAdds a ghost Cancel button that closes the dialog, placed left of your actions.
initialFocusDialogContentboolean | React.RefObject<HTMLElement | null> | ((openType: InteractionType) => boolean | HTMLElement | null | void) = trueThe element to focus when the dialog opens. Pass a ref to focus a specific element, or a function returning an element, true for default behavior, or false/null/void to leave focus alone.
renderDialogTrigger, DialogCloseReact.ReactElementReplaces the default trigger or close button with your own element, e.g. render={<Button>Done</Button>}. Base UI merges props onto it.