ToastProvider
Manages toast ordering, timers, and placement. Requires a ThemeProvider ancestor.
Preview
Props
ToastProvider renders its children and a portaled toast viewport.
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | — | App content (wraps the component tree). |
placement | ToastPlacement | "bottom-right" | "top-left" | "top-center" | "top-right" | "bottom-left" | "bottom-center" | "bottom-right". |
duration | number | 5000 | Default auto-dismiss in ms. 0 or Infinity makes toasts sticky. |
limit | number | 4 | Maximum visible toast count. Exceeding the limit dismisses the oldest toast. |
ToastOptions
The object passed to toast():
| Prop | Type | Default | Description |
|---|---|---|---|
id | string | — | Toast ID. Reusing the ID updates the existing toast. |
children | ReactNode | — | Toast message content. |
tone | ToastTone | "ok" | "ok" | "warn" | "err". |
href | string | — | Makes the link label clickable. |
linkLabel | string | — | Link text when href is provided. |
duration | number | — | Per-toast override of the provider's duration. |
onDismiss | () => void | — | Called once the toast leaves. |
useToast()
useToast() returns an object with toast, dismiss, and toasts.
| Prop | Type | Default | Description |
|---|---|---|---|
toast | ToastFn | — | Fire a toast. Also has .ok(), .warn(), .err() shorthands. |
dismiss | (id?: string) => void | — | Dismiss one toast by id, or all when called with no id. |
toasts | ToastRecord[] | — | Current toast records. |
Examples
Basic setup
import {
ThemeProvider,
ToastProvider,
useToast,
Button,
} from "@solarius/onset";
// Wrap your app
function App() {
return (
<ThemeProvider>
<ToastProvider placement="bottom-right" duration={5000}>
<SaveButton />
</ToastProvider>
</ThemeProvider>
);
}
// Fire from anywhere below
function SaveButton() {
const { toast } = useToast();
return (
<Button
onClick={() => {
toast("Changes saved");
// or with options:
toast({ children: "Changes saved", tone: "ok" });
// or using shorthands:
toast.ok("Deployment started");
toast.warn("Storage at 85%");
toast.err("Save failed — retry");
}}
>
Save
</Button>
);
}Idempotent toasts
const { toast } = useToast();
function save() {
const id = toast({ id: "save", children: "Saving…", tone: "ok" });
// Later, update in place rather than stacking a second card:
toast({ id: "save", children: "Saved", tone: "ok" });
}Dismiss toasts
const { toast, dismiss } = useToast();
// Dismiss one
const id = toast("Loading…");
dismiss(id);
// Dismiss all
dismiss();Custom placement and duration
<ToastProvider placement="top-right" duration={3000} limit={3}>
<App />
</ToastProvider>