preact-arco-design
Version:
Arco Design React UI Library.
30 lines (25 loc) • 947 B
JavaScript
// only used by trigger. Plan to replace ../Portal
import { useRef, useEffect } from "preact/compat";
import ReactDOM from "preact/compat";
import { isServerRendering } from "../_util/dom";
import useIsFirstRender from "../_util/hooks/useIsFirstRender";
var Portal = function Portal(props) {
var getContainer = props.getContainer,
children = props.children;
var containerRef = useRef();
var isFirstRender = useIsFirstRender();
if ((isFirstRender || containerRef.current === null) && !isServerRendering) {
containerRef.current = getContainer();
}
useEffect(function () {
return function () {
var container = containerRef.current;
if (container && container.parentNode) {
container.parentNode.removeChild(container);
containerRef.current = null;
}
};
}, []);
return containerRef.current ? ReactDOM.createPortal(children, containerRef.current) : null;
};
export default Portal;