Components
An opinionated toast component for React.
Playground
toast() function imported from the sonner package itself — there is no per-call component to render.// app/layout.tsx — mount once
import { Toaster } from "@workspace/ui/components/sonner"
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
{children}
<Toaster />
</body>
</html>
)
}
// any client component
import { toast } from "sonner"
<Button variant="outline" onClick={() => toast("Event has been created.")}>
Show toast
</Button>toast.success, toast.info, toast.warning and toast.error prepend the matching Phosphor icon configured in the wrapper (CheckCircle, Info, Warning, XCircle), so state is conveyed by an icon as well as by the variant's color.toast.success("Changes saved")
toast.info("A new version is available")
toast.warning("Storage is almost full")
toast.error("Something went wrong")description for a second, muted line under the title. Keep the title short and put the detail — dates, file names, counts — in the description.toast("Event has been created", {
description: "Monday, January 3rd at 6:00pm",
})action renders a small button at the end of the toast; clicking it runs onClick and dismisses the toast. An Undo action is the classic use — it turns a destructive flow into a reversible one without a blocking confirm dialog.toast("Event has been created", {
action: {
label: "Undo",
onClick: () => restoreEvent(),
},
})toast.promise follows an async operation through its lifecycle: it shows the loading message with the wrapper's spinning icon, then swaps to the success or error message in place when the promise settles. success and error accept a function that receives the resolved value or the rejection reason.toast.promise(saveEvent(), {
loading: "Saving event…",
success: (data) => `${data.name} has been created`,
error: "Could not save the event",
})richColors — per toast, or on the Toaster for all of them — tints the surface with Sonner's built-in green/red/amber/blue palette for louder success and error feedback.toast.success("Changes saved", { richColors: true })
toast.error("Something went wrong", { richColors: true })duration per toast — or set it to Infinity with closeButton for messages that must be acknowledged — and call toast.dismiss() to clear one toast by id, or all of them with no argument.toast("This toast stays until you close it", {
duration: Infinity,
closeButton: true,
})
toast.dismiss() // dismiss alltoast("Event deleted", {
action: { label: "Undo", onClick: () => restoreEvent() },
duration: 10000, // more time to reach the action
closeButton: true,
})sonner Toaster that resolves its theme from next-themes, swaps the default icons for Phosphor ones and maps the toast surface to the popover tokens. Everything else is the toast() function from sonner. Toaster accepts every upstream prop; per-toast options override the Toaster-level defaults. See the Sonner documentation for the full API.| Part | Prop | Default |
|---|---|---|
| Toaster | position?: "top-left" | "top-center" | "top-right" | "bottom-left" | "bottom-center" | "bottom-right" | "bottom-right" |
| Toaster | duration?: number | 4000 |
| Toaster | richColors?: boolean, closeButton?: boolean, invert?: boolean | false |
| Toaster | expand?: boolean | false |
| Toaster | visibleToasts?: number | 3 |
| Toaster | dir?: "ltr" | "rtl" | "auto" | "auto" |
| toast() | description?: ReactNode, action?: { label, onClick } | — |
| toast() | duration?, position?, richColors?, closeButton? | Toaster |
| toast() | id?: string | number | auto |
toast("Event has been created.")
toast.success("Changes saved")
toast.info("A new version is available")
toast.warning("Storage is almost full")
toast.error("Something went wrong")
toast.loading("Uploading…")
toast.promise(promise, { loading, success, error })
toast.custom((id) => <MyToast id={id} />)
toast.dismiss(id) // no id → dismiss allTokens used
5 design tokens consumed by the Sonner component.
--popover
Popover
--popover-foreground
Popover Foreground
--border
Border
--radius
Radius (10px)
--spacing
Spacing unit (4px)