-
-
Notifications
You must be signed in to change notification settings - Fork 422
Expand file tree
/
Copy pathpage.tsx
More file actions
384 lines (377 loc) · 11.1 KB
/
page.tsx
File metadata and controls
384 lines (377 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
'use client';
import React from 'react';
import { Toaster, toast } from 'sonner';
import { action } from '@/app/action';
const promise = () => new Promise((resolve) => setTimeout(resolve, 2000));
export default function Home({ searchParams }: any) {
const [showAutoClose, setShowAutoClose] = React.useState(false);
const [showDismiss, setShowDismiss] = React.useState(false);
const [theme, setTheme] = React.useState(searchParams.theme || 'light');
const [isFinally, setIsFinally] = React.useState(false);
const [showAriaLabels, setShowAriaLabels] = React.useState(false);
return (
<>
<button data-testid="theme-button" className="button" onClick={() => setTheme('dark')}>
Change theme
</button>
<button data-testid="default-button" className="button" onClick={() => toast('My Toast')}>
Render Toast
</button>
<button data-testid="default-button-top" className="button" onClick={() => toast('My Toast')}>
Render Toast Top
</button>
<button data-testid="success" className="button" onClick={() => toast.success('My Success Toast')}>
Render Success Toast
</button>
<button data-testid="error" className="button" onClick={() => toast.error('My Error Toast')}>
Render Error Toast
</button>
<button
data-testid="action"
className="button"
onClick={() =>
toast('My Message', {
action: {
label: 'Action',
onClick: () => console.log('Action'),
},
})
}
>
Render Action Toast
</button>
<button
data-testid="action-prevent"
className="button"
onClick={() =>
toast('My Message', {
action: {
label: 'Action',
onClick: (event) => {
event.preventDefault();
console.log('Action');
},
},
})
}
>
Render Action Toast
</button>
<button
data-testid="promise"
data-finally={isFinally ? '1' : '0'}
className="button"
onClick={() =>
toast.promise(promise, {
loading: 'Loading...',
success: 'Loaded',
error: 'Error',
finally: () => setIsFinally(true),
})
}
>
Render Promise Toast
</button>
<button
data-testid="rsf-promise"
data-finally={isFinally ? '1' : '0'}
className="button"
onClick={() =>
toast.promise(action(), {
loading: 'Loading...',
success: 'Loaded',
error: 'Error',
finally: () => setIsFinally(true),
})
}
>
Render React Server Function Toast
</button>
<button
data-testid="custom"
className="button"
onClick={() =>
toast.custom((t) => (
<div>
<h1>jsx</h1>
<button data-testid="dismiss-button" onClick={() => toast.dismiss(t)}>
Dismiss
</button>
</div>
))
}
>
Render Custom Toast
</button>
<button
data-testid="custom-cancel-button-toast"
className="button"
onClick={() =>
toast('My Custom Cancel Button', {
cancel: {
label: 'Cancel',
onClick: () => console.log('Cancel'),
},
})
}
>
Render Custom Cancel Button
</button>
<button data-testid="infinity-toast" className="button" onClick={() => toast('My Toast', { duration: Infinity })}>
Render Infinity Toast
</button>
<button
data-testid="auto-close-toast-callback"
className="button"
onClick={() =>
toast('My Toast', {
onAutoClose: () => setShowAutoClose(true),
})
}
>
Render Toast With onAutoClose callback
</button>
<button
data-testid="dismiss-toast-callback"
className="button"
onClick={() =>
toast('My Toast', {
onDismiss: () => {
setShowDismiss(true);
},
})
}
>
Dismiss toast callback
</button>
<button
data-testid="non-dismissible-toast"
className="button"
onClick={() =>
toast('My Toast', {
dismissible: false,
})
}
>
Non-dismissible Toast
</button>
<button
data-testid="update-toast"
className="button"
onClick={() => {
const toastId = toast('My Unupdated Toast', {
duration: 10000,
});
toast('My Updated Toast', {
id: toastId,
duration: 10000,
});
}}
>
Updated Toast
</button>
<button
data-testid="update-toast-duration"
className="button"
onClick={() => {
const toastId = toast('My Unupdated Toast, Updated After 3 Seconds', {
duration: 10000,
});
setTimeout(() => {
toast('My Updated Toast, Close After 1 Second', {
id: toastId,
duration: 1000,
});
}, 3000);
}}
>
Updated Toast Duration
</button>
<button
data-testid="string-description"
className="button"
onClick={() => toast('Custom Description', { description: 'string description' })}
>
String Description
</button>
<button
data-testid="react-node-description"
className="button"
onClick={() => toast('Custom Description', { description: <div>This is my custom ReactNode description</div> })}
>
ReactNode Description
</button>
<button
data-testid="close-button"
className="button"
onClick={() => toast('Toast with close button', { closeButton: true })}
>
Render close button
</button>
<button
data-testid="extended-promise"
className="button"
onClick={() =>
toast.promise(
new Promise((resolve) => {
setTimeout(() => {
resolve({ name: 'Sonner' });
}, 2000);
}),
{
loading: 'Loading...',
success: (data: any) => ({
message: `${data.name} toast has been added`,
description: 'Custom description for the Success state',
}),
error: {
message: 'An error occurred',
description: undefined,
action: {
label: 'Retry',
onClick: () => {
console.log('retrying');
},
},
},
description: 'Global description',
},
)
}
>
Extended Promise Toast
</button>
<button
data-testid="extended-promise-error"
className="button"
onClick={() =>
toast.promise(
new Promise((_, reject) => {
setTimeout(() => {
reject(new Error('Simulated error'));
}, 2000);
}),
{
loading: 'Loading...',
success: (data: any) => ({
message: `${data.name} toast has been added`,
description: 'Custom description for the Success state',
}),
error: {
message: 'An error occurred',
description: undefined,
action: {
label: 'Retry',
onClick: (event) => {
event.preventDefault();
console.log('retrying');
},
},
},
description: 'Global description',
},
)
}
>
Extended Promise Error Toast
</button>
<button
data-testid="persistent-toast"
className="button"
onClick={() => toast('Persistent Toast', { persistent: true, closeButton: true })}
>
Persistent toast
</button>
<button
data-testid="persistent-extended-promise-toast"
className="button"
onClick={() =>
toast.promise(
new Promise((resolve) => {
setTimeout(() => {
resolve({ name: 'Sonner' });
}, 2000);
}),
{
loading: 'Loading...',
success: (data: any) => ({
message: `${data.name} toast has been added`,
description: 'Custom description for the Success state',
}),
description: 'Global description',
persistent: true,
closeButton: true,
},
)
}
>
Persistent Promise Toast
</button>
<button
data-testid="error-promise"
className="button"
onClick={() => {
const whatWillHappen = async () => {
throw new Error('Not implemented');
};
toast.promise(whatWillHappen, {
loading: 'Saving project...',
success: (result: any) => {
if (result?.ok) {
return 'Project saved';
} else {
return `${result?.error}`;
}
},
error: (e) => `Error Raise: ${e}`,
});
}}
>
Error Promise Toast
</button>
<button
className="button"
onClick={() => {
setShowAriaLabels(true);
toast('Toast with custom ARIA labels', { closeButton: true, onAutoClose: () => setShowAriaLabels(false) });
}}
>
With custom ARIA labels
</button>
{showAutoClose ? <div data-testid="auto-close-el" /> : null}
{showDismiss ? <div data-testid="dismiss-el" /> : null}
<Toaster
offset={32}
position={searchParams.position || 'bottom-right'}
toastOptions={{
actionButtonStyle: { backgroundColor: 'rgb(219, 239, 255)' },
cancelButtonStyle: { backgroundColor: 'rgb(254, 226, 226)' },
closeButtonAriaLabel: showAriaLabels ? 'Yeet the notice' : undefined,
}}
theme={theme}
dir={searchParams.dir || 'auto'}
containerAriaLabel={showAriaLabels ? 'Notices' : undefined}
icons={{
close:
searchParams.customCloseIcon === '' ? (
<svg
xmlns="http://www.w3.org/2000/svg"
width="12"
height="12"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="3"
strokeLinecap="round"
strokeLinejoin="round"
>
<line x1="18" y1="6" x2="6" y2="18"></line>
<line x1="6" y1="6" x2="18" y2="18"></line>
</svg>
) : undefined,
}}
/>
</>
);
}
Home.theme = 'light';