-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathToast.tsx
More file actions
59 lines (55 loc) · 1.68 KB
/
Toast.tsx
File metadata and controls
59 lines (55 loc) · 1.68 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
'use client';
import {
UNSTABLE_ToastRegion as ToastRegion,
UNSTABLE_Toast as Toast,
UNSTABLE_ToastQueue as ToastQueue,
UNSTABLE_ToastContent as ToastContent,
ToastProps,
Text
} from 'react-aria-components';
import {Button} from './Button';
import {X} from 'lucide-react';
import './Toast.css';
import {flushSync} from 'react-dom';
// Define the type for your toast content. This interface defines the properties of your toast content, affecting what you
// pass to the queue calls as arguments.
interface MyToastContent {
title: string;
description?: string;
}
// This is a global toast queue, to be imported and called where ever you want to queue a toast via queue.add().
export const queue = new ToastQueue<MyToastContent>({
// Wrap state updates in a CSS view transition.
wrapUpdate(fn) {
if ('startViewTransition' in document) {
document.startViewTransition(() => {
flushSync(fn);
});
} else {
fn();
}
}
});
export function MyToastRegion() {
return (
// The ToastRegion should be rendered at the root of your app.
<ToastRegion queue={queue}>
{({toast}) => (
<MyToast toast={toast} style={{viewTransitionName: toast.key}}>
<ToastContent>
<Text slot="title">{toast.content.title}</Text>
{toast.content.description && (
<Text slot="description">{toast.content.description}</Text>
)}
</ToastContent>
<Button slot="close" aria-label="Close" variant="quiet">
<X size={16} />
</Button>
</MyToast>
)}
</ToastRegion>
);
}
export function MyToast(props: ToastProps<MyToastContent>) {
return <Toast {...props} />;
}