UNPKG

14.1 kBJavaScriptView Raw
1"use strict";
2'use client';
3
4var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
5var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
6Object.defineProperty(exports, "__esModule", {
7 value: true
8});
9exports.default = void 0;
10var React = _interopRequireWildcard(require("react"));
11var _propTypes = _interopRequireDefault(require("prop-types"));
12var _clsx = _interopRequireDefault(require("clsx"));
13var _HTMLElementType = _interopRequireDefault(require("@mui/utils/HTMLElementType"));
14var _elementAcceptingRef = _interopRequireDefault(require("@mui/utils/elementAcceptingRef"));
15var _composeClasses = _interopRequireDefault(require("@mui/utils/composeClasses"));
16var _Unstable_TrapFocus = _interopRequireDefault(require("../Unstable_TrapFocus"));
17var _Portal = _interopRequireDefault(require("../Portal"));
18var _zeroStyled = require("../zero-styled");
19var _memoTheme = _interopRequireDefault(require("../utils/memoTheme"));
20var _DefaultPropsProvider = require("../DefaultPropsProvider");
21var _Backdrop = _interopRequireDefault(require("../Backdrop"));
22var _useModal = _interopRequireDefault(require("./useModal"));
23var _modalClasses = require("./modalClasses");
24var _useSlot = _interopRequireDefault(require("../utils/useSlot"));
25var _utils = require("../utils");
26var _jsxRuntime = require("react/jsx-runtime");
27const useUtilityClasses = ownerState => {
28 const {
29 open,
30 exited,
31 classes
32 } = ownerState;
33 const slots = {
34 root: ['root', !open && exited && 'hidden'],
35 backdrop: ['backdrop']
36 };
37 return (0, _composeClasses.default)(slots, _modalClasses.getModalUtilityClass, classes);
38};
39const ModalRoot = (0, _zeroStyled.styled)('div', {
40 name: 'MuiModal',
41 slot: 'Root',
42 overridesResolver: (props, styles) => {
43 const {
44 ownerState
45 } = props;
46 return [styles.root, !ownerState.open && ownerState.exited && styles.hidden];
47 }
48})((0, _memoTheme.default)(({
49 theme
50}) => ({
51 position: 'fixed',
52 zIndex: (theme.vars || theme).zIndex.modal,
53 right: 0,
54 bottom: 0,
55 top: 0,
56 left: 0,
57 variants: [{
58 props: ({
59 ownerState
60 }) => !ownerState.open && ownerState.exited,
61 style: {
62 visibility: 'hidden'
63 }
64 }]
65})));
66const ModalBackdrop = (0, _zeroStyled.styled)(_Backdrop.default, {
67 name: 'MuiModal',
68 slot: 'Backdrop',
69 overridesResolver: (props, styles) => {
70 return styles.backdrop;
71 }
72})({
73 zIndex: -1
74});
75
76/**
77 * Modal is a lower-level construct that is leveraged by the following components:
78 *
79 * - [Dialog](/material-ui/api/dialog/)
80 * - [Drawer](/material-ui/api/drawer/)
81 * - [Menu](/material-ui/api/menu/)
82 * - [Popover](/material-ui/api/popover/)
83 *
84 * If you are creating a modal dialog, you probably want to use the [Dialog](/material-ui/api/dialog/) component
85 * rather than directly using Modal.
86 *
87 * This component shares many concepts with [react-overlays](https://react-bootstrap.github.io/react-overlays/#modals).
88 */
89const Modal = /*#__PURE__*/React.forwardRef(function Modal(inProps, ref) {
90 const props = (0, _DefaultPropsProvider.useDefaultProps)({
91 name: 'MuiModal',
92 props: inProps
93 });
94 const {
95 BackdropComponent = ModalBackdrop,
96 BackdropProps,
97 classes: classesProp,
98 className,
99 closeAfterTransition = false,
100 children,
101 container,
102 component,
103 components = {},
104 componentsProps = {},
105 disableAutoFocus = false,
106 disableEnforceFocus = false,
107 disableEscapeKeyDown = false,
108 disablePortal = false,
109 disableRestoreFocus = false,
110 disableScrollLock = false,
111 hideBackdrop = false,
112 keepMounted = false,
113 onBackdropClick,
114 onClose,
115 onTransitionEnter,
116 onTransitionExited,
117 open,
118 slotProps = {},
119 slots = {},
120 // eslint-disable-next-line react/prop-types
121 theme,
122 ...other
123 } = props;
124 const propsWithDefaults = {
125 ...props,
126 closeAfterTransition,
127 disableAutoFocus,
128 disableEnforceFocus,
129 disableEscapeKeyDown,
130 disablePortal,
131 disableRestoreFocus,
132 disableScrollLock,
133 hideBackdrop,
134 keepMounted
135 };
136 const {
137 getRootProps,
138 getBackdropProps,
139 getTransitionProps,
140 portalRef,
141 isTopModal,
142 exited,
143 hasTransition
144 } = (0, _useModal.default)({
145 ...propsWithDefaults,
146 rootRef: ref
147 });
148 const ownerState = {
149 ...propsWithDefaults,
150 exited
151 };
152 const classes = useUtilityClasses(ownerState);
153 const childProps = {};
154 if (children.props.tabIndex === undefined) {
155 childProps.tabIndex = '-1';
156 }
157
158 // It's a Transition like component
159 if (hasTransition) {
160 const {
161 onEnter,
162 onExited
163 } = getTransitionProps();
164 childProps.onEnter = onEnter;
165 childProps.onExited = onExited;
166 }
167 const externalForwardedProps = {
168 ...other,
169 slots: {
170 root: components.Root,
171 backdrop: components.Backdrop,
172 ...slots
173 },
174 slotProps: {
175 ...componentsProps,
176 ...slotProps
177 }
178 };
179 const [RootSlot, rootProps] = (0, _useSlot.default)('root', {
180 elementType: ModalRoot,
181 externalForwardedProps,
182 getSlotProps: getRootProps,
183 additionalProps: {
184 ref,
185 as: component
186 },
187 ownerState,
188 className: (0, _clsx.default)(className, classes?.root, !ownerState.open && ownerState.exited && classes?.hidden)
189 });
190 const [BackdropSlot, backdropProps] = (0, _useSlot.default)('backdrop', {
191 elementType: BackdropComponent,
192 externalForwardedProps,
193 additionalProps: BackdropProps,
194 getSlotProps: otherHandlers => {
195 return getBackdropProps({
196 ...otherHandlers,
197 onClick: event => {
198 if (onBackdropClick) {
199 onBackdropClick(event);
200 }
201 if (otherHandlers?.onClick) {
202 otherHandlers.onClick(event);
203 }
204 }
205 });
206 },
207 className: (0, _clsx.default)(BackdropProps?.className, classes?.backdrop),
208 ownerState
209 });
210 const backdropRef = (0, _utils.useForkRef)(BackdropProps?.ref, backdropProps.ref);
211 if (!keepMounted && !open && (!hasTransition || exited)) {
212 return null;
213 }
214 return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Portal.default, {
215 ref: portalRef,
216 container: container,
217 disablePortal: disablePortal,
218 children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(RootSlot, {
219 ...rootProps,
220 children: [!hideBackdrop && BackdropComponent ? /*#__PURE__*/(0, _jsxRuntime.jsx)(BackdropSlot, {
221 ...backdropProps,
222 ref: backdropRef
223 }) : null, /*#__PURE__*/(0, _jsxRuntime.jsx)(_Unstable_TrapFocus.default, {
224 disableEnforceFocus: disableEnforceFocus,
225 disableAutoFocus: disableAutoFocus,
226 disableRestoreFocus: disableRestoreFocus,
227 isEnabled: isTopModal,
228 open: open,
229 children: /*#__PURE__*/React.cloneElement(children, childProps)
230 })]
231 })
232 });
233});
234process.env.NODE_ENV !== "production" ? Modal.propTypes /* remove-proptypes */ = {
235 // ┌────────────────────────────── Warning ──────────────────────────────┐
236 // │ These PropTypes are generated from the TypeScript type definitions. │
237 // │ To update them, edit the d.ts file and run `pnpm proptypes`. │
238 // └─────────────────────────────────────────────────────────────────────┘
239 /**
240 * A backdrop component. This prop enables custom backdrop rendering.
241 * @deprecated Use `slots.backdrop` instead. While this prop currently works, it will be removed in the next major version.
242 * Use the `slots.backdrop` prop to make your application ready for the next version of Material UI.
243 * @default styled(Backdrop, {
244 * name: 'MuiModal',
245 * slot: 'Backdrop',
246 * overridesResolver: (props, styles) => {
247 * return styles.backdrop;
248 * },
249 * })({
250 * zIndex: -1,
251 * })
252 */
253 BackdropComponent: _propTypes.default.elementType,
254 /**
255 * Props applied to the [`Backdrop`](https://mui.com/material-ui/api/backdrop/) element.
256 * @deprecated Use `slotProps.backdrop` instead.
257 */
258 BackdropProps: _propTypes.default.object,
259 /**
260 * A single child content element.
261 */
262 children: _elementAcceptingRef.default.isRequired,
263 /**
264 * Override or extend the styles applied to the component.
265 */
266 classes: _propTypes.default.object,
267 /**
268 * @ignore
269 */
270 className: _propTypes.default.string,
271 /**
272 * When set to true the Modal waits until a nested Transition is completed before closing.
273 * @default false
274 */
275 closeAfterTransition: _propTypes.default.bool,
276 /**
277 * The component used for the root node.
278 * Either a string to use a HTML element or a component.
279 */
280 component: _propTypes.default.elementType,
281 /**
282 * The components used for each slot inside.
283 *
284 * @deprecated Use the `slots` prop instead. This prop will be removed in v7. See [Migrating from deprecated APIs](https://mui.com/material-ui/migration/migrating-from-deprecated-apis/) for more details.
285 *
286 * @default {}
287 */
288 components: _propTypes.default.shape({
289 Backdrop: _propTypes.default.elementType,
290 Root: _propTypes.default.elementType
291 }),
292 /**
293 * The extra props for the slot components.
294 * You can override the existing props or add new ones.
295 *
296 * @deprecated Use the `slotProps` prop instead. This prop will be removed in v7. See [Migrating from deprecated APIs](https://mui.com/material-ui/migration/migrating-from-deprecated-apis/) for more details.
297 *
298 * @default {}
299 */
300 componentsProps: _propTypes.default.shape({
301 backdrop: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.object]),
302 root: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.object])
303 }),
304 /**
305 * An HTML element or function that returns one.
306 * The `container` will have the portal children appended to it.
307 *
308 * You can also provide a callback, which is called in a React layout effect.
309 * This lets you set the container from a ref, and also makes server-side rendering possible.
310 *
311 * By default, it uses the body of the top-level document object,
312 * so it's simply `document.body` most of the time.
313 */
314 container: _propTypes.default /* @typescript-to-proptypes-ignore */.oneOfType([_HTMLElementType.default, _propTypes.default.func]),
315 /**
316 * If `true`, the modal will not automatically shift focus to itself when it opens, and
317 * replace it to the last focused element when it closes.
318 * This also works correctly with any modal children that have the `disableAutoFocus` prop.
319 *
320 * Generally this should never be set to `true` as it makes the modal less
321 * accessible to assistive technologies, like screen readers.
322 * @default false
323 */
324 disableAutoFocus: _propTypes.default.bool,
325 /**
326 * If `true`, the modal will not prevent focus from leaving the modal while open.
327 *
328 * Generally this should never be set to `true` as it makes the modal less
329 * accessible to assistive technologies, like screen readers.
330 * @default false
331 */
332 disableEnforceFocus: _propTypes.default.bool,
333 /**
334 * If `true`, hitting escape will not fire the `onClose` callback.
335 * @default false
336 */
337 disableEscapeKeyDown: _propTypes.default.bool,
338 /**
339 * The `children` will be under the DOM hierarchy of the parent component.
340 * @default false
341 */
342 disablePortal: _propTypes.default.bool,
343 /**
344 * If `true`, the modal will not restore focus to previously focused element once
345 * modal is hidden or unmounted.
346 * @default false
347 */
348 disableRestoreFocus: _propTypes.default.bool,
349 /**
350 * Disable the scroll lock behavior.
351 * @default false
352 */
353 disableScrollLock: _propTypes.default.bool,
354 /**
355 * If `true`, the backdrop is not rendered.
356 * @default false
357 */
358 hideBackdrop: _propTypes.default.bool,
359 /**
360 * Always keep the children in the DOM.
361 * This prop can be useful in SEO situation or
362 * when you want to maximize the responsiveness of the Modal.
363 * @default false
364 */
365 keepMounted: _propTypes.default.bool,
366 /**
367 * Callback fired when the backdrop is clicked.
368 * @deprecated Use the `onClose` prop with the `reason` argument to handle the `backdropClick` events.
369 */
370 onBackdropClick: _propTypes.default.func,
371 /**
372 * Callback fired when the component requests to be closed.
373 * The `reason` parameter can optionally be used to control the response to `onClose`.
374 *
375 * @param {object} event The event source of the callback.
376 * @param {string} reason Can be: `"escapeKeyDown"`, `"backdropClick"`.
377 */
378 onClose: _propTypes.default.func,
379 /**
380 * A function called when a transition enters.
381 */
382 onTransitionEnter: _propTypes.default.func,
383 /**
384 * A function called when a transition has exited.
385 */
386 onTransitionExited: _propTypes.default.func,
387 /**
388 * If `true`, the component is shown.
389 */
390 open: _propTypes.default.bool.isRequired,
391 /**
392 * The props used for each slot inside the Modal.
393 * @default {}
394 */
395 slotProps: _propTypes.default.shape({
396 backdrop: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.object]),
397 root: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.object])
398 }),
399 /**
400 * The components used for each slot inside the Modal.
401 * Either a string to use a HTML element or a component.
402 * @default {}
403 */
404 slots: _propTypes.default.shape({
405 backdrop: _propTypes.default.elementType,
406 root: _propTypes.default.elementType
407 }),
408 /**
409 * The system prop that allows defining system overrides as well as additional CSS styles.
410 */
411 sx: _propTypes.default.oneOfType([_propTypes.default.arrayOf(_propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.object, _propTypes.default.bool])), _propTypes.default.func, _propTypes.default.object])
412} : void 0;
413var _default = exports.default = Modal;
\No newline at end of file