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
|
import React, { Fragment, PureComponent } from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import CloseIcon from '@material-ui/icons/Close';
import { Typography, Button, Grid, Snackbar, Slide, Fade, IconButton } from '@material-ui/core';
const styles = theme => ({
row: {
display: 'flex',
justifyContent: 'flex-start',
},
close: {
width: theme.spacing(4)
},
divider: {
display: 'block',
margin: `${theme.spacing(3)}px 0`,
},
button: {
margin: theme.spacing(1)
}
});
function TransitionLeft(props) {
return <Slide {...props} direction="left" />;
}
function TransitionUp(props) {
return <Slide {...props} direction="up" />;
}
function TransitionRight(props) {
return <Slide {...props} direction="right" />;
}
function TransitionDown(props) {
return <Slide {...props} direction="down" />;
}
class TransitionNotif extends PureComponent {
state = {
open: false,
open2: false,
open3: false,
transition: null,
messageInfo: {},
};
queue = [];
handleClickQueue = message => () => {
this.queue.push({
message,
key: new Date().getTime(),
});
if (this.state.open3) {
// immediately begin dismissing current message
// to start showing new one
this.setState({ open3: false });
} else {
this.processQueue();
}
};
processQueue = () => {
if (this.queue.length > 0) {
this.setState({
messageInfo: this.queue.shift(),
open3: true,
});
}
};
handleCloseQueue = (event, reason) => {
if (reason === 'clickaway') {
return;
}
this.setState({ open3: false });
};
handleExited = () => {
this.processQueue();
};
handleClick = transition => () => {
this.setState({ open: true, transition });
};
handleClose = () => {
this.setState({ open: false });
};
handleClick2 = () => {
this.setState({ open2: true });
};
handleClose2 = () => {
this.setState({ open2: false });
};
render() {
const { classes } = this.props;
const { message, key } = this.state.messageInfo;
return (
<Fragment>
<Grid
container
alignItems="flex-start"
justify="flex-start"
direction="row"
spacing={2}
>
<Grid
item
md={4}
>
<Typography variant="button" className={classes.divider}>Transition</Typography>
<div>
<Button variant="contained" className={classes.button} onClick={this.handleClick(TransitionLeft)}>Right</Button>
<Button variant="contained" className={classes.button} onClick={this.handleClick(TransitionUp)}>Up</Button>
<Button variant="contained" className={classes.button} onClick={this.handleClick(TransitionRight)}>Left</Button>
<Button variant="contained" className={classes.button} onClick={this.handleClick(TransitionDown)}>Down</Button>
<Snackbar
open={this.state.open}
onClose={this.handleClose}
TransitionComponent={this.state.transition}
ContentProps={{
'aria-describedby': 'message-id',
}}
message={<span>I love snacks</span>}
/>
</div>
</Grid>
<Grid
item
md={4}
>
<Typography variant="button" className={classes.divider}>Change Transition</Typography>
<div>
<Button variant="contained" className={classes.button} onClick={this.handleClick2}>Open with Fade Transition</Button>
<Snackbar
open={this.state.open2}
onClose={this.handleClose2}
TransitionComponent={Fade}
ContentProps={{
'aria-describedby': 'message-id',
}}
message={<span>I love snacks</span>}
/>
</div>
</Grid>
<Grid
item
md={4}
>
<Typography variant="button" className={classes.divider}>Consecutive Snackbars</Typography>
<div>
<Button variant="contained" className={classes.button} onClick={this.handleClickQueue('message a')}>Show message A</Button>
<Button variant="contained" className={classes.button} onClick={this.handleClickQueue('message b')}>Show message B</Button>
<Snackbar
key={key}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'left',
}}
open={this.state.open3}
autoHideDuration={6000}
onClose={this.handleCloseQueue}
onExited={this.handleExited}
ContentProps={{
'aria-describedby': 'message-id',
}}
message={<span>{message}</span>}
action={[
<Button key="undo" color="secondary" size="small" onClick={this.handleClose2}>
UNDO
</Button>,
<IconButton
key="close"
aria-label="Close"
color="inherit"
className={classes.close}
onClick={this.handleCloseQueue}
>
<CloseIcon />
</IconButton>
]}
/>
</div>
</Grid>
</Grid>
</Fragment>
);
}
}
TransitionNotif.propTypes = {
classes: PropTypes.object.isRequired,
};
export default withStyles(styles)(TransitionNotif);
|