UNPKG

4.01 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
6
7Object.defineProperty(exports, "__esModule", {
8 value: true
9});
10exports.default = void 0;
11
12var React = _interopRequireWildcard(require("react"));
13
14var ReactDOM = _interopRequireWildcard(require("react-dom"));
15
16var _propTypes = _interopRequireDefault(require("prop-types"));
17
18var _utils = require("@material-ui/utils");
19
20var _setRef = _interopRequireDefault(require("../utils/setRef"));
21
22var _useForkRef = _interopRequireDefault(require("../utils/useForkRef"));
23
24function getContainer(container) {
25 container = typeof container === 'function' ? container() : container; // #StrictMode ready
26
27 return ReactDOM.findDOMNode(container);
28}
29
30var useEnhancedEffect = typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;
31/**
32 * Portals provide a first-class way to render children into a DOM node
33 * that exists outside the DOM hierarchy of the parent component.
34 */
35
36var Portal = /*#__PURE__*/React.forwardRef(function Portal(props, ref) {
37 var children = props.children,
38 container = props.container,
39 _props$disablePortal = props.disablePortal,
40 disablePortal = _props$disablePortal === void 0 ? false : _props$disablePortal,
41 onRendered = props.onRendered;
42
43 var _React$useState = React.useState(null),
44 mountNode = _React$useState[0],
45 setMountNode = _React$useState[1];
46
47 var handleRef = (0, _useForkRef.default)( /*#__PURE__*/React.isValidElement(children) ? children.ref : null, ref);
48 useEnhancedEffect(function () {
49 if (!disablePortal) {
50 setMountNode(getContainer(container) || document.body);
51 }
52 }, [container, disablePortal]);
53 useEnhancedEffect(function () {
54 if (mountNode && !disablePortal) {
55 (0, _setRef.default)(ref, mountNode);
56 return function () {
57 (0, _setRef.default)(ref, null);
58 };
59 }
60
61 return undefined;
62 }, [ref, mountNode, disablePortal]);
63 useEnhancedEffect(function () {
64 if (onRendered && (mountNode || disablePortal)) {
65 onRendered();
66 }
67 }, [onRendered, mountNode, disablePortal]);
68
69 if (disablePortal) {
70 if ( /*#__PURE__*/React.isValidElement(children)) {
71 return /*#__PURE__*/React.cloneElement(children, {
72 ref: handleRef
73 });
74 }
75
76 return children;
77 }
78
79 return mountNode ? /*#__PURE__*/ReactDOM.createPortal(children, mountNode) : mountNode;
80});
81process.env.NODE_ENV !== "production" ? Portal.propTypes = {
82 // ----------------------------- Warning --------------------------------
83 // | These PropTypes are generated from the TypeScript type definitions |
84 // | To update them edit the d.ts file and run "yarn proptypes" |
85 // ----------------------------------------------------------------------
86
87 /**
88 * The children to render into the `container`.
89 */
90 children: _propTypes.default.node,
91
92 /**
93 * A HTML element, component instance, or function that returns either.
94 * The `container` will have the portal children appended to it.
95 *
96 * By default, it uses the body of the top-level document object,
97 * so it's simply `document.body` most of the time.
98 */
99 container: _propTypes.default
100 /* @typescript-to-proptypes-ignore */
101 .oneOfType([_utils.HTMLElementType, _propTypes.default.instanceOf(React.Component), _propTypes.default.func]),
102
103 /**
104 * Disable the portal behavior.
105 * The children stay within it's parent DOM hierarchy.
106 */
107 disablePortal: _propTypes.default.bool,
108
109 /**
110 * Callback fired once the children has been mounted into the `container`.
111 *
112 * This prop will be deprecated and removed in v5, the ref can be used instead.
113 */
114 onRendered: _propTypes.default.func
115} : void 0;
116
117if (process.env.NODE_ENV !== 'production') {
118 // eslint-disable-next-line
119 Portal['propTypes' + ''] = (0, _utils.exactProp)(Portal.propTypes);
120}
121
122var _default = Portal;
123exports.default = _default;
\No newline at end of file