Sonner
Stacked toast notification system with promise/loading states, built on the sonner library with Unified UI styling.
Basic
Stacked toast notification system with promise/loading states, built on the sonner library with Unified UI styling. Trigger toasts from anywhere using the toast() function.
Installation
Install the component via the CLI in one command.
npx @work-rjkashyap/unified-ui add sonnerpnpm dlx @work-rjkashyap/unified-ui add sonnernpx @work-rjkashyap/unified-ui add sonnerbunx @work-rjkashyap/unified-ui add sonnerIf you haven't initialized your project yet, run npx @work-rjkashyap/unified-ui init first. See the CLI docs for details.
Or install the full package
Use this approach if you prefer to install the entire design system as a dependency instead of copying individual components.
npm install @work-rjkashyap/unified-uipnpm add @work-rjkashyap/unified-uiyarn add @work-rjkashyap/unified-uibun add @work-rjkashyap/unified-uiAnatomy
import { SonnerToaster } from "@work-rjkashyap/unified-ui";Setup
Place <SonnerToaster /> once in your root layout:
// app/layout.tsx
import { SonnerToaster } from "@work-rjkashyap/unified-ui";
export default function RootLayout({ children }) {
return (
<html>
<body>
{children}
<SonnerToaster />
</body>
</html>
);
}Examples
Basic Toasts
With Description
With Action
Promise-Based
Loading Toast
Custom Duration
Custom JSX
Props (SonnerToaster)
| Prop | Type | Default | Description |
|---|---|---|---|
position | "top-left" | "top-center" | ... | "bottom-right" | "bottom-right" | Toast stack position. |
richColors | boolean | true | Use distinct colors per toast type. |
closeButton | boolean | true | Show close button on each toast. |
duration | number | 4000 | Auto-dismiss duration in ms. |
visibleToasts | number | 3 | Max visible toasts at once. |
expand | boolean | true | Expand toast stack on hover. |
theme | "light" | "dark" | "system" | "system" | Theme override. |
offset | number | string | 16 | Offset from viewport edge. |
gap | number | 14 | Gap between toasts. |
toastOptions | object | — | Custom class names for toast elements. |
className | string | — | Additional CSS classes on container. |
toast() API
| Method | Description |
|---|---|
toast(message) | Default toast. |
toast.success() | Success toast with green styling. |
toast.error() | Error toast with red styling. |
toast.warning() | Warning toast with amber styling. |
toast.info() | Info toast with blue styling. |
toast.loading() | Persistent loading toast with spinner. |
toast.promise() | Promise-based with loading/success/error. |
toast.custom() | Render custom JSX. |
toast.dismiss() | Dismiss a toast by ID. |
Toast Options
| Option | Type | Description |
|---|---|---|
description | string | Body text below the title. |
duration | number | Override auto-dismiss duration. |
action | { label: string, onClick: () => void } | Action button config. |
cancel | { label: string, onClick: () => void } | Cancel button config. |
id | string | number | Custom toast ID for updates. |
icon | ReactNode | Custom icon. |
onDismiss | () => void | Callback when toast dismisses. |
Accessibility
- Toasts use
role="status"andaria-live="polite"for screen reader announcements. - Keyboard dismissible via
Escapeor the close button. - Focus management handled by sonner internally.
- Action and cancel buttons are fully keyboard accessible.
For critical, blocking messages that require user action, use AlertDialog
or ConfirmDialog instead of toast notifications.
Design Tokens
| Token | Usage |
|---|---|
--background | Toast background |
--foreground | Toast title text |
--muted-foreground | Toast description text |
--border | Toast border |
--primary | Action button background |
--primary-foreground | Action button text |
--secondary | Cancel button background |
--success | Success toast accent |
--danger | Error toast accent |
--warning | Warning toast accent |
--info | Info toast accent |
--radius-lg | Toast border radius |
--shadow-lg | Toast shadow |