@kiwicom/orbit-components
Version:
Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com’s products.
34 lines • 850 B
JavaScript
import * as React from "react";
import ReactDOM from "react-dom";
const Portal = ({
renderInto,
children
}) => {
const [el] = React.useState(() => {
if (typeof window !== "undefined") {
return document.createElement("div");
}
return null;
});
const [node] = React.useState(() => {
if (typeof window !== "undefined") {
return renderInto && document.getElementById(renderInto) ? document.getElementById(renderInto) : document.body;
}
return null;
});
React.useLayoutEffect(() => {
if (node && el) {
node.appendChild(el);
}
return () => {
if (node && el) {
node.removeChild(el);
}
};
}, [el, node]);
if (typeof window !== "undefined" && el !== null) {
return /*#__PURE__*/ReactDOM.createPortal(children, el);
}
return null;
};
export default Portal;