# Toast

Notes pinned to the corner of the page that stack, fan out and swipe away; toast() from anywhere.

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

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

```tsx
// app/layout.tsx: once, near the root
import { Toaster } from "@/components/ui/toast"

<body>
  {children}
  <Toaster />
</body>

// anywhere
import { toast } from "@/components/ui/toast"

toast("Page torn out", { action: { label: "Undo", onClick: restore } })
toast.success("Saved")
toast.error("Couldn't send")
toast.promise(save(), { loading: "Saving…", success: "Saved", error: "Couldn't save" })
```

Props:

- `toast(title, options)`: `string id`. Also toast.success, .error, .warning, .info, .loading (stays until dismissed), .promise, .update and .dismiss(id?).
- `options`: `{ description, action, timeout, priority, id, onClose }`. action is { label, onClick }; timeout 0 keeps it up; priority "high" announces it at once.
- `Toaster limit`: `number` (default `3`). How many notes show at once; older ones wait.
- `Toaster timeout`: `number` (default `5000`). ms before a note takes itself down.

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

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

## Example

```tsx
"use client";

import { Button } from "@/components/ui/button";
import { Toaster, toast } from "@/components/ui/toast";

const save = () => new Promise<string>((resolve) => setTimeout(() => resolve("page 12"), 1600));

export default function ToastDemo() {
  return (
    <div className="flex flex-wrap gap-5">
      <Button variant="outline" seed="toast-plain" onClick={() => toast("Page torn out", { description: "It's in the bin for 30 days.", action: { label: "Undo", onClick: () => toast.success("Page put back") } })}>
        Tear out a page
      </Button>
      <Button variant="outline" seed="toast-success" onClick={() => toast.success("Saved", { description: "Every line, safe and sound." })}>
        Save
      </Button>
      <Button variant="outline" seed="toast-error" onClick={() => toast.error("Couldn't send", { description: "The post office is closed. Try again later." })}>
        Send
      </Button>
      <Button variant="outline" seed="toast-promise" onClick={() => toast.promise(save(), { loading: "Saving…", success: (page) => `Saved ${page}`, error: "Couldn't save" })}>
        Save slowly
      </Button>
      <Toaster />
    </div>
  );
}
```

## More kinds

Info, warning, a note that stays until closed, and clearing them all.

```tsx
"use client";

import { Button } from "@/components/ui/button";
import { toast } from "@/components/ui/toast";

// Render <Toaster /> once near your root (the first example does); toast() works from anywhere.
export default function ToastActions() {
  return (
    <div className="flex flex-wrap gap-5">
      <Button variant="outline" seed="ta-info" onClick={() => toast.info("New refill available", { description: "Medium point, blue." })}>
        Info
      </Button>
      <Button variant="outline" seed="ta-warn" onClick={() => toast.warning("Running low", { description: "About a page of ink left." })}>
        Warning
      </Button>
      <Button variant="outline" seed="ta-sticky" onClick={() => toast("Stays until you close it", { timeout: 0, description: "No timer on this one." })}>
        Sticky
      </Button>
      <Button variant="ghost" seed="ta-clear" onClick={() => toast.dismiss()}>
        Clear all
      </Button>
    </div>
  );
}
```

## Promise

One note that follows a save: pending, then done or failed.

```tsx
"use client";

import { Button } from "@/components/ui/button";
import { toast } from "@/components/ui/toast";

const save = (ok: boolean) => new Promise<void>((resolve, reject) => setTimeout(() => (ok ? resolve() : reject(new Error("offline"))), 1600));

// One note that follows the work: pending, then done, or failed.
export default function ToastPromise() {
  return (
    <div className="flex flex-wrap gap-5">
      <Button seed="tp-ok" onClick={() => toast.promise(save(true), { loading: "Saving the page…", success: "Page saved", error: "Couldn't save" })}>
        Save
      </Button>
      <Button variant="outline" seed="tp-fail" onClick={() => toast.promise(save(false), { loading: "Saving the page…", success: "Page saved", error: "Couldn't save: you're offline" })}>
        Save, offline
      </Button>
    </div>
  );
}
```

## Undo

Removing something posts a note whose action puts it back.

```tsx
"use client";

import { useState } from "react";
import { Button } from "@/components/ui/button";
import { toast } from "@/components/ui/toast";

const start = ["Dear Ada", "Shopping list", "Sketch of a lake", "Recipe"];

export default function ToastUndo() {
  const [pages, setPages] = useState(start);
  const remove = (page: string, at: number) => {
    setPages((all) => all.filter((p) => p !== page));
    toast(`Tore out "${page}"`, {
      action: { label: "Undo", onClick: () => setPages((all) => [...all.slice(0, at), page, ...all.slice(at)]) },
    });
  };
  return (
    <ul className="flex w-full max-w-sm flex-col gap-2">
      {pages.map((page, at) => (
        <li key={page} className="flex items-center justify-between gap-3">
          {page}
          <Button variant="ghost" size="sm" seed={`tu-${page}`} onClick={() => remove(page, at)}>
            Tear out
          </Button>
        </li>
      ))}
      {pages.length === 0 && <li className="text-ink-3">An empty notebook.</li>}
    </ul>
  );
}
```

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