typetitledescriptiontimeoutimport { ToastList, ToastPortal, ToastProvider, ToastViewport, createToastManager } from "@/components/ui/toast"
import { Button } from "@/components/ui/button"
const toastManager = createToastManager()
export function SendMessage() {
const showToast = () => {
toastManager.add({
title: "Success",
description: "Your message has been sent successfully.",
type: "success",
})
}
return (
<ToastProvider toastManager={toastManager}>
<Button onClick={showToast}>Show toast</Button>
<ToastPortal>
<ToastViewport>
<ToastList />
</ToastViewport>
</ToastPortal>
</ToastProvider>
)
}Usage
import { Toast, useToastManager } from "@/components/ui/toast"
import { Button } from "@/components/ui/button"
function SaveButton() {
const toast = useToastManager()
return (
<Button onClick={() => toast.add({ title: "Saved", description: "Your changes are live.", type: "success" })}>
Save
</Button>
)
}
export function App() {
return (
<Toast>
<SaveButton />
</Toast>
)
}Examples
import { ToastProvider, ToastPortal, ToastViewport, ToastList, createToastManager } from '@/components/ui/toast';
import { Button } from '@/components/ui/button';
const toastManager = createToastManager();
export function Example() {
const showToast = () => {
toastManager.add({
title: "Success",
description: "Your message has been sent successfully.",
type: "success"
});
};
return (
<ToastProvider toastManager={toastManager}>
<Button onClick={showToast}>Show Toast</Button>
<ToastPortal>
<ToastViewport>
<ToastList />
</ToastViewport>
</ToastPortal>
</ToastProvider>
);
}import { ToastProvider, ToastPortal, ToastViewport, ToastList, createToastManager } from '@/components/ui/toast';
import { Button } from '@/components/ui/button';
const toastManager = createToastManager();
export function Example() {
const variants = {
success: { title: "Success!", description: "Action completed.", type: "success" },
error: { title: "Error", description: "Something went wrong.", type: "error" },
warning: { title: "Warning", description: "Please review your input.", type: "warning" }
};
return (
<ToastProvider toastManager={toastManager}>
<div className="flex gap-2">
{Object.entries(variants).map(([key, toast]) => (
<Button key={key} onClick={() => toastManager.add(toast)} variant="outline">
{key}
</Button>
))}
</div>
<ToastPortal>
<ToastViewport>
<ToastList />
</ToastViewport>
</ToastPortal>
</ToastProvider>
);
}import { ToastProvider, ToastPortal, ToastViewport, ToastList, useToastManager, createToastManager } from '@/components/ui/toast';
import { Button } from '@/components/ui/button';
const toastManager = createToastManager();
function LoadingToastButtons() {
const manager = useToastManager();
const handleUpdate = () => {
const id = manager.add({
title: "Saving changes...",
description: "Please wait while we save your data.",
type: "loading",
timeout: 0,
});
setTimeout(() => {
manager.update(id, {
title: "Changes saved",
description: "Your data has been saved successfully.",
type: "success",
timeout: 5000,
});
}, 2000);
};
const handleUpdateWithError = () => {
const id = manager.add({
title: "Uploading file...",
description: "Please wait while we upload your file.",
type: "loading",
timeout: 0,
});
setTimeout(() => {
manager.update(id, {
title: "Upload failed",
description: "The file could not be uploaded.",
type: "error",
timeout: 5000,
actionProps: {
children: "Retry",
onClick: () => {
manager.update(id, {
title: "Retrying upload...",
description: "Attempting to upload again.",
type: "loading",
timeout: 0,
actionProps: undefined,
});
setTimeout(() => {
manager.update(id, {
title: "Upload complete",
description: "Your file has been uploaded.",
type: "success",
timeout: 5000,
});
}, 1500);
},
},
});
}, 2000);
};
const handlePromise = () => {
const fakeRequest = new Promise((resolve) =>
setTimeout(() => resolve("Done!"), 2500)
);
manager.promise(fakeRequest, {
loading: {
title: "Processing...",
description: "Running your request.",
},
success: (result) => ({
title: "Complete",
description: `Request finished: ${result}`,
}),
error: (err) => ({
title: "Request failed",
description: err?.message || "Something went wrong.",
}),
});
};
return (
<div className="flex gap-2 flex-wrap">
<Button onClick={handleUpdate} variant="outline">
Loading → Success
</Button>
<Button onClick={handleUpdateWithError} variant="outline">
Loading → Error → Retry
</Button>
<Button onClick={handlePromise} variant="outline">
Promise Toast
</Button>
</div>
);
}
export function Example() {
return (
<ToastProvider toastManager={toastManager}>
<LoadingToastButtons />
<ToastPortal>
<ToastViewport>
<ToastList />
</ToastViewport>
</ToastPortal>
</ToastProvider>
);
}import { ToastProvider, ToastPortal, ToastViewport, ToastList, ToastContent, ToastTitle, ToastDescription, ToastClose, createToastManager } from '@/components/ui/toast';
import { Button } from '@/components/ui/button';
import { CheckCircle2Icon, AlertCircleIcon, InfoIcon, Loader2Icon } from 'lucide-react';
const toastManager = createToastManager();
const typeIcons = {
success: <CheckCircle2Icon className="h-5 w-5 text-green-500" />,
error: <AlertCircleIcon className="h-5 w-5 text-red-500" />,
loading: <Loader2Icon className="h-5 w-5 animate-spin text-blue-500" />,
};
export function Example() {
const showToast = (type) => {
toastManager.add({
title: type === "success" ? "Saved" : type === "error" ? "Failed" : "Saving...",
description: type === "success" ? "Your changes have been saved." : type === "error" ? "Could not save changes." : "Please wait...",
type,
});
};
return (
<ToastProvider toastManager={toastManager}>
<div className="flex gap-2">
<Button onClick={() => showToast("success")} variant="outline">Success</Button>
<Button onClick={() => showToast("error")} variant="outline">Error</Button>
<Button onClick={() => showToast("loading")} variant="outline">Loading</Button>
</div>
<ToastPortal>
<ToastViewport>
<ToastList renderToast={(toast) => (
<ToastContent>
<div className="flex items-start gap-3">
<div className="mt-0.5">
{typeIcons[toast.type] ?? <InfoIcon className="h-5 w-5 text-muted-foreground" />}
</div>
<div className="grid gap-1">
{toast.title && <ToastTitle>{toast.title}</ToastTitle>}
{toast.description && <ToastDescription>{toast.description}</ToastDescription>}
</div>
</div>
<ToastClose />
</ToastContent>
)} />
</ToastViewport>
</ToastPortal>
</ToastProvider>
);
}import { ToastProvider, ToastPortal, ToastViewport, ToastList, useToastManager, createToastManager } from '@/components/ui/toast';
import { Button } from '@/components/ui/button';
const toastManager = createToastManager();
function CloseAllButtons() {
const manager = useToastManager();
let counter = 0;
const addMultiple = () => {
for (let i = 0; i < 3; i++) {
counter++;
manager.add({
title: `Toast #${counter}`,
description: "This is a queued toast.",
type: "default",
});
}
};
return (
<div className="flex gap-2">
<Button onClick={addMultiple} variant="outline">
Add 3 Toasts
</Button>
<Button onClick={() => manager.close()} variant="destructive">
Close All
</Button>
</div>
);
}
export function Example() {
return (
<ToastProvider toastManager={toastManager}>
<CloseAllButtons />
<ToastPortal>
<ToastViewport>
<ToastList />
</ToastViewport>
</ToastPortal>
</ToastProvider>
);
}Notes
The toast manager
useToastManager() inside React and createToastManager() outside it both return add, update, close and promise. The hook also returns the current toasts.
const id = manager.add({ title: "Uploading", type: "loading", timeout: 0 })
// Change it in place: title, description, type, timeout or action.
manager.update(id, { title: "Upload complete", type: "success", timeout: 5000 })
manager.close(id) // one toast
manager.close() // every toastpromise runs a toast through loading, success and error. Each state takes a string, toast options, or a function of the result or error.
manager.promise(fetch("/api/save", { method: "POST" }), {
loading: "Saving...",
success: { title: "Saved", description: "Changes applied." },
error: (err) => ({ title: "Error", description: err.message }),
})Toast options
add and update take the same options.
{
title: ReactNode,
description: ReactNode,
type: "success" | "error" | "warning" | "loading" | string,
timeout: number, // 0 keeps it until dismissed
priority: "low" | "high",
actionProps: ButtonHTMLAttributes,
data: object, // anything renderToast needs
onClose: () => void,
onRemove: () => void,
}The type sets the variant: error maps to destructive; success, warning and loading keep their names; anything else is default. getVariantFromType does this mapping for a custom list.
Toasts from outside React
dinachi add toast also writes lib/toast.ts with a shared manager. Pass it to Toast once, then call it from API clients and error handlers.
// lib/toast.ts, written by the CLI
import { createToastManager } from "@/components/ui/toast"
export const toastManager = createToastManager()// providers.tsx
import { Toast } from "@/components/ui/toast"
import { toastManager } from "@/lib/toast"
export function Providers({ children }: { children: React.ReactNode }) {
return <Toast toastManager={toastManager}>{children}</Toast>
}// api-client.ts
import { toastManager } from "@/lib/toast"
export async function apiCall() {
try {
const res = await fetch("/api/data")
if (!res.ok) throw new Error("Request failed")
return res.json()
} catch (err) {
toastManager.add({ title: (err as Error).message, type: "error" })
throw err
}
}Custom rendering
Pass renderToast to Toast or ToastList. Anything passed as data to add is on toast.data.
manager.add({ title: "New message", data: { avatarUrl: "/img/user.png", sender: "Alice" } })
<Toast
renderToast={(toast) => (
<ToastContent>
<img src={toast.data?.avatarUrl} alt={toast.data?.sender} />
<div>
<ToastTitle>{toast.title}</ToastTitle>
<ToastDescription>{toast.description}</ToastDescription>
</div>
<ToastClose />
</ToastContent>
)}
/>Keep ToastClose in a custom layout: it is the only non-swipe dismiss and the button the agent presses.
The stack
- Collapsed, toasts behind the front one are scaled back and offset, and cut to its height.
- Hovering or focusing the stack expands it into a column.
limitsets how many it holds; more than about three stops reading as depth.
Timing
timeout on the provider or on a single add() sets how long a toast lasts; timeout: 0 keeps it until dismissed. Hovering or focusing the viewport pauses every timer.
<Toast timeout={5000}>
// Stays until dismissed.
add({ title: "Quota at 90%", timeout: 0 })API
0 keeps it until dismissed.createToastManager(), for firing toasts outside React.0 keeps it until dismissed.createToastManager().Takes the props of the element it renders.
ToastRoot.ToastList sets it from the type.actionProps has no children.Takes the props of the element it renders.
Agent
Inside an AgentProvider, it appears in agent.snapshot() as toast.
Reads
labelThe toast's title, or toast when it has none.
valueThe description.
typeThe toast's type, such as success or error.
Actions
dismiss()Presses the toast's ToastClose button.
Nothing to refuse: dismiss presses the close button as a person would. Without ToastClose in a custom layout, dismiss fails with a message. Action buttons are listed separately as button and take press without approval, even at high priority.
await agent.act({
steps: [{ ref: "Success", action: "dismiss" }],
})Accessibility
- The viewport is a region labelled Notifications, aria-live polite.
priority: "high"toasts are also announced assertively. Do not announce them again yourself.- Each toast is a dialog named by
ToastTitleand described byToastDescription; omit both and it is an empty dialog. - Put any action in a real button inside the toast, so F6 then Tab reaches it.
- Hover or focus on the viewport pauses every countdown.
- Use
timeout: 0for anything that must be acted on. - The stack expands on focus too. Swiping is pointer only, so keep a close button.