Skip to the content

Controls and surfaces

alert-dialog

A dialog that interrupts with a question and waits for an answer: it has no close button and does not close on a click outside.

Primitives

When to use it

Confirming something that cannot be undone (deleting, sending, leaving with unsaved work). Focus starts on AlertDialogCancel, so Enter never destroys by accident. For a task with fields, or anything that may simply be dismissed, use dialog; where the action can be taken back, do it at once and offer undo-line.

See it working

The demonstration the browser checks drive, with the keyboard as well as the pointer. More of it is on the Primitives page.

Alert dialog

The project is kept.

Collapsible

The options are closed.

Hover card

The first published program was written by Ada Lovelace.

Menubar

Command: none yet. Ruler: hidden. Zoom: 100.

Navigation menu

Toolbar

Styles: none. Saved 0 times.

One-time code

Typed: nothing yet. Sent: nothing yet.

Password

At least twelve characters.

Words for screen readers

Add it

npx shadcn@latest add @rivier-ui/alert-dialog

The registry is named once in components.json: see Installation.

Example

Import it from @/components/ui/alert-dialog.

<AlertDialog>
  <AlertDialogTrigger asChild><Button variant="outline">Delete project</Button></AlertDialogTrigger>
  <AlertDialogContent>
    <AlertDialogHeader>
      <AlertDialogTitle>Delete this project?</AlertDialogTitle>
      <AlertDialogDescription>Its files and history are removed for everyone. This cannot be undone.</AlertDialogDescription>
    </AlertDialogHeader>
    <AlertDialogFooter>
      <AlertDialogCancel>Keep it</AlertDialogCancel>
      <AlertDialogAction variant="destructive" onClick={remove}>Delete</AlertDialogAction>
    </AlertDialogFooter>
  </AlertDialogContent>
</AlertDialog>

What it needs

A client component: it uses state, effects or browser behaviour. Packages: class-variance-authority, radix-ui.

It imports

Source

The whole file. Once copied it is yours: the registry item holds the same text.

src/components/ui/alert-dialog.tsx
"use client";
/**
 * @name alert-dialog
 * @category ui
 * @description A dialog that interrupts with a question and waits for an answer: it has no close button and does not close on a click outside.
 * @when Confirming something that cannot be undone (deleting, sending, leaving with unsaved work). Focus starts on AlertDialogCancel, so Enter never destroys by accident. For a task with fields, or anything that may simply be dismissed, use dialog; where the action can be taken back, do it at once and offer undo-line.
 * @example
 * <AlertDialog>
 *   <AlertDialogTrigger asChild><Button variant="outline">Delete project</Button></AlertDialogTrigger>
 *   <AlertDialogContent>
 *     <AlertDialogHeader>
 *       <AlertDialogTitle>Delete this project?</AlertDialogTitle>
 *       <AlertDialogDescription>Its files and history are removed for everyone. This cannot be undone.</AlertDialogDescription>
 *     </AlertDialogHeader>
 *     <AlertDialogFooter>
 *       <AlertDialogCancel>Keep it</AlertDialogCancel>
 *       <AlertDialogAction variant="destructive" onClick={remove}>Delete</AlertDialogAction>
 *     </AlertDialogFooter>
 *   </AlertDialogContent>
 * </AlertDialog>
 */
import { AlertDialog as AlertDialogPrimitive } from "radix-ui";
import type { VariantProps } from "class-variance-authority";
import type { ComponentProps } from "react";
import { buttonVariants } from "@/components/ui/button";
import { cn } from "@/lib/cn";

export function AlertDialog(props: ComponentProps<typeof AlertDialogPrimitive.Root>) {
  return <AlertDialogPrimitive.Root {...props} />;
}

export function AlertDialogTrigger(props: ComponentProps<typeof AlertDialogPrimitive.Trigger>) {
  return <AlertDialogPrimitive.Trigger data-slot="alert-dialog-trigger" {...props} />;
}

export function AlertDialogOverlay({ className, ...props }: ComponentProps<typeof AlertDialogPrimitive.Overlay>) {
  return (
    <AlertDialogPrimitive.Overlay
      data-slot="alert-dialog-overlay"
      className={cn(
        "fixed inset-0 z-50 bg-overlay ease-out animation-duration-(--duration-base) fade-in-0 fade-out-0 data-[state=closed]:animate-out data-[state=open]:animate-in motion-reduce:animate-none!",
        className,
      )}
      {...props}
    />
  );
}

/** The window, with its portal and overlay. Give it an AlertDialogTitle and an AlertDialogDescription: they are what is read out when it opens. */
export function AlertDialogContent({ className, ...props }: ComponentProps<typeof AlertDialogPrimitive.Content>) {
  return (
    <AlertDialogPrimitive.Portal>
      <AlertDialogOverlay />
      <AlertDialogPrimitive.Content
        data-slot="alert-dialog-content"
        className={cn(
          "fixed inset-0 z-50 m-auto flex h-fit max-h-[calc(100dvh-2rem)] w-[calc(100%-2rem)] max-w-md flex-col gap-4 overflow-y-auto rounded-surface border bg-card p-6 text-card-foreground ease-out animation-duration-(--duration-base) fade-in-0 fade-out-0 zoom-in-95 zoom-out-95 data-[state=closed]:animate-out data-[state=open]:animate-in motion-reduce:animate-none!",
          className,
        )}
        {...props}
      />
    </AlertDialogPrimitive.Portal>
  );
}

export function AlertDialogHeader({ className, ...props }: ComponentProps<"div">) {
  return <div data-slot="alert-dialog-header" className={cn("flex flex-col gap-1.5 text-start", className)} {...props} />;
}

export function AlertDialogTitle({ className, ...props }: ComponentProps<typeof AlertDialogPrimitive.Title>) {
  return <AlertDialogPrimitive.Title data-slot="alert-dialog-title" className={cn("text-base font-medium leading-tight", className)} {...props} />;
}

export function AlertDialogDescription({ className, ...props }: ComponentProps<typeof AlertDialogPrimitive.Description>) {
  return <AlertDialogPrimitive.Description data-slot="alert-dialog-description" className={cn("text-sm text-muted-foreground", className)} {...props} />;
}

/** Actions: stacked on phones with the action first, in a row at the end from `sm`. */
export function AlertDialogFooter({ className, ...props }: ComponentProps<"div">) {
  return <div data-slot="alert-dialog-footer" className={cn("flex flex-col-reverse gap-2 sm:flex-row sm:justify-end", className)} {...props} />;
}

type ButtonLook = VariantProps<typeof buttonVariants>;

/** The answer that goes ahead. It closes the dialog; `variant="destructive"` when it deletes. */
export function AlertDialogAction({ className, variant, size, ...props }: ComponentProps<typeof AlertDialogPrimitive.Action> & ButtonLook) {
  return <AlertDialogPrimitive.Action data-slot="alert-dialog-action" className={cn(buttonVariants({ variant, size }), className)} {...props} />;
}

/** The answer that changes nothing. Focus starts here, and Escape does the same. */
export function AlertDialogCancel({ className, variant = "outline", size, ...props }: ComponentProps<typeof AlertDialogPrimitive.Cancel> & ButtonLook) {
  return <AlertDialogPrimitive.Cancel data-slot="alert-dialog-cancel" className={cn(buttonVariants({ variant, size }), className)} {...props} />;
}