1 | "use strict";
|
2 |
|
3 | var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
|
4 |
|
5 | exports.__esModule = true;
|
6 | exports["default"] = void 0;
|
7 |
|
8 | var _react = _interopRequireWildcard(require("react"));
|
9 |
|
10 | var _reactDom = require("react-dom");
|
11 |
|
12 | var _utils = require("../utils");
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | function getContainer(container) {
|
21 | container = typeof container === "function" ? container() : container;
|
22 | return (0, _reactDom.findDOMNode)(container);
|
23 | }
|
24 |
|
25 | var Portal = (0, _react.forwardRef)(function (_ref, ref) {
|
26 | var children = _ref.children,
|
27 | container = _ref.container,
|
28 | _ref$isDisabled = _ref.isDisabled,
|
29 | isDisabled = _ref$isDisabled === void 0 ? false : _ref$isDisabled,
|
30 | onRendered = _ref.onRendered;
|
31 |
|
32 | var _useState = (0, _react.useState)(null),
|
33 | mountNode = _useState[0],
|
34 | setMountNode = _useState[1];
|
35 |
|
36 | var handleRef = (0, _utils.useForkRef)(children.ref, ref);
|
37 | (0, _utils.useEnhancedEffect)(function () {
|
38 | if (!isDisabled) {
|
39 | setMountNode(getContainer(container) || document.body);
|
40 | }
|
41 | }, [container, isDisabled]);
|
42 | (0, _utils.useEnhancedEffect)(function () {
|
43 | if (mountNode && !isDisabled) {
|
44 | (0, _utils.setRef)(ref, mountNode);
|
45 | return function () {
|
46 | (0, _utils.setRef)(ref, null);
|
47 | };
|
48 | }
|
49 |
|
50 | return undefined;
|
51 | }, [ref, mountNode, isDisabled]);
|
52 | (0, _utils.useEnhancedEffect)(function () {
|
53 | if (onRendered && (mountNode || isDisabled)) {
|
54 | onRendered();
|
55 | }
|
56 | }, [onRendered, mountNode, isDisabled]);
|
57 |
|
58 | if (isDisabled) {
|
59 | _react.Children.only(children);
|
60 |
|
61 | return (0, _react.cloneElement)(children, {
|
62 | ref: handleRef
|
63 | });
|
64 | }
|
65 |
|
66 | return mountNode ? (0, _reactDom.createPortal)(children, mountNode) : mountNode;
|
67 | });
|
68 | Portal.displayName = "Portal";
|
69 | var _default = Portal;
|
70 | exports["default"] = _default; |
\ | No newline at end of file |