A modal that interrupts the flow to confirm an action that is hard or impossible to undo, built on Radix Alert Dialog.
Stableimport { AlertDialog } from "@repo/ui/alert-dialog";Source
Select the button to open the dialog. Only the buttons in the footer and Escape close it. A click on the backdrop does not.
When to use
To confirm a destructive or irreversible action: deleting a task, cancelling a protocol, discarding unsaved work.
To confirm an action that affects other people, such as returning a task to a reviewer.
When the user must answer before anything else on the page can happen.
When not to use
Collecting information or any form field. Use Dialog. An alert dialog has no close button and cannot be dismissed by a click outside, so it is a poor fit for a task.
Details, filters or long content. Use a Drawer or a Sheet.
A message that needs no answer. Use Alert or a toast.
Actions that can be undone. Run them and offer an undo instead of asking first.
The element that opens it. Use asChild with a Button.
AlertDialogContent
The centered panel and its backdrop.
AlertDialogHeader
Groups the title and the description.
AlertDialogTitle
Names the dialog. Required for assistive technology.
AlertDialogDescription
The consequence in one or two sentences. Required.
AlertDialogFooter
The actions. Cancel first, confirm last.
AlertDialogCancel
Closes without acting. Styled as a secondary Button. Receives focus when the dialog opens.
AlertDialogAction
Closes and confirms. Styled as an invertedButton unless you pass another variant.
Examples
Destructive confirmation
Use variant="critical" on AlertDialogAction and say what will be lost.
Neutral confirmation
Keep the default inverted variant when the action is not destructive.
Content
Write the title as the question: “Excluir a tarefa “Revisar contestação”?”, not “Atenção”.
The description states what is lost or who is affected, not a repetition of the title.
Name the action on both buttons: “Excluir tarefa” and “Manter tarefa”. Avoid “Sim”, “Não” and “OK”.
Put the cancel button first and the confirm button last.
Do and don’t
DoName the action on both buttons so the choice is clear without rereading.
Don'tDon't ask “Tem certeza?” with Sim and Não. The user has to reread the question to know what each button does.
Accessibility
Built on Radix Alert Dialog: the content has role="alertdialog", is aria-labelledby the title and aria-describedby the description. Screen readers announce both when it opens.
Focus moves into the dialog on open, to AlertDialogCancel, so a stray Enter does not confirm a destructive action. Focus is trapped inside and returns to the trigger on close.
Escape closes the dialog. A click or tap on the backdrop does not, by design: the user has to answer.
The page behind is inert while the dialog is open.
AlertDialogTitle and AlertDialogDescription are required. Radix warns in development when they are missing.
Code
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger,} from "@repo/ui/alert-dialog";import { Button } from "@repo/ui/button";<AlertDialog> <AlertDialogTrigger asChild> <Button variant="critical">Excluir tarefa</Button> </AlertDialogTrigger> <AlertDialogContent> <AlertDialogHeader> <AlertDialogTitle> Excluir a tarefa “Revisar contestação”? </AlertDialogTitle> <AlertDialogDescription> Os documentos e comentários desta tarefa também serão removidos. </AlertDialogDescription> </AlertDialogHeader> <AlertDialogFooter> <AlertDialogCancel>Cancelar</AlertDialogCancel> <AlertDialogAction variant="critical">Excluir tarefa</AlertDialogAction> </AlertDialogFooter> </AlertDialogContent></AlertDialog>;
When the action is asynchronous, use a Button instead of AlertDialogAction and close the dialog yourself with open and onOpenChange, as the app does for loading states. AlertDialogAction closes the dialog as soon as it is pressed.
Props
AlertDialog
Prop
Type
Default
Description
defaultOpen
boolean
false
Initial open state when the dialog is uncontrolled
onOpenChange
(open: boolean) => void
—
Called when the dialog opens or closes
open
boolean
—
Controlled open state
AlertDialogContent
Prop
Type
Default
Description
forceMount
boolean
—
Keeps the content in the DOM while closed, for animation libraries.
onEscapeKeyDown
(event: KeyboardEvent) => void
—
Called when `Escape` is pressed. Call `event.preventDefault()` to keep the dialog open.
onOpenAutoFocus
(event: Event) => void
—
Called when focus moves into the dialog. Prevent it to choose another element.
AlertDialogAction, AlertDialogCancel
Prop
Type
Default
Description
variant
ButtonProps['variant']
"inverted" (action), "secondary" (cancel)
Any `Button` variant. Use `critical` for destructive actions.
size
ButtonProps['size']
—
Any `Button` size.
asChild
boolean
false
Renders the child element instead of a button.
AlertDialogTrigger
Prop
Type
Default
Description
asChild
boolean
false
Renders the child element instead of a button. Use it with a `Button`.
Known issues
Issue
Effect
The backdrop is bg-black/80, a translucent fill.
The repository avoids transparency in UI.
AlertDialogTitle and AlertDialogDescription use raw classes (text-lg, text-sm) instead of Typography.
The text does not follow the type scale tokens.
AlertDialogTitle sets no text color and uses flex.
The color depends on what the dialog inherits. A long title can lay out as a flex row.
AlertDialogFooter separates buttons with space-x-2 and stacks them in reverse order below sm.
Spacing is not on the token scale. On narrow widths the main action appears first, unlike the desktop order.
AlertDialogAction and AlertDialogCancel close the dialog before async work finishes.
Loading states need a plain Button and a controlled open, as the app already does.