Dialog
A modal dialog on Base UI Dialog: 16px tray radius, scale-in on desktop, a popped card on phones.
For a short task that has to finish or be dismissed before the page goes on. A decision that must not be dismissed by a click outside is an @khaeli/alert-dialog; a create or edit form is a @khaeli/form-dialog.
Default
import { Button } from "@/components/ui/button"
import {
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog"
export default function DialogDefault() {
return (
<Dialog>
<DialogTrigger render={<Button variant="outline" />}>Archive project</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Archive Website refresh</DialogTitle>
<DialogDescription>
The project leaves the sidebar and its tasks become read-only. You can restore it from the archive at any
time.
</DialogDescription>
</DialogHeader>
<DialogFooter>
<DialogClose render={<Button variant="outline" />}>Cancel</DialogClose>
<DialogClose render={<Button />}>Archive</DialogClose>
</DialogFooter>
</DialogContent>
</Dialog>
)
}
With a form
"use client"
import { useState } from "react"
import { Button } from "@/components/ui/button"
import {
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog"
import { Field, FieldLabel } from "@/components/ui/field"
import { Input } from "@/components/ui/input"
export default function DialogWithAForm() {
const [open, setOpen] = useState(false)
const [name, setName] = useState("Website refresh")
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger render={<Button variant="outline" />}>Rename {name}</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Rename project</DialogTitle>
<DialogDescription>The new name shows in the sidebar and in every link to the project.</DialogDescription>
</DialogHeader>
<form
id="rename-project"
onSubmit={(event) => {
event.preventDefault()
const next = new FormData(event.currentTarget).get("name")
if (typeof next === "string" && next.trim()) setName(next.trim())
setOpen(false)
}}
>
<Field>
<FieldLabel htmlFor="rename-project-name">Name</FieldLabel>
<Input id="rename-project-name" name="name" defaultValue={name} autoComplete="off" required />
</Field>
</form>
<DialogFooter>
<DialogClose render={<Button variant="outline" />}>Cancel</DialogClose>
<Button type="submit" form="rename-project">
Rename
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
)
}
Close button in the footer
import { Button } from "@/components/ui/button"
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog"
const changes = [
"Invoices export to CSV with one row per line item.",
"Tasks keep their order when a project is duplicated.",
"The release calendar starts the week on Monday.",
]
export default function DialogCloseButtonInTheFooter() {
return (
<Dialog>
<DialogTrigger render={<Button variant="outline" />}>What changed in 4.2</DialogTrigger>
<DialogContent showCloseButton={false}>
<DialogHeader>
<DialogTitle>Release 4.2</DialogTitle>
<DialogDescription>Three changes shipped on 12 November.</DialogDescription>
</DialogHeader>
<ul className="flex list-disc flex-col gap-1 pl-4">
{changes.map((change) => (
<li key={change}>{change}</li>
))}
</ul>
<DialogFooter showCloseButton />
</DialogContent>
</Dialog>
)
}
Installation
npx shadcn@latest add @khaeli/dialogbunx --bun shadcn@latest add @khaeli/dialogInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- Registry items
- npm packages
- @base-ui/react ^1.7.0
- @keyline-icons/react ^2.2.0
Keyboard
EnterSpace- On the trigger, opens the dialog. Focus moves to the first control inside it: the first field, or the first button when it has no field.
TabShift Tab- Move through the controls inside the dialog and wrap at either end. The close button in the corner comes last. Focus cannot leave the dialog while it is open.
Escape- Closes the dialog and returns focus to the trigger. A click on the dimmed page does the same.
Accessible name
- The dialog is named by
DialogTitleand described byDialogDescription: Base UI wires both witharia-labelledbyandaria-describedby. Always render a title. - The close button in the corner is named Close by the item.
showCloseButton={false}removes it, so keep another way out in the footer: aDialogClose, orDialogFooter showCloseButton. DialogTriggerandDialogClosetake your button throughrender; the name is the button's text.- While the dialog is open the rest of the page is hidden from assistive technology with
aria-hidden.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/dialog.json