|
| 1 | +import React from "react"; |
| 2 | +import { createPortal } from "react-dom"; |
| 3 | +import { Classes as BlueprintClasses } from "@blueprintjs/core"; |
| 4 | +import { createPopper } from "@popperjs/core"; |
| 5 | + |
| 6 | +import { CLASSPREFIX as eccgui } from "../../configuration/constants"; |
| 7 | +import { ContextOverlayProps, TestableComponent, TooltipSize } from "../../index"; |
| 8 | + |
| 9 | +/** |
| 10 | + * Overlay without the necessity to use a target that need to be rendered in place. |
| 11 | + * The target is referenced by a selector string or element object. |
| 12 | + * It can exist somewhere in the DOM, but it must exist when the overlay is rendered. |
| 13 | + * It is always displayed, close it by removement. |
| 14 | + */ |
| 15 | +export interface DecoupledOverlayProps |
| 16 | + extends React.HTMLAttributes<HTMLDivElement>, |
| 17 | + TestableComponent, |
| 18 | + Pick<ContextOverlayProps, "usePortal" | "portalContainer" | "placement" | "minimal"> { |
| 19 | + /** |
| 20 | + * Element that should be used. The step content is displayed as a tooltip instead of a modal. |
| 21 | + * In case of an array, the first match is highlighted. */ |
| 22 | + targetSelectorOrElement: string | Element; |
| 23 | + /** |
| 24 | + * The size of the overlay. |
| 25 | + * */ |
| 26 | + size?: TooltipSize; |
| 27 | +} |
| 28 | + |
| 29 | +/** Popover that is displayed and points at the highlighted element. */ |
| 30 | +export const DecoupledOverlay = ({ |
| 31 | + targetSelectorOrElement, |
| 32 | + usePortal = true, |
| 33 | + portalContainer = document.body, |
| 34 | + minimal = false, |
| 35 | + placement = "auto", |
| 36 | + size = "large", |
| 37 | + children, |
| 38 | +}: DecoupledOverlayProps) => { |
| 39 | + const overlayRef = React.useCallback( |
| 40 | + (overlay: HTMLDivElement | null) => { |
| 41 | + const target = |
| 42 | + typeof targetSelectorOrElement === "string" |
| 43 | + ? document.querySelector(targetSelectorOrElement) |
| 44 | + : targetSelectorOrElement; |
| 45 | + if (overlay && target) { |
| 46 | + createPopper(target, overlay, { |
| 47 | + placement: placement, |
| 48 | + modifiers: [ |
| 49 | + { |
| 50 | + name: "offset", |
| 51 | + options: { |
| 52 | + offset: [0, 15], |
| 53 | + }, |
| 54 | + }, |
| 55 | + ], |
| 56 | + }); |
| 57 | + } |
| 58 | + }, |
| 59 | + [targetSelectorOrElement] |
| 60 | + ); |
| 61 | + |
| 62 | + const overlay = ( |
| 63 | + <div |
| 64 | + className={ |
| 65 | + `${eccgui}-visual-tour__overlay` + |
| 66 | + ` ${eccgui}-visual-tour__overlay--${size}` + |
| 67 | + ` ${BlueprintClasses.POPOVER}` + |
| 68 | + (minimal ? ` ${BlueprintClasses.MINIMAL}` : "") |
| 69 | + } |
| 70 | + role="tooltip" |
| 71 | + ref={overlayRef} |
| 72 | + > |
| 73 | + {!minimal && ( |
| 74 | + <div |
| 75 | + className={`${eccgui}-visual-tour__arrow ${BlueprintClasses.POPOVER_ARROW}`} |
| 76 | + data-popper-arrow |
| 77 | + aria-hidden |
| 78 | + /> |
| 79 | + )} |
| 80 | + <div className={`${BlueprintClasses.POPOVER_CONTENT} ${eccgui}-visual-tour__overlay__content`}> |
| 81 | + {children} |
| 82 | + </div> |
| 83 | + </div> |
| 84 | + ); |
| 85 | + |
| 86 | + return usePortal ? createPortal(overlay, portalContainer) : overlay; |
| 87 | +}; |
| 88 | + |
| 89 | +export default DecoupledOverlay; |
0 commit comments