Alert Dialog
An Alert Dialog interrupts users with a message that needs a response.
When to Use
- to confirm an action that can’t be undone, such as discarding a draft or deleting an attachment
- for a message that users can’t dismiss with a stray click
When Not to Use
See Dialog for a comparison of the Dialog, the Alert Dialog, the Sheet, and the Drawer. See Alert for a comparison of the Alert, the toast, and the Alert Dialog.
Anatomy
| Element | Usage |
|---|---|
| Overlay | Required |
| Popup | Required |
| Media | Optional |
| Title* | Required |
| Description | Optional |
| Action | Optional |
| Cancel* | Required |
| Footer | Optional |
* The title is the Alert Dialog’s accessible name. The cancel button is the only part that closes the Alert Dialog.
AlertDialogContent renders the portal and the overlay, so you don’t need to add them. An Alert Dialog has no close button in its corner. The figure is 352px wide, so it shows the layout that is used below 640px.
Sizes
| Size | Width | Header | Footer |
|---|---|---|---|
default | 384px from 640px, 320px below | Aligned to the start from 640px, centered below | A row at its end from 640px, a stack in reverse order below |
sm | 320px | Centered | Two columns of equal width |
See Layout for the widths of the other overlays.
Behavior
An Alert Dialog is always modal because Base UI’s Alert Dialog has no modal prop. Focus stays in the popup, and the page’s scroll is locked.
AlertDialogAction is a Button, and a click on it doesn’t close the Alert Dialog. To close the Alert Dialog, keep the state in the parent with open and onOpenChange, and set it in the action’s handler.
When an Alert Dialog opens, focus is moved to the first control in the popup. Write AlertDialogCancel before AlertDialogAction so that the cancel button receives focus and a second press of Enter cancels.
Media
AlertDialogMedia contains an icon above the title. Set aria-hidden on the icon inside it.
Content
| String | Rule | Example | Counterexample |
|---|---|---|---|
| Title | Ask the question that the Buttons answer | “Discard this draft?” | “Are you sure?” |
| Description | Say what is lost and whether it can be undone | “Your changes to the draft are lost. This cannot be undone.” | “This action is permanent.” |
| Action | Repeat the verb of the title | “Discard” | “Yes” |
Accessibility
Base UI’s Alert Dialog sets the role, keeps focus in the popup, and closes when users press Escape. You provide the title and the cancel button.
An Alert Dialog needs an AlertDialogTitle so that screen readers can announce what it is for, as
WCAG 2.2 SC 4.1.2 Name, Role, Value requires.
Keep an AlertDialogCancel in the popup. A click on the overlay doesn’t close an Alert Dialog, so if there is no cancel button, Escape is the only way to close it, and users on a touch screen have no Escape key. See Dialog for the recommendation.
Installation
npx shadcn@latest add @summit/alert-dialogThe CLI also adds @summit/button.
Usage
Keep the open state in the parent because the action’s handler has to close the Alert Dialog.
import { useState } from 'react';
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from '@/components/ui/alert-dialog';
import { Button } from '@/components/ui/button';
const [open, setOpen] = useState(false);
<AlertDialog open={open} onOpenChange={setOpen}>
<AlertDialogTrigger render={<Button variant="outline" />}>Discard draft</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Discard this draft?</AlertDialogTitle>
<AlertDialogDescription>Your changes to the draft are lost. This cannot be undone.</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction
onClick={() => {
discardDraft();
setOpen(false);
}}
>
Discard
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>;If there is no AlertDialogTrigger, anything that sets open can open the Alert Dialog, such as the onClick of an item in a Dropdown Menu.
A Destructive Action
AlertDialogAction accepts the props of a Button. If the action deletes, set variant="destructive".
<AlertDialogAction variant="destructive" onClick={deleteAttachment}>
Delete
</AlertDialogAction>With Media
Place AlertDialogMedia first in the header. If you want it to stay centered above the title at every width, set size="sm" on AlertDialogContent.
import { TrashIcon } from '@phosphor-icons/react';
<AlertDialogContent size="sm">
<AlertDialogHeader>
<AlertDialogMedia>
<TrashIcon aria-hidden />
</AlertDialogMedia>
<AlertDialogTitle>Delete attachment?</AlertDialogTitle>
<AlertDialogDescription>The file is removed from this message right away.</AlertDialogDescription>
</AlertDialogHeader>
</AlertDialogContent>;API Reference
The parts accept the props of their matching parts in Base UI’s Alert Dialog, where AlertDialogContent is AlertDialog.Popup, AlertDialogOverlay is AlertDialog.Backdrop, and AlertDialogCancel is AlertDialog.Close. AlertDialogHeader, AlertDialogMedia, and AlertDialogFooter accept the props of a div. See the Base UI Alert Dialog documentation for open, onOpenChange, initialFocus, and finalFocus.
AlertDialogContent
| Prop | Type | Default | Description |
|---|---|---|---|
size | 'default' | 'sm' | 'default' | The width, and the layout of the header and the footer |
AlertDialogCancel
| Prop | Type | Default | Description |
|---|---|---|---|
variant | The variant of Button | 'outline' | The variant of the Button |
size | The size of Button | 'default' | The size of the Button |
AlertDialogAction
AlertDialogAction adds no props. It accepts the props of a Button.