Toast
Brief notifications that don't block the page. They stack, expand on hover, swipe away, and can track a promise from loading to done.
// Plain markup
import { ToastProvider, useToast } from "@shavin/ui";Setup
npx @shavin/cli init mounts ToastProvider directly inside ThemeProvider in the root layout, and shavin doctor checks it is there. Then call useToast() in any component below it. At most three toasts show at once, an identical message is not repeated, plain errors stay 8 seconds, and below the md breakpoint the stack moves to the bottom centre at full width. Toasts stack, expand on hover or focus, and can be swiped sideways to dismiss. Only the front toast counts down. Its timer stops while the stack is expanded and starts again when it collapses.
import { ThemeProvider, ToastProvider, useToast, Button } from "@shavin/ui";
// app/layout.tsx (what init writes)
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en" suppressHydrationWarning>
<body>
<ThemeProvider><ToastProvider>{children}</ToastProvider></ThemeProvider>
</body>
</html>
);
}
export function SaveButton() {
const t = useToast();
return <Button onClick={() => t.success("Budget saved")}>Save</Button>;
}Position
Pick a position to move this page's toaster and show a toast there.
<ToastProvider position="top-center">{children}</ToastProvider>Types
Each type sets the icon. Show several to see them stack; hover or focus the stack to expand it.
const t = useToast();
t.toast("Budget updated");
t.success("Transaction categorised");
t.error("Could not sync Joint checking");
t.warning("Groceries is 90% spent");
t.info("Statements for October are ready");Description, action and duration
A toast with an action stays until dismissed, unless you pass a duration.
t.success("Bill paid", {
description: "City Water & Power, $84.20 from Joint checking.",
duration: 8000,
});
t.toast("Transaction deleted", {
action: { label: "Undo", onClick: () => t.success("Transaction restored") },
});Loading and promise
loading() stays until you dismiss it. promise() shows the loading message, then swaps it for success or error in place.
const id = t.loading("Importing statement…");
// later
t.dismiss(id);
t.promise(saveBudget(), {
loading: "Saving budget…",
success: "Budget saved",
error: "Could not save the budget",
});Custom content
custom() renders any node inside the toast card.
import { Icon, useToast } from "@shavin/ui";
t.custom(
<div className="flex w-full items-center gap-3">
<Icon name="Users" size="md" color="brand" />
<div className="min-w-0">
<div className="text-sm font-medium text-fg">Sam joined your household</div>
<div className="truncate text-xs text-fg-muted">They can see shared accounts and budgets.</div>
</div>
</div>,
);Accessibility
The toaster is a region labelled "Notifications". Each toast is read out through a live region: errors interrupt, other types wait their turn. Toasts disappear on their own, so do not put the only copy of important information or the only way to do something in one. Use an action toast for undo, and keep the action available elsewhere too.
ToastProvider props
| Prop | Type | Default | Description |
|---|---|---|---|
| children* | ReactNode | — | The app, or the part of it that shows toasts. |
| position | "top-left" | "top-center" | "top-right" | "bottom-left" | "bottom-center" | "bottom-right" | "bottom-right" | Where the stack sits on screen. |
| duration | number | 4000 | Default auto-dismiss time in milliseconds. Plain error toasts stay at least 8000; a toast with an action stays until dismissed. |
* required
useToast
Throws if called outside a ToastProvider. Every method that shows a toast returns its id.
| Prop | Type | Default | Description |
|---|---|---|---|
| toast(title, options?) | string | — | Plain toast with no icon. |
| success / error / warning / info(title, options?) | string | — | Toast with the matching icon. |
| loading(title, options?) | string | — | Spinner toast that stays until dismissed. |
| custom(node, options?) | string | — | Renders your own content in the toast card. |
| promise(promise, { loading, success, error }) | Promise<T> | — | Loading toast that becomes success or error when the promise settles. success can be a function of the result. Returns the same promise. |
| dismiss(id?) | void | — | Removes one toast, or all of them when called without an id. |
options accepts:
| Prop | Type | Default | Description |
|---|---|---|---|
| description | ReactNode | — | Second line under the title. |
| duration | number | provider duration | Milliseconds before it closes. Infinity keeps it open. Toasts with an action default to Infinity. |
| action | { label: string; onClick: () => void } | — | One button inside the toast. |