# Dialog

A sheet of paper laid over the page: pencilled round, lifted off a hatched shadow, drawn in as it lands.

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

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

```tsx
import { Button } from "@/components/ui/button"
import {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/components/ui/dialog"

<Dialog>
  <DialogTrigger render={<Button variant="outline" />}>Edit profile</DialogTrigger>
  <DialogContent>
    <DialogHeader>
      <DialogTitle>Edit profile</DialogTitle>
      <DialogDescription>Save when you're done.</DialogDescription>
    </DialogHeader>
    <DialogFooter>
      <DialogClose render={<Button />}>Save</DialogClose>
    </DialogFooter>
  </DialogContent>
</Dialog>
```

Props:

- `DialogContent showCloseButton`: `boolean` (default `true`). A drawn cross in the top-right corner.
- `DialogFooter showCloseButton`: `boolean` (default `false`). Adds an outlined Close button to the footer.
- `DialogTrigger render`: `ReactElement`. Renders the trigger as your own element, usually a Button.
- `DialogContent size`: `"sm" | "default" | "lg" | "xl"` (default `"default"`). How wide it is: 24, 28, 42 or 56rem at most, and never wider than the screen.
- `seed`: `string | number`. On DialogContent: pins the drawing.

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

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

## Example

```tsx
import { Button } from "@/components/ui/button";
import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog";
import { Field, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";

export default function DialogDemo() {
  return (
    <Dialog>
      <DialogTrigger render={<Button variant="outline" seed="dialog-open" />}>Edit profile</DialogTrigger>
      <DialogContent seed="dialog">
        <DialogHeader>
          <DialogTitle>Edit profile</DialogTitle>
          <DialogDescription>How your name and handle appear on your notes. Save when you&apos;re done.</DialogDescription>
        </DialogHeader>
        <div className="grid gap-4">
          <Field>
            <FieldLabel htmlFor="dialog-name">Name</FieldLabel>
            <Input id="dialog-name" defaultValue="Ada Lovelace" seed="dialog-name" />
          </Field>
          <Field>
            <FieldLabel htmlFor="dialog-handle">Handle</FieldLabel>
            <Input id="dialog-handle" defaultValue="@ada" seed="dialog-handle" />
          </Field>
        </div>
        <DialogFooter>
          <DialogClose render={<Button variant="outline" seed="dialog-cancel" />}>Cancel</DialogClose>
          <DialogClose render={<Button seed="dialog-save" />}>Save changes</DialogClose>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
}
```

## Controlled

Own the open state to close the dialog from code, here when the form is submitted.

```tsx
"use client";

import { useState, type FormEvent } from "react";
import { Button } from "@/components/ui/button";
import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog";
import { Field, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";

// Own the open state to close the dialog from your own code, here when the form is submitted.
export default function DialogControlled() {
  const [open, setOpen] = useState(false);
  const [name, setName] = useState("Untitled");
  const [saved, setSaved] = useState<string>();

  const submit = (e: FormEvent) => {
    e.preventDefault();
    setSaved(name);
    setOpen(false);
  };

  return (
    <div className="flex flex-col items-center gap-4">
      <Dialog open={open} onOpenChange={setOpen}>
        <DialogTrigger render={<Button variant="outline" seed="dc-open" />}>Rename notebook</DialogTrigger>
        <DialogContent seed="dc">
          <form onSubmit={submit} className="grid gap-5">
            <DialogHeader>
              <DialogTitle>Rename notebook</DialogTitle>
              <DialogDescription>Press Enter to save.</DialogDescription>
            </DialogHeader>
            <Field>
              <FieldLabel htmlFor="dc-name">Name</FieldLabel>
              <Input id="dc-name" value={name} onChange={(e) => setName(e.target.value)} seed="dc-name" />
            </Field>
            <DialogFooter>
              <DialogClose render={<Button variant="outline" type="button" seed="dc-cancel" />}>Cancel</DialogClose>
              <Button type="submit" seed="dc-save">
                Save
              </Button>
            </DialogFooter>
          </form>
        </DialogContent>
      </Dialog>
      {saved && <p className="text-ink-2">Saved as &quot;{saved}&quot;.</p>}
    </div>
  );
}
```

## Long content

A scrolling region inside a dialog, reachable from the keyboard.

```tsx
import { Button } from "@/components/ui/button";
import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog";

const clauses = Array.from({ length: 12 }, (_, i) => `${i + 1}. The pen stays with its owner, and any lines drawn with it are drawn with care.`);

export default function DialogScroll() {
  return (
    <Dialog>
      <DialogTrigger render={<Button variant="outline" seed="dsc-open" />}>Read the terms</DialogTrigger>
      <DialogContent seed="dsc">
        <DialogHeader>
          <DialogTitle>Terms of the pen</DialogTitle>
          <DialogDescription>Scroll to the end, then accept.</DialogDescription>
        </DialogHeader>
        <div tabIndex={0} aria-label="Terms" className="max-h-60 overflow-y-auto pr-2 text-ink-2">
          {clauses.map((clause) => (
            <p key={clause} className="mb-3">
              {clause}
            </p>
          ))}
        </div>
        <DialogFooter>
          <DialogClose render={<Button seed="dsc-accept" />}>I accept</DialogClose>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
}
```

## A form

A wider dialog holding two columns of fields and a textarea.

```tsx
"use client";

import { Button } from "@/components/ui/button";
import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog";
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea";

export default function DialogForm() {
  return (
    <Dialog>
      <DialogTrigger render={<Button seed="df-open" />}>Edit profile</DialogTrigger>
      <DialogContent size="lg" seed="df">
        <form className="grid gap-5" onSubmit={(event) => event.preventDefault()}>
          <DialogHeader>
            <DialogTitle>Edit profile</DialogTitle>
            <DialogDescription>How you appear on the letters you send.</DialogDescription>
          </DialogHeader>
          <FieldGroup>
            <div className="grid gap-5 sm:grid-cols-2">
              <Field>
                <FieldLabel htmlFor="df-first">First name</FieldLabel>
                <Input id="df-first" defaultValue="Grace" seed="df-first" />
              </Field>
              <Field>
                <FieldLabel htmlFor="df-last">Last name</FieldLabel>
                <Input id="df-last" defaultValue="Hopper" seed="df-last" />
              </Field>
            </div>
            <Field>
              <FieldLabel htmlFor="df-bio">About you</FieldLabel>
              <Textarea id="df-bio" rows={3} defaultValue="Writing since 1952." seed="df-bio" />
            </Field>
          </FieldGroup>
          <DialogFooter>
            <DialogClose render={<Button variant="outline" type="button" seed="df-cancel" />}>Cancel</DialogClose>
            <DialogClose render={<Button type="submit" seed="df-save" />}>Save changes</DialogClose>
          </DialogFooter>
        </form>
      </DialogContent>
    </Dialog>
  );
}
```

## Share a link

A small dialog with a read-only link and a copy button.

```tsx
import { Button } from "@/components/ui/button";
import { CopyButton } from "@/components/ui/copy-button";
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";

export default function DialogShare() {
  return (
    <Dialog>
      <DialogTrigger render={<Button variant="outline" seed="dsh-open" />}>Share</DialogTrigger>
      <DialogContent size="sm" seed="dsh">
        <DialogHeader>
          <DialogTitle>Share this notebook</DialogTitle>
          <DialogDescription>Anyone with the link can read it. Nobody can write in it.</DialogDescription>
        </DialogHeader>
        <div className="flex items-center gap-2">
          <Input readOnly aria-label="Link" defaultValue="https://example.com/n/4fK9q" seed="dsh-link" className="min-w-0 flex-1" />
          <CopyButton value="https://example.com/n/4fK9q" variant="outline" size="icon" seed="dsh-copy">
            Copy link
          </CopyButton>
        </div>
        <DialogFooter showCloseButton />
      </DialogContent>
    </Dialog>
  );
}
```

## Sizes

From a small confirmation to a wide editor.

```tsx
import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog";

const sizes = ["sm", "default", "lg", "xl"] as const;

export default function DialogSizes() {
  return (
    <div className="flex flex-wrap items-center justify-center gap-4">
      {sizes.map((size) => (
        <Dialog key={size}>
          <DialogTrigger render={<Button variant="outline" seed={`dz-${size}`} />}>{size}</DialogTrigger>
          <DialogContent size={size} seed={`dz-c-${size}`}>
            <DialogHeader>
              <DialogTitle>size=&quot;{size}&quot;</DialogTitle>
              <DialogDescription>The dialog is as wide as it needs to be, and never wider than the screen.</DialogDescription>
            </DialogHeader>
            <DialogFooter showCloseButton />
          </DialogContent>
        </Dialog>
      ))}
    </div>
  );
}
```

## 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.
