Menhir

Alert Dialog

View as Markdown

A dialog that interrupts the user to ask for an answer to an important message, such as confirming an action that cannot be undone: the WAI-ARIA alert dialog pattern. It is a Dialog with the alertdialog role, which a press on the backdrop does not close, so a click that missed it is not taken as an answer; Escape still closes it, as Cancel does. Use Dialog for anything else on top of the page, such as a form. Focus goes to the first focusable element, so place the least destructive action, such as Cancel, first, or give it autofocus. Every action is an AlertDialog.Close; give the confirming one an onClick. Root, Trigger, Title, Description and Close are Dialog's parts, so an app with both components pays for them once.

Size
3.4 kB

Example

Styling

Delete this file?

It cannot be undone.

AlertDialogExample.tsx
import { AlertDialog } from "menhir/alert-dialog";

export default function AlertDialogExample() {
  return (
    <AlertDialog.Root>
      <AlertDialog.Trigger class="cursor-pointer rounded-md border border-stone-300 px-3 py-1.5 text-sm focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-500 dark:border-stone-600">
        Delete file
      </AlertDialog.Trigger>
      <AlertDialog.Popup class="m-auto w-[min(24rem,calc(100%-2rem))] rounded-xl bg-white p-6 text-sm text-inherit transition-opacity duration-150 backdrop:bg-black/40 backdrop:backdrop-blur-xs backdrop:transition-opacity backdrop:duration-150 data-closed:opacity-0 data-closed:backdrop:opacity-0 dark:bg-stone-900 starting:open:opacity-0 starting:open:backdrop:opacity-0">
        <AlertDialog.Title class="m-0 text-base font-semibold">
          Delete this file?
        </AlertDialog.Title>
        <AlertDialog.Description class="mt-2 text-stone-600 dark:text-stone-400">
          It cannot be undone.
        </AlertDialog.Description>
        <div class="mt-6 flex justify-end gap-2">
          {/* First, so that it takes focus: the least destructive action. */}
          <AlertDialog.Close class="cursor-pointer rounded-md border border-stone-300 px-3 py-1.5 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-500 dark:border-stone-600">
            Cancel
          </AlertDialog.Close>
          <AlertDialog.Close class="cursor-pointer rounded-md bg-red-700 px-3 py-1.5 text-white focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-red-700">
            Delete
          </AlertDialog.Close>
        </div>
      </AlertDialog.Popup>
    </AlertDialog.Root>
  );
}

Anatomy

<AlertDialog.Root>
  <AlertDialog.Trigger />
  <AlertDialog.Popup>
    <AlertDialog.Title />
    <AlertDialog.Description />
    <AlertDialog.Close />
  </AlertDialog.Popup>
</AlertDialog.Root>

API reference

Root

Holds the open state and renders no element. Every part has data-open or data-closed.

PropTypeDefaultDescription
openboolean—Whether the alert dialog is open, when controlled. Pair it with onOpenChange. A controlled alert dialog needs no Trigger: open it from any button of your own, and focus returns to that button. Control it to keep it open while a confirmed action is in flight.
defaultOpenbooleanfalseWhether the alert dialog is open at first, when uncontrolled.
onOpenChange(open: boolean, details: { reason, event }) => void—Called with the new value when the user opens or closes the alert dialog, and why: "trigger-press", "close-press", "escape-key" or "close-request" (such as the back gesture on Android or a <form method="dialog">). A controlled alert dialog changes only if open does, so keeping open true refuses the close.

Trigger

<button type="button" aria-haspopup="dialog"> that opens the alert dialog. Name it with its content. Its render function must render a <button>.

PropTypeDefaultDescription
render(props) => JSX.Element—Renders the trigger with your own button component. Read once.

Popup

<dialog role="alertdialog">, opened with showModal(). Named by the Title in aria-labelledby and described by the Description, the message, in aria-describedby, while they are rendered; without a Title, give it an aria-label. A press on the backdrop does not close it. Its render function must render a <dialog>. While it plays an exit animation it has data-closed and is inert.

PropTypeDefaultDescription
initialFocusHTMLElement | ((focused) => HTMLElement | null | undefined)—Where focus goes once it has opened, instead of the browser's choice: the element with autofocus, else the first focusable one. Point it at the least destructive action. A function gets the browser's choice; use one for an element you hold in a ref.
returnFocusHTMLElement | ((focused) => HTMLElement | null | undefined)—Where focus goes once it has closed, instead of the browser's choice: the element focused before it opened. A function gets that element, or null if it is gone, as in (focused) => focused ?? fallback.
render(props) => JSX.Element—Renders the popup with your own component, which spreads its props onto a <dialog>. Read once.

Title

<h2> that names the Popup. Takes your id, else a generated one.

PropTypeDefaultDescription
render(props) => JSX.Element—Renders the title with your own component. Read once.

Description

<p> with the message, which describes the Popup. Takes your id, else a generated one.

PropTypeDefaultDescription
render(props) => JSX.Element—Renders the description with your own component. Read once.

Close

<button type="button"> that closes the alert dialog: each action, such as Cancel and Delete, with its own onClick for the action. Its render function must render a <button>.

PropTypeDefaultDescription
render(props) => JSX.Element—Renders the close button with your own button component. Read once.

Keyboard

KeysAction
SpaceOn the Trigger, opens the alert dialog.
EnterOn the Trigger, opens the alert dialog.
TabMoves focus to the next element in the alert dialog, from the last back to the first.
Shift + TabMoves focus to the previous element in the alert dialog, from the first to the last.
EscapeCloses the alert dialog.