@geneui/components
Version:
The Gene UI components library designed for BI tools
59 lines (56 loc) • 1.88 kB
JavaScript
import { memo, useState, useContext, useEffect } from 'react';
import ReactDOM__default from 'react-dom';
import PropTypes from 'prop-types';
import { GeneUIDesignSystemContext } from '../GeneUIProvider/index.js';
import '../style-inject.es-746bb8ed.js';
function Portal(_ref) {
let {
isOpen,
children,
className,
container: propsContainer
} = _ref;
const [container, setContainer] = useState(propsContainer);
const {
geneUIProviderRef
} = useContext(GeneUIDesignSystemContext);
useEffect(() => {
if (!propsContainer) {
var _geneUIProviderRef$cu;
const div = document.createElement('div');
if (className) {
div.className = className;
}
setContainer(div);
(_geneUIProviderRef$cu = geneUIProviderRef.current) === null || _geneUIProviderRef$cu === void 0 ? void 0 : _geneUIProviderRef$cu.appendChild(div);
return () => {
var _geneUIProviderRef$cu2;
(_geneUIProviderRef$cu2 = geneUIProviderRef.current) === null || _geneUIProviderRef$cu2 === void 0 ? void 0 : _geneUIProviderRef$cu2.removeChild(div);
};
}
}, [className, propsContainer, geneUIProviderRef.current]);
return isOpen && container ? /*#__PURE__*/ReactDOM__default.createPortal(children, container) : null;
}
Portal.propTypes = {
/**
* Specify Portal visibility
*/
isOpen: PropTypes.bool,
/**
* A DOM element for Portal to be appended. When not specified the Portal will create a "div" element and append in body
*/
container: PropTypes.instanceOf(Element),
/**
* Additional className
*/
className: PropTypes.string,
/**
* Anything passed to the Portal as child will be rendered in "container". Any valid React node
*/
children: PropTypes.node
};
Portal.defaultProps = {
isOpen: false
};
var Portal$1 = /*#__PURE__*/memo(Portal);
export { Portal$1 as default };