Raw
1 import type { Meta, StoryObj } from "@storybook/react"
2 import * as React from "react"
3
4 import { Button } from "@/components/button"
5 import { toast } from "@/utils/toast"
6 import { Toaster } from "./toaster"
7
8 const meta: Meta<typeof Toaster> = {
9 title: "Components/Toaster",
10 component: Toaster,
11 parameters: {
12 layout: "centered",
13 },
14 render: (args) => {
15 const makeMessageToast = () => {
16 toast("This is a message toast.")
17 }
18
19 const makeInfoToast = () => {
20 toast.info("This is an info toast.")
21 }
22
23 const makeSuccessToast = () => {
24 toast.success("This is a success toast.")
25 }
26
27 const makeWarningToast = () => {
28 toast.warning("This is a warning toast.")
29 }
30
31 const makeErrorToast = () => {
32 toast.error("This is an error toast.")
33 }
34
35 const makeActionToast = () => {
36 toast.error("This is an error toast with an action.", {
37 action: {
38 label: "Retry",
39 altText: "Retry the request",
40 onClick: () => {
41 console.log("Retrying the request...")
42 },
43 },
44 })
45 }
46
47 const doAsyncStuff = async () => {
48 return new Promise((resolve, reject) => {
49 setTimeout(() => {
50 if (Math.random() > 0.2) {
51 resolve("Success!")
52 } else {
53 reject("Failed!")
54 }
55 }, 3000)
56 })
57 }
58
59 const makeAsyncToast = async () => {
60 toast.promise(doAsyncStuff, {
61 loading: "Loading...",
62 success: "Success!",
63 error: "Failed!",
64 })
65 }
66
67 const makeUpdatingToast = () => {
68 let id: number | string = Math.random()
69
70 const retry = async () => {
71 const coinFlip = Math.random() > 0.5
72
73 console.log("retrying", id)
74
75 toast.loading("Request in progress", {
76 id: id,
77 description: "The request is in progress.",
78 })
79
80 // wait 3 seconds
81 await new Promise((resolve) => setTimeout(resolve, 3000))
82
83 if (coinFlip) {
84 toast.success("Request succeeded", {
85 id: id,
86 description: "The request succeeded.",
87 })
88 } else {
89 toast.error("Request failed", {
90 id: id,
91 description:
92 "Insert the description here. Can be semi-long and still work.",
93 action: {
94 label: "Retry",
95 altText: "Retry the request",
96 onClick: retry,
97 },
98 })
99 }
100 }
101
102 toast.error("Request failed", {
103 id: id,
104 description:
105 "Insert the description here. Can be semi-long and still work.",
106 action: {
107 label: "Retry",
108 altText: "Retry the request",
109 onClick: retry,
110 },
111 })
112 }
113
114 return (
115 <div className="size-full">
116 <div className="flex flex-col gap-y-2">
117 <Button size="small" variant="secondary" onClick={makeMessageToast}>
118 Make message toast
119 </Button>
120 <Button size="small" variant="secondary" onClick={makeInfoToast}>
121 Make info toast
122 </Button>
123 <Button size="small" variant="secondary" onClick={makeSuccessToast}>
124 Make success toast
125 </Button>
126 <Button size="small" variant="secondary" onClick={makeWarningToast}>
127 Make warning toast
128 </Button>
129 <Button size="small" variant="secondary" onClick={makeErrorToast}>
130 Make error toast
131 </Button>
132 <Button size="small" variant="secondary" onClick={makeActionToast}>
133 Make action toast
134 </Button>
135 <Button size="small" variant="secondary" onClick={makeUpdatingToast}>
136 Make toast
137 </Button>
138 <Button size="small" variant="secondary" onClick={makeAsyncToast}>
139 Make async toast
140 </Button>
141 </div>
142 <Toaster {...args} />
143 </div>
144 )
145 },
146 }
147
148 export default meta
149
150 type Story = StoryObj<typeof Toaster>
151
152 export const TopRight: Story = {
153 args: {
154 position: "top-right",
155 },
156 }
157
158 export const TopCenter: Story = {
159 args: {
160 position: "top-center",
161 },
162 }
163
164 export const TopLeft: Story = {
165 args: {
166 position: "top-left",
167 },
168 }
169
170 export const BottomRight: Story = {
171 args: {
172 position: "bottom-right",
173 },
174 }
175
176 export const BottomCenter: Story = {
177 args: {
178 position: "bottom-center",
179 },
180 }
181
182 export const BottomLeft: Story = {
183 args: {
184 position: "bottom-left",
185 },
186 }