|
1
|
import { ComponentExample } from "@/components/ComponentExample" |
|
2
|
import { ComponentReference } from "@/components/ComponentReference" |
|
3
|
|
|
4
|
export const metadata = { |
|
5
|
title: `Toaster and Toast Messages`, |
|
6
|
} |
|
7
|
|
|
8
|
# {metadata.title} |
|
9
|
|
|
10
|
|
|
11
|
A component and utility for displaying brief messages to users, typically used for notifications or alerts. Toast messages appear momentarily on top of the application UI. |
|
12
|
|
|
13
|
You can display multiple toast messages at once, and they will be stacked neatly. |
|
14
|
|
|
15
|
In this guide, you'll learn how to use the Toaster component. |
|
16
|
|
|
17
|
<ComponentExample name="toaster-demo" /> |
|
18
|
|
|
19
|
## Usage |
|
20
|
|
|
21
|
First, import the `toast` utility and `Toaster` component from `@medusajs/ui`: |
|
22
|
|
|
23
|
```tsx |
|
24
|
import { Toaster, toast } from "@medusajs/ui" |
|
25
|
``` |
|
26
|
|
|
27
|
Then, add the `Toaster` component somewhere in your tree hierarchy. For example, in your main application layout: |
|
28
|
|
|
29
|
```tsx highlights={[["6"]]} |
|
30
|
export default function AppLayout({ children }) { |
|
31
|
return ( |
|
32
|
<html> |
|
33
|
<body> |
|
34
|
{children} |
|
35
|
<Toaster /> |
|
36
|
</body> |
|
37
|
</html> |
|
38
|
) |
|
39
|
} |
|
40
|
``` |
|
41
|
|
|
42
|
Finally, use the `toast` utility in your components to display a toast message: |
|
43
|
|
|
44
|
```tsx highlights={[["5", "info", "Display an informational message"]]} |
|
45
|
export default function MyComponent() { |
|
46
|
return ( |
|
47
|
<Button |
|
48
|
onClick={() => |
|
49
|
toast.info("Toast title", { |
|
50
|
description: "Toast body", |
|
51
|
}) |
|
52
|
} |
|
53
|
> |
|
54
|
Trigger |
|
55
|
</Button> |
|
56
|
) |
|
57
|
} |
|
58
|
``` |
|
59
|
|
|
60
|
--- |
|
61
|
|
|
62
|
## API Reference |
|
63
|
|
|
64
|
### Toast Utility Functions |
|
65
|
|
|
66
|
The `toast` utility has the following functions to display different variants of toast messages: |
|
67
|
|
|
68
|
- `info`: Display a toast message with an informational style. |
|
69
|
- `error`: Display a toast message with an error style. |
|
70
|
- `success`: Display a toast message with a success style. |
|
71
|
- `warning`: Display a toast message with a warning style. |
|
72
|
- `loading`: Display a toast message with a loading style. |
|
73
|
|
|
74
|
Each of these functions accept two parameters: |
|
75
|
|
|
76
|
1. A string indicating the title of the toast. |
|
77
|
2. An object of [Toast component props](#toast-props). |
|
78
|
|
|
79
|
### Toast Props |
|
80
|
|
|
81
|
<ComponentReference mainComponent="Toast" /> |
|
82
|
|
|
83
|
### Toaster Props |
|
84
|
|
|
85
|
<ComponentReference mainComponent="Toaster" /> |
|
86
|
|
|
87
|
--- |
|
88
|
|
|
89
|
## Examples |
|
90
|
|
|
91
|
### Toast Variants |
|
92
|
|
|
93
|
<Note> |
|
94
|
|
|
95
|
The following example assumes you already have the `Toaster` component in [your application's tree](#usage). |
|
96
|
|
|
97
|
</Note> |
|
98
|
|
|
99
|
<ComponentExample name="toaster-all-variants" /> |
|
100
|
|
|
101
|
### Dismissable Toast |
|
102
|
|
|
103
|
<Note> |
|
104
|
|
|
105
|
The following example assumes you already have the `Toaster` component in [your application's tree](#usage). |
|
106
|
|
|
107
|
</Note> |
|
108
|
|
|
109
|
<ComponentExample name="toaster-dismiss" /> |
|
110
|
|
|
111
|
### Toast with Action |
|
112
|
|
|
113
|
<Note> |
|
114
|
|
|
115
|
The following example assumes you already have the `Toaster` component in [your application's tree](#usage). |
|
116
|
|
|
117
|
</Note> |
|
118
|
|
|
119
|
<ComponentExample name="toaster-with-action" hideFeedback /> |