UNPKG

2.27 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4Object.defineProperty(exports, "__esModule", {
5 value: true
6});
7exports.default = void 0;
8var _react = require("react");
9var _reactDom = _interopRequireDefault(require("react-dom"));
10var _canUseDom = _interopRequireDefault(require("./Dom/canUseDom"));
11var Portal = /*#__PURE__*/(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 // Ref return nothing, only for wrapper check exist
19 (0, _react.useImperativeHandle)(ref, function () {
20 return {};
21 });
22
23 // Create container in client side with sync to avoid useEffect not get ref
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 // [Legacy] Used by `rc-trigger`
32 (0, _react.useEffect)(function () {
33 didUpdate === null || didUpdate === void 0 || didUpdate(props);
34 });
35 (0, _react.useEffect)(function () {
36 // Restore container to original place
37 // React 18 StrictMode will unmount first and mount back for effect test:
38 // https://reactjs.org/blog/2022/03/29/react-v18.html#new-strict-mode-behaviors
39 if (containerRef.current.parentNode === null && parentRef.current !== null) {
40 parentRef.current.appendChild(containerRef.current);
41 }
42 return function () {
43 var _containerRef$current;
44 // [Legacy] This should not be handle by Portal but parent PortalWrapper instead.
45 // Since some component use `Portal` directly, we have to keep the logic here.
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 ? /*#__PURE__*/_reactDom.default.createPortal(children, containerRef.current) : null;
50});
51var _default = exports.default = Portal;
\No newline at end of file