1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 |
|
5 | var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
|
6 |
|
7 | Object.defineProperty(exports, "__esModule", {
|
8 | value: true
|
9 | });
|
10 | exports.default = void 0;
|
11 |
|
12 | var React = _interopRequireWildcard(require("react"));
|
13 |
|
14 | var ReactDOM = _interopRequireWildcard(require("react-dom"));
|
15 |
|
16 | var _propTypes = _interopRequireDefault(require("prop-types"));
|
17 |
|
18 | var _utils = require("@material-ui/utils");
|
19 |
|
20 | var _setRef = _interopRequireDefault(require("../utils/setRef"));
|
21 |
|
22 | var _useForkRef = _interopRequireDefault(require("../utils/useForkRef"));
|
23 |
|
24 | function getContainer(container) {
|
25 | container = typeof container === 'function' ? container() : container;
|
26 |
|
27 | return ReactDOM.findDOMNode(container);
|
28 | }
|
29 |
|
30 | var useEnhancedEffect = typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | var Portal = 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)( 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 ( React.isValidElement(children)) {
|
71 | return React.cloneElement(children, {
|
72 | ref: handleRef
|
73 | });
|
74 | }
|
75 |
|
76 | return children;
|
77 | }
|
78 |
|
79 | return mountNode ? ReactDOM.createPortal(children, mountNode) : mountNode;
|
80 | });
|
81 | process.env.NODE_ENV !== "production" ? Portal.propTypes = {
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | |
88 |
|
89 |
|
90 | children: _propTypes.default.node,
|
91 |
|
92 | |
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | container: _propTypes.default
|
100 |
|
101 | .oneOfType([_utils.HTMLElementType, _propTypes.default.instanceOf(React.Component), _propTypes.default.func]),
|
102 |
|
103 | |
104 |
|
105 |
|
106 |
|
107 | disablePortal: _propTypes.default.bool,
|
108 |
|
109 | |
110 |
|
111 |
|
112 |
|
113 |
|
114 | onRendered: _propTypes.default.func
|
115 | } : void 0;
|
116 |
|
117 | if (process.env.NODE_ENV !== 'production') {
|
118 |
|
119 | Portal['propTypes' + ''] = (0, _utils.exactProp)(Portal.propTypes);
|
120 | }
|
121 |
|
122 | var _default = Portal;
|
123 | exports.default = _default; |
\ | No newline at end of file |