1 | 'use client';
|
2 |
|
3 | import * as React from 'react';
|
4 | import PropTypes from 'prop-types';
|
5 | import clsx from 'clsx';
|
6 | import HTMLElementType from '@mui/utils/HTMLElementType';
|
7 | import elementAcceptingRef from '@mui/utils/elementAcceptingRef';
|
8 | import composeClasses from '@mui/utils/composeClasses';
|
9 | import FocusTrap from "../Unstable_TrapFocus/index.js";
|
10 | import Portal from "../Portal/index.js";
|
11 | import { styled } from "../zero-styled/index.js";
|
12 | import memoTheme from "../utils/memoTheme.js";
|
13 | import { useDefaultProps } from "../DefaultPropsProvider/index.js";
|
14 | import Backdrop from "../Backdrop/index.js";
|
15 | import useModal from "./useModal.js";
|
16 | import { getModalUtilityClass } from "./modalClasses.js";
|
17 | import useSlot from "../utils/useSlot.js";
|
18 | import { useForkRef } from "../utils/index.js";
|
19 | import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
20 | const useUtilityClasses = ownerState => {
|
21 | const {
|
22 | open,
|
23 | exited,
|
24 | classes
|
25 | } = ownerState;
|
26 | const slots = {
|
27 | root: ['root', !open && exited && 'hidden'],
|
28 | backdrop: ['backdrop']
|
29 | };
|
30 | return composeClasses(slots, getModalUtilityClass, classes);
|
31 | };
|
32 | const ModalRoot = styled('div', {
|
33 | name: 'MuiModal',
|
34 | slot: 'Root',
|
35 | overridesResolver: (props, styles) => {
|
36 | const {
|
37 | ownerState
|
38 | } = props;
|
39 | return [styles.root, !ownerState.open && ownerState.exited && styles.hidden];
|
40 | }
|
41 | })(memoTheme(({
|
42 | theme
|
43 | }) => ({
|
44 | position: 'fixed',
|
45 | zIndex: (theme.vars || theme).zIndex.modal,
|
46 | right: 0,
|
47 | bottom: 0,
|
48 | top: 0,
|
49 | left: 0,
|
50 | variants: [{
|
51 | props: ({
|
52 | ownerState
|
53 | }) => !ownerState.open && ownerState.exited,
|
54 | style: {
|
55 | visibility: 'hidden'
|
56 | }
|
57 | }]
|
58 | })));
|
59 | const ModalBackdrop = styled(Backdrop, {
|
60 | name: 'MuiModal',
|
61 | slot: 'Backdrop',
|
62 | overridesResolver: (props, styles) => {
|
63 | return styles.backdrop;
|
64 | }
|
65 | })({
|
66 | zIndex: -1
|
67 | });
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | const Modal = React.forwardRef(function Modal(inProps, ref) {
|
83 | const props = useDefaultProps({
|
84 | name: 'MuiModal',
|
85 | props: inProps
|
86 | });
|
87 | const {
|
88 | BackdropComponent = ModalBackdrop,
|
89 | BackdropProps,
|
90 | classes: classesProp,
|
91 | className,
|
92 | closeAfterTransition = false,
|
93 | children,
|
94 | container,
|
95 | component,
|
96 | components = {},
|
97 | componentsProps = {},
|
98 | disableAutoFocus = false,
|
99 | disableEnforceFocus = false,
|
100 | disableEscapeKeyDown = false,
|
101 | disablePortal = false,
|
102 | disableRestoreFocus = false,
|
103 | disableScrollLock = false,
|
104 | hideBackdrop = false,
|
105 | keepMounted = false,
|
106 | onBackdropClick,
|
107 | onClose,
|
108 | onTransitionEnter,
|
109 | onTransitionExited,
|
110 | open,
|
111 | slotProps = {},
|
112 | slots = {},
|
113 |
|
114 | theme,
|
115 | ...other
|
116 | } = props;
|
117 | const propsWithDefaults = {
|
118 | ...props,
|
119 | closeAfterTransition,
|
120 | disableAutoFocus,
|
121 | disableEnforceFocus,
|
122 | disableEscapeKeyDown,
|
123 | disablePortal,
|
124 | disableRestoreFocus,
|
125 | disableScrollLock,
|
126 | hideBackdrop,
|
127 | keepMounted
|
128 | };
|
129 | const {
|
130 | getRootProps,
|
131 | getBackdropProps,
|
132 | getTransitionProps,
|
133 | portalRef,
|
134 | isTopModal,
|
135 | exited,
|
136 | hasTransition
|
137 | } = useModal({
|
138 | ...propsWithDefaults,
|
139 | rootRef: ref
|
140 | });
|
141 | const ownerState = {
|
142 | ...propsWithDefaults,
|
143 | exited
|
144 | };
|
145 | const classes = useUtilityClasses(ownerState);
|
146 | const childProps = {};
|
147 | if (children.props.tabIndex === undefined) {
|
148 | childProps.tabIndex = '-1';
|
149 | }
|
150 |
|
151 |
|
152 | if (hasTransition) {
|
153 | const {
|
154 | onEnter,
|
155 | onExited
|
156 | } = getTransitionProps();
|
157 | childProps.onEnter = onEnter;
|
158 | childProps.onExited = onExited;
|
159 | }
|
160 | const externalForwardedProps = {
|
161 | ...other,
|
162 | slots: {
|
163 | root: components.Root,
|
164 | backdrop: components.Backdrop,
|
165 | ...slots
|
166 | },
|
167 | slotProps: {
|
168 | ...componentsProps,
|
169 | ...slotProps
|
170 | }
|
171 | };
|
172 | const [RootSlot, rootProps] = useSlot('root', {
|
173 | elementType: ModalRoot,
|
174 | externalForwardedProps,
|
175 | getSlotProps: getRootProps,
|
176 | additionalProps: {
|
177 | ref,
|
178 | as: component
|
179 | },
|
180 | ownerState,
|
181 | className: clsx(className, classes?.root, !ownerState.open && ownerState.exited && classes?.hidden)
|
182 | });
|
183 | const [BackdropSlot, backdropProps] = useSlot('backdrop', {
|
184 | elementType: BackdropComponent,
|
185 | externalForwardedProps,
|
186 | additionalProps: BackdropProps,
|
187 | getSlotProps: otherHandlers => {
|
188 | return getBackdropProps({
|
189 | ...otherHandlers,
|
190 | onClick: event => {
|
191 | if (onBackdropClick) {
|
192 | onBackdropClick(event);
|
193 | }
|
194 | if (otherHandlers?.onClick) {
|
195 | otherHandlers.onClick(event);
|
196 | }
|
197 | }
|
198 | });
|
199 | },
|
200 | className: clsx(BackdropProps?.className, classes?.backdrop),
|
201 | ownerState
|
202 | });
|
203 | const backdropRef = useForkRef(BackdropProps?.ref, backdropProps.ref);
|
204 | if (!keepMounted && !open && (!hasTransition || exited)) {
|
205 | return null;
|
206 | }
|
207 | return _jsx(Portal, {
|
208 | ref: portalRef,
|
209 | container: container,
|
210 | disablePortal: disablePortal,
|
211 | children: _jsxs(RootSlot, {
|
212 | ...rootProps,
|
213 | children: [!hideBackdrop && BackdropComponent ? _jsx(BackdropSlot, {
|
214 | ...backdropProps,
|
215 | ref: backdropRef
|
216 | }) : null, _jsx(FocusTrap, {
|
217 | disableEnforceFocus: disableEnforceFocus,
|
218 | disableAutoFocus: disableAutoFocus,
|
219 | disableRestoreFocus: disableRestoreFocus,
|
220 | isEnabled: isTopModal,
|
221 | open: open,
|
222 | children: React.cloneElement(children, childProps)
|
223 | })]
|
224 | })
|
225 | });
|
226 | });
|
227 | process.env.NODE_ENV !== "production" ? Modal.propTypes = {
|
228 |
|
229 |
|
230 |
|
231 |
|
232 | |
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 | BackdropComponent: PropTypes.elementType,
|
247 | |
248 |
|
249 |
|
250 |
|
251 | BackdropProps: PropTypes.object,
|
252 | |
253 |
|
254 |
|
255 | children: elementAcceptingRef.isRequired,
|
256 | |
257 |
|
258 |
|
259 | classes: PropTypes.object,
|
260 | |
261 |
|
262 |
|
263 | className: PropTypes.string,
|
264 | |
265 |
|
266 |
|
267 |
|
268 | closeAfterTransition: PropTypes.bool,
|
269 | |
270 |
|
271 |
|
272 |
|
273 | component: PropTypes.elementType,
|
274 | |
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 | components: PropTypes.shape({
|
282 | Backdrop: PropTypes.elementType,
|
283 | Root: PropTypes.elementType
|
284 | }),
|
285 | |
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 | componentsProps: PropTypes.shape({
|
294 | backdrop: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
|
295 | root: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
|
296 | }),
|
297 | |
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 | container: PropTypes .oneOfType([HTMLElementType, PropTypes.func]),
|
308 | |
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 | disableAutoFocus: PropTypes.bool,
|
318 | |
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 | disableEnforceFocus: PropTypes.bool,
|
326 | |
327 |
|
328 |
|
329 |
|
330 | disableEscapeKeyDown: PropTypes.bool,
|
331 | |
332 |
|
333 |
|
334 |
|
335 | disablePortal: PropTypes.bool,
|
336 | |
337 |
|
338 |
|
339 |
|
340 |
|
341 | disableRestoreFocus: PropTypes.bool,
|
342 | |
343 |
|
344 |
|
345 |
|
346 | disableScrollLock: PropTypes.bool,
|
347 | |
348 |
|
349 |
|
350 |
|
351 | hideBackdrop: PropTypes.bool,
|
352 | |
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 | keepMounted: PropTypes.bool,
|
359 | |
360 |
|
361 |
|
362 |
|
363 | onBackdropClick: PropTypes.func,
|
364 | |
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 | onClose: PropTypes.func,
|
372 | |
373 |
|
374 |
|
375 | onTransitionEnter: PropTypes.func,
|
376 | |
377 |
|
378 |
|
379 | onTransitionExited: PropTypes.func,
|
380 | |
381 |
|
382 |
|
383 | open: PropTypes.bool.isRequired,
|
384 | |
385 |
|
386 |
|
387 |
|
388 | slotProps: PropTypes.shape({
|
389 | backdrop: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
|
390 | root: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
|
391 | }),
|
392 | |
393 |
|
394 |
|
395 |
|
396 |
|
397 | slots: PropTypes.shape({
|
398 | backdrop: PropTypes.elementType,
|
399 | root: PropTypes.elementType
|
400 | }),
|
401 | |
402 |
|
403 |
|
404 | sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object])
|
405 | } : void 0;
|
406 | export default Modal; |
\ | No newline at end of file |