UNPKG

4.57 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4Object.defineProperty(exports, "__esModule", {
5 value: true
6});
7exports.default = void 0;
8var React = _interopRequireWildcard(require("react"));
9var ReactDOM = _interopRequireWildcard(require("react-dom"));
10var _propTypes = _interopRequireDefault(require("prop-types"));
11var _utils = require("@mui/utils");
12var _jsxRuntime = require("react/jsx-runtime");
13function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
14function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
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/base/react-portal/)
26 *
27 * API:
28 *
29 * - [Portal API](https://mui.com/base/react-portal/components-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 // @ts-expect-error TODO upstream fix
39 const handleRef = (0, _utils.unstable_useForkRef)( /*#__PURE__*/React.isValidElement(children) ? children.ref : null, forwardedRef);
40 (0, _utils.unstable_useEnhancedEffect)(() => {
41 if (!disablePortal) {
42 setMountNode(getContainer(container) || document.body);
43 }
44 }, [container, disablePortal]);
45 (0, _utils.unstable_useEnhancedEffect)(() => {
46 if (mountNode && !disablePortal) {
47 (0, _utils.unstable_setRef)(forwardedRef, mountNode);
48 return () => {
49 (0, _utils.unstable_setRef)(forwardedRef, null);
50 };
51 }
52 return undefined;
53 }, [forwardedRef, mountNode, disablePortal]);
54 if (disablePortal) {
55 if ( /*#__PURE__*/React.isValidElement(children)) {
56 const newProps = {
57 ref: handleRef
58 };
59 return /*#__PURE__*/React.cloneElement(children, newProps);
60 }
61 return /*#__PURE__*/(0, _jsxRuntime.jsx)(React.Fragment, {
62 children: children
63 });
64 }
65 return /*#__PURE__*/(0, _jsxRuntime.jsx)(React.Fragment, {
66 children: mountNode ? /*#__PURE__*/ReactDOM.createPortal(children, mountNode) : mountNode
67 });
68});
69process.env.NODE_ENV !== "production" ? Portal.propTypes /* remove-proptypes */ = {
70 // ----------------------------- Warning --------------------------------
71 // | These PropTypes are generated from the TypeScript type definitions |
72 // | To update them edit TypeScript types and run "yarn proptypes" |
73 // ----------------------------------------------------------------------
74 /**
75 * The children to render into the `container`.
76 */
77 children: _propTypes.default.node,
78 /**
79 * An HTML element or function that returns one.
80 * The `container` will have the portal children appended to it.
81 *
82 * By default, it uses the body of the top-level document object,
83 * so it's simply `document.body` most of the time.
84 */
85 container: _propTypes.default /* @typescript-to-proptypes-ignore */.oneOfType([_utils.HTMLElementType, _propTypes.default.func]),
86 /**
87 * The `children` will be under the DOM hierarchy of the parent component.
88 * @default false
89 */
90 disablePortal: _propTypes.default.bool
91} : void 0;
92if (process.env.NODE_ENV !== 'production') {
93 // eslint-disable-next-line
94 Portal['propTypes' + ''] = (0, _utils.exactProp)(Portal.propTypes);
95}
96var _default = Portal;
97exports.default = _default;
\No newline at end of file