1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
4 | Object.defineProperty(exports, "__esModule", {
|
5 | value: true
|
6 | });
|
7 | exports.default = void 0;
|
8 | var _react = require("react");
|
9 | var _reactDom = _interopRequireDefault(require("react-dom"));
|
10 | var _canUseDom = _interopRequireDefault(require("./Dom/canUseDom"));
|
11 | var Portal = (0, _react.forwardRef)(function (props, ref) {
|
12 | var didUpdate = props.didUpdate,
|
13 | getContainer = props.getContainer,
|
14 | children = props.children;
|
15 | var parentRef = (0, _react.useRef)();
|
16 | var containerRef = (0, _react.useRef)();
|
17 |
|
18 |
|
19 | (0, _react.useImperativeHandle)(ref, function () {
|
20 | return {};
|
21 | });
|
22 |
|
23 |
|
24 | var initRef = (0, _react.useRef)(false);
|
25 | if (!initRef.current && (0, _canUseDom.default)()) {
|
26 | containerRef.current = getContainer();
|
27 | parentRef.current = containerRef.current.parentNode;
|
28 | initRef.current = true;
|
29 | }
|
30 |
|
31 |
|
32 | (0, _react.useEffect)(function () {
|
33 | didUpdate === null || didUpdate === void 0 || didUpdate(props);
|
34 | });
|
35 | (0, _react.useEffect)(function () {
|
36 |
|
37 |
|
38 |
|
39 | if (containerRef.current.parentNode === null && parentRef.current !== null) {
|
40 | parentRef.current.appendChild(containerRef.current);
|
41 | }
|
42 | return function () {
|
43 | var _containerRef$current;
|
44 |
|
45 |
|
46 | (_containerRef$current = containerRef.current) === null || _containerRef$current === void 0 || (_containerRef$current = _containerRef$current.parentNode) === null || _containerRef$current === void 0 || _containerRef$current.removeChild(containerRef.current);
|
47 | };
|
48 | }, []);
|
49 | return containerRef.current ? _reactDom.default.createPortal(children, containerRef.current) : null;
|
50 | });
|
51 | var _default = exports.default = Portal; |
\ | No newline at end of file |