1 | import { useRef, useEffect, forwardRef, useImperativeHandle } from 'react';
|
2 | import ReactDOM from 'react-dom';
|
3 | import canUseDom from "./Dom/canUseDom";
|
4 | var Portal = forwardRef(function (props, ref) {
|
5 | var didUpdate = props.didUpdate,
|
6 | getContainer = props.getContainer,
|
7 | children = props.children;
|
8 | var parentRef = useRef();
|
9 | var containerRef = useRef();
|
10 |
|
11 |
|
12 | useImperativeHandle(ref, function () {
|
13 | return {};
|
14 | });
|
15 |
|
16 |
|
17 | var initRef = useRef(false);
|
18 | if (!initRef.current && canUseDom()) {
|
19 | containerRef.current = getContainer();
|
20 | parentRef.current = containerRef.current.parentNode;
|
21 | initRef.current = true;
|
22 | }
|
23 |
|
24 |
|
25 | useEffect(function () {
|
26 | didUpdate === null || didUpdate === void 0 || didUpdate(props);
|
27 | });
|
28 | useEffect(function () {
|
29 |
|
30 |
|
31 |
|
32 | if (containerRef.current.parentNode === null && parentRef.current !== null) {
|
33 | parentRef.current.appendChild(containerRef.current);
|
34 | }
|
35 | return function () {
|
36 | var _containerRef$current;
|
37 |
|
38 |
|
39 | (_containerRef$current = containerRef.current) === null || _containerRef$current === void 0 || (_containerRef$current = _containerRef$current.parentNode) === null || _containerRef$current === void 0 || _containerRef$current.removeChild(containerRef.current);
|
40 | };
|
41 | }, []);
|
42 | return containerRef.current ? ReactDOM.createPortal(children, containerRef.current) : null;
|
43 | });
|
44 | export default Portal; |
\ | No newline at end of file |