GooseUI

Components

Toast

New

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

Popover

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.

Your browser:
@starting-style

Toasts appear through @starting-style and a CSS transition, no animation library. Without support they simply show up without the slide.

Your browser:

Preview

Click a button. Fire five toasts and hover the stack to expand it.

Installation

npx shadcn@latest add @gooseui/toast
pnpm dlx shadcn@latest add @gooseui/toast
yarn dlx shadcn@latest add @gooseui/toast
bunx --bun shadcn@latest add @gooseui/toast

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 toast

Update 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

PropTypeDefaultDescription
position"top-left" | "top-center" | "top-right" | "bottom-left" | "bottom-center" | "bottom-right""top-right"Corner or edge of the screen
visibleToastsnumber3How many toasts show in the stack
expandbooleanfalseKeep the stack always expanded
richColorsbooleanfalseTint toasts by their type
closeButtonbooleantrueShow × on every toast, false hides them
gapnumber14Space between toasts in px
offsetnumber | string24 (16 on phones)Distance to the screen edge, px or any CSS length
dir"ltr" | "rtl" | "auto"-Text direction
durationnumber4000Default lifetime in ms
hotkeystring[]["altKey", "KeyT"]Keys that focus the toasts
labelstring"Notifications"Accessible name of the region
closeLabelstring"Close"Accessible name of the × button
classNamestring-Additional CSS classes of the container
styleCSSProperties-Styles of the container. Variables: --w width (356px), --oy vertical offset only

toast() options

PropTypeDefaultDescription
idstring | numberautoExisting id updates that toast instead of adding a new one
descriptionReactNode-Second line under the title
durationnumberToaster durationLifetime 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
iconReactNodeicon of the typeOwn icon instead of the icon of the type, null hides it
progressbooleantrueBar that runs down until the toast closes, false hides it
closeButtonbooleanToaster valueShow × on this toast
classNamestring-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

MethodReturnsDescription
toast(title, options?)idPlain toast
toast.success | error | warning | info | loadingidToast of the type with its icon
toast.promise(promise, { loading, success, error })idLoading toast that turns into success or error
toast.dismiss(id?)voidCloses 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