|
| 1 | +import React from "react"; |
| 2 | + |
| 3 | +import { CLASSPREFIX as eccgui } from "../../configuration/constants"; |
| 4 | +import Button from "../Button/Button"; |
| 5 | +import { SimpleDialog } from "../Dialog"; |
| 6 | + |
| 7 | +export interface VisualTourProps { |
| 8 | + /** The CSS query of the container element that contains the feature that will be given a tour of. |
| 9 | + * If element highlighting is enabled, this will grey out every other element in the container element. |
| 10 | + * Default: body*/ |
| 11 | + containerElementQuery?: string; |
| 12 | + /** The steps of the tour. */ |
| 13 | + steps: VisualTourStep[]; |
| 14 | + /** Called when the tour is cancelled or closed at then end. This should usually remove the component from the outside. */ |
| 15 | + onClose: () => void; |
| 16 | + /** Label of the button to close the tour. */ |
| 17 | + closeLabel?: string; |
| 18 | + /** The label for the next button. */ |
| 19 | + nextLabel?: string; |
| 20 | +} |
| 21 | + |
| 22 | +interface VisualTourStep { |
| 23 | + /** (Short) Title of the current step. */ |
| 24 | + title: string; |
| 25 | + /** The description or more elaborate content element that is shown in the modal/overlay. */ |
| 26 | + content: string | (() => React.JSX.Element); |
| 27 | + /** Optional element that should be highlighted, every other element in the container element is greyed out. */ |
| 28 | + highlightElementQuery?: string; |
| 29 | +} |
| 30 | + |
| 31 | +const containerHighlightClass = `${eccgui}-visual-tour__container`; |
| 32 | +const highlightElementClass = `${eccgui}-visual-tour__highlighted-element`; |
| 33 | + |
| 34 | +/** A visual tour multi-step tour of the current view. */ |
| 35 | +export const VisualTour = ({ |
| 36 | + containerElementQuery = "body", |
| 37 | + steps, |
| 38 | + onClose, |
| 39 | + closeLabel = "Close", |
| 40 | + nextLabel = "Next", |
| 41 | +}: VisualTourProps) => { |
| 42 | + const [currentStepIndex, setCurrentStepIndex] = React.useState<number>(0); |
| 43 | + const [currentStepComponent, setCurrentStepComponent] = React.useState<React.JSX.Element | null>(null); |
| 44 | + |
| 45 | + React.useEffect(() => { |
| 46 | + const step = steps[currentStepIndex]; |
| 47 | + if (!step) { |
| 48 | + // This should not happen |
| 49 | + setCurrentStepComponent(null); |
| 50 | + onClose(); |
| 51 | + return; |
| 52 | + } |
| 53 | + const hasNextStep = currentStepIndex + 1 < steps.length; |
| 54 | + // Configure optional highlighting |
| 55 | + const element = document.querySelector(containerElementQuery); |
| 56 | + if (element) { |
| 57 | + // Remove previous element highlight |
| 58 | + element.classList.remove(containerHighlightClass); |
| 59 | + document.querySelector(`.${highlightElementClass}`)?.classList.remove(highlightElementClass); |
| 60 | + if (step.highlightElementQuery) { |
| 61 | + const elementToHighlight = document.querySelector(step.highlightElementQuery); |
| 62 | + if (elementToHighlight) { |
| 63 | + element.classList.add(containerHighlightClass); |
| 64 | + elementToHighlight.classList.add(highlightElementClass); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + setCurrentStepComponent( |
| 69 | + <SimpleDialog |
| 70 | + title={step.title + ` ${currentStepIndex + 1} / ${steps.length}`} |
| 71 | + isOpen={true} |
| 72 | + preventSimpleClosing={true} |
| 73 | + onClose={onClose} |
| 74 | + actions={[ |
| 75 | + <Button onClick={onClose}>{closeLabel}</Button>, |
| 76 | + hasNextStep ? ( |
| 77 | + <Button |
| 78 | + onClick={() => { |
| 79 | + setCurrentStepIndex(currentStepIndex + 1); |
| 80 | + }} |
| 81 | + > |
| 82 | + {nextLabel}: {steps[currentStepIndex + 1].title} |
| 83 | + </Button> |
| 84 | + ) : null, |
| 85 | + ]} |
| 86 | + > |
| 87 | + {typeof step.content === "string" ? step.content : step.content()} |
| 88 | + </SimpleDialog> |
| 89 | + ); |
| 90 | + }, [currentStepIndex]); |
| 91 | + |
| 92 | + return currentStepComponent; |
| 93 | +}; |
| 94 | + |
| 95 | +export default VisualTour; |
0 commit comments