@atlaskit/portal
Version:
A wrapper for rendering components in React portals.
22 lines (21 loc) • 828 B
JavaScript
import { useEffect, useMemo } from 'react';
import { createPortal } from 'react-dom';
import { appendPortalContainerIfNotAppended, createContainer, removePortalContainer } from '../utils/portal-dom-utils';
export default function InternalPortal(props) {
const {
zIndex,
children
} = props;
const container = useMemo(() => createContainer(zIndex), [zIndex]);
// This is in the render method instead of useEffect so that
// the portal will be added to the DOM before the children render.
// For any further changes, ensure that the container does not have a
// parent besides the portal parent.
appendPortalContainerIfNotAppended(container);
useEffect(() => {
return () => {
removePortalContainer(container);
};
}, [container]);
return /*#__PURE__*/createPortal(children, container);
}