# Alert Dialog

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.

- Entry point: `menhir/alert-dialog`
- Size: 3.4 kB, minified and gzipped, Solid left out

> To add Alert Dialog to a project, install the packages below with the project's package manager, then copy the example that matches how the project is styled, Tailwind CSS or plain CSS, and adapt its markup and class names to the project.

## Install

```sh
pnpm add menhir solid-js @solidjs/web
```

## Anatomy

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

## Example

### Tailwind CSS

`AlertDialogExample.tsx`:

```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>
  );
}
```

### CSS

`AlertDialogExample.tsx`:

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

import "./alert-dialog.css";

export default function AlertDialogExample() {
  return (
    <AlertDialog.Root>
      <AlertDialog.Trigger class="alert-dialog-button">
        Delete file
      </AlertDialog.Trigger>
      <AlertDialog.Popup class="alert-dialog-popup">
        <AlertDialog.Title class="alert-dialog-title">
          Delete this file?
        </AlertDialog.Title>
        <AlertDialog.Description class="alert-dialog-description">
          It cannot be undone.
        </AlertDialog.Description>
        <div class="alert-dialog-actions">
          {/* First, so that it takes focus: the least destructive action. */}
          <AlertDialog.Close class="alert-dialog-button">
            Cancel
          </AlertDialog.Close>
          <AlertDialog.Close class="alert-dialog-button alert-dialog-destructive">
            Delete
          </AlertDialog.Close>
        </div>
      </AlertDialog.Popup>
    </AlertDialog.Root>
  );
}
```

`alert-dialog.css`:

```css
/* The least CSS that makes the Alert Dialog look like one: a card over a
   dimmed page, with a red destructive action. The <dialog> is centred in
   the top layer by the browser, and ::backdrop covers the page behind it.
   It fades in on data-open and out on data-closed: the Popup stays shown
   until the exit animation has ended. Colours are Tailwind's stone and red
   palettes; light-dark() picks the second one in dark mode
   (color-scheme: dark). */
.alert-dialog-button {
  padding: 0.375rem 0.75rem;
  border: 1px solid
    light-dark(
      oklch(86.9% 0.005 56.366) /* stone-300 */,
      oklch(44.4% 0.011 73.639) /* stone-600 */
    );
  border-radius: 0.375rem;
  background: transparent;
  color: inherit;
  font:
    0.875rem system-ui,
    sans-serif;
  cursor: pointer;
}

.alert-dialog-destructive {
  border-color: transparent;
  background: oklch(50.5% 0.213 27.518) /* red-700 */;
  color: white;
}

.alert-dialog-popup {
  width: min(24rem, calc(100% - 2rem));
  padding: 1.5rem;
  border: 0;
  border-radius: 0.75rem;
  background: light-dark(white, oklch(21.6% 0.006 56.043) /* stone-900 */);
  color: inherit;
  font:
    0.875rem system-ui,
    sans-serif;
}

.alert-dialog-popup::backdrop {
  background: oklch(0% 0 0 / 40%);
  backdrop-filter: blur(4px);
}

.alert-dialog-popup[data-open],
.alert-dialog-popup[data-open]::backdrop {
  animation: alert-dialog-fade 150ms ease-out;
}

.alert-dialog-popup[data-closed],
.alert-dialog-popup[data-closed]::backdrop {
  animation: alert-dialog-fade 150ms ease-in reverse;
}

@keyframes alert-dialog-fade {
  from {
    opacity: 0;
  }
}

.alert-dialog-title {
  margin: 0;
  font-size: 1rem;
}

.alert-dialog-description {
  margin: 0.5rem 0 0;
  color: light-dark(
    oklch(44.4% 0.011 73.639) /* stone-600 */,
    oklch(70.9% 0.01 56.259) /* stone-400 */
  );
}

.alert-dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 1.5rem;
}
```

## API reference

### Root

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

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `open` | `boolean` | — | 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. |
| `defaultOpen` | `boolean` | `false` | Whether 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>.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `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.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `initialFocus` | `HTMLElement \| ((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. |
| `returnFocus` | `HTMLElement \| ((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.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `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.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `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>.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `render` | `(props) => JSX.Element` | — | Renders the close button with your own button component. Read once. |

## Keyboard

| Keys | Action |
| --- | --- |
| `Space` | On the Trigger, opens the alert dialog. |
| `Enter` | On the Trigger, opens the alert dialog. |
| `Tab` | Moves focus to the next element in the alert dialog, from the last back to the first. |
| `Shift + Tab` | Moves focus to the previous element in the alert dialog, from the first to the last. |
| `Escape` | Closes the alert dialog. |
