sonner

repository·main·Indexed 28 days ago

https://github.com/emilkowalski/sonner

An opinionated toast component library for React. It provides a <Toaster /> container and a toast() function to trigger notifications. Features include support for promise-based toasts, custom actions, rich colors, and an unstyled mode for Tailwind CSS integration. It includes a useSonner hook to access active toasts and supports various semantic types such as success, error, info, warning, and loading.

Tokens
6.8K
Snippets
31
Records
41
Agent score
95%

What's inside sonner

  1. Apply Tailwind styles to specific toast types

    main

    When using unstyled: true, you can define specific Tailwind classes for different toast types (error, success, warning, info) within the classNames object inside toastOptions.

    <Toaster
      toastOptions={{
        unstyled: true,
        classNames: {
          error: 'bg-red-400',
          success: 'text-green-400',
          warning: 'text-yellow-400',
          info: 'bg-blue-400',
        },
      }}
    />
  2. Add Toaster to your app

    main

    To enable toast notifications, you must include the <Toaster /> component in your application. It can be placed anywhere, including server components like layout.tsx.

    import { Toaster } from 'sonner';
    
    export default function RootLayout({ children }: { children: React.ReactNode }) {
      return (
        <html lang="en">
          <body>
            {children}
            <Toaster />
          </body>
        </html>
      );
    }
  3. Use sonner in your React app

    main

    To use sonner, you must first add the <Toaster /> component to your application's component tree. This component acts as the container where all toasts will be rendered. Once <Toaster /> is mounted, you can trigger toasts from anywhere in your app using the toast() function.

    import { Toaster, toast } from 'sonner';
    
    // ...
    
    function App() {
      return (
        <div>
          <Toaster />
          <button onClick={() => toast('My first toast')}>Give me a toast</button>
        </div>
      );
    }
  4. Apply global styling via Toaster toastOptions

    main

    To apply consistent styling to every toast in your application, use the toastOptions prop on the <Toaster /> component. You can provide a style object for inline styles or a className string for CSS classes.

    <Toaster
      toastOptions={{
        style: {
          background: 'red',
        },
        className: 'class',
      }}
    />
  5. Style toasts with Tailwind CSS using unstyled mode

    main

    The recommended way to use Tailwind CSS with Sonner is to set unstyled: true within toastOptions. This removes default styles and allows you to use the classNames object to target specific elements: toast, title, description, actionButton, cancelButton, and closeButton.

    <Toaster
      toastOptions={{
        unstyled: true,
        classNames: {
          toast: 'bg-blue-400',
          title: 'text-red-400',
          description: 'text-red-400',
          actionButton: 'bg-zinc-400',
          cancelButton: 'bg-orange-400',
          closeButton: 'bg-lime-400',
        },
      }}
    />
  6. Render multiple Toasters with unique IDs

    main

    You can render multiple Toaster components simultaneously by assigning each a unique id. When calling the toast() function, use the toasterId option to target a specific toaster.

    <Toaster id="global" position="top-right" />
    <Toaster id="canvas" position="bottom-left" />
    
    <button onClick={() => toast('Global toast', { toasterId: 'global' })}>
      Show in Global Toaster
    </button>
    <button onClick={() => toast('Canvas toast', { toasterId: 'canvas' })}>
      Show in Canvas Toaster
    </button>