Containment · Material 3: Dialogs
Dialog
A window that needs attention. Flies out of the button that opened it and back into it. Alert dialog is a preset for irreversible decisions.
Examples and variants
Basic
Title, text and actions.
function DialogBasic() {
return (
<Dialog>
<DialogTrigger render={<Button />}>Open dialog</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Invite people</DialogTitle>
<DialogDescription>It flies out of the button you clicked and returns to it.</DialogDescription>
</DialogHeader>
<DialogFooter>
<DialogClose render={<Button variant="secondary" />}>Cancel</DialogClose>
<DialogClose render={<Button />}>Invite</DialogClose>
</DialogFooter>
</DialogContent>
</Dialog>
)
}Sizes
sm, md, lg (and full).
function DialogSizes() {
return (
<div className="flex flex-wrap justify-center gap-3">
{(["sm", "md", "lg"] as const).map((size) => (
<Dialog key={size}>
<DialogTrigger render={<Button variant="secondary" />}>Size {size}</DialogTrigger>
<DialogContent size={size}>
<DialogHeader>
<DialogTitle>Size {size}</DialogTitle>
<DialogDescription>Widths come from --dialog-width-{size}.</DialogDescription>
</DialogHeader>
<DialogFooter>
<DialogClose render={<Button />}>Done</DialogClose>
</DialogFooter>
</DialogContent>
</Dialog>
))}
</div>
)
}Alert preset
No dismiss on scrim click (it shakes); focus starts on Cancel.
function DialogAlert() {
return (
<AlertDialog>
<AlertDialogTrigger render={<Button tone="danger" />}>Delete project</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Delete Northwind?</AlertDialogTitle>
<AlertDialogDescription>This can’t be undone. Clicking outside shakes the dialog.</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel render={<Button variant="secondary" />}>Cancel</AlertDialogCancel>
<AlertDialogAction render={<Button tone="danger" confirm="hold" />}>Hold to delete</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
)
}Spec drawing
Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.
Install
With the CLI
The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/dialog.json
npx shadcn@latest add https://isotopi.com/r/alert-dialog.jsonBy hand
No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.
Install the packages it uses.
Terminalnpm install @base-ui/react cnCopy these 2 files to the same paths in your project. Imports use the
@/alias for your project root.components/ui/dialog.tsxdialog
components/ui/dialog.tsx"use client" import * as React from "react" import { Dialog as DialogPrimitive } from "@base-ui/react/dialog" import { cn } from "cn" import { motionLevel, usePortalMotion } from "@/lib/overlay" type Point = { x: number; y: number } const PointContext = React.createContext<{ point: Point | null setPoint: (point: Point) => void } | null>(null) function Dialog(props: DialogPrimitive.Root.Props) { const [point, setPoint] = React.useState<Point | null>(null) return ( <PointContext.Provider value={{ point, setPoint }}> <DialogPrimitive.Root {...props} /> </PointContext.Provider> ) } function DialogTrigger({ onClick, ...props }: DialogPrimitive.Trigger.Props) { const context = React.useContext(PointContext) return ( <DialogPrimitive.Trigger data-slot="dialog-trigger" {...props} onClick={(event) => { const rect = event.currentTarget.getBoundingClientRect() context?.setPoint({ x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 }) onClick?.(event) }} /> ) } function DialogClose({ ref, ...props }: DialogPrimitive.Close.Props & { ref?: React.Ref<HTMLButtonElement> }) { return <DialogPrimitive.Close ref={ref} data-slot="dialog-close" {...props} /> } function DialogTitle({ className, ...props }: DialogPrimitive.Title.Props) { return <DialogPrimitive.Title className={cn("text-title", className)} {...props} /> } function DialogHeader({ className, ...props }: React.ComponentProps<"div">) { return <div data-slot="dialog-header" className={cn("flex flex-col gap-1", className)} {...props} /> } function DialogFooter({ className, ...props }: React.ComponentProps<"div">) { return ( <div data-slot="dialog-footer" className={cn("flex flex-wrap justify-end gap-inline-sm pt-1", className)} {...props} /> ) } function DialogDescription({ className, ...props }: DialogPrimitive.Description.Props) { return ( <DialogPrimitive.Description className={cn("text-sm text-muted-foreground", className)} {...props} /> ) } function DialogContent({ className, children, firm = false, shake = false, size = "sm", ...props }: DialogPrimitive.Popup.Props & { firm?: boolean shake?: boolean /** sm 400 · md 520 · lg 720 · full (phones: always near full width). */ size?: "sm" | "md" | "lg" | "full" }) { const ref = React.useRef<HTMLDivElement>(null) const context = React.useContext(PointContext) usePortalMotion(ref) const level = typeof window === "undefined" ? "liquid" : motionLevel() const point = context?.point const onScreen = point && point.x >= 0 && point.y >= 0 && point.x <= window.innerWidth && point.y <= window.innerHeight const fly = level === "liquid" && onScreen return ( <DialogPrimitive.Portal> <DialogPrimitive.Backdrop className={cn("overlay-scrim fixed inset-0 z-modal", firm && "overlay-scrim-firm")} onMouseDown={() => { if (!shake || !ref.current) return ref.current.classList.remove("alert-shake") void ref.current.offsetWidth ref.current.classList.add("alert-shake") }} /> <DialogPrimitive.Popup ref={ref} data-slot="dialog-content" className={cn( "overlay-surface overlay-grow dialog-fly fixed top-1/2 left-1/2 z-modal max-w-[calc(100vw-var(--space-6))] -translate-x-1/2 -translate-y-1/2 rounded-(--dialog-corner) p-(--dialog-padding) text-foreground", size === "sm" && "w-(--dialog-width-sm)", size === "md" && "w-(--dialog-width-md)", size === "lg" && "w-(--dialog-width-lg)", size === "full" && "h-[calc(100vh-var(--space-6))] w-[calc(100vw-var(--space-6))]", firm && "dialog-firm", className )} style={ fly ? ({ "--from-x": `${point.x - window.innerWidth / 2}px`, "--from-y": `${point.y - window.innerHeight / 2}px`, } as React.CSSProperties) : undefined } {...props} > <div className="dialog-copy flex flex-col gap-stack-md">{children}</div> </DialogPrimitive.Popup> </DialogPrimitive.Portal> ) } export { Dialog, DialogTrigger, DialogClose, DialogContent, DialogHeader, DialogFooter, DialogTitle, DialogDescription, }components/ui/alert-dialog.tsxalert-dialog
components/ui/alert-dialog.tsx"use client" import * as React from "react" import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog" // Alert Dialog is a preset of Dialog (component-model.md §3): role alertdialog, // no dismiss on scrim click (it shakes instead), focus starts on Cancel. const CancelContext = React.createContext<React.RefObject<HTMLButtonElement | null> | null>(null) function AlertDialog(props: React.ComponentProps<typeof Dialog>) { return <Dialog disablePointerDismissal {...props} /> } function AlertDialogTrigger(props: React.ComponentProps<typeof DialogTrigger>) { return <DialogTrigger {...props} /> } function AlertDialogContent({ children, ...props }: React.ComponentProps<typeof DialogContent>) { const cancelRef = React.useRef<HTMLButtonElement>(null) return ( <CancelContext.Provider value={cancelRef}> <DialogContent firm shake role="alertdialog" initialFocus={cancelRef} {...props}> {children} </DialogContent> </CancelContext.Provider> ) } function AlertDialogTitle(props: React.ComponentProps<typeof DialogTitle>) { return <DialogTitle {...props} /> } function AlertDialogDescription(props: React.ComponentProps<typeof DialogDescription>) { return <DialogDescription {...props} /> } function AlertDialogCancel(props: React.ComponentProps<typeof DialogClose>) { const cancelRef = React.useContext(CancelContext) return <DialogClose ref={cancelRef ?? undefined} {...props} /> } /** Closes the dialog. Render your Button through it: render={<Button tone="danger" confirm="hold" />}. */ function AlertDialogAction(props: React.ComponentProps<typeof DialogClose>) { return <DialogClose data-slot="alert-dialog-action" {...props} /> } const AlertDialogHeader = DialogHeader const AlertDialogFooter = DialogFooter export { AlertDialog, AlertDialogHeader, AlertDialogFooter, AlertDialogTrigger, AlertDialogContent, AlertDialogTitle, AlertDialogDescription, AlertDialogCancel, AlertDialogAction, }
Imports
What the examples on this page import:
import {
import { Button } from "@/components/ui/button"
import {Anatomy
- 1Scrim · --dialog-scrim.
- 2Container · --dialog-container, --dialog-corner, --dialog-padding, --dialog-elevation, --dialog-width-*.
- 3Header · Title and description.
- 4Body · Any content.
- 5Footer · Buttons, end-aligned.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
open / defaultOpen / onOpenChange | boolean | — | Controlled or uncontrolled. |
DialogContent size | "sm" | "md" | "lg" | "full" | "sm" | Width. |
AlertDialogAction render | ReactElement | — | Usually <Button tone="danger" confirm="hold" />. |
Tokens
Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.
| Token | Points to | Light | Dark |
|---|---|---|---|
--dialog-container | var(--surface-container-raised) | oklch(1 0 0) | oklch(0.22 0 0) |
--dialog-corner | var(--shape-lg) | 22px | same |
--dialog-padding | var(--space-inset-dialog) | 24px | same |
--dialog-gap | var(--space-4) | 16px | same |
--dialog-elevation | var(--elevation-overlay) | 0 1px 2px oklch(0.22 0.01 260 / 6%), 0 2.8px 2.2px oklch(0.22 0.01 260 / 3.4%), 0 6.7px 5.3px oklch(0.22 0.01 260 / 4.8%), 0 12.5px 10px oklch(0.22 0.01 260 / 6%), 0 22.3px 17.9px oklch(0.22 0.01 260 / 7.2%), 0 41.8px 33.4px oklch(0.22 0.01 260 / 8.6%), 0 100px 80px oklch(0.22 0.01 260 / 12%) | 0 16px 36px oklch(0 0 0 / 55%), 0 32px 72px oklch(0 0 0 / 50%) |
--dialog-width-sm | var(--layout-dialog-sm) | 400px | same |
--dialog-width-md | var(--layout-dialog-md) | 520px | same |
--dialog-width-lg | var(--layout-dialog-lg) | 720px | same |
--dialog-scrim | var(--color-bg-overlay) | oklch(0.22 0.01 260 / 40%) | oklch(0 0 0 / 60%) |
Motion
| Trigger | What happens | Tokens |
|---|---|---|
| Open | Flies out of the trigger and settles; the scrim fades in. | --duration-fast, --ease-bounce |
| Close | Returns to the trigger, a little faster. | --duration-quick, --ease-in |
| Scrim click (alert) | A polite 2px shake instead of closing. | --duration-fast |
Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.
Accessibility
- Escape closes and focus returns to the trigger.
- The trigger is always a Button (render={<Button />}).
- Focus is trapped inside; the title labels the dialog.
- Alert dialogs use role=alertdialog.
Usage
Do
- Use for decisions that block the task.
- Name the thing in the title.
Don’t
- Don't confirm reversible actions; offer Undo instead.
Code
"use client"
import * as React from "react"
import { Dialog as DialogPrimitive } from "@base-ui/react/dialog"
import { cn } from "cn"
import { motionLevel, usePortalMotion } from "@/lib/overlay"
type Point = { x: number; y: number }
const PointContext = React.createContext<{
point: Point | null
setPoint: (point: Point) => void
} | null>(null)
function Dialog(props: DialogPrimitive.Root.Props) {
const [point, setPoint] = React.useState<Point | null>(null)
return (
<PointContext.Provider value={{ point, setPoint }}>
<DialogPrimitive.Root {...props} />
</PointContext.Provider>
)
}
function DialogTrigger({ onClick, ...props }: DialogPrimitive.Trigger.Props) {
const context = React.useContext(PointContext)
return (
<DialogPrimitive.Trigger
data-slot="dialog-trigger"
{...props}
onClick={(event) => {
const rect = event.currentTarget.getBoundingClientRect()
context?.setPoint({ x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 })
onClick?.(event)
}}
/>
)
}
function DialogClose({ ref, ...props }: DialogPrimitive.Close.Props & { ref?: React.Ref<HTMLButtonElement> }) {
return <DialogPrimitive.Close ref={ref} data-slot="dialog-close" {...props} />
}
function DialogTitle({ className, ...props }: DialogPrimitive.Title.Props) {
return <DialogPrimitive.Title className={cn("text-title", className)} {...props} />
}
function DialogHeader({ className, ...props }: React.ComponentProps<"div">) {
return <div data-slot="dialog-header" className={cn("flex flex-col gap-1", className)} {...props} />
}
function DialogFooter({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="dialog-footer"
className={cn("flex flex-wrap justify-end gap-inline-sm pt-1", className)}
{...props}
/>
)
}
function DialogDescription({ className, ...props }: DialogPrimitive.Description.Props) {
return (
<DialogPrimitive.Description
className={cn("text-sm text-muted-foreground", className)}
{...props}
/>
)
}
function DialogContent({
className,
children,
firm = false,
shake = false,
size = "sm",
...props
}: DialogPrimitive.Popup.Props & {
firm?: boolean
shake?: boolean
/** sm 400 · md 520 · lg 720 · full (phones: always near full width). */
size?: "sm" | "md" | "lg" | "full"
}) {
const ref = React.useRef<HTMLDivElement>(null)
const context = React.useContext(PointContext)
usePortalMotion(ref)
const level = typeof window === "undefined" ? "liquid" : motionLevel()
const point = context?.point
const onScreen =
point &&
point.x >= 0 &&
point.y >= 0 &&
point.x <= window.innerWidth &&
point.y <= window.innerHeight
const fly = level === "liquid" && onScreen
return (
<DialogPrimitive.Portal>
<DialogPrimitive.Backdrop
className={cn("overlay-scrim fixed inset-0 z-modal", firm && "overlay-scrim-firm")}
onMouseDown={() => {
if (!shake || !ref.current) return
ref.current.classList.remove("alert-shake")
void ref.current.offsetWidth
ref.current.classList.add("alert-shake")
}}
/>
<DialogPrimitive.Popup
ref={ref}
data-slot="dialog-content"
className={cn(
"overlay-surface overlay-grow dialog-fly fixed top-1/2 left-1/2 z-modal max-w-[calc(100vw-var(--space-6))] -translate-x-1/2 -translate-y-1/2 rounded-(--dialog-corner) p-(--dialog-padding) text-foreground",
size === "sm" && "w-(--dialog-width-sm)",
size === "md" && "w-(--dialog-width-md)",
size === "lg" && "w-(--dialog-width-lg)",
size === "full" && "h-[calc(100vh-var(--space-6))] w-[calc(100vw-var(--space-6))]",
firm && "dialog-firm",
className
)}
style={
fly
? ({
"--from-x": `${point.x - window.innerWidth / 2}px`,
"--from-y": `${point.y - window.innerHeight / 2}px`,
} as React.CSSProperties)
: undefined
}
{...props}
>
<div className="dialog-copy flex flex-col gap-stack-md">{children}</div>
</DialogPrimitive.Popup>
</DialogPrimitive.Portal>
)
}
export {
Dialog,
DialogTrigger,
DialogClose,
DialogContent,
DialogHeader,
DialogFooter,
DialogTitle,
DialogDescription,
}
"use client"
import * as React from "react"
import {
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog"
// Alert Dialog is a preset of Dialog (component-model.md §3): role alertdialog,
// no dismiss on scrim click (it shakes instead), focus starts on Cancel.
const CancelContext = React.createContext<React.RefObject<HTMLButtonElement | null> | null>(null)
function AlertDialog(props: React.ComponentProps<typeof Dialog>) {
return <Dialog disablePointerDismissal {...props} />
}
function AlertDialogTrigger(props: React.ComponentProps<typeof DialogTrigger>) {
return <DialogTrigger {...props} />
}
function AlertDialogContent({ children, ...props }: React.ComponentProps<typeof DialogContent>) {
const cancelRef = React.useRef<HTMLButtonElement>(null)
return (
<CancelContext.Provider value={cancelRef}>
<DialogContent firm shake role="alertdialog" initialFocus={cancelRef} {...props}>
{children}
</DialogContent>
</CancelContext.Provider>
)
}
function AlertDialogTitle(props: React.ComponentProps<typeof DialogTitle>) {
return <DialogTitle {...props} />
}
function AlertDialogDescription(props: React.ComponentProps<typeof DialogDescription>) {
return <DialogDescription {...props} />
}
function AlertDialogCancel(props: React.ComponentProps<typeof DialogClose>) {
const cancelRef = React.useContext(CancelContext)
return <DialogClose ref={cancelRef ?? undefined} {...props} />
}
/** Closes the dialog. Render your Button through it: render={<Button tone="danger" confirm="hold" />}. */
function AlertDialogAction(props: React.ComponentProps<typeof DialogClose>) {
return <DialogClose data-slot="alert-dialog-action" {...props} />
}
const AlertDialogHeader = DialogHeader
const AlertDialogFooter = DialogFooter
export {
AlertDialog,
AlertDialogHeader,
AlertDialogFooter,
AlertDialogTrigger,
AlertDialogContent,
AlertDialogTitle,
AlertDialogDescription,
AlertDialogCancel,
AlertDialogAction,
}