forked from bmcmahen/sancho
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToast.tsx
89 lines (80 loc) · 1.87 KB
/
Toast.tsx
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
/** @jsx jsx */
import { jsx, Global, css } from "@emotion/core";
import * as React from "react";
import toaster, { Position } from "toasted-notes";
import { Alert, AlertIntentions } from "./Alert";
import { Theme } from "./Theme";
import { useTheme, ThemeProvider } from "./Theme/Providers";
const toastStyles = css`
.Toaster__message-wrapper {
padding: 8px;
text-align: left;
}
`;
interface RenderArgs {
id: string;
onClose: () => void;
}
interface Toast {
position?: keyof typeof Position;
duration?: number | null;
title?: string;
subtitle?: string;
theme?: Theme;
intent?: AlertIntentions;
render?: (options: RenderArgs) => React.ReactNode;
}
/**
* We export toast as a hook because it allows us to consume
* the current theme context that it's in and pass that
* onto our render function
*/
export function useToast() {
const theme = useTheme();
function notify({
position,
duration,
render,
title,
subtitle,
intent
}: Toast) {
const options = {
position,
duration
};
if (render) {
return toaster.notify(
({ onClose, id }) => (
<React.Fragment>
<Global styles={toastStyles} />
<ThemeProvider theme={theme}>
{render({ onClose, id })}
</ThemeProvider>
</React.Fragment>
),
options
);
}
toaster.notify(
({ onClose, id }) => (
<React.Fragment>
<Global styles={toastStyles} />
<ThemeProvider theme={theme}>
<Alert
id={String(id)}
title={title}
component="div"
elevation={"md"}
subtitle={subtitle}
intent={intent}
onRequestClose={onClose}
/>
</ThemeProvider>
</React.Fragment>
),
options
);
}
return notify;
}