-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
/
TransitionComponentSnackbar.tsx
179 lines (162 loc) · 4.27 KB
/
TransitionComponentSnackbar.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
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
import * as React from 'react';
import { Transition } from 'react-transition-group';
import { styled } from '@mui/system';
import CloseIcon from '@mui/icons-material/Close';
import { Snackbar } from '@mui/base/Snackbar';
import { SnackbarCloseReason } from '@mui/base/useSnackbar';
export default function TransitionComponentSnackbar() {
const [open, setOpen] = React.useState(false);
const [exited, setExited] = React.useState(true);
const nodeRef = React.useRef(null);
const handleClose = (_: any, reason: SnackbarCloseReason) => {
if (reason === 'clickaway') {
return;
}
setOpen(false);
};
const handleClick = () => {
setOpen(true);
};
const handleOnEnter = () => {
setExited(false);
};
const handleOnExited = () => {
setExited(true);
};
return (
<React.Fragment>
<TriggerButton type="button" onClick={handleClick}>
Open snackbar
</TriggerButton>
<StyledSnackbar
autoHideDuration={5000}
open={open}
onClose={handleClose}
exited={exited}
>
<Transition
timeout={{ enter: 400, exit: 400 }}
in={open}
appear
unmountOnExit
onEnter={handleOnEnter}
onExited={handleOnExited}
nodeRef={nodeRef}
>
{(status) => (
<SnackbarContent
style={{
transform: positioningStyles[status],
transition: 'transform 300ms ease',
}}
ref={nodeRef}
>
<div className="snackbar-title">Hello World</div>
<CloseIcon onClick={handleClose} className="snackbar-close-icon" />
</SnackbarContent>
)}
</Transition>
</StyledSnackbar>
</React.Fragment>
);
}
const blue = {
200: '#99CCF3',
300: '#66B2FF',
400: '#3399FF',
500: '#007FFF',
600: '#0072E5',
700: '#0066CC',
};
const grey = {
50: '#F3F6F9',
100: '#E5EAF2',
200: '#DAE2ED',
300: '#C7D0DD',
400: '#B0B8C4',
500: '#9DA8B7',
600: '#6B7A90',
700: '#434D5B',
800: '#303740',
900: '#1C2025',
};
const TriggerButton = styled('button')(
({ theme }) => `
font-family: 'IBM Plex Sans', sans-serif;
font-weight: 600;
font-size: 0.875rem;
line-height: 1.5;
padding: 8px 16px;
border-radius: 8px;
color: white;
transition: all 150ms ease;
cursor: pointer;
background: ${theme.palette.mode === 'dark' ? grey[900] : '#fff'};
border: 1px solid ${theme.palette.mode === 'dark' ? grey[700] : grey[200]};
color: ${theme.palette.mode === 'dark' ? grey[200] : grey[900]};
box-shadow: 0 1px 2px ${
theme.palette.mode === 'dark' ? 'rgba(0, 0, 0, 0.5)' : 'rgba(45, 45, 60, 0.15)'
};
&:hover {
background: ${theme.palette.mode === 'dark' ? grey[800] : grey[50]};
border-color: ${theme.palette.mode === 'dark' ? grey[600] : grey[300]};
}
&:active {
background: ${theme.palette.mode === 'dark' ? grey[700] : grey[100]};
}
&:focus-visible {
box-shadow: 0 0 0 4px ${theme.palette.mode === 'dark' ? blue[300] : blue[200]};
outline: none;
}
`,
);
const StyledSnackbar = styled(Snackbar)`
position: fixed;
z-index: 5500;
display: flex;
bottom: 16px;
right: 16px;
`;
const SnackbarContent = styled('div')(
({ theme }) => `
position: relative;
overflow: hidden;
z-index: 5500;
display: flex;
left: auto;
justify-content: space-between;
max-width: 560px;
min-width: 300px;
background-color: ${theme.palette.mode === 'dark' ? grey[900] : '#fff'};
border-radius: 8px;
border: 1px solid ${theme.palette.mode === 'dark' ? grey[700] : grey[200]};
box-shadow: ${
theme.palette.mode === 'dark'
? `0 2px 8px rgb(0 0 0 / 0.5)`
: `0 2px 8px ${grey[200]}`
};
padding: 0.75rem;
color: ${theme.palette.mode === 'dark' ? grey[50] : grey[900]};
font-family: 'IBM Plex Sans', sans-serif;
font-weight: 600;
& .snackbar-title {
margin-right: 0.5rem;
}
& .snackbar-close-icon {
cursor: pointer;
font-size: 10px;
width: 1.25rem;
height: 1.5rem;
display: flex;
align-items: center;
justify-content: center;
}
`,
);
const positioningStyles = {
entering: 'translateX(0)',
entered: 'translateX(0)',
exiting: 'translateX(500px)',
exited: 'translateX(500px)',
unmounted: 'translateX(500px)',
};