Docs

Toast

A production-grade toast notification component built on top of Sonner with theme-aware styling and comprehensive positioning options.

Overview

The Toast component provides a comprehensive notification system with theme-aware styling, multiple positioning options, and rich customization capabilities. Built on top of Sonner, it offers a production-ready solution for displaying temporary messages to users.

Installation

pnpm dlx shadcn@latest add https://deltacomponents.dev/r/toast.json

Usage

Add the Toaster Component

First, add the Toaster component to your root layout:

import { Toaster } from "@/components/ui/sonner"
export default function RootLayout({ children }) {
return (
<html lang="en">
<head />
<body>
<main>{children}</main>
<Toaster />
</body>
</html>
)
}

Use the Toast Function

import { toast } from "@/registry/components/toast"
export default function App() {
return (
<div>
<button onClick={() => toast.success("Success!")}>Show Toast</button>
</div>
)
}

API Reference

Toast Component

PropTypeDefaultDescription
position"top-left" | "top-center" | "top-right" | "bottom-left" | "bottom-center" | "bottom-right""bottom-right"Position of the toast
richColorsbooleantrueEnable rich colors for better visual hierarchy
visibleToastsnumber3Maximum number of toasts to show at once
classNamestringundefinedCustom class name for the toaster
toastOptionsobjectundefinedCustom styling and behavior options

Toast Function

MethodDescription
toast(message, options?)Display a default toast
toast.success(message, options?)Display a success toast
toast.error(message, options?)Display an error toast
toast.warning(message, options?)Display a warning toast
toast.info(message, options?)Display an info toast
toast.promise(promise, options)Display a promise-based toast

Toast Options

OptionTypeDescription
durationnumberDuration in milliseconds
descriptionstringAdditional description text
action{label: string, onClick: () => void}Action button configuration
cancel{label: string, onClick: () => void}Cancel button configuration
idstringCustom toast ID
onDismiss() => voidCallback when toast is dismissed
onAutoClose() => voidCallback when toast auto-closes

Examples

Basic Toast Types

Positioning

Current position: bottom-right

Action Buttons and Promises

Custom Content and Styling

Theme-Aware Colors

Current:

Theme Integration

The Toast component automatically adapts to your theme using the same color system as the Admonition component:

  • Success: Emerald colors (emerald-50/emerald-950 backgrounds)
  • Error: Red colors (red-50/red-950 backgrounds)
  • Warning: Amber colors (amber-50/amber-950 backgrounds)
  • Info: Blue colors (blue-50/blue-950 backgrounds)
  • Default: Gray colors (gray-50/gray-950 backgrounds)

Each color variant includes proper dark mode support with adjusted opacity and contrast ratios.

Best Practices

  1. Position: Use bottom-right for general notifications, top-center for important system messages
  2. Duration: Set appropriate durations - success messages can be shorter (2-3s), errors longer (5-8s)
  3. Actions: Include action buttons for recoverable errors or important confirmations
  4. Content: Keep messages concise and actionable
  5. Accessibility: Messages are automatically announced to screen readers

Accessibility

  • Toast messages are announced to screen readers
  • Supports keyboard navigation for action buttons
  • Proper ARIA labeling for toast types
  • Color contrast meets WCAG guidelines
  • Respects prefers-reduced-motion settings