-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
Expand file tree
/
Copy pathSnackbar.js
More file actions
238 lines (215 loc) · 6.4 KB
/
Snackbar.js
File metadata and controls
238 lines (215 loc) · 6.4 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import React, {PropTypes, Component} from 'react';
import transitions from '../styles/transitions';
import ClickAwayListener from '../internal/ClickAwayListener';
import SnackbarBody from './SnackbarBody';
function getStyles(props, context, state) {
const {
muiTheme: {
baseTheme: {
spacing: {
desktopSubheaderHeight,
},
},
zIndex,
},
} = context;
const {open} = state;
const styles = {
root: {
position: 'fixed',
left: 0,
display: 'flex',
right: 0,
bottom: 0,
zIndex: zIndex.snackbar,
visibility: open ? 'visible' : 'hidden',
transform: open ?
'translate(0, 0)' :
`translate(0, ${desktopSubheaderHeight}px)`,
transition: `${transitions.easeOut('400ms', 'transform')}, ${
transitions.easeOut('400ms', 'visibility')}`,
},
};
return styles;
}
class Snackbar extends Component {
static propTypes = {
/**
* The label for the action on the snackbar.
*/
action: PropTypes.node,
/**
* The number of milliseconds to wait before automatically dismissing.
* If no value is specified the snackbar will dismiss normally.
* If a value is provided the snackbar can still be dismissed normally.
* If a snackbar is dismissed before the timer expires, the timer will be cleared.
*/
autoHideDuration: PropTypes.number,
/**
* Override the inline-styles of the body element.
*/
bodyStyle: PropTypes.object,
/**
* The css class name of the root element.
*/
className: PropTypes.string,
/**
* Override the inline-styles of the content element.
*/
contentStyle: PropTypes.object,
/**
* The message to be displayed.
*
* (Note: If the message is an element or array, and the `Snackbar` may re-render while it is still open,
* ensure that the same object remains as the `message` property if you want to avoid the `Snackbar` hiding and
* showing again)
*/
message: PropTypes.node.isRequired,
/**
* Fired when the action button is touchtapped.
*
* @param {object} event Action button event.
*/
onActionTouchTap: PropTypes.func,
/**
* Fired when the `Snackbar` is requested to be closed by a click outside the `Snackbar`, or after the
* `autoHideDuration` timer expires.
*
* Typically `onRequestClose` is used to set state in the parent component, which is used to control the `Snackbar`
* `open` prop.
*
* The `reason` parameter can optionally be used to control the response to `onRequestClose`,
* for example ignoring `clickaway`.
*
* @param {string} reason Can be:`"timeout"` (`autoHideDuration` expired) or: `"clickaway"`
*/
onRequestClose: PropTypes.func,
/**
* Controls whether the `Snackbar` is opened or not.
*/
open: PropTypes.bool.isRequired,
/**
* Override the inline-styles of the root element.
*/
style: PropTypes.object,
};
static contextTypes = {
muiTheme: PropTypes.object.isRequired,
};
componentWillMount() {
this.setState({
open: this.props.open,
message: this.props.message,
action: this.props.action,
});
}
componentDidMount() {
if (this.state.open) {
this.setAutoHideTimer();
this.setTransitionTimer();
}
}
componentWillReceiveProps(nextProps) {
if (this.props.open && nextProps.open &&
(nextProps.message !== this.props.message || nextProps.action !== this.props.action)) {
this.setState({
open: false,
});
clearTimeout(this.timerOneAtTheTimeId);
this.timerOneAtTheTimeId = setTimeout(() => {
this.setState({
message: nextProps.message,
action: nextProps.action,
open: true,
});
}, 400);
} else {
const open = nextProps.open;
this.setState({
open: open !== null ? open : this.state.open,
message: nextProps.message,
action: nextProps.action,
});
}
}
componentDidUpdate(prevProps, prevState) {
if (prevState.open !== this.state.open) {
if (this.state.open) {
this.setAutoHideTimer();
this.setTransitionTimer();
} else {
clearTimeout(this.timerAutoHideId);
}
}
}
componentWillUnmount() {
clearTimeout(this.timerAutoHideId);
clearTimeout(this.timerTransitionId);
clearTimeout(this.timerOneAtTheTimeId);
}
componentClickAway = () => {
if (this.timerTransitionId) {
// If transitioning, don't close the snackbar.
return;
}
if (this.props.open !== null && this.props.onRequestClose) {
this.props.onRequestClose('clickaway');
} else {
this.setState({open: false});
}
};
// Timer that controls delay before snackbar auto hides
setAutoHideTimer() {
const autoHideDuration = this.props.autoHideDuration;
if (autoHideDuration > 0) {
clearTimeout(this.timerAutoHideId);
this.timerAutoHideId = setTimeout(() => {
if (this.props.open !== null && this.props.onRequestClose) {
this.props.onRequestClose('timeout');
} else {
this.setState({open: false});
}
}, autoHideDuration);
}
}
// Timer that controls delay before click-away events are captured (based on when animation completes)
setTransitionTimer() {
this.timerTransitionId = setTimeout(() => {
this.timerTransitionId = undefined;
}, 400);
}
render() {
const {
autoHideDuration, // eslint-disable-line no-unused-vars
contentStyle,
bodyStyle,
message: messageProp, // eslint-disable-line no-unused-vars
onRequestClose, // eslint-disable-line no-unused-vars
onActionTouchTap,
style,
...other,
} = this.props;
const {
action,
message,
open,
} = this.state;
const {prepareStyles} = this.context.muiTheme;
const styles = getStyles(this.props, this.context, this.state);
return (
<ClickAwayListener onClickAway={open && this.componentClickAway}>
<div {...other} style={prepareStyles(Object.assign(styles.root, style))}>
<SnackbarBody
action={action}
contentStyle={contentStyle}
message={message}
open={open}
onActionTouchTap={onActionTouchTap}
style={bodyStyle}
/>
</div>
</ClickAwayListener>
);
}
}
export default Snackbar;