UNPKG

7.47 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4Object.defineProperty(exports, "__esModule", {
5 value: true
6});
7exports.default = void 0;
8var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
9var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
10var React = _interopRequireWildcard(require("react"));
11var _propTypes = _interopRequireDefault(require("prop-types"));
12var _ClickAwayListener = _interopRequireDefault(require("../ClickAwayListener"));
13var _composeClasses = _interopRequireDefault(require("../composeClasses"));
14var _snackbarClasses = require("./snackbarClasses");
15var _useSnackbar = _interopRequireDefault(require("../useSnackbar"));
16var _utils = require("../utils");
17var _ClassNameConfigurator = require("../utils/ClassNameConfigurator");
18var _jsxRuntime = require("react/jsx-runtime");
19const _excluded = ["autoHideDuration", "children", "disableWindowBlurListener", "exited", "onBlur", "onClose", "onFocus", "onMouseEnter", "onMouseLeave", "open", "resumeHideDuration", "slotProps", "slots"];
20function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
21function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
22const useUtilityClasses = () => {
23 const slots = {
24 root: ['root']
25 };
26 return (0, _composeClasses.default)(slots, (0, _ClassNameConfigurator.useClassNamesOverride)(_snackbarClasses.getSnackbarUtilityClass));
27};
28/**
29 *
30 * Demos:
31 *
32 * - [Snackbar](https://mui.com/base/react-snackbar/)
33 * - [Snackbar](https://mui.com/material-ui/react-snackbar/)
34 *
35 * API:
36 *
37 * - [Snackbar API](https://mui.com/base/react-snackbar/components-api/#snackbar)
38 */
39const Snackbar = /*#__PURE__*/React.forwardRef(function Snackbar(props, forwardedRef) {
40 const {
41 autoHideDuration = null,
42 children,
43 disableWindowBlurListener = false,
44 exited = true,
45 onClose,
46 open,
47 resumeHideDuration,
48 slotProps = {},
49 slots = {}
50 } = props,
51 other = (0, _objectWithoutPropertiesLoose2.default)(props, _excluded);
52 const classes = useUtilityClasses();
53 const {
54 getRootProps,
55 onClickAway
56 } = (0, _useSnackbar.default)((0, _extends2.default)({}, props, {
57 autoHideDuration,
58 disableWindowBlurListener,
59 onClose,
60 open,
61 resumeHideDuration
62 }));
63 const ownerState = props;
64 const Root = slots.root || 'div';
65 const rootProps = (0, _utils.useSlotProps)({
66 elementType: Root,
67 getSlotProps: getRootProps,
68 externalForwardedProps: other,
69 externalSlotProps: slotProps.root,
70 additionalProps: {
71 ref: forwardedRef
72 },
73 ownerState,
74 className: classes.root
75 });
76 const clickAwayListenerProps = (0, _utils.useSlotProps)({
77 elementType: _ClickAwayListener.default,
78 externalSlotProps: slotProps.clickAwayListener,
79 additionalProps: {
80 onClickAway
81 },
82 ownerState
83 });
84
85 // ClickAwayListener doesn't support ownerState
86 delete clickAwayListenerProps.ownerState;
87
88 // So that we only render active snackbars.
89 if (!open && exited) {
90 return null;
91 }
92 return /*#__PURE__*/(0, _jsxRuntime.jsx)(_ClickAwayListener.default, (0, _extends2.default)({}, clickAwayListenerProps, {
93 children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Root, (0, _extends2.default)({}, rootProps, {
94 children: children
95 }))
96 }));
97});
98process.env.NODE_ENV !== "production" ? Snackbar.propTypes /* remove-proptypes */ = {
99 // ----------------------------- Warning --------------------------------
100 // | These PropTypes are generated from the TypeScript type definitions |
101 // | To update them edit TypeScript types and run "yarn proptypes" |
102 // ----------------------------------------------------------------------
103 /**
104 * The number of milliseconds to wait before automatically calling the
105 * `onClose` function. `onClose` should then set the state of the `open`
106 * prop to hide the Snackbar. This behavior is disabled by default with
107 * the `null` value.
108 * @default null
109 */
110 autoHideDuration: _propTypes.default.number,
111 /**
112 * @ignore
113 */
114 children: _propTypes.default.node,
115 /**
116 * If `true`, the `autoHideDuration` timer will expire even if the window is not focused.
117 * @default false
118 */
119 disableWindowBlurListener: _propTypes.default.bool,
120 /**
121 * The prop used to handle exited transition and unmount the component.
122 * @default true
123 */
124 exited: _propTypes.default.bool,
125 /**
126 * Callback fired when the component requests to be closed.
127 * Typically `onClose` is used to set state in the parent component,
128 * which is used to control the `Snackbar` `open` prop.
129 * The `reason` parameter can optionally be used to control the response to `onClose`,
130 * for example ignoring `clickaway`.
131 *
132 * @param {React.SyntheticEvent<any> | Event} event The event source of the callback.
133 * @param {string} reason Can be: `"timeout"` (`autoHideDuration` expired), `"clickaway"`, or `"escapeKeyDown"`.
134 */
135 onClose: _propTypes.default.func,
136 /**
137 * If `true`, the component is shown.
138 */
139 open: _propTypes.default.bool,
140 /**
141 * The number of milliseconds to wait before dismissing after user interaction.
142 * If `autoHideDuration` prop isn't specified, it does nothing.
143 * If `autoHideDuration` prop is specified but `resumeHideDuration` isn't,
144 * we default to `autoHideDuration / 2` ms.
145 */
146 resumeHideDuration: _propTypes.default.number,
147 /**
148 * The props used for each slot inside the Snackbar.
149 * @default {}
150 */
151 slotProps: _propTypes.default.shape({
152 clickAwayListener: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.shape({
153 children: _propTypes.default.element.isRequired,
154 disableReactTree: _propTypes.default.bool,
155 mouseEvent: _propTypes.default.oneOf(['onClick', 'onMouseDown', 'onMouseUp', 'onPointerDown', 'onPointerUp', false]),
156 onClickAway: _propTypes.default.func,
157 touchEvent: _propTypes.default.oneOf(['onTouchEnd', 'onTouchStart', false])
158 })]),
159 root: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.object])
160 }),
161 /**
162 * The components used for each slot inside the Snackbar.
163 * Either a string to use a HTML element or a component.
164 * @default {}
165 */
166 slots: _propTypes.default.shape({
167 root: _propTypes.default.elementType
168 })
169} : void 0;
170var _default = Snackbar;
171exports.default = _default;
\No newline at end of file