A toast stack with the API of sonner: toast.success(), toast.promise(), actions, swipe to dismiss and a stack that expands on hover. The container lives in the browser top layer through the Popover API, so toasts show above dialogs. Animations are plain CSS transitions with @starting-style, JavaScript only measures heights and runs the timers.
Compared to sonner: about 4.7 kB gzip against 11 kB for sonner with its styles, icons and theme wrapper, and no npm dependencies at all. Not repeated: toast.custom(), vertical swipe, expand on touch tap and the unwrap() of toast.promise().
Browser Support
The toast container is a popover=manual element, so it sits in the top layer above everything, including a modal dialog opened later. Without the API it falls back to position: fixed with the highest z-index.
Toasts appear through @starting-style and a CSS transition, no animation library. Without support they simply show up without the slide.
Preview
Click a button. Fire five toasts and hover the stack to expand it.
Installation
Usage
Mount one Toaster, for example in the root layout. After that toast() works from any place, even outside React. The file is a client module, so call toast from client code: event handlers, effects, callbacks.
// app/layout.tsx
import { Toaster } from "@/components/ui/toast"
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
{children}
<Toaster />
</body>
</html>
)
}
// anywhere in client code
import { toast } from "@/components/ui/toast"
toast("Event created")
toast.success("Changes saved")Examples
Types
success, error, warning, info and loading come with an icon. An error toast gets role=alert, so screen readers read it right away.
toast("Plain toast")
toast.success("Changes saved")
toast.error("Could not save the file")
toast.warning("Disk is almost full")
toast.info("A new version is available")
toast.loading("Uploading the file")Description and action
description adds a second line. action and cancel add buttons: a click runs onClick and closes the toast, unless you call event.preventDefault().
toast("Message sent", {
description: "Anna will get it in a minute.",
action: {
label: "Undo",
onClick: () => toast.info("Message recalled"),
},
// cancel: { label: "Later" },
})Promise
toast.promise() shows a loading toast, then turns it into success or error by the same id.
toast.promise(saveDraft(), {
loading: "Saving the draft…",
success: (draft) => `${draft.name} saved`,
error: "The server did not answer",
})Positions
Six positions. On narrow screens the toasts take the full width. The offset follows the safe area of the phone.
<Toaster position="top-center" />
// top-left | top-center | top-right
// bottom-left | bottom-center | bottom-right
<Toaster position="bottom-left" offset={32} gap={10} />Stack of toasts
Collapsed toasts lie behind each other and get smaller. Hover or focus the stack: it expands by the real height of each toast, timers stop, and start again when the pointer leaves. Only visibleToasts toasts show in the stack, the others wait behind.
<Toaster visibleToasts={3} />
// <Toaster expand /> keeps the stack always expanded
for (const title of titles) toast(title)Swipe
Drag a toast toward the screen edge. It follows the pointer, and in the other direction it resists. Past 45 px or a fast flick closes it, a short drag springs back. The × button closes a toast too.
toast("Swipe me to the right", {
description: "Drag past 45 px or flick it.",
duration: 20000,
})Toast that does not close
duration: Infinity keeps the toast until the user closes it. toast.dismiss() without an id closes all of them.
toast.info("I do not close on my own", {
duration: Infinity,
closeButton: true,
})
toast.dismiss() // all toasts
toast.dismiss(id) // one toastUpdate by id
A toast with an existing id is not added again, it is updated in place: the text changes, the timer and the progress bar start over.
toast.loading("Uploading 0%", { id: "upload" })
// later, the same id updates the toast in place
toast.loading("Uploading 60%", { id: "upload" })
toast.success("Upload finished", { id: "upload" })Rich colors and close button
richColors tints the whole toast by its type. Every toast has a × button, closeButton={false} removes it. Both are options of the Toaster.
<Toaster richColors />
// <Toaster closeButton={false} />Progress bar
Every toast draws a bar that runs down until it closes. It is a CSS animation, so hover and focus pause it through animation-play-state. progress: false removes the bar.
toast.success("Closes in 6 seconds", { duration: 6000 })
toast.success("No bar", { progress: false })Toaster props
| Prop | Type | Default | Description |
|---|---|---|---|
| position | "top-left" | "top-center" | "top-right" | "bottom-left" | "bottom-center" | "bottom-right" | "top-right" | Corner or edge of the screen |
| visibleToasts | number | 3 | How many toasts show in the stack |
| expand | boolean | false | Keep the stack always expanded |
| richColors | boolean | false | Tint toasts by their type |
| closeButton | boolean | true | Show × on every toast, false hides them |
| gap | number | 14 | Space between toasts in px |
| offset | number | string | 24 (16 on phones) | Distance to the screen edge, px or any CSS length |
| dir | "ltr" | "rtl" | "auto" | - | Text direction |
| duration | number | 4000 | Default lifetime in ms |
| hotkey | string[] | ["altKey", "KeyT"] | Keys that focus the toasts |
| label | string | "Notifications" | Accessible name of the region |
| closeLabel | string | "Close" | Accessible name of the × button |
| className | string | - | Additional CSS classes of the container |
| style | CSSProperties | - | Styles of the container. Variables: --w width (356px), --oy vertical offset only |
toast() options
| Prop | Type | Default | Description |
|---|---|---|---|
| id | string | number | auto | Existing id updates that toast instead of adding a new one |
| description | ReactNode | - | Second line under the title |
| duration | number | Toaster duration | Lifetime in ms. Infinity does not close; loading toasts never close on their own |
| action | { label: ReactNode; onClick?: (event) => void } | - | Button with onClick, closes the toast unless prevented |
| cancel | { label: ReactNode; onClick?: (event) => void } | - | Second button, closes the toast |
| icon | ReactNode | icon of the type | Own icon instead of the icon of the type, null hides it |
| progress | boolean | true | Bar that runs down until the toast closes, false hides it |
| closeButton | boolean | Toaster value | Show × on this toast |
| className | string | - | Class of the toast |
| onDismiss | (toast) => void | - | Called when the toast is closed by a user or toast.dismiss() |
| onAutoClose | (toast) => void | - | Called when the timer closes the toast |
Methods
| Method | Returns | Description |
|---|---|---|
| toast(title, options?) | id | Plain toast |
| toast.success | error | warning | info | loading | id | Toast of the type with its icon |
| toast.promise(promise, { loading, success, error }) | id | Loading toast that turns into success or error |
| toast.dismiss(id?) | void | Closes a toast by id, or all toasts without an id |
Features
- Top layer — a popover container above dialogs; the toasts do not receive clicks while a modal dialog is open, that is how <dialog> works
- Real heights — the stack measures each toast, CSS variables animate it
- RTL — dir=rtl mirrors the layout, the close button and the progress bar
- Theme — colors come from shadcn tokens, success, warning and info tokens are used when they exist