Glrk UI

Toast

A wrapper for Base UI Toast with type variants, icons, promise support, action buttons, position routing, and a useToast hook.

Position

Installation

npx shadcn@latest add @glrk-ui/toast

If you haven't set up the prerequisites yet, check out registry setup.

Usage

Basic

Wrap your app (or subtree) with <Toaster>. Call useToast() from any child component:

import { Toaster, useToast } from "@/components/ui/toast"

function SaveButton() {
  const toast = useToast()
  return (
    <button onClick={() => toast.success("Changes saved.")}>Save</button>
  )
}

export function App() {
  return (
    <Toaster>
      <SaveButton />
    </Toaster>
  )
}

Variants

const toast = useToast()

toast.add({ description: "Notification sent." })                   // default
toast.success("Changes saved.")
toast.error("Something went wrong.", { title: "Error" })
toast.warning("Session expires in 5 minutes.")
toast.info("Update available.", { title: "v2.0" })

Each variant renders a colored border/background and a matching icon.

Title and description

toast.add({
  title: "Sync complete",
  description: "All 42 files have been uploaded.",
  type: "success",
})

Promise

Transitions automatically: loading → success or error when the promise settles:

toast.promise(
  uploadFile(file),
  {
    loading: "Uploading...",
    success: (data) => `${data.name} uploaded.`,
    error: "Upload failed.",
  },
)

Route to a specific position:

toast.promise(fetch("/api"), messages, { position: "top-center" })

Action button

const id = toast.add({
  title: "File deleted",
  description: "document.pdf removed.",
  actionProps: {
    children: "Undo",
    onClick: () => {
      toast.close(id)
      toast.success("document.pdf restored.")
    },
  },
})

Persistent toast

Set timeout: 0 to disable auto-dismiss:

toast.add({
  title: "Pinned",
  description: "This won't auto-dismiss.",
  timeout: 0,
})

Update

Update an existing toast by id — useful for loading → result transitions:

const id = toast.add({ description: "Uploading...", type: "loading", timeout: 0 })

// later...
toast.update(id, { description: "Upload complete.", type: "success", timeout: 3000 })

Position

Each toast method accepts position to override the Toaster default:

toast.info("Done.", { position: "top-center" })
toast.success("Saved.", { position: "bottom-left" })

Available positions: top-left · top-center · top-right · bottom-left · bottom-center · bottom-right

Priority

Controls the aria-live politeness for screen readers:

toast.add({ description: "Critical update.", priority: "high" })
toast.info("Background task complete.", { priority: "low" })

Custom icon

toast.success("Report ready.", { icon: <FileTextIcon className="text-blue-500" /> })

// suppress icon entirely
toast.error("Silent error.", { icon: null })

External manager

For triggering toasts outside React (axios interceptors, utils, etc.), use createToastManager with primitives directly:

import {
  createToastManager,
  ToastProvider,
  ToastPortal,
  ToastViewport,
  ToastRoot,
  ToastContent,
  ToastTitle,
  ToastDescription,
  ToastClose,
  useToastManager,
} from "@/components/ui/toast"

export const toast = createToastManager()

function ToastList() {
  const { toasts } = useToastManager()
  return toasts.map((t) => (
    <ToastRoot key={t.id} toast={t}>
      <ToastContent>
        <ToastTitle />
        <ToastDescription />
        <ToastClose />
      </ToastContent>
    </ToastRoot>
  ))
}

// In root layout
<ToastProvider toastManager={toast}>
  <ToastPortal>
    <ToastViewport>
      <ToastList />
    </ToastViewport>
  </ToastPortal>
</ToastProvider>

// From anywhere — no hook needed
toast.add({ description: "Done.", type: "success" })

Reference

useToast

const toast = useToast()

toast.add(options)                           // → string (toast id)
toast.success(description, options?)         // → string
toast.error(description, options?)           // → string
toast.warning(description, options?)         // → string
toast.info(description, options?)            // → string
toast.promise(promise, messages, opts?)      // → Promise<Value>
toast.update(id, partialOptions)
toast.close(id?, position?)                 // omit id to close all

toast.add options:

OptionTypeDefaultDescription
titleReactNode—Toast heading
descriptionReactNode—Toast body
typetoastTypeT"default"Variant — drives icon and color
timeoutnumber5000Auto-dismiss ms. 0 = no auto-dismiss
priority'low' | 'high''low'aria-live politeness
positiontoastPositionTToaster defaultOverride placement
actionPropsButtonProps—Renders an action button inside the toast
onClose() => void—Called when toast is dismissed
onRemove() => void—Called after exit animation completes
iconReactNode | nulltype defaultCustom icon. null suppresses the icon

Toaster

Prop

Type

toastTypeT

type toastTypeT = "default" | "success" | "error" | "warning" | "info" | "loading"