# Alert Dialog

A note that needs an answer before you go on, laid over the page with its choices.

Docs: https://ballpoint.st9wd.com/docs/alert-dialog

```bash
npx shadcn@latest add @ballpoint/alert-dialog
```

```tsx
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
} from "@/components/ui/alert-dialog"
import { Button } from "@/components/ui/button"

<AlertDialog>
  <AlertDialogTrigger render={<Button variant="destructive" />}>Delete</AlertDialogTrigger>
  <AlertDialogContent>
    <AlertDialogHeader>
      <AlertDialogTitle>Delete this notebook?</AlertDialogTitle>
      <AlertDialogDescription>This can't be undone.</AlertDialogDescription>
    </AlertDialogHeader>
    <AlertDialogFooter>
      <AlertDialogCancel>Keep it</AlertDialogCancel>
      <AlertDialogAction variant="destructive">Delete</AlertDialogAction>
    </AlertDialogFooter>
  </AlertDialogContent>
</AlertDialog>
```

Props:

- `AlertDialogContent size`: `"default" | "sm"` (default `"default"`). Small centres everything and puts the two buttons side by side.
- `AlertDialogMedia`: `component`. An icon in a ring drawn round it, beside the title.
- `AlertDialogAction / AlertDialogCancel`: `Button props`. The answer, and the way out. Cancel closes the dialog and is outlined by default.
- `seed`: `string | number`. On AlertDialogContent: pins the drawing.

Pen settings: `draw`, `roughness`, `passes`, `radius`, `corners`, `shadow`, `weight`, `speed` (see Pen settings).

Every other prop goes to Base UI's Alert Dialog: https://base-ui.com/react/components/alert-dialog

## Example

```tsx
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogMedia,
  AlertDialogTitle,
  AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
import { Button } from "@/components/ui/button";
import { InkGlyph } from "@/lib/ink-glyphs";

export default function AlertDialogDemo() {
  return (
    <div className="flex flex-wrap gap-5">
      <AlertDialog>
        <AlertDialogTrigger render={<Button variant="destructive" seed="alert-open" />}>Delete notebook</AlertDialogTrigger>
        <AlertDialogContent seed="alert">
          <AlertDialogHeader>
            <AlertDialogMedia seed="alert-media" className="text-destructive">
              <InkGlyph name="alert" />
            </AlertDialogMedia>
            <AlertDialogTitle>Delete this notebook?</AlertDialogTitle>
            <AlertDialogDescription>All 48 pages go with it. This can&apos;t be undone.</AlertDialogDescription>
          </AlertDialogHeader>
          <AlertDialogFooter>
            <AlertDialogCancel seed="alert-cancel">Keep it</AlertDialogCancel>
            <AlertDialogAction variant="destructive" seed="alert-delete">
              Delete
            </AlertDialogAction>
          </AlertDialogFooter>
        </AlertDialogContent>
      </AlertDialog>
      <AlertDialog>
        <AlertDialogTrigger render={<Button variant="outline" seed="alert-sm-open" />}>Small</AlertDialogTrigger>
        <AlertDialogContent size="sm" seed="alert-sm">
          <AlertDialogHeader>
            <AlertDialogTitle>Leave without saving?</AlertDialogTitle>
            <AlertDialogDescription>Your last few lines will be lost.</AlertDialogDescription>
          </AlertDialogHeader>
          <AlertDialogFooter>
            <AlertDialogCancel seed="alert-sm-cancel">Stay</AlertDialogCancel>
            <AlertDialogAction seed="alert-sm-leave">Leave</AlertDialogAction>
          </AlertDialogFooter>
        </AlertDialogContent>
      </AlertDialog>
    </div>
  );
}
```

## Confirming an action

The action runs only when confirmed; cancelling leaves things as they were.

```tsx
"use client";

import { useState } from "react";
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger } from "@/components/ui/alert-dialog";
import { Button } from "@/components/ui/button";

export default function AlertDialogConfirm() {
  const [pages, setPages] = useState(48);
  return (
    <div className="flex flex-col items-center gap-4">
      <p className="text-ink-2">{pages ? `${pages} pages in this notebook.` : "The notebook is empty."}</p>
      <AlertDialog>
        <AlertDialogTrigger render={<Button variant="outline" disabled={!pages} seed="adc-open" />}>Tear out 12 pages</AlertDialogTrigger>
        <AlertDialogContent size="sm" seed="adc">
          <AlertDialogHeader>
            <AlertDialogTitle>Tear out 12 pages?</AlertDialogTitle>
            <AlertDialogDescription>They go to the bin, where they stay for 30 days.</AlertDialogDescription>
          </AlertDialogHeader>
          <AlertDialogFooter>
            <AlertDialogCancel seed="adc-cancel">Keep them</AlertDialogCancel>
            <AlertDialogAction onClick={() => setPages((n) => Math.max(0, n - 12))} seed="adc-confirm">
              Tear out
            </AlertDialogAction>
          </AlertDialogFooter>
        </AlertDialogContent>
      </AlertDialog>
    </div>
  );
}
```

## Type to confirm

The destructive action stays disabled until the notebook's name is typed.

```tsx
"use client";

import { useState } from "react";
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger } from "@/components/ui/alert-dialog";
import { Button } from "@/components/ui/button";
import { Field, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";

const NAME = "Field notes";

export default function AlertDialogType() {
  const [typed, setTyped] = useState("");
  const [gone, setGone] = useState(false);
  return (
    <div className="flex flex-col items-center gap-4">
      <AlertDialog onOpenChange={() => setTyped("")}>
        <AlertDialogTrigger render={<Button variant="destructive" seed="adt-open" disabled={gone} />}>Delete notebook</AlertDialogTrigger>
        <AlertDialogContent seed="adt">
          <AlertDialogHeader>
            <AlertDialogTitle>Delete &quot;{NAME}&quot;?</AlertDialogTitle>
            <AlertDialogDescription>All 96 pages go with it, and they do not come back. Type the name to be sure.</AlertDialogDescription>
          </AlertDialogHeader>
          <Field>
            <FieldLabel htmlFor="adt-name">Notebook name</FieldLabel>
            <Input id="adt-name" value={typed} onChange={(event) => setTyped(event.target.value)} placeholder={NAME} seed="adt-name" />
          </Field>
          <AlertDialogFooter>
            <AlertDialogCancel seed="adt-cancel">Keep it</AlertDialogCancel>
            <AlertDialogAction variant="destructive" disabled={typed !== NAME} onClick={() => setGone(true)} seed="adt-go">
              Delete for good
            </AlertDialogAction>
          </AlertDialogFooter>
        </AlertDialogContent>
      </AlertDialog>
      {gone && <p className="text-ink-2">&quot;{NAME}&quot; is gone.</p>}
    </div>
  );
}
```

## Small, with an icon

size="sm" with an AlertDialogMedia icon and two short buttons.

```tsx
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogMedia, AlertDialogTitle, AlertDialogTrigger } from "@/components/ui/alert-dialog";
import { Button } from "@/components/ui/button";
import { InkIcon } from "@/components/ui/ink-icons";

export default function AlertDialogSmall() {
  return (
    <AlertDialog>
      <AlertDialogTrigger render={<Button variant="outline" seed="ads-open" />}>Leave the page</AlertDialogTrigger>
      <AlertDialogContent size="sm" seed="ads">
        <AlertDialogHeader>
          <AlertDialogMedia>
            <InkIcon name="save" />
          </AlertDialogMedia>
          <AlertDialogTitle>Leave without saving?</AlertDialogTitle>
          <AlertDialogDescription>Your last three lines will be lost.</AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel seed="ads-stay">Stay</AlertDialogCancel>
          <AlertDialogAction seed="ads-leave">Leave</AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  );
}
```

## Pen settings

- `draw`: `"auto" | "mount" | "none"` (default `"auto"`). When the strokes draw themselves in: the first time they scroll into view, as soon as they render, or never (already drawn).
- `roughness`: `number` (default `1`). 0 is ruler-neat, 1 a quick confident hand, 2 a scrawl.
- `passes`: `1 | 2 | 3`. How many times an outline is gone over. Each component picks its own default.
- `radius`: `number | "full"` (default `0`). Corner radius in px; "full" draws a pill.
- `corners`: `"crossed" | "joined"` (default `"crossed"`). Square corners: sides pulled separately past each other, or the box drawn in one motion.
- `shadow`: `"hatch" | "solid" | "none"` (default `"hatch"`). What a lifted box leaves on the paper. "none" turns the lift off too.
- `weight`: `number` (default `1`). Line weight multiplier. Also settable in CSS as --ink-weight.
- `speed`: `number` (default `1`). Drawing speed multiplier. Also settable in CSS as --ink-speed.
