Build a toast notification system that any component can trigger.
<ToastProvider duration={5000} max={3}>
<App />
</ToastProvider>
// anywhere inside:
const toast = useToast();
toast.show({ type: 'success', message: 'Changes saved.' });
ToastProvider renders a toast stack in the bottom-right corner and exposes show({ type, message }) through context. type is success, error or info, each with its own colour and icon.
Each toast has a Dismiss button. Its accessible name is "Dismiss notification".
Auto-dismiss after duration ms (default 5000). The timer pauses while the toast is hovered or has keyboard focus and resumes with the time that was left.
At most max toasts (default 3) are visible. Showing another one drops the oldest.
Announcements:
role="status")role="alert").The live-region containers must exist before any toast is added.
Toasts don't steal focus.
Demo: three buttons ("Show success", "Show error", "Show info"). App accepts a toastDuration prop that it passes to the provider.
useToast return a stable object, and why does that matter?Spotted a mistake or a rights concern? Report an issue