Skip to content

Pickers and overlays

Alert Dialog

A modal layer for a serious decision that needs attention.

When to use it: Use it before destructive or hard-to-undo actions.

On this page

Example

Delete this draft?

This action cannot be undone.

Alert Dialog.tsxtsx
import { AlertDialog, AlertDialogContent, Button, DialogTrigger } from "@comp0/react";

export function Example() {
  return (
    <AlertDialog>
      <DialogTrigger className="rounded border border-red-950/10 px-3 py-2.5 text-base text-red-700 sm:py-2 sm:text-sm dark:border-red-200/10 dark:text-red-300">
        Delete draft
      </DialogTrigger>
      <AlertDialogContent
        aria-label="Delete draft"
        className="m-auto w-[min(24rem,calc(100vw-2rem))] rounded-[min(1vw,12px)] bg-white p-4 text-zinc-900 shadow-2xl ring-1 ring-red-950/10 backdrop:bg-zinc-950/40 dark:bg-zinc-900 dark:text-zinc-50 dark:shadow-none dark:ring-red-200/10"
      >
        <div className="flex flex-col gap-2">
          <p className="text-base font-medium sm:text-sm">Delete this draft?</p>
          <p className="text-base text-zinc-600 sm:text-sm dark:text-zinc-400">
            This action cannot be undone.
          </p>
          <form method="dialog" className="flex gap-2 pt-2">
            <Button
              className="rounded border border-zinc-950/10 px-3 py-2.5 text-base text-zinc-800 sm:py-2 sm:text-sm dark:border-white/10 dark:text-zinc-100"
              type="submit"
            >
              Cancel
            </Button>
            <Button
              className="rounded bg-red-600 px-3 py-2.5 text-base text-white sm:py-2 sm:text-sm"
              type="submit"
            >
              Delete
            </Button>
          </form>
        </div>
      </AlertDialogContent>
    </AlertDialog>
  );
}

Anatomy

Dashed frames are invisible state providers; shaded shapes own real DOM. Numbered pins match the list below.

A wireframe sketch of the assembled component. Each numbered marker matches a part in the list that follows.

  1. AlertDialog

    Modal open-state provider for an urgent decision. Does not add a DOM element.

  2. DialogTrigger

    Button that opens the alert. Owns a DOM element.

  3. AlertDialogContent

    Modal alert dialog content. Owns a DOM element.

Step by step

  1. 1

    Add the main part

    Start AlertDialog with the button that begins the decision.

  2. 2

    Add the supporting parts

    Put plain consequences and clear choices in AlertDialogContent.

  3. 3

    Make the behavior clear

    Make the safe or cancel choice easy to find.

    Exampletsx
    <AlertDialog><DialogTrigger>Delete</DialogTrigger><AlertDialogContent aria-label="Delete item">This cannot be undone.</AlertDialogContent></AlertDialog>

Keyboard

Esc
Closes when dismissal is allowed.
Cycles inside the modal.

Forms and accessibility

No native form behavior; put a confirmation form inside when needed.

Accessibility checklist

  • Name the consequence, not just the button.
  • Put the safer choice where it is easy to find.
  • Do not use an alert dialog for ordinary, reversible actions.

API reference

Importtsx
import { AlertDialog, AlertDialogContent, DialogTrigger } from "@comp0/react";

AlertDialog

Context only

Modal open-state provider for an urgent decision.

PropTypeDescription
open / defaultOpenbooleanControlled or initial open state.
onToggle(open: boolean) => voidReceives the next open state.

DialogTrigger

DOM element

Button that opens the alert.

PropTypeDescription
asElementType | FragmentFragment merges the trigger onto your own element child.

AlertDialogContent

DOM element

Modal alert dialog content.

PropTypeDescription
aria-labelstringAccessible name for the decision.
portalbooleanRenders into document.body; on by default.
closedby"any" | "closerequest" | "none"Native dismissal policy; none blocks Escape when the decision must be explicit.

Style hooks

Attributes that appear while a state is true. Target them with Tailwind data variants such as data-open:bg-zinc-100, or with any CSS selector.

[data-open]

on DialogTrigger, AlertDialogContent

The alert is open.
:open

on AlertDialogContent

Native pseudo-class equivalent.

Keep exploring