UNPKG

12 kBJavaScriptView Raw
1'use client';
2
3import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
4import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
5import _extends from "@babel/runtime/helpers/esm/extends";
6import * as React from 'react';
7import PropTypes from 'prop-types';
8import { useSlotProps } from '@mui/base/utils';
9import composeClasses from '@mui/utils/composeClasses';
10import { ClickAwayListener } from '@mui/base/ClickAwayListener';
11import { useSnackbar } from '@mui/base/useSnackbar';
12import styled from '../styles/styled';
13import useTheme from '../styles/useTheme';
14import { useDefaultProps } from '../DefaultPropsProvider';
15import capitalize from '../utils/capitalize';
16import Grow from '../Grow';
17import SnackbarContent from '../SnackbarContent';
18import { getSnackbarUtilityClass } from './snackbarClasses';
19import { jsx as _jsx } from "react/jsx-runtime";
20var useUtilityClasses = function useUtilityClasses(ownerState) {
21 var classes = ownerState.classes,
22 anchorOrigin = ownerState.anchorOrigin;
23 var slots = {
24 root: ['root', "anchorOrigin".concat(capitalize(anchorOrigin.vertical)).concat(capitalize(anchorOrigin.horizontal))]
25 };
26 return composeClasses(slots, getSnackbarUtilityClass, classes);
27};
28var SnackbarRoot = styled('div', {
29 name: 'MuiSnackbar',
30 slot: 'Root',
31 overridesResolver: function overridesResolver(props, styles) {
32 var ownerState = props.ownerState;
33 return [styles.root, styles["anchorOrigin".concat(capitalize(ownerState.anchorOrigin.vertical)).concat(capitalize(ownerState.anchorOrigin.horizontal))]];
34 }
35})(function (_ref) {
36 var theme = _ref.theme,
37 ownerState = _ref.ownerState;
38 var center = {
39 left: '50%',
40 right: 'auto',
41 transform: 'translateX(-50%)'
42 };
43 return _extends({
44 zIndex: (theme.vars || theme).zIndex.snackbar,
45 position: 'fixed',
46 display: 'flex',
47 left: 8,
48 right: 8,
49 justifyContent: 'center',
50 alignItems: 'center'
51 }, ownerState.anchorOrigin.vertical === 'top' ? {
52 top: 8
53 } : {
54 bottom: 8
55 }, ownerState.anchorOrigin.horizontal === 'left' && {
56 justifyContent: 'flex-start'
57 }, ownerState.anchorOrigin.horizontal === 'right' && {
58 justifyContent: 'flex-end'
59 }, _defineProperty({}, theme.breakpoints.up('sm'), _extends({}, ownerState.anchorOrigin.vertical === 'top' ? {
60 top: 24
61 } : {
62 bottom: 24
63 }, ownerState.anchorOrigin.horizontal === 'center' && center, ownerState.anchorOrigin.horizontal === 'left' && {
64 left: 24,
65 right: 'auto'
66 }, ownerState.anchorOrigin.horizontal === 'right' && {
67 right: 24,
68 left: 'auto'
69 })));
70});
71var Snackbar = /*#__PURE__*/React.forwardRef(function Snackbar(inProps, ref) {
72 var props = useDefaultProps({
73 props: inProps,
74 name: 'MuiSnackbar'
75 });
76 var theme = useTheme();
77 var defaultTransitionDuration = {
78 enter: theme.transitions.duration.enteringScreen,
79 exit: theme.transitions.duration.leavingScreen
80 };
81 var action = props.action,
82 _props$anchorOrigin = props.anchorOrigin,
83 _props$anchorOrigin2 = _props$anchorOrigin === void 0 ? {
84 vertical: 'bottom',
85 horizontal: 'left'
86 } : _props$anchorOrigin,
87 vertical = _props$anchorOrigin2.vertical,
88 horizontal = _props$anchorOrigin2.horizontal,
89 _props$autoHideDurati = props.autoHideDuration,
90 autoHideDuration = _props$autoHideDurati === void 0 ? null : _props$autoHideDurati,
91 children = props.children,
92 className = props.className,
93 ClickAwayListenerProps = props.ClickAwayListenerProps,
94 ContentProps = props.ContentProps,
95 _props$disableWindowB = props.disableWindowBlurListener,
96 disableWindowBlurListener = _props$disableWindowB === void 0 ? false : _props$disableWindowB,
97 message = props.message,
98 onBlur = props.onBlur,
99 onClose = props.onClose,
100 onFocus = props.onFocus,
101 onMouseEnter = props.onMouseEnter,
102 onMouseLeave = props.onMouseLeave,
103 open = props.open,
104 resumeHideDuration = props.resumeHideDuration,
105 _props$TransitionComp = props.TransitionComponent,
106 TransitionComponent = _props$TransitionComp === void 0 ? Grow : _props$TransitionComp,
107 _props$transitionDura = props.transitionDuration,
108 transitionDuration = _props$transitionDura === void 0 ? defaultTransitionDuration : _props$transitionDura,
109 _props$TransitionProp = props.TransitionProps,
110 _props$TransitionProp2 = _props$TransitionProp === void 0 ? {} : _props$TransitionProp,
111 onEnter = _props$TransitionProp2.onEnter,
112 onExited = _props$TransitionProp2.onExited,
113 TransitionProps = _objectWithoutProperties(_props$TransitionProp2, ["onEnter", "onExited"]),
114 other = _objectWithoutProperties(props, ["action", "anchorOrigin", "autoHideDuration", "children", "className", "ClickAwayListenerProps", "ContentProps", "disableWindowBlurListener", "message", "onBlur", "onClose", "onFocus", "onMouseEnter", "onMouseLeave", "open", "resumeHideDuration", "TransitionComponent", "transitionDuration", "TransitionProps"]);
115 var ownerState = _extends({}, props, {
116 anchorOrigin: {
117 vertical: vertical,
118 horizontal: horizontal
119 },
120 autoHideDuration: autoHideDuration,
121 disableWindowBlurListener: disableWindowBlurListener,
122 TransitionComponent: TransitionComponent,
123 transitionDuration: transitionDuration
124 });
125 var classes = useUtilityClasses(ownerState);
126 var _useSnackbar = useSnackbar(_extends({}, ownerState)),
127 getRootProps = _useSnackbar.getRootProps,
128 onClickAway = _useSnackbar.onClickAway;
129 var _React$useState = React.useState(true),
130 exited = _React$useState[0],
131 setExited = _React$useState[1];
132 var rootProps = useSlotProps({
133 elementType: SnackbarRoot,
134 getSlotProps: getRootProps,
135 externalForwardedProps: other,
136 ownerState: ownerState,
137 additionalProps: {
138 ref: ref
139 },
140 className: [classes.root, className]
141 });
142 var handleExited = function handleExited(node) {
143 setExited(true);
144 if (onExited) {
145 onExited(node);
146 }
147 };
148 var handleEnter = function handleEnter(node, isAppearing) {
149 setExited(false);
150 if (onEnter) {
151 onEnter(node, isAppearing);
152 }
153 };
154
155 // So we only render active snackbars.
156 if (!open && exited) {
157 return null;
158 }
159 return /*#__PURE__*/_jsx(ClickAwayListener, _extends({
160 onClickAway: onClickAway
161 }, ClickAwayListenerProps, {
162 children: /*#__PURE__*/_jsx(SnackbarRoot, _extends({}, rootProps, {
163 children: /*#__PURE__*/_jsx(TransitionComponent, _extends({
164 appear: true,
165 in: open,
166 timeout: transitionDuration,
167 direction: vertical === 'top' ? 'down' : 'up',
168 onEnter: handleEnter,
169 onExited: handleExited
170 }, TransitionProps, {
171 children: children || /*#__PURE__*/_jsx(SnackbarContent, _extends({
172 message: message,
173 action: action
174 }, ContentProps))
175 }))
176 }))
177 }));
178});
179process.env.NODE_ENV !== "production" ? Snackbar.propTypes /* remove-proptypes */ = {
180 // ┌────────────────────────────── Warning ──────────────────────────────┐
181 // │ These PropTypes are generated from the TypeScript type definitions. │
182 // │ To update them, edit the d.ts file and run `pnpm proptypes`. │
183 // └─────────────────────────────────────────────────────────────────────┘
184 /**
185 * The action to display. It renders after the message, at the end of the snackbar.
186 */
187 action: PropTypes.node,
188 /**
189 * The anchor of the `Snackbar`.
190 * On smaller screens, the component grows to occupy all the available width,
191 * the horizontal alignment is ignored.
192 * @default { vertical: 'bottom', horizontal: 'left' }
193 */
194 anchorOrigin: PropTypes.shape({
195 horizontal: PropTypes.oneOf(['center', 'left', 'right']).isRequired,
196 vertical: PropTypes.oneOf(['bottom', 'top']).isRequired
197 }),
198 /**
199 * The number of milliseconds to wait before automatically calling the
200 * `onClose` function. `onClose` should then set the state of the `open`
201 * prop to hide the Snackbar. This behavior is disabled by default with
202 * the `null` value.
203 * @default null
204 */
205 autoHideDuration: PropTypes.number,
206 /**
207 * Replace the `SnackbarContent` component.
208 */
209 children: PropTypes.element,
210 /**
211 * Override or extend the styles applied to the component.
212 */
213 classes: PropTypes.object,
214 /**
215 * @ignore
216 */
217 className: PropTypes.string,
218 /**
219 * Props applied to the `ClickAwayListener` element.
220 */
221 ClickAwayListenerProps: PropTypes.object,
222 /**
223 * Props applied to the [`SnackbarContent`](/material-ui/api/snackbar-content/) element.
224 */
225 ContentProps: PropTypes.object,
226 /**
227 * If `true`, the `autoHideDuration` timer will expire even if the window is not focused.
228 * @default false
229 */
230 disableWindowBlurListener: PropTypes.bool,
231 /**
232 * When displaying multiple consecutive snackbars using a single parent-rendered
233 * `<Snackbar/>`, add the `key` prop to ensure independent treatment of each message.
234 * For instance, use `<Snackbar key={message} />`. Otherwise, messages might update
235 * in place, and features like `autoHideDuration` could be affected.
236 */
237 key: function key() {
238 return null;
239 },
240 /**
241 * The message to display.
242 */
243 message: PropTypes.node,
244 /**
245 * @ignore
246 */
247 onBlur: PropTypes.func,
248 /**
249 * Callback fired when the component requests to be closed.
250 * Typically `onClose` is used to set state in the parent component,
251 * which is used to control the `Snackbar` `open` prop.
252 * The `reason` parameter can optionally be used to control the response to `onClose`,
253 * for example ignoring `clickaway`.
254 *
255 * @param {React.SyntheticEvent<any> | Event} event The event source of the callback.
256 * @param {string} reason Can be: `"timeout"` (`autoHideDuration` expired), `"clickaway"`, or `"escapeKeyDown"`.
257 */
258 onClose: PropTypes.func,
259 /**
260 * @ignore
261 */
262 onFocus: PropTypes.func,
263 /**
264 * @ignore
265 */
266 onMouseEnter: PropTypes.func,
267 /**
268 * @ignore
269 */
270 onMouseLeave: PropTypes.func,
271 /**
272 * If `true`, the component is shown.
273 */
274 open: PropTypes.bool,
275 /**
276 * The number of milliseconds to wait before dismissing after user interaction.
277 * If `autoHideDuration` prop isn't specified, it does nothing.
278 * If `autoHideDuration` prop is specified but `resumeHideDuration` isn't,
279 * we default to `autoHideDuration / 2` ms.
280 */
281 resumeHideDuration: PropTypes.number,
282 /**
283 * The system prop that allows defining system overrides as well as additional CSS styles.
284 */
285 sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
286 /**
287 * The component used for the transition.
288 * [Follow this guide](/material-ui/transitions/#transitioncomponent-prop) to learn more about the requirements for this component.
289 * @default Grow
290 */
291 TransitionComponent: PropTypes.elementType,
292 /**
293 * The duration for the transition, in milliseconds.
294 * You may specify a single timeout for all transitions, or individually with an object.
295 * @default {
296 * enter: theme.transitions.duration.enteringScreen,
297 * exit: theme.transitions.duration.leavingScreen,
298 * }
299 */
300 transitionDuration: PropTypes.oneOfType([PropTypes.number, PropTypes.shape({
301 appear: PropTypes.number,
302 enter: PropTypes.number,
303 exit: PropTypes.number
304 })]),
305 /**
306 * Props applied to the transition element.
307 * By default, the element is based on this [`Transition`](https://reactcommunity.org/react-transition-group/transition/) component.
308 * @default {}
309 */
310 TransitionProps: PropTypes.object
311} : void 0;
312export default Snackbar;
\No newline at end of file