import React from "react"; import { createPortal } from "react-dom"; import { CLASSPREFIX as eccgui } from "../../configuration/constants"; import { Badge, Button, Card, CardActions, CardActionsAux, CardContent, CardHeader, CardOptions, CardTitle, DecoupledOverlay, IconButton, Markdown, ModalSize, SimpleDialog, Spacing, TooltipSize, } from "../../index"; export interface VisualTourProps { /** The steps of the tour. */ steps: VisualTourStep[]; /** Called when the tour is cancelled or closed at then end. This should usually remove the component from the outside. */ onClose: () => void; /** Label of the button to close the tour. */ closeLabel?: string; /** The label for the 'next' button. */ nextLabel?: string; /** The label for the 'previous' button. */ prevLabel?: string; /** The step target is usable, e.g. it can be clicked. */ usableStepTarget?: boolean; /** Need to be set to `true` that the tour is displayed. */ isOpen?: boolean; } export interface VisualTourStep { title: string; /** The description or more elaborate content element that is shown in the modal/overlay. */ content: string | (() => React.JSX.Element); /** Optional element that should be highlighted. The step content is displayed as a tooltip instead of a modal. * In case of an array, the first match is highlighted. */ highlightElementQuery?: string | string[]; /** The texts used in the step, e.g. when custom layouts are rendered, these will be used for the text strings. */ texts?: Record; /** An image URL. This will be displayed in the step description. */ image?: string; /** The size of the tooltip or modal. */ size?: TooltipSize | ModalSize; /** The step target is usable, e.g. it can be clicked. Overwrites the setting in ``. */ usableStepTarget?: boolean; } /** This should be used for defining steps in a separate object/file. Use with 'satisfies' after the object definition. */ export type VisualTourStepDefinitions = Record>; const highlightElementBaseClass = `${eccgui}-visual-tour__highlighted-element`; /** A visual tour multi-step tour of the current view. */ export const VisualTour = ({ steps, onClose, closeLabel = "Close", nextLabel = "Next", prevLabel = "Back", usableStepTarget = false, isOpen = false, }: VisualTourProps) => { const [currentStepIndex, setCurrentStepIndex] = React.useState(0); const [currentStepComponent, setCurrentStepComponent] = React.useState(null); React.useEffect(() => { const closeTour = () => { // clear observer and disconnect if (lastObserver) { lastObserver.takeRecords(); lastObserver.disconnect(); } // empty step setCurrentStepComponent(null); // remove highlight classes document.querySelector(`.${highlightElementBaseClass}`)?.classList.remove(highlightElementBaseClass); document .querySelector(`.${highlightElementBaseClass}--useable`) ?.classList.remove(`${highlightElementBaseClass}--useable`); // call callback function from outside onClose(); }; const step = steps[currentStepIndex]; if (!step) { // This should not happen closeTour(); return; } const highlightElementClass = ( typeof step["usableStepTarget"] === "undefined" ? usableStepTarget : step["usableStepTarget"] ) ? `${highlightElementBaseClass}--useable` : highlightElementBaseClass; const hasNextStep = currentStepIndex + 1 < steps.length; const hasPreviousStep = currentStepIndex > 0; // Configure optional highlighting let elementToHighlight: HTMLElement | null = null; let lastObserver: MutationObserver | null = null; const setStepComponent = () => { const stepDisplay = ( {` ${currentStepIndex + 1}/${steps.length} `} ); const closeButton = ; const titleOptions = ( <> {stepDisplay} {closeButton} ); const actionButtons = [ hasNextStep ? ( ) : ( ) : null, ]; // TODO: What to do if an element should have been highlighted, but none was found? if (elementToHighlight) { setCurrentStepComponent( , ); } else { setCurrentStepComponent( , ); } }; const addElementHighlighting = () => { if (step.highlightElementQuery) { const queries: string[] = typeof step.highlightElementQuery === "string" ? [step.highlightElementQuery] : step.highlightElementQuery; queries.forEach((query) => { if (elementToHighlight == null) { elementToHighlight = document.querySelector(query); } }); } else { elementToHighlight = null; } if (elementToHighlight) { // Typescript for some reason incorrectly infers the type of elementToHighlight as never (elementToHighlight as HTMLElement).classList.add(highlightElementClass); (elementToHighlight as HTMLElement).scrollIntoView({ behavior: "smooth", block: "center", }); if (lastObserver) { lastObserver.disconnect(); } lastObserver = new MutationObserver(function () { // Re-new element highlighting if (step.highlightElementQuery) { if (!document.body.contains(elementToHighlight)) { // Element has been removed or replaced elementToHighlight = null; addElementHighlighting(); } else if (!elementToHighlight?.classList.contains(highlightElementClass)) { // Only the classes have been removed elementToHighlight?.classList.add(highlightElementClass); } } }); lastObserver.observe(document.body, { childList: true, subtree: true }); } setStepComponent(); }; addElementHighlighting(); return () => { // Remove previous element highlight document.querySelector(`.${highlightElementClass}`)?.classList.remove(highlightElementClass); if (lastObserver) { lastObserver.disconnect(); } }; }, [currentStepIndex, usableStepTarget]); if (isOpen === false) { return null; } else { return currentStepComponent; } }; interface StepModalProps { step: VisualTourStep; // Current step starting with 1 titleOption: React.JSX.Element; // Close the visual tour onClose: () => void; // The navigation buttons actionButtons: (React.JSX.Element | null)[]; } // Main content of a step const StepContent = ({ step }: { step: VisualTourStep }) => { return ( <> {step.image && ( <> )} {typeof step.content === "string" ? {step.content} : step.content()} ); }; /** Modal that is displayed for a step. */ const StepModal = ({ step, titleOption, onClose, actionButtons }: StepModalProps) => { return ( ); }; interface StepPopoverProps { highlightedElement: Element; step: VisualTourStep; // Current step starting with 1 titleOption: React.JSX.Element; // The navigation buttons actionButtons: (React.JSX.Element | null)[]; } /** Popover that is displayed and points at the highlighted element. */ const StepPopover = ({ highlightedElement, step, titleOption, actionButtons }: StepPopoverProps) => { const backdropRef = React.useCallback( (backdrop: HTMLDivElement | null) => { const highlightStencil = () => { const targetRect = highlightedElement.getBoundingClientRect(); backdrop!.style.left = `calc(${ targetRect.left + window.scrollX + "px" } - var(--${eccgui}-visual-tour-focus-padding))`; backdrop!.style.top = `calc(${ targetRect.top + window.scrollY + "px" } - var(--${eccgui}-visual-tour-focus-padding))`; backdrop!.style.width = `calc(${ targetRect.width + "px" } + 2 * var(--${eccgui}-visual-tour-focus-padding))`; backdrop!.style.height = `calc(${ targetRect.height + "px" } + 2 * var(--${eccgui}-visual-tour-focus-padding))`; }; if (backdrop) { highlightStencil(); window.addEventListener("resize", highlightStencil); return () => { window.removeEventListener("resize", highlightStencil); }; } return; }, [highlightedElement], ); // map to only tooltip size because the `DecoupledOverlay` only supports them let overlaySize: TooltipSize = "large"; switch (step.size) { case "tiny": overlaySize = "small"; break; case "regular": overlaySize = "medium"; break; case "xlarge": case "fullscreen": overlaySize = "large"; break; } return createPortal(
{step.title} {titleOption} {actionButtons}
, document.body, ); }; export default VisualTour;