UNPKG

@atlaskit/portal

Version:

A wrapper for rendering components in React portals.

26 lines (25 loc) 1.1 kB
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) { var zIndex = props.zIndex, children = props.children; var container = useMemo(function () { return createContainer(zIndex); }, [zIndex]); var 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(function () { return function () { removePortalContainer(container); }; }, [container]); return /*#__PURE__*/createPortal(colorMode ? /*#__PURE__*/React.createElement(ThemeProvider, { defaultColorMode: colorMode }, children) : children, container); }