mirror of
https://github.com/shadcn-ui/ui.git
synced 2026-07-08 06:28:37 +00:00
125 lines
2.7 KiB
Plaintext
125 lines
2.7 KiB
Plaintext
---
|
|
title: Toast
|
|
description: A succinct message that is displayed temporarily.
|
|
component: true
|
|
radix:
|
|
link: https://www.radix-ui.com/docs/primitives/components/toast
|
|
api: https://www.radix-ui.com/docs/primitives/components/toast#api-reference
|
|
---
|
|
|
|
<ComponentExample src="/components/examples/toast/demo.tsx">
|
|
<ToastDemo />
|
|
</ComponentExample>
|
|
|
|
## Installation
|
|
|
|
1. Install the `@radix-ui/react-toast` component from radix-ui:
|
|
|
|
```bash
|
|
npm install @radix-ui/react-toast
|
|
```
|
|
|
|
2. Copy and paste the following code into your project.
|
|
|
|
<ComponentSource src="/components/ui/toast.tsx" />
|
|
|
|
<Callout>
|
|
This is the `<Toast />` primitive. You can place it in a file at
|
|
`components/ui/toast.tsx`.
|
|
</Callout>
|
|
|
|
3. Copy and paste the `<Toaster />` component into your project.
|
|
|
|
<ComponentSource src="/components/ui/toaster.tsx" />
|
|
|
|
<Callout>
|
|
This is the `<Toaster />` primitive. You can place it in a file at
|
|
`components/ui/toaster.tsx`.
|
|
</Callout>
|
|
|
|
4. Add the `<Toaster />` component to your app.
|
|
|
|
```tsx title="_app.tsx" {1,7}
|
|
import { Toaster } from "@/components/ui/toaster"
|
|
|
|
export default function MyApp({ Component, pageProps }) {
|
|
return (
|
|
<>
|
|
<Component {...pageProps} />
|
|
<Toaster />
|
|
</>
|
|
)
|
|
}
|
|
```
|
|
|
|
5. Copy and paste the `useToast` hook into your project.
|
|
|
|
<ComponentSource src="/hooks/use-toast.ts" />
|
|
|
|
<Callout>
|
|
You can place the `use-toast` hook in a file at `hooks/ui/use-toast.tsx`.
|
|
</Callout>
|
|
|
|
## Usage
|
|
|
|
```tsx
|
|
import { useToast } from "@/hooks/use-toast"
|
|
```
|
|
|
|
```tsx {2,8-11}
|
|
export const ToastDemo = () => {
|
|
const { toast } = useToast()
|
|
|
|
return (
|
|
<Button
|
|
onClick={() => {
|
|
toast({
|
|
title: "Scheduled: Catch up",
|
|
description: "Friday, February 10, 2023 at 5:57 PM",
|
|
})
|
|
}}
|
|
>
|
|
Show Toast
|
|
</Button>
|
|
)
|
|
}
|
|
```
|
|
|
|
The `<Toaster />` component is where your toasts are displayed. You can place it anywhere in your app, but it's recommended to place it at the root of your app.
|
|
|
|
The `useToast` hook returns a `toast` function that you can use to display a toast.
|
|
|
|
<Callout>
|
|
|
|
To display multiple toasts at the same time, you can update the `TOAST_LIMIT` in `use-toast.tsx`.
|
|
|
|
</Callout>
|
|
|
|
## Examples
|
|
|
|
### Simple
|
|
|
|
<ComponentExample src="/components/examples/toast/simple.tsx">
|
|
<ToastSimple />
|
|
</ComponentExample>
|
|
|
|
### With title
|
|
|
|
<ComponentExample src="/components/examples/toast/with-title.tsx">
|
|
<ToastWithTitle />
|
|
</ComponentExample>
|
|
|
|
### With Action
|
|
|
|
<ComponentExample src="/components/examples/toast/with-action.tsx">
|
|
<ToastWithAction />
|
|
</ComponentExample>
|
|
|
|
### Destructive
|
|
|
|
Use `toast({ variant: "destructive" }})` to display a destructive toast.
|
|
|
|
<ComponentExample src="/components/examples/toast/destructive.tsx">
|
|
<ToastDestructive />
|
|
</ComponentExample>
|