UNPKG

@elastic/eui

Version:

Elastic UI Component Library

86 lines (82 loc) 3.66 kB
import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; /* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License * 2.0 and the Server Side Public License, v 1; you may not use this file except * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ import { memo, useContext, useEffect, useLayoutEffect, useRef, useState } from 'react'; import { createPortal } from 'react-dom'; import { EuiNestedThemeContext } from '../../services'; import { usePropsWithComponentDefaults } from '../provider/component_defaults'; var usePortalEffect = typeof document === 'undefined' ? useEffect : useLayoutEffect; var INSERT_POSITIONS = ['after', 'before']; var insertPositions = { after: 'afterend', before: 'beforebegin' }; export var EuiPortal = /*#__PURE__*/memo(function (_props) { var props = usePropsWithComponentDefaults('EuiPortal', _props); var children = props.children, insert = props.insert, setPortalRef = props.portalRef; var portalRef = useRef(setPortalRef); var _useContext = useContext(EuiNestedThemeContext), hasDifferentColorFromGlobalTheme = _useContext.hasDifferentColorFromGlobalTheme, colorClassName = _useContext.colorClassName; var _useState = useState(null), _useState2 = _slicedToArray(_useState, 2), portalNode = _useState2[0], setPortalNode = _useState2[1]; // Set the inherited color of the portal based on the wrapping EuiThemeProvider var setThemeColor = function setThemeColor(portalNode) { if (hasDifferentColorFromGlobalTheme && insert == null) { portalNode.classList.add(colorClassName); } }; var updatePortalRef = function updatePortalRef(ref) { var _portalRef$current; (_portalRef$current = portalRef.current) === null || _portalRef$current === void 0 || _portalRef$current.call(portalRef, ref); }; useEffect(function () { portalRef.current = setPortalRef; }, [setPortalRef]); /* Uses `useLayoutEffect` on client-side instead of `useEffect` to ensure the portal node is created and inserted into the DOM synchronously. This matches the same timing as the previous class component `componentDidMount` behavior. Using `useEffect` would add an additional render cycle that would break expected behavior of e.g. `@hello-pangea/dnd` which handles keyboard focus and doesn't expect a rerender. This falls back to `useEffect` for SSR to avoid console errors. `useEffect` will be a no-op, same as `componentDidMount` */ usePortalEffect(function () { var node = document.createElement('div'); node.dataset.euiportal = 'true'; if (insert == null) { // no insertion defined, append to body document.body.appendChild(node); } else { // inserting before or after an element var sibling = insert.sibling, position = insert.position; sibling.insertAdjacentElement(insertPositions[position], node); } setThemeColor(node); updatePortalRef(node); // Update state with portalNode to intentionally trigger component rerender // and call createPortal with the correct root element setPortalNode(node); return function () { if (node !== null && node !== void 0 && node.parentNode) { node.parentNode.removeChild(node); } updatePortalRef(null); }; // eslint-disable-next-line react-hooks/exhaustive-deps -- on mount only }, []); if (!portalNode) { return null; } return /*#__PURE__*/createPortal(children, portalNode); }); EuiPortal.displayName = 'EuiPortal';