Alert dialog
A confirmation that demands a choice, focusing the safe option first, built on Radix.
Use AlertDialog to confirm something that matters, usually a destructive or irreversible action such as deleting an account or cancelling a payment. It interrupts the user and asks for a clear yes or no. For anything else, such as editing a form, use a Dialog.
Installation
pnpm dlx shadcn@latest add https://components.abdulqadirmsingi.com/r/alert-dialog.json
This adds @radix-ui/react-alert-dialog and also installs Button, which styles its two buttons.
Usage
import {
AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent,
AlertDialogDescription, AlertDialogFooter, AlertDialogHeader,
AlertDialogTitle, AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
<AlertDialog>
<AlertDialogTrigger>Delete account</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Delete your account?</AlertDialogTitle>
<AlertDialogDescription>This cannot be undone.</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Keep account</AlertDialogCancel>
<AlertDialogAction variant="destructive" onClick={deleteAccount}>Delete</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>Do the work in the action's onClick. The dialog closes itself afterwards. Label the buttons with what they do ("Delete", "Keep account"), not "OK" and "Cancel".
Props
AlertDialog takes open, defaultOpen and onOpenChange. The parts take the Radix props and native element props.
| Prop | Type | Default | Description |
|---|---|---|---|
| variant | "default" | "destructive" | "default" | On AlertDialogAction. Use destructive when the action deletes or can't be undone. |
| className | string | – | Merged with the built-in classes; yours win on conflicts. |
Accessibility
- After a mouse click, focus returns to the button that opened it without drawing a focus ring (no stray border); after a key press the ring shows as normal. This comes from the small
focus-modalityhelper, which installs with this component. - It has
role="alertdialog"and is named by its title and described by its description. Include both. - Focus moves to Cancel, the safe choice, when it opens, so pressing Enter by reflex doesn't delete anything. It is trapped inside, and returns to the trigger on close.
- Clicking outside does not close it, because it demands a choice. Escape does cancel it.
- The rest of the page is hidden from screen readers and can't be scrolled while it is open.
- It fades and scales in using the
--duration-*tokens, which are almost zero underprefers-reduced-motion.
Tokens used
--background, --foreground, --muted-foreground, --border, --danger (through Button), --duration-base, --ease-out, --z-overlay, --z-modal.
Known limitations
- It does not animate out; it unmounts when closed.
- The action can't be asynchronous with a loading state yet: it closes straight away. For a slow action, use a Dialog with a loading Button.
- It renders into a portal at the end of
<body>.
Source
"use client";import * as AlertDialogPrimitive from "@radix-ui/react-alert-dialog";import type { ComponentProps } from "react";import { buttonVariants } from "@/components/ui/button";import { useQuietFocusRestore } from "@/lib/focus-modality";import { cn } from "@/lib/utils";/** Compose with AlertDialogTrigger, AlertDialogContent, AlertDialogTitle and the two buttons. */export const AlertDialog = AlertDialogPrimitive.Root;export const AlertDialogTrigger = AlertDialogPrimitive.Trigger;/** * Interrupts the user to confirm something that matters, usually a destructive action. * Unlike a Dialog it does NOT close on an outside click, and it puts focus on Cancel (the safe * choice) when it opens. Escape still cancels. Always include a title and a description. */export function AlertDialogContent({ className, children, onCloseAutoFocus, ...props}: ComponentProps<typeof AlertDialogPrimitive.Content>) { const quiet = useQuietFocusRestore(onCloseAutoFocus); return ( <AlertDialogPrimitive.Portal> <AlertDialogPrimitive.Overlay className="fixed inset-0 z-[var(--z-overlay)] bg-foreground/40 transition-opacity duration-[var(--duration-base)] starting:opacity-0" /> <AlertDialogPrimitive.Content className={cn( "fixed start-1/2 top-1/2 z-[var(--z-modal)] flex max-h-[calc(100dvh-2rem)] w-[calc(100%-2rem)] max-w-md -translate-x-1/2 -translate-y-1/2 flex-col gap-4 overflow-y-auto rounded-xl border border-border bg-background p-6 text-foreground transition-[opacity,transform,translate,scale,rotate] duration-[var(--duration-base)] ease-[var(--ease-out)] starting:scale-95 starting:opacity-0 rtl:translate-x-1/2 focus-visible:outline-none", className, )} onCloseAutoFocus={quiet.onCloseAutoFocus} {...props} > <span hidden ref={quiet.probe} /> {children} </AlertDialogPrimitive.Content> </AlertDialogPrimitive.Portal> );}export function AlertDialogHeader({ className, ...props }: ComponentProps<"div">) { return <div className={cn("flex flex-col gap-1.5", className)} {...props} />;}export function AlertDialogFooter({ className, ...props }: ComponentProps<"div">) { return ( <div className={cn("flex flex-col-reverse gap-2 sm:flex-row sm:justify-end", className)} {...props} /> );}export function AlertDialogTitle({ className, ...props}: ComponentProps<typeof AlertDialogPrimitive.Title>) { return ( <AlertDialogPrimitive.Title className={cn("text-lg font-semibold leading-tight", className)} {...props} /> );}export function AlertDialogDescription({ className, ...props}: ComponentProps<typeof AlertDialogPrimitive.Description>) { return ( <AlertDialogPrimitive.Description className={cn("text-sm text-muted-foreground", className)} {...props} /> );}/** Dismisses without doing anything. Radix focuses this one first. */export function AlertDialogCancel({ className, ...props}: ComponentProps<typeof AlertDialogPrimitive.Cancel>) { return ( <AlertDialogPrimitive.Cancel className={cn(buttonVariants({ variant: "outline" }), className)} {...props} /> );}export interface AlertDialogActionProps extends ComponentProps<typeof AlertDialogPrimitive.Action> { /** Use "destructive" when the action deletes or cannot be undone. */ variant?: "default" | "destructive";}/** Confirms the action and closes the dialog. Do the work in `onClick`. */export function AlertDialogAction({ className, variant = "default", ...props}: AlertDialogActionProps) { return ( <AlertDialogPrimitive.Action className={cn(buttonVariants({ variant }), className)} {...props} /> );}