UNPKG

14.5 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
5var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
6
7Object.defineProperty(exports, "__esModule", {
8 value: true
9});
10exports.default = exports.styles = void 0;
11
12var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
13
14var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
15
16var _extends8 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
17
18var React = _interopRequireWildcard(require("react"));
19
20var _propTypes = _interopRequireDefault(require("prop-types"));
21
22var _clsx = _interopRequireDefault(require("clsx"));
23
24var _withStyles = _interopRequireDefault(require("../styles/withStyles"));
25
26var _transitions = require("../styles/transitions");
27
28var _ClickAwayListener = _interopRequireDefault(require("../ClickAwayListener"));
29
30var _useEventCallback = _interopRequireDefault(require("../utils/useEventCallback"));
31
32var _capitalize = _interopRequireDefault(require("../utils/capitalize"));
33
34var _createChainedFunction = _interopRequireDefault(require("../utils/createChainedFunction"));
35
36var _Grow = _interopRequireDefault(require("../Grow"));
37
38var _SnackbarContent = _interopRequireDefault(require("../SnackbarContent"));
39
40var styles = function styles(theme) {
41 var top1 = {
42 top: 8
43 };
44 var bottom1 = {
45 bottom: 8
46 };
47 var right = {
48 justifyContent: 'flex-end'
49 };
50 var left = {
51 justifyContent: 'flex-start'
52 };
53 var top3 = {
54 top: 24
55 };
56 var bottom3 = {
57 bottom: 24
58 };
59 var right3 = {
60 right: 24
61 };
62 var left3 = {
63 left: 24
64 };
65 var center = {
66 left: '50%',
67 right: 'auto',
68 transform: 'translateX(-50%)'
69 };
70 return {
71 /* Styles applied to the root element. */
72 root: {
73 zIndex: theme.zIndex.snackbar,
74 position: 'fixed',
75 display: 'flex',
76 left: 8,
77 right: 8,
78 justifyContent: 'center',
79 alignItems: 'center'
80 },
81
82 /* Styles applied to the root element if `anchorOrigin={{ 'top', 'center' }}`. */
83 anchorOriginTopCenter: (0, _extends8.default)({}, top1, (0, _defineProperty2.default)({}, theme.breakpoints.up('sm'), (0, _extends8.default)({}, top3, center))),
84
85 /* Styles applied to the root element if `anchorOrigin={{ 'bottom', 'center' }}`. */
86 anchorOriginBottomCenter: (0, _extends8.default)({}, bottom1, (0, _defineProperty2.default)({}, theme.breakpoints.up('sm'), (0, _extends8.default)({}, bottom3, center))),
87
88 /* Styles applied to the root element if `anchorOrigin={{ 'top', 'right' }}`. */
89 anchorOriginTopRight: (0, _extends8.default)({}, top1, right, (0, _defineProperty2.default)({}, theme.breakpoints.up('sm'), (0, _extends8.default)({
90 left: 'auto'
91 }, top3, right3))),
92
93 /* Styles applied to the root element if `anchorOrigin={{ 'bottom', 'right' }}`. */
94 anchorOriginBottomRight: (0, _extends8.default)({}, bottom1, right, (0, _defineProperty2.default)({}, theme.breakpoints.up('sm'), (0, _extends8.default)({
95 left: 'auto'
96 }, bottom3, right3))),
97
98 /* Styles applied to the root element if `anchorOrigin={{ 'top', 'left' }}`. */
99 anchorOriginTopLeft: (0, _extends8.default)({}, top1, left, (0, _defineProperty2.default)({}, theme.breakpoints.up('sm'), (0, _extends8.default)({
100 right: 'auto'
101 }, top3, left3))),
102
103 /* Styles applied to the root element if `anchorOrigin={{ 'bottom', 'left' }}`. */
104 anchorOriginBottomLeft: (0, _extends8.default)({}, bottom1, left, (0, _defineProperty2.default)({}, theme.breakpoints.up('sm'), (0, _extends8.default)({
105 right: 'auto'
106 }, bottom3, left3)))
107 };
108};
109
110exports.styles = styles;
111var Snackbar = /*#__PURE__*/React.forwardRef(function Snackbar(props, ref) {
112 var action = props.action,
113 _props$anchorOrigin = props.anchorOrigin;
114 _props$anchorOrigin = _props$anchorOrigin === void 0 ? {
115 vertical: 'bottom',
116 horizontal: 'center'
117 } : _props$anchorOrigin;
118 var vertical = _props$anchorOrigin.vertical,
119 horizontal = _props$anchorOrigin.horizontal,
120 _props$autoHideDurati = props.autoHideDuration,
121 autoHideDuration = _props$autoHideDurati === void 0 ? null : _props$autoHideDurati,
122 children = props.children,
123 classes = props.classes,
124 className = props.className,
125 ClickAwayListenerProps = props.ClickAwayListenerProps,
126 ContentProps = props.ContentProps,
127 _props$disableWindowB = props.disableWindowBlurListener,
128 disableWindowBlurListener = _props$disableWindowB === void 0 ? false : _props$disableWindowB,
129 message = props.message,
130 onClose = props.onClose,
131 onEnter = props.onEnter,
132 onEntered = props.onEntered,
133 onEntering = props.onEntering,
134 onExit = props.onExit,
135 onExited = props.onExited,
136 onExiting = props.onExiting,
137 onMouseEnter = props.onMouseEnter,
138 onMouseLeave = props.onMouseLeave,
139 open = props.open,
140 resumeHideDuration = props.resumeHideDuration,
141 _props$TransitionComp = props.TransitionComponent,
142 TransitionComponent = _props$TransitionComp === void 0 ? _Grow.default : _props$TransitionComp,
143 _props$transitionDura = props.transitionDuration,
144 transitionDuration = _props$transitionDura === void 0 ? {
145 enter: _transitions.duration.enteringScreen,
146 exit: _transitions.duration.leavingScreen
147 } : _props$transitionDura,
148 TransitionProps = props.TransitionProps,
149 other = (0, _objectWithoutProperties2.default)(props, ["action", "anchorOrigin", "autoHideDuration", "children", "classes", "className", "ClickAwayListenerProps", "ContentProps", "disableWindowBlurListener", "message", "onClose", "onEnter", "onEntered", "onEntering", "onExit", "onExited", "onExiting", "onMouseEnter", "onMouseLeave", "open", "resumeHideDuration", "TransitionComponent", "transitionDuration", "TransitionProps"]);
150 var timerAutoHide = React.useRef();
151
152 var _React$useState = React.useState(true),
153 exited = _React$useState[0],
154 setExited = _React$useState[1];
155
156 var handleClose = (0, _useEventCallback.default)(function () {
157 if (onClose) {
158 onClose.apply(void 0, arguments);
159 }
160 });
161 var setAutoHideTimer = (0, _useEventCallback.default)(function (autoHideDurationParam) {
162 if (!onClose || autoHideDurationParam == null) {
163 return;
164 }
165
166 clearTimeout(timerAutoHide.current);
167 timerAutoHide.current = setTimeout(function () {
168 handleClose(null, 'timeout');
169 }, autoHideDurationParam);
170 });
171 React.useEffect(function () {
172 if (open) {
173 setAutoHideTimer(autoHideDuration);
174 }
175
176 return function () {
177 clearTimeout(timerAutoHide.current);
178 };
179 }, [open, autoHideDuration, setAutoHideTimer]); // Pause the timer when the user is interacting with the Snackbar
180 // or when the user hide the window.
181
182 var handlePause = function handlePause() {
183 clearTimeout(timerAutoHide.current);
184 }; // Restart the timer when the user is no longer interacting with the Snackbar
185 // or when the window is shown back.
186
187
188 var handleResume = React.useCallback(function () {
189 if (autoHideDuration != null) {
190 setAutoHideTimer(resumeHideDuration != null ? resumeHideDuration : autoHideDuration * 0.5);
191 }
192 }, [autoHideDuration, resumeHideDuration, setAutoHideTimer]);
193
194 var handleMouseEnter = function handleMouseEnter(event) {
195 if (onMouseEnter) {
196 onMouseEnter(event);
197 }
198
199 handlePause();
200 };
201
202 var handleMouseLeave = function handleMouseLeave(event) {
203 if (onMouseLeave) {
204 onMouseLeave(event);
205 }
206
207 handleResume();
208 };
209
210 var handleClickAway = function handleClickAway(event) {
211 if (onClose) {
212 onClose(event, 'clickaway');
213 }
214 };
215
216 var handleExited = function handleExited() {
217 setExited(true);
218 };
219
220 var handleEnter = function handleEnter() {
221 setExited(false);
222 };
223
224 React.useEffect(function () {
225 if (!disableWindowBlurListener && open) {
226 window.addEventListener('focus', handleResume);
227 window.addEventListener('blur', handlePause);
228 return function () {
229 window.removeEventListener('focus', handleResume);
230 window.removeEventListener('blur', handlePause);
231 };
232 }
233
234 return undefined;
235 }, [disableWindowBlurListener, handleResume, open]); // So we only render active snackbars.
236
237 if (!open && exited) {
238 return null;
239 }
240
241 return /*#__PURE__*/React.createElement(_ClickAwayListener.default, (0, _extends8.default)({
242 onClickAway: handleClickAway
243 }, ClickAwayListenerProps), /*#__PURE__*/React.createElement("div", (0, _extends8.default)({
244 className: (0, _clsx.default)(classes.root, classes["anchorOrigin".concat((0, _capitalize.default)(vertical)).concat((0, _capitalize.default)(horizontal))], className),
245 onMouseEnter: handleMouseEnter,
246 onMouseLeave: handleMouseLeave,
247 ref: ref
248 }, other), /*#__PURE__*/React.createElement(TransitionComponent, (0, _extends8.default)({
249 appear: true,
250 in: open,
251 onEnter: (0, _createChainedFunction.default)(handleEnter, onEnter),
252 onEntered: onEntered,
253 onEntering: onEntering,
254 onExit: onExit,
255 onExited: (0, _createChainedFunction.default)(handleExited, onExited),
256 onExiting: onExiting,
257 timeout: transitionDuration,
258 direction: vertical === 'top' ? 'down' : 'up'
259 }, TransitionProps), children || /*#__PURE__*/React.createElement(_SnackbarContent.default, (0, _extends8.default)({
260 message: message,
261 action: action
262 }, ContentProps)))));
263});
264process.env.NODE_ENV !== "production" ? Snackbar.propTypes = {
265 // ----------------------------- Warning --------------------------------
266 // | These PropTypes are generated from the TypeScript type definitions |
267 // | To update them edit the d.ts file and run "yarn proptypes" |
268 // ----------------------------------------------------------------------
269
270 /**
271 * The action to display. It renders after the message, at the end of the snackbar.
272 */
273 action: _propTypes.default.node,
274
275 /**
276 * The anchor of the `Snackbar`.
277 */
278 anchorOrigin: _propTypes.default.shape({
279 horizontal: _propTypes.default.oneOf(['center', 'left', 'right']).isRequired,
280 vertical: _propTypes.default.oneOf(['bottom', 'top']).isRequired
281 }),
282
283 /**
284 * The number of milliseconds to wait before automatically calling the
285 * `onClose` function. `onClose` should then set the state of the `open`
286 * prop to hide the Snackbar. This behavior is disabled by default with
287 * the `null` value.
288 */
289 autoHideDuration: _propTypes.default.number,
290
291 /**
292 * Replace the `SnackbarContent` component.
293 */
294 children: _propTypes.default.element,
295
296 /**
297 * Override or extend the styles applied to the component.
298 * See [CSS API](#css) below for more details.
299 */
300 classes: _propTypes.default.object,
301
302 /**
303 * @ignore
304 */
305 className: _propTypes.default.string,
306
307 /**
308 * Props applied to the `ClickAwayListener` element.
309 */
310 ClickAwayListenerProps: _propTypes.default.object,
311
312 /**
313 * Props applied to the [`SnackbarContent`](/api/snackbar-content/) element.
314 */
315 ContentProps: _propTypes.default.object,
316
317 /**
318 * If `true`, the `autoHideDuration` timer will expire even if the window is not focused.
319 */
320 disableWindowBlurListener: _propTypes.default.bool,
321
322 /**
323 * When displaying multiple consecutive Snackbars from a parent rendering a single
324 * <Snackbar/>, add the key prop to ensure independent treatment of each message.
325 * e.g. <Snackbar key={message} />, otherwise, the message may update-in-place and
326 * features such as autoHideDuration may be canceled.
327 */
328 key: _propTypes.default.any,
329
330 /**
331 * The message to display.
332 */
333 message: _propTypes.default.node,
334
335 /**
336 * Callback fired when the component requests to be closed.
337 * Typically `onClose` is used to set state in the parent component,
338 * which is used to control the `Snackbar` `open` prop.
339 * The `reason` parameter can optionally be used to control the response to `onClose`,
340 * for example ignoring `clickaway`.
341 *
342 * @param {object} event The event source of the callback.
343 * @param {string} reason Can be: `"timeout"` (`autoHideDuration` expired), `"clickaway"`.
344 */
345 onClose: _propTypes.default.func,
346
347 /**
348 * Callback fired before the transition is entering.
349 */
350 onEnter: _propTypes.default.func,
351
352 /**
353 * Callback fired when the transition has entered.
354 */
355 onEntered: _propTypes.default.func,
356
357 /**
358 * Callback fired when the transition is entering.
359 */
360 onEntering: _propTypes.default.func,
361
362 /**
363 * Callback fired before the transition is exiting.
364 */
365 onExit: _propTypes.default.func,
366
367 /**
368 * Callback fired when the transition has exited.
369 */
370 onExited: _propTypes.default.func,
371
372 /**
373 * Callback fired when the transition is exiting.
374 */
375 onExiting: _propTypes.default.func,
376
377 /**
378 * @ignore
379 */
380 onMouseEnter: _propTypes.default.func,
381
382 /**
383 * @ignore
384 */
385 onMouseLeave: _propTypes.default.func,
386
387 /**
388 * If `true`, `Snackbar` is open.
389 */
390 open: _propTypes.default.bool,
391
392 /**
393 * The number of milliseconds to wait before dismissing after user interaction.
394 * If `autoHideDuration` prop isn't specified, it does nothing.
395 * If `autoHideDuration` prop is specified but `resumeHideDuration` isn't,
396 * we default to `autoHideDuration / 2` ms.
397 */
398 resumeHideDuration: _propTypes.default.number,
399
400 /**
401 * The component used for the transition.
402 * [Follow this guide](/components/transitions/#transitioncomponent-prop) to learn more about the requirements for this component.
403 */
404 TransitionComponent: _propTypes.default.elementType,
405
406 /**
407 * The duration for the transition, in milliseconds.
408 * You may specify a single timeout for all transitions, or individually with an object.
409 */
410 transitionDuration: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.shape({
411 appear: _propTypes.default.number,
412 enter: _propTypes.default.number,
413 exit: _propTypes.default.number
414 })]),
415
416 /**
417 * Props applied to the [`Transition`](http://reactcommunity.org/react-transition-group/transition#Transition-props) element.
418 */
419 TransitionProps: _propTypes.default.object
420} : void 0;
421
422var _default = (0, _withStyles.default)(styles, {
423 flip: false,
424 name: 'MuiSnackbar'
425})(Snackbar);
426
427exports.default = _default;
\No newline at end of file