@atlaskit/portal
Version:
A wrapper for rendering components in React portals.
26 lines (25 loc) • 1.05 kB
JavaScript
import React, { useEffect, useMemo } from 'react';
import { createPortal } from 'react-dom';
import { ThemeProvider, useColorMode } from '@atlaskit/app-provider';
import { appendPortalContainerIfNotAppended, createContainer, removePortalContainer } from '../utils/portal-dom-utils';
export default function InternalPortal(props) {
const {
zIndex,
children
} = props;
const container = useMemo(() => createContainer(zIndex), [zIndex]);
const colorMode = useColorMode();
// 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(colorMode ? /*#__PURE__*/React.createElement(ThemeProvider, {
defaultColorMode: colorMode
}, children) : children, container);
}