import React from "react"; import { createPortal } from "react-dom"; import { Classes as BlueprintClasses } from "@blueprintjs/core"; import { createPopper } from "@popperjs/core"; import { CLASSPREFIX as eccgui } from "../../configuration/constants"; import { ContextOverlayProps } from "../ContextOverlay"; import { TestableComponent } from "../interfaces"; import { TooltipSize } from "../Tooltip/Tooltip"; import WhiteSpaceContainer from "../Typography/WhiteSpaceContainer"; export interface DecoupledOverlayProps extends React.HTMLAttributes, TestableComponent, Pick { /** * Element that should be used. The step content is displayed as a tooltip instead of a modal. * In case of an array, the first match is highlighted. */ targetSelectorOrElement: string | Element; /** * The size of the overlay. * */ size?: TooltipSize; } /** * Use an overlay popover without the necessity to use a target that need to be rendered in place. * The target is referenced by a selector string or element object. * It can exist somewhere in the DOM, but it must exist when the overlay is rendered. * It is always displayed, close it by removement. */ export const DecoupledOverlay = ({ targetSelectorOrElement, usePortal = true, portalContainer = document.body, minimal = false, placement = "auto", size = "large", paddingSize, children, }: DecoupledOverlayProps) => { const overlayRef = React.useCallback( (overlay: HTMLDivElement | null) => { const target = typeof targetSelectorOrElement === "string" ? document.querySelector(targetSelectorOrElement) : targetSelectorOrElement; if (overlay && target) { createPopper(target, overlay, { placement: placement, modifiers: [ { name: "offset", options: { offset: [0, 15], }, }, ], }); } }, [targetSelectorOrElement], ); const overlay = (
{!minimal && (
)}
{paddingSize ? ( {children} ) : ( children )}
); return usePortal ? createPortal(overlay, portalContainer) : overlay; }; export default DecoupledOverlay;