Sonner
shadcn's Sonner, by its name: Ballpoint's Toast, with the same toast() calls. Add either one.
"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>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ sonner// shadcn's Sonner is a toast library; Ballpoint's Toast is the same thing
// drawn with the pen and built on Base UI, with the same `toast()` calls.
// This file only gives it the name people look for: add either one.
export { Toaster, toast } from "@/components/ui/toast";Usage
import { Toaster, toast } from "@/components/ui/sonner"
// Once, near the root of the app:
<Toaster />
// Anywhere:
toast.success("Saved to your notebook")Examples
From a promise
toast.promise follows a promise through loading, success and error.
"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.
- Dear Ada
- Shopping list
- Sketch of a lake
- Recipe
"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>
);
}API reference
- Toaster
componentRender it once. It draws the notes, stacked, in a corner; see Toast for its props.
- toast(title, options)
functionRaises 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.