Sonner Component

An opinionated toast component for React with beautiful animations and a clean design. Sonner is built and maintained by emilkowalski.

  • Multiple toast types: success, error, warning, info
  • Custom toast content including actions
  • Smooth animations
  • Promise-based toasts
  • Accessible design

Installation

The Sonner component is part of our UI library. You can import it directly from the components directory.


Usage

Here is how to use Sonner in your project.

React TSX
1import { Toaster, toast } from "@/components/modern-ui/sonner"

First, add the <Toaster /> component to your app:

React TSX
1// In your layout or root component
2import { SonnerGlobal } from "@/components/modern-ui/sonner"
3
4export default function RootLayout({ children }) {
5  return (
6    <html lang="en">
7      <body>
8        {children}
9        <SonnerGlobal />
10      </body>
11    </html>
12  )
13}

Then, use the toast() function anywhere in your components:

React TSX
1import { toast } from "@/components/modern-ui/sonner"
2
3function MyComponent() {
4  return (
5    <button onClick={() => toast("My first toast")}>
6      Show Toast
7    </button>
8  )
9}

Examples

Basic Toast

Toast Types

Custom Toast

Toast with Actions

Promise Toast

API Reference

Toaster

PropTypeDefaultDescription
position"top-left" | "top-right" | "bottom-left" | "bottom-right" | "top-center" | "bottom-center""bottom-right"Position of the toasts
expandbooleanfalseWhether to expand toasts on hover
durationnumber4000Default duration for toasts in milliseconds
visibleToastsnumber3Maximum number of visible toasts
closeButtonbooleanfalseWhether to show close buttons on toasts
offsetstring | number-Offset from the edges of the screen
theme"light" | "dark" | "system""system"Color theme
richColorsbooleanfalseWhether to use rich colors for different toast types
classNamestring-Additional CSS classes for the toast container
toastOptionsToastOptions-Default options for all toasts

toast() Function

The toast() function accepts the following parameters:

React TSX
1toast(message, options?)
ParameterTypeDescription
messagestring | React.ReactNodeContent of the toast
optionsToastOptionsConfiguration options for the toast

ToastOptions

OptionTypeDescription
idstring | numberCustom ID for the toast
durationnumberDuration in milliseconds
iconReact.ReactNodeCustom icon to display
descriptionstring | React.ReactNodeAdditional description text
action{ label: string, onClick: () => void }Action button configuration
cancel{ label: string, onClick: () => void }Cancel button configuration
onDismiss() => voidFunction called when toast is dismissed
onAutoClose(toast: ToastT) => voidFunction called when toast automatically closes
positionSame as Toaster positionPosition override for individual toasts
classNamestringAdditional CSS classes for this toast
dismissiblebooleanWhether the toast can be dismissed by clicking
importantbooleanPrevents toast from being automatically removed