# Sonner

shadcn's Sonner, by its name: Ballpoint's Toast, with the same toast() calls. Add either one.

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

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

```tsx
import { Toaster, toast } from "@/components/ui/sonner"

// Once, near the root of the app:
<Toaster />

// Anywhere:
toast.success("Saved to your notebook")
```

Props:

- `Toaster`: `component`. Render it once. It draws the notes, stacked, in a corner; see Toast for its props.
- `toast(title, options)`: `function`. Raises a note. toast.success, .error, .warning, .info, .loading, .promise and .dismiss work as they do in sonner.

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

## From a promise

toast.promise follows a promise through loading, success and error.

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

## With an action

A note with a button to take the action 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>
  );
}
```
