UNPKG

4.01 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 ReactDOM = _interopRequireWildcard(require("react-dom"));
12var _propTypes = _interopRequireDefault(require("prop-types"));
13var _utils = require("@mui/utils");
14var _jsxRuntime = require("react/jsx-runtime");
15function getContainer(container) {
16 return typeof container === 'function' ? container() : container;
17}
18
19/**
20 * Portals provide a first-class way to render children into a DOM node
21 * that exists outside the DOM hierarchy of the parent component.
22 *
23 * Demos:
24 *
25 * - [Portal](https://mui.com/material-ui/react-portal/)
26 *
27 * API:
28 *
29 * - [Portal API](https://mui.com/material-ui/api/portal/)
30 */
31const Portal = /*#__PURE__*/React.forwardRef(function Portal(props, forwardedRef) {
32 const {
33 children,
34 container,
35 disablePortal = false
36 } = props;
37 const [mountNode, setMountNode] = React.useState(null);
38 const handleRef = (0, _utils.unstable_useForkRef)(/*#__PURE__*/React.isValidElement(children) ? (0, _utils.unstable_getReactElementRef)(children) : null, forwardedRef);
39 (0, _utils.unstable_useEnhancedEffect)(() => {
40 if (!disablePortal) {
41 setMountNode(getContainer(container) || document.body);
42 }
43 }, [container, disablePortal]);
44 (0, _utils.unstable_useEnhancedEffect)(() => {
45 if (mountNode && !disablePortal) {
46 (0, _utils.unstable_setRef)(forwardedRef, mountNode);
47 return () => {
48 (0, _utils.unstable_setRef)(forwardedRef, null);
49 };
50 }
51 return undefined;
52 }, [forwardedRef, mountNode, disablePortal]);
53 if (disablePortal) {
54 if (/*#__PURE__*/React.isValidElement(children)) {
55 const newProps = {
56 ref: handleRef
57 };
58 return /*#__PURE__*/React.cloneElement(children, newProps);
59 }
60 return /*#__PURE__*/(0, _jsxRuntime.jsx)(React.Fragment, {
61 children: children
62 });
63 }
64 return /*#__PURE__*/(0, _jsxRuntime.jsx)(React.Fragment, {
65 children: mountNode ? /*#__PURE__*/ReactDOM.createPortal(children, mountNode) : mountNode
66 });
67});
68process.env.NODE_ENV !== "production" ? Portal.propTypes /* remove-proptypes */ = {
69 // ┌────────────────────────────── Warning ──────────────────────────────┐
70 // │ These PropTypes are generated from the TypeScript type definitions. │
71 // │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
72 // └─────────────────────────────────────────────────────────────────────┘
73 /**
74 * The children to render into the `container`.
75 */
76 children: _propTypes.default.node,
77 /**
78 * An HTML element or function that returns one.
79 * The `container` will have the portal children appended to it.
80 *
81 * You can also provide a callback, which is called in a React layout effect.
82 * This lets you set the container from a ref, and also makes server-side rendering possible.
83 *
84 * By default, it uses the body of the top-level document object,
85 * so it's simply `document.body` most of the time.
86 */
87 container: _propTypes.default /* @typescript-to-proptypes-ignore */.oneOfType([_utils.HTMLElementType, _propTypes.default.func]),
88 /**
89 * The `children` will be under the DOM hierarchy of the parent component.
90 * @default false
91 */
92 disablePortal: _propTypes.default.bool
93} : void 0;
94if (process.env.NODE_ENV !== 'production') {
95 // eslint-disable-next-line
96 Portal['propTypes' + ''] = (0, _utils.exactProp)(Portal.propTypes);
97}
98var _default = exports.default = Portal;
\No newline at end of file