Installation
npx @kuzenbo/cli add toast
Install the required dependencies:npm install @kuzenbo/notifications
Copy the component source:
// See source at packages/notifications/src/ui/toast/
Usage
import { ToastProvider, useToast } from "@kuzenbo/notifications";
function App() {
return (
<ToastProvider>
<YourApp />
</ToastProvider>
);
}
function YourComponent() {
const toast = useToast();
return (
<button onClick={() => toast.add({ title: "Toast notification" })}>
Show Toast
</button>
);
}
Examples
Toast Types
Use semantic methods for different notification types.
const toast = useToast();
// Success toast
toast.success({
title: "Success",
description: "Your changes have been saved."
});
// Error toast
toast.error({
title: "Error",
description: "Something went wrong."
});
// Warning toast
toast.warning({
title: "Warning",
description: "Your trial is expiring soon."
});
// Info toast
toast.info({
title: "Info",
description: "This feature is in beta."
});
// Loading toast
toast.loading({
title: "Loading",
description: "Please wait..."
});
Promise Toast
Automatically handle loading, success, and error states.
const toast = useToast();
toast.promise(
fetchData(),
{
loading: { title: "Fetching data..." },
success: { title: "Data loaded successfully" },
error: { title: "Failed to load data" }
}
);
Update Toast
Update an existing toast programmatically.
const toast = useToast();
const toastId = toast.add({
title: "Processing...",
type: "loading"
});
// Later, update it
toast.update(toastId, {
title: "Done!",
type: "success"
});
Close Toast
Manually close a toast.
const toast = useToast();
const toastId = toast.add({ title: "Click to dismiss" });
// Close it
toast.close(toastId);
Stacked Toasts
Multiple toasts stack automatically.
const toast = useToast();
toast.add({ title: "First notification" });
toast.add({ title: "Second notification" });
toast.add({ title: "Third notification" });
Anchored Toasts
Position toasts relative to specific elements.
import { Toast } from "@kuzenbo/notifications";
<Toast.Root toast={toast}>
<Toast.Positioner anchor={anchorRef}>
<Toast.Content>
<Toast.Title>Anchored toast</Toast.Title>
<Toast.Description>Positioned near the trigger.</Toast.Description>
</Toast.Content>
</Toast.Positioner>
</Toast.Root>
Toast Sizes
Control toast size with the size prop.
const toast = useToast();
toast.add({
title: "Small toast",
size: "sm"
});
toast.add({
title: "Large toast",
size: "lg"
});
API Reference
useToast
Hook that returns toast manager methods.
Methods
add
(options: ToastOptions) => string
Add a new toast and return its ID.
success
(options: ToastOptions) => string
Add a success toast.
error
(options: ToastOptions) => string
Add an error toast.
warning
(options: ToastOptions) => string
Add a warning toast.
info
(options: ToastOptions) => string
Add an info toast.
loading
(options: ToastOptions) => string
Add a loading toast.
promise
<T>(promise: Promise<T>, options: PromiseOptions) => Promise<T>
Create a promise-based toast with loading, success, and error states.
update
(toastId: string, options: UpdateOptions) => void
Update an existing toast.
close
(toastId: string) => void
Close a specific toast.
Array of all active toasts.
ToastProvider
Default size for all toasts.Options: "xs" | "sm" | "md" | "lg" | "xl"
Toast.Root
Toast object from the toast manager.
Toast.Content
Toast.Title
Toast.Description
Toast.Action
Container for action buttons.
Toast.Close
Close button for dismissing toasts.
Accessibility
- Toasts use ARIA live regions for screen reader announcements
- Toasts can be dismissed with keyboard (Escape key)
- Focus management preserves user context
- Sufficient duration for users to read content
- Color is not the only indicator of toast type
Best Practices
- Use toasts for temporary, non-critical notifications
- Don’t use toasts for critical errors requiring user action
- Keep toast messages concise (1-2 lines)
- Provide meaningful titles and descriptions
- Set appropriate auto-dismiss durations
- Limit the number of simultaneous toasts
- Use promise toasts for async operations