Skip to content
Thrum

Alert dialog

A dialog that stops the work to ask one question and waits for the answer: delete, discard, revoke. A press outside does not close it, and focus starts on Cancel. For a form or details that can be dismissed freely, use a dialog; for a small confirmation next to its button, a confirm popover.

Install

npx shadcn@latest add https://thrumui.dev/r/alert-dialog.json

Adds components/ui/alert-dialog.tsx and installs @base-ui/react@1.9.0. Nothing else in your project changes.

import { AlertDialog } from "@/components/ui/alert-dialog";

Props

PropTypeDefaultDescription
AlertDialog: openboolean–Whether it is open, when the parent holds the state. Use it with onOpenChange.
AlertDialog: defaultOpenbooleanDefault falseWhether it starts open, when the dialog holds its own state.
AlertDialog: onOpenChange(open: boolean) => void–Called when the dialog opens or closes, whatever caused it.
AlertDialog: soundbooleanDefault truePlay a soft sound on opening and on closing, if the visitor has sound on.
AlertDialogTriggerbutton props–The button that opens the dialog. Style it with buttonClass().
AlertDialogContent: initialFocusRefObject<HTMLElement> | boolean–Where the focus goes on opening. The default is AlertDialogCancel, or the first control when there is none.
AlertDialogContent: classNamestring–Extra classes for the window. max-w-lg makes it wider; the default is 448 pixels.
AlertDialogHeader, AlertDialogFooterComponentProps<"div">–Layout for the title block and for the two answers. The footer stacks its buttons on a phone.
AlertDialogTitleheading props–The question, announced when the dialog opens. Every alert dialog needs one.
AlertDialogDescriptionparagraph props–What the action does and whether it can be undone. It is read after the title.
AlertDialogAction: variantButtonVariantDefault "primary"The look of the button that goes ahead. Use destructive when the action deletes or cannot be undone.
AlertDialogAction: onClick(event) => void–Runs on the press, before the dialog closes.
AlertDialogCancelbutton props–The button that backs out. It is an outline button and takes the focus when the dialog opens.

Motion and sound

Motion

  • The window fades in and grows from 96% to full size over 150 ms. The veil fades with it.
  • Closing plays the same change backwards. Closing during the opening reverses it from where it is.
  • Reduced motion: the window and the veil appear and leave at once.

Sound

  • A soft rising tone when the dialog opens.
  • A soft falling tone when it closes, whichever answer closed it.

Sound is off until the visitor turns it on. Pass sound={false} to keep this component silent. How sound works

Accessibility

  • It follows the WAI-ARIA alert dialog pattern: role alertdialog, named by its title and described by its description.
  • While it is open the rest of the page cannot be reached by pointer, keyboard or screen reader, and the page behind does not scroll.
  • A press on the veil does nothing. The dialog closes only through one of its buttons or Escape, so an answer is never given by accident.
  • Focus starts on Cancel, the answer that changes nothing, and goes back to the trigger when the dialog closes.
  • The destructive action differs from Cancel by its filled colour and by its words. Write the action as a verb: Delete project, not OK.
  • A long explanation scrolls inside the window, which never grows taller than the screen.
  • Under a glass material the window is translucent. Its tint keeps the text at 4.5 to 1 over any page.
KeysAction
Enter / SpaceOn the trigger: open the dialog. Focus moves to Cancel.
Tab / Shift TabMove between Cancel and the action. Focus cannot leave the dialog.
Enter / SpacePress the focused button. Both close the dialog; the action runs its onClick first.
EscClose the dialog without running the action. Focus returns to the trigger.

Source

components/ui/alert-dialog.tsx
"use client";

import { createContext, useContext, useRef, type ComponentProps, type RefObject } from "react";
import { AlertDialog as AlertDialogPrimitive } from "@base-ui/react/alert-dialog";

import { cn } from "@/lib/utils";
import { play } from "@/lib/thrum-sound";
import { BUTTON_SURFACE, buttonClass, type ButtonVariant } from "@/components/ui/button";

/** The Cancel button registers itself here, so the window can hand it the focus when it opens. */
const CancelRef = createContext<RefObject<HTMLButtonElement | null> | null>(null);

export type AlertDialogProps = AlertDialogPrimitive.Root.Props & {
  /** Play a soft sound when the dialog opens and when it closes, if the visitor has sound on. */
  sound?: boolean;
};

/**
 * A dialog that interrupts with a question and waits for the answer: delete, discard, sign out everywhere.
 * A press outside does not close it; only its own buttons and Escape do. For anything that is not a
 * yes-or-no question, use a dialog.
 */
export function AlertDialog({ sound = true, onOpenChange, ...props }: AlertDialogProps) {
  const cancel = useRef<HTMLButtonElement>(null);
  return (
    <CancelRef value={cancel}>
      <AlertDialogPrimitive.Root
        onOpenChange={(open, details) => {
          if (sound) play(open ? "open" : "close");
          onOpenChange?.(open, details);
        }}
        {...props}
      />
    </CancelRef>
  );
}

/** The button that opens the dialog. Style it with `buttonClass()` or pass a `Button` through `render`. */
export function AlertDialogTrigger(props: AlertDialogPrimitive.Trigger.Props) {
  return <AlertDialogPrimitive.Trigger data-slot="alert-dialog-trigger" {...props} />;
}

/**
 * The window itself, with the veil behind it. It has no close button: the answer is one of its actions.
 * Focus starts on `AlertDialogCancel`, so Enter on a freshly opened dialog never destroys anything.
 */
export function AlertDialogContent({ className, initialFocus, ...props }: AlertDialogPrimitive.Popup.Props) {
  const cancel = useContext(CancelRef);
  return (
    <AlertDialogPrimitive.Portal>
      <AlertDialogPrimitive.Backdrop
        data-slot="alert-dialog-backdrop"
        className="fixed inset-0 z-50 bg-background/70 transition-opacity duration-150 ease-out data-ending-style:opacity-0 data-starting-style:opacity-0 motion-reduce:transition-none"
      />
      <AlertDialogPrimitive.Popup
        data-slot="alert-dialog"
        data-surface="floating"
        initialFocus={initialFocus ?? cancel ?? undefined}
        className={cn(
          "fixed top-1/2 left-1/2 z-50 grid max-h-[calc(100dvh-2rem)] w-[calc(100%-2rem)] max-w-md -translate-x-1/2 -translate-y-1/2 gap-4 overflow-y-auto overscroll-contain rounded-lg border bg-popover p-5 text-popover-foreground shadow-lg outline-none",
          "transition-[opacity,scale] duration-150 ease-out data-ending-style:scale-[0.96] data-ending-style:opacity-0 data-starting-style:scale-[0.96] data-starting-style:opacity-0 motion-reduce:transition-none",
          className,
        )}
        {...props}
      />
    </AlertDialogPrimitive.Portal>
  );
}

/** The question and its explanation. */
export function AlertDialogHeader({ className, ...props }: ComponentProps<"div">) {
  return <div data-slot="alert-dialog-header" className={cn("grid gap-1.5", className)} {...props} />;
}

/** The question: every alert dialog needs one. Screen readers announce it when the dialog opens. */
export function AlertDialogTitle({ className, ...props }: AlertDialogPrimitive.Title.Props) {
  return <AlertDialogPrimitive.Title data-slot="alert-dialog-title" className={cn("text-base leading-5 font-semibold", className)} {...props} />;
}

/** What the action does and whether it can be undone. It is read after the title. */
export function AlertDialogDescription({ className, ...props }: AlertDialogPrimitive.Description.Props) {
  return <AlertDialogPrimitive.Description data-slot="alert-dialog-description" className={cn("text-sm text-muted-foreground", className)} {...props} />;
}

/** The two answers, right-aligned; stacked with the action on top on a phone. Put `AlertDialogCancel` first. */
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} />;
}

export type AlertDialogActionProps = AlertDialogPrimitive.Close.Props & {
  /** The button variant. Use `destructive` when the action deletes or cannot be undone. */
  variant?: ButtonVariant;
};

/** The button that goes ahead. Its `onClick` runs, then the dialog closes. */
export function AlertDialogAction({ variant = "primary", className, ...props }: AlertDialogActionProps) {
  return <AlertDialogPrimitive.Close data-slot="alert-dialog-action" data-surface={BUTTON_SURFACE[variant]} className={cn(buttonClass({ variant }), className)} {...props} />;
}

/** The button that backs out. It takes the focus when the dialog opens. */
export function AlertDialogCancel({ className, ...props }: AlertDialogPrimitive.Close.Props) {
  return <AlertDialogPrimitive.Close ref={useContext(CancelRef)} data-slot="alert-dialog-cancel" data-surface="control" className={cn(buttonClass({ variant: "outline" }), className)} {...props} />;
}

Files and dependencies

Adds
components/ui/alert-dialog.tsx
Packages
@base-ui/react@1.9.0
Brings along
Sound engineButton

Changelog

  1. 1.0.0 · 2026-12-03First release.

Loading index…

Keyboard shortcuts

/Ctrl KSearch components, docs and actions
?Show this list
AAppearance studio
SSound on or off
MFull or reduced motion
TTheme: auto, light, dark
GSurface: solid or frosted
BBackdrop photograph (under glass)
CCorner radius: square to round
RReplay the demo, on a component page
DDraw new data, on a component page
←→HomeEndMove the cursor in a focused chart
EscClose a dialog, or leave a chart cursor