Snackbar
A brief message about something the app just did, with at most one action. It rises in from the bottom on a spring, waits while you hover it, and can be swiped away.
Install
npx shadcn@latest add @nelumbo/snackbarMessage
Short and in the past tense: what just happened. Gone after four seconds.
With an action
One action, usually Undo. It stays seven seconds so there's time to reach it, and F8 jumps to it from the keyboard.
Longer messages
Two lines at most. When the text is long, the action moves below it.
Persistent
For something that needs reading, keep it until it's dismissed and add a close button. Errors can interrupt screen readers with urgent.
Queue
One at a time, as in Material: later messages wait their turn instead of piling up.
Sonner-style API
toast and Toaster are sonner's names for the same API, with its types, descriptions and promise toasts. Types add an icon; turn on rich colors (below) to tint them too.
// Coming from shadcn's Sonner? Change the imports:- import { toast } from "sonner"- import { Toaster } from "@/components/ui/sonner"+ import { toast, Toaster } from "@/components/ui/snackbar"toast.success("Changes saved")toast.error("Couldn't save", { description: "You're offline." })toast.promise(upload(), { loading: "Uploading…", success: (n) => `${n} photos uploaded`, error: "Upload failed",})// <Toaster /> stacks and takes richColors, like sonner's.// Differences: bottom positions only, a single action (no cancel), no toast.custom.Stack and colors
Stack shows up to three at once like sonner: the newest in front, older ones tucked behind; hover or focus fans them out, and each keeps its own timer. Rich colors tint success, error, warning and info with their container colors. Both are host settings; Toaster stacks by default.
<Snackbars /> // one at a time (Material)<Snackbars stack richColors /> // sonner-style<Toaster /> // stacks by default, like sonner<Toaster richColors visibleToasts={4} />toast.error("Payment failed", { richColors: true }) // tint just this oneUsage
Mount the host once, near the root of your app. Above a navigation bar, pass offset so snackbars clear it.
// app/layout.tsximport { Snackbars } from "@/components/ui/snackbar"<body> {children} <Snackbars /> {/* or <Snackbars offset={80} /> above a navigation bar */}</body>// anywhereimport { snackbar } from "@/components/ui/snackbar"snackbar("Message archived", { action: { label: "Undo", onClick: restore },})Options
| Prop | Type | Default | Notes |
|---|---|---|---|
snackbar / toast | (message, options?) => id | .success .error .warning .info .loading .message .promise .dismiss | |
description | ReactNode | Second line; the message reads as a title | |
action | { label, onClick, altText? } | One action; altText describes it for screen readers | |
icon | IconName | ReactNode | Leading icon; types set one | |
id | number | string | Reuse to update a snackbar in place | |
closable / closeButton | boolean | false | Close button |
duration | number (ms) | 4000, 7000 with an action | Infinity keeps it; loading stays until updated |
urgent | boolean | false (true for errors) | Announce assertively |
onDismiss / onAutoClose | (id) => void | Closed by the person / by the timer | |
richColors | boolean | Per snackbar: tint it with its type's color | |
<Snackbars position> | "bottom-center" | "bottom-start" | "bottom-end" | "bottom-center" | Also bottom-left / bottom-right |
<Snackbars stack> | boolean | false (true on <Toaster>) | Several at once, fanned out on hover |
<Snackbars visibleToasts> | number | 3 | How many a stack shows |
<Snackbars richColors> | boolean | false | Tint success, error, warning and info |
<Snackbars offset> | number (px) | 0 | Extra bottom space |