-
Notifications
You must be signed in to change notification settings - Fork 3
/
Snackbar.tsx
135 lines (127 loc) · 2.74 KB
/
Snackbar.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
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
import {
Button,
IconButton,
Snackbar as MuiSnackbar,
SnackbarContent
} from "@mui/material";
import { CheckCircle, Close, Error, Info, Warning } from "@mui/icons-material";
import { amber, green } from "@mui/material/colors";
import React from "react";
import type { SnackbarProps } from "./Snackbar.types";
import { Theme } from "@mui/material/styles";
// icon map
const icons = {
error: Error,
info: Info,
success: CheckCircle,
warning: Warning
};
// styling
const sx = {
action: {
color: "#fff"
},
close: {
fontSize: 15
},
error: {
backgroundColor: (theme: Theme) => theme.palette.error.dark,
color: "#fff",
flexWrap: "nowrap"
},
icon: {
float: "left",
fontSize: 20,
marginRight: (theme: Theme) => theme.spacing(1),
opacity: 0.9
},
info: {
backgroundColor: (theme: Theme) => theme.palette.primary.main,
color: "#fff",
flexWrap: "nowrap"
},
success: {
backgroundColor: green[600],
color: "#fff",
flexWrap: "nowrap"
},
warning: {
backgroundColor: amber[700],
color: "#fff",
flexWrap: "nowrap"
}
};
/**
* Snackbars provide brief messages about app processes. The component is also known as a toast.
*/
export default function Snackbar({
actionCallback,
actionText,
autoHideDuration,
message,
onClose,
open,
variant = "info"
}: SnackbarProps) {
// get icon based on variant
const Icon = icons[variant];
// get styling based on variant
const variantStyle = sx[variant];
// callback for closing
const handleClose: SnackbarProps["onClose"] = (event, reason = "timeout") => {
if (reason === "clickaway") return;
if (onClose) {
onClose(event, reason);
}
};
// action button
const actionButton =
actionText && actionCallback ? (
<Button
key="action"
onClick={event => {
handleClose(event, "timeout");
actionCallback(event);
}}
sx={sx.action}
>
{actionText}
</Button>
) : null;
// return snackbar
return (
<MuiSnackbar
autoHideDuration={autoHideDuration}
anchorOrigin={{
horizontal: "center",
vertical: "bottom"
}}
onClose={handleClose}
open={open}
>
<SnackbarContent
sx={variantStyle}
message={
<span>
<Icon sx={sx.icon} />
{message}
</span>
}
action={[
actionButton,
<IconButton
key="close"
color="inherit"
onClick={event => {
handleClose(event, "timeout");
}}
size="large"
sx={sx.action}
>
<Close sx={sx.close} />
</IconButton>
]}
/>
</MuiSnackbar>
);
}