mirror of
https://github.com/shadcn-ui/ui.git
synced 2026-06-22 12:15:43 +00:00
155 lines
2.9 KiB
Plaintext
155 lines
2.9 KiB
Plaintext
---
|
|
title: Sonner
|
|
description: An opinionated toast component for React.
|
|
component: true
|
|
links:
|
|
doc: https://sonner.emilkowal.ski
|
|
---
|
|
|
|
<ComponentPreview name="sonner-demo" />
|
|
|
|
## About
|
|
|
|
Sonner is built and maintained by [emilkowalski](https://twitter.com/emilkowalski).
|
|
|
|
## Installation
|
|
|
|
<CodeTabs>
|
|
|
|
<TabsList>
|
|
<TabsTrigger value="cli">CLI</TabsTrigger>
|
|
<TabsTrigger value="manual">Manual</TabsTrigger>
|
|
</TabsList>
|
|
<TabsContent value="cli">
|
|
|
|
<Steps>
|
|
|
|
<Step>Run the following command:</Step>
|
|
|
|
```bash
|
|
npx shadcn@latest add sonner
|
|
```
|
|
|
|
<Step>Add the Toaster component</Step>
|
|
|
|
```tsx title="app/layout.tsx" {1,9}
|
|
import { Toaster } from "@/components/ui/sonner"
|
|
|
|
export default function RootLayout({ children }) {
|
|
return (
|
|
<html lang="en">
|
|
<head />
|
|
<body>
|
|
<main>{children}</main>
|
|
<Toaster />
|
|
</body>
|
|
</html>
|
|
)
|
|
}
|
|
```
|
|
|
|
</Steps>
|
|
|
|
</TabsContent>
|
|
|
|
<TabsContent value="manual">
|
|
|
|
<Steps>
|
|
|
|
<Step>Install the following dependencies:</Step>
|
|
|
|
```bash
|
|
npm install sonner next-themes
|
|
```
|
|
|
|
<Step>Copy and paste the following code into your project.</Step>
|
|
|
|
<ComponentSource name="sonner" title="components/ui/sonner.tsx" />
|
|
|
|
<Step>Add the Toaster component</Step>
|
|
|
|
```tsx showLineNumbers title="app/layout.tsx" {1,8}
|
|
import { Toaster } from "@/components/ui/sonner"
|
|
|
|
export default function RootLayout({ children }) {
|
|
return (
|
|
<html lang="en">
|
|
<head />
|
|
<body>
|
|
<Toaster />
|
|
<main>{children}</main>
|
|
</body>
|
|
</html>
|
|
)
|
|
}
|
|
```
|
|
|
|
</Steps>
|
|
|
|
</TabsContent>
|
|
|
|
</CodeTabs>
|
|
|
|
## Usage
|
|
|
|
```tsx
|
|
import { toast } from "sonner"
|
|
```
|
|
|
|
```tsx
|
|
toast("Event has been created.")
|
|
```
|
|
|
|
## Examples
|
|
|
|
<ComponentPreview name="sonner-types" />
|
|
|
|
## Changelog
|
|
|
|
### 2025-10-13 Icons
|
|
|
|
We've updated the Sonner component to use icons from `lucide`. Update your `sonner.tsx` file to use the new icons.
|
|
|
|
```tsx showLineNumbers title="components/ui/sonner.tsx" {3-9,20-26}
|
|
"use client"
|
|
|
|
import {
|
|
CircleCheckIcon,
|
|
InfoIcon,
|
|
Loader2Icon,
|
|
OctagonXIcon,
|
|
TriangleAlertIcon,
|
|
} from "lucide-react"
|
|
import { useTheme } from "next-themes"
|
|
import { Toaster as Sonner, ToasterProps } from "sonner"
|
|
|
|
const Toaster = ({ ...props }: ToasterProps) => {
|
|
const { theme = "system" } = useTheme()
|
|
|
|
return (
|
|
<Sonner
|
|
theme={theme as ToasterProps["theme"]}
|
|
className="toaster group"
|
|
icons={{
|
|
success: <CircleCheckIcon className="size-4" />,
|
|
info: <InfoIcon className="size-4" />,
|
|
warning: <TriangleAlertIcon className="size-4" />,
|
|
error: <OctagonXIcon className="size-4" />,
|
|
loading: <Loader2Icon className="size-4 animate-spin" />,
|
|
}}
|
|
style={
|
|
{
|
|
"--normal-bg": "var(--popover)",
|
|
"--normal-text": "var(--popover-foreground)",
|
|
"--normal-border": "var(--border)",
|
|
"--border-radius": "var(--radius)",
|
|
} as React.CSSProperties
|
|
}
|
|
{...props}
|
|
/>
|
|
)
|
|
}
|
|
|
|
export { Toaster }
|
|
```
|