1 | 'use client';
|
2 |
|
3 | import * as React from 'react';
|
4 | import PropTypes from 'prop-types';
|
5 | import composeClasses from '@mui/utils/composeClasses';
|
6 | import useSlotProps from '@mui/utils/useSlotProps';
|
7 | import useSnackbar from "./useSnackbar.js";
|
8 | import ClickAwayListener from "../ClickAwayListener/index.js";
|
9 | import { styled, useTheme } from "../zero-styled/index.js";
|
10 | import memoTheme from "../utils/memoTheme.js";
|
11 | import { useDefaultProps } from "../DefaultPropsProvider/index.js";
|
12 | import capitalize from "../utils/capitalize.js";
|
13 | import Grow from "../Grow/index.js";
|
14 | import SnackbarContent from "../SnackbarContent/index.js";
|
15 | import { getSnackbarUtilityClass } from "./snackbarClasses.js";
|
16 | import { jsx as _jsx } from "react/jsx-runtime";
|
17 | const useUtilityClasses = ownerState => {
|
18 | const {
|
19 | classes,
|
20 | anchorOrigin
|
21 | } = ownerState;
|
22 | const slots = {
|
23 | root: ['root', `anchorOrigin${capitalize(anchorOrigin.vertical)}${capitalize(anchorOrigin.horizontal)}`]
|
24 | };
|
25 | return composeClasses(slots, getSnackbarUtilityClass, classes);
|
26 | };
|
27 | const SnackbarRoot = styled('div', {
|
28 | name: 'MuiSnackbar',
|
29 | slot: 'Root',
|
30 | overridesResolver: (props, styles) => {
|
31 | const {
|
32 | ownerState
|
33 | } = props;
|
34 | return [styles.root, styles[`anchorOrigin${capitalize(ownerState.anchorOrigin.vertical)}${capitalize(ownerState.anchorOrigin.horizontal)}`]];
|
35 | }
|
36 | })(memoTheme(({
|
37 | theme
|
38 | }) => ({
|
39 | zIndex: (theme.vars || theme).zIndex.snackbar,
|
40 | position: 'fixed',
|
41 | display: 'flex',
|
42 | left: 8,
|
43 | right: 8,
|
44 | justifyContent: 'center',
|
45 | alignItems: 'center',
|
46 | variants: [{
|
47 | props: ({
|
48 | ownerState
|
49 | }) => ownerState.anchorOrigin.vertical === 'top',
|
50 | style: {
|
51 | top: 8,
|
52 | [theme.breakpoints.up('sm')]: {
|
53 | top: 24
|
54 | }
|
55 | }
|
56 | }, {
|
57 | props: ({
|
58 | ownerState
|
59 | }) => ownerState.anchorOrigin.vertical !== 'top',
|
60 | style: {
|
61 | bottom: 8,
|
62 | [theme.breakpoints.up('sm')]: {
|
63 | bottom: 24
|
64 | }
|
65 | }
|
66 | }, {
|
67 | props: ({
|
68 | ownerState
|
69 | }) => ownerState.anchorOrigin.horizontal === 'left',
|
70 | style: {
|
71 | justifyContent: 'flex-start',
|
72 | [theme.breakpoints.up('sm')]: {
|
73 | left: 24,
|
74 | right: 'auto'
|
75 | }
|
76 | }
|
77 | }, {
|
78 | props: ({
|
79 | ownerState
|
80 | }) => ownerState.anchorOrigin.horizontal === 'right',
|
81 | style: {
|
82 | justifyContent: 'flex-end',
|
83 | [theme.breakpoints.up('sm')]: {
|
84 | right: 24,
|
85 | left: 'auto'
|
86 | }
|
87 | }
|
88 | }, {
|
89 | props: ({
|
90 | ownerState
|
91 | }) => ownerState.anchorOrigin.horizontal === 'center',
|
92 | style: {
|
93 | [theme.breakpoints.up('sm')]: {
|
94 | left: '50%',
|
95 | right: 'auto',
|
96 | transform: 'translateX(-50%)'
|
97 | }
|
98 | }
|
99 | }]
|
100 | })));
|
101 | const Snackbar = React.forwardRef(function Snackbar(inProps, ref) {
|
102 | const props = useDefaultProps({
|
103 | props: inProps,
|
104 | name: 'MuiSnackbar'
|
105 | });
|
106 | const theme = useTheme();
|
107 | const defaultTransitionDuration = {
|
108 | enter: theme.transitions.duration.enteringScreen,
|
109 | exit: theme.transitions.duration.leavingScreen
|
110 | };
|
111 | const {
|
112 | action,
|
113 | anchorOrigin: {
|
114 | vertical,
|
115 | horizontal
|
116 | } = {
|
117 | vertical: 'bottom',
|
118 | horizontal: 'left'
|
119 | },
|
120 | autoHideDuration = null,
|
121 | children,
|
122 | className,
|
123 | ClickAwayListenerProps,
|
124 | ContentProps,
|
125 | disableWindowBlurListener = false,
|
126 | message,
|
127 | onBlur,
|
128 | onClose,
|
129 | onFocus,
|
130 | onMouseEnter,
|
131 | onMouseLeave,
|
132 | open,
|
133 | resumeHideDuration,
|
134 | TransitionComponent = Grow,
|
135 | transitionDuration = defaultTransitionDuration,
|
136 | TransitionProps: {
|
137 | onEnter,
|
138 | onExited,
|
139 | ...TransitionProps
|
140 | } = {},
|
141 | ...other
|
142 | } = props;
|
143 | const ownerState = {
|
144 | ...props,
|
145 | anchorOrigin: {
|
146 | vertical,
|
147 | horizontal
|
148 | },
|
149 | autoHideDuration,
|
150 | disableWindowBlurListener,
|
151 | TransitionComponent,
|
152 | transitionDuration
|
153 | };
|
154 | const classes = useUtilityClasses(ownerState);
|
155 | const {
|
156 | getRootProps,
|
157 | onClickAway
|
158 | } = useSnackbar({
|
159 | ...ownerState
|
160 | });
|
161 | const [exited, setExited] = React.useState(true);
|
162 | const rootProps = useSlotProps({
|
163 | elementType: SnackbarRoot,
|
164 | getSlotProps: getRootProps,
|
165 | externalForwardedProps: other,
|
166 | ownerState,
|
167 | additionalProps: {
|
168 | ref
|
169 | },
|
170 | className: [classes.root, className]
|
171 | });
|
172 | const handleExited = node => {
|
173 | setExited(true);
|
174 | if (onExited) {
|
175 | onExited(node);
|
176 | }
|
177 | };
|
178 | const handleEnter = (node, isAppearing) => {
|
179 | setExited(false);
|
180 | if (onEnter) {
|
181 | onEnter(node, isAppearing);
|
182 | }
|
183 | };
|
184 |
|
185 |
|
186 | if (!open && exited) {
|
187 | return null;
|
188 | }
|
189 | return _jsx(ClickAwayListener, {
|
190 | onClickAway: onClickAway,
|
191 | ...ClickAwayListenerProps,
|
192 | children: _jsx(SnackbarRoot, {
|
193 | ...rootProps,
|
194 | children: _jsx(TransitionComponent, {
|
195 | appear: true,
|
196 | in: open,
|
197 | timeout: transitionDuration,
|
198 | direction: vertical === 'top' ? 'down' : 'up',
|
199 | onEnter: handleEnter,
|
200 | onExited: handleExited,
|
201 | ...TransitionProps,
|
202 | children: children || _jsx(SnackbarContent, {
|
203 | message: message,
|
204 | action: action,
|
205 | ...ContentProps
|
206 | })
|
207 | })
|
208 | })
|
209 | });
|
210 | });
|
211 | process.env.NODE_ENV !== "production" ? Snackbar.propTypes = {
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | |
217 |
|
218 |
|
219 | action: PropTypes.node,
|
220 | |
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 | anchorOrigin: PropTypes.shape({
|
227 | horizontal: PropTypes.oneOf(['center', 'left', 'right']).isRequired,
|
228 | vertical: PropTypes.oneOf(['bottom', 'top']).isRequired
|
229 | }),
|
230 | |
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 | autoHideDuration: PropTypes.number,
|
238 | |
239 |
|
240 |
|
241 | children: PropTypes.element,
|
242 | |
243 |
|
244 |
|
245 | classes: PropTypes.object,
|
246 | |
247 |
|
248 |
|
249 | className: PropTypes.string,
|
250 | |
251 |
|
252 |
|
253 | ClickAwayListenerProps: PropTypes.object,
|
254 | |
255 |
|
256 |
|
257 | ContentProps: PropTypes.object,
|
258 | |
259 |
|
260 |
|
261 |
|
262 | disableWindowBlurListener: PropTypes.bool,
|
263 | |
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 | key: () => null,
|
270 | |
271 |
|
272 |
|
273 | message: PropTypes.node,
|
274 | |
275 |
|
276 |
|
277 | onBlur: PropTypes.func,
|
278 | |
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 | onClose: PropTypes.func,
|
289 | |
290 |
|
291 |
|
292 | onFocus: PropTypes.func,
|
293 | |
294 |
|
295 |
|
296 | onMouseEnter: PropTypes.func,
|
297 | |
298 |
|
299 |
|
300 | onMouseLeave: PropTypes.func,
|
301 | |
302 |
|
303 |
|
304 | open: PropTypes.bool,
|
305 | |
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 | resumeHideDuration: PropTypes.number,
|
312 | |
313 |
|
314 |
|
315 | sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
|
316 | |
317 |
|
318 |
|
319 |
|
320 |
|
321 | TransitionComponent: PropTypes.elementType,
|
322 | |
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 | transitionDuration: PropTypes.oneOfType([PropTypes.number, PropTypes.shape({
|
331 | appear: PropTypes.number,
|
332 | enter: PropTypes.number,
|
333 | exit: PropTypes.number
|
334 | })]),
|
335 | |
336 |
|
337 |
|
338 |
|
339 |
|
340 | TransitionProps: PropTypes.object
|
341 | } : void 0;
|
342 | export default Snackbar; |
\ | No newline at end of file |