{"version":3,"file":"SpotlightTour.mjs","names":[],"sources":["../../../src/spotlight/SpotlightTour.tsx"],"sourcesContent":["/*!\n *\n *  Copyright (c) \"Neo4j\"\n *  Neo4j Sweden AB [http://neo4j.com]\n *\n *  This file is part of Neo4j.\n *\n *  Neo4j is free software: you can redistribute it and/or modify\n *  it under the terms of the GNU General Public License as published by\n *  the Free Software Foundation, either version 3 of the License, or\n *  (at your option) any later version.\n *\n *  This program is distributed in the hope that it will be useful,\n *  but WITHOUT ANY WARRANTY; without even the implied warranty of\n *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n *  GNU General Public License for more details.\n *\n *  You should have received a copy of the GNU General Public License\n *  along with this program.  If not, see <http://www.gnu.org/licenses/>.\n */\nimport type React from 'react';\nimport { useCallback } from 'react';\n\nimport { OutlinedButton } from '../outlined-button';\nimport { TextButton } from '../text-button';\nimport { Spotlight, type SpotlightCloseAction } from './Spotlight';\nimport { useSpotlightContext } from './SpotlightProvider';\n\ntype SpotlightTourAction =\n  | SpotlightCloseAction\n  | 'dismiss'\n  | 'back'\n  | 'next'\n  | 'close'\n  | 'open';\n\ntype SpotlightTourProps = {\n  /** An array of `Spotlight` component props objects. SpotlightTour overrides the following props, to simplify your implementation, if they are undefined: `actions`, `beforeActions`, `initialFocus`, `onClose`. If you want custom behaviour for a certain `Spotlight` you can set these props yourself. */\n  spotlights: React.ComponentProps<typeof Spotlight>[];\n  /** A string that will be the text for the dismiss button */\n  dismissButtonLabel?: string;\n  /** A string that will be the text for the back button */\n  backButtonLabel?: string;\n  /** A string that will be the text for the next button */\n  nextButtonLabel?: string;\n  /** A string that will be the text for the close button */\n  closeButtonLabel?: string;\n  /** A callback function that is called when some actions are performed inside a `Spotlight`. It receives two arguments: `target` (string identifying the `Spotlight` that the action was performed in) and `action` (string describing the action that has been performed. These actions can be \"dismiss\", \"back\", \"next\", \"close\", \"clickOutside\" or \"escapeKeyDown\") */\n  onAction?: (target: string, action: SpotlightTourAction) => void;\n};\n\nexport const SpotlightTour = ({\n  spotlights,\n  dismissButtonLabel = 'Dismiss',\n  backButtonLabel = 'Back',\n  nextButtonLabel = 'Next',\n  closeButtonLabel = 'Close',\n  onAction,\n}: SpotlightTourProps) => {\n  const { setActiveSpotlight, setIsOpen } = useSpotlightContext();\n\n  const next = useCallback(\n    (index: number) => {\n      onAction?.(spotlights[index].target, 'next');\n      setActiveSpotlight(spotlights[index + 1].target);\n    },\n    [onAction, spotlights, setActiveSpotlight],\n  );\n\n  const back = useCallback(\n    (index: number) => {\n      onAction?.(spotlights[index].target, 'back');\n      setActiveSpotlight(spotlights[index - 1].target);\n    },\n    [onAction, spotlights, setActiveSpotlight],\n  );\n\n  const dismiss = useCallback(\n    (index: number) => {\n      onAction?.(spotlights[index].target, 'dismiss');\n      setActiveSpotlight(null);\n      setIsOpen(false);\n    },\n    [onAction, spotlights, setActiveSpotlight, setIsOpen],\n  );\n\n  const close = useCallback(\n    (index: number) => {\n      onAction?.(spotlights[index].target, 'close');\n      setActiveSpotlight(null);\n      setIsOpen(false);\n    },\n    [onAction, spotlights, setActiveSpotlight, setIsOpen],\n  );\n\n  const getActions = useCallback(\n    (index: number, actions: React.ReactNode): React.ReactNode => {\n      if (actions !== undefined && actions !== null) {\n        return actions;\n      }\n\n      const isFirst = index === 0;\n      const isLast = index === spotlights.length - 1;\n      const hasMultiple = spotlights.length > 1;\n\n      const newActions: React.ReactNode[] = [];\n\n      if (!isLast && hasMultiple) {\n        newActions.push(\n          <TextButton onClick={() => dismiss(index)}>\n            {dismissButtonLabel}\n          </TextButton>,\n        );\n      }\n\n      if (!isFirst && hasMultiple) {\n        newActions.push(\n          <TextButton onClick={() => back(index)}>\n            {backButtonLabel}\n          </TextButton>,\n        );\n      }\n\n      if (isLast) {\n        newActions.push(\n          <OutlinedButton onClick={() => close(index)}>\n            {closeButtonLabel}\n          </OutlinedButton>,\n        );\n      } else {\n        newActions.push(\n          <OutlinedButton onClick={() => next(index)}>\n            {nextButtonLabel}\n          </OutlinedButton>,\n        );\n      }\n\n      return newActions;\n    },\n    [\n      spotlights,\n      dismissButtonLabel,\n      dismiss,\n      backButtonLabel,\n      back,\n      closeButtonLabel,\n      close,\n      nextButtonLabel,\n      next,\n    ],\n  );\n\n  const getBeforeActions: (\n    index: number,\n    beforeActions: React.ReactNode,\n  ) => React.ReactNode = useCallback(\n    (index: number, beforeActions: React.ReactNode) => {\n      if (beforeActions !== undefined && beforeActions !== null) {\n        return beforeActions;\n      }\n\n      return spotlights.length > 1 ? `${index + 1}/${spotlights.length}` : null;\n    },\n    [spotlights],\n  );\n\n  const getOnClose: (\n    index: number,\n    onClose: ((reason: SpotlightCloseAction) => void) | undefined,\n  ) => ((reason: SpotlightCloseAction) => void) | undefined = useCallback(\n    (\n      index: number,\n      onClose: ((reason: SpotlightCloseAction) => void) | undefined,\n    ) => {\n      if (onClose) {\n        return onClose;\n      }\n\n      return (reason) => {\n        onAction?.(spotlights[index].target, reason);\n      };\n    },\n    [onAction, spotlights],\n  );\n\n  const getOnOpen: (\n    index: number,\n    onOpen: (() => void) | undefined,\n  ) => (() => void) | undefined = useCallback(\n    (index: number, onOpen: (() => void) | undefined) => {\n      if (onOpen) {\n        return onOpen;\n      }\n\n      return () => {\n        onAction?.(spotlights[index].target, 'open');\n      };\n    },\n    [onAction, spotlights],\n  );\n\n  return (\n    <>\n      {spotlights.map((spotlight, index) => {\n        const {\n          actions,\n          beforeActions,\n          initialFocus,\n          onClose,\n          onOpen,\n          ...restSpotlight\n        } = spotlight;\n        const newActions = getActions(index, actions);\n        const newBeforeActions = getBeforeActions(index, beforeActions);\n        const newOnClose = getOnClose(index, onClose);\n        const newOnOpen = getOnOpen(index, onOpen);\n\n        return (\n          <Spotlight\n            key={index}\n            {...restSpotlight}\n            actions={newActions}\n            beforeActions={newBeforeActions}\n            initialFocus={initialFocus}\n            onClose={newOnClose}\n            onOpen={newOnOpen}\n          />\n        );\n      })}\n    </>\n  );\n};\n"],"mappings":";;;;;;;;;;CA6MU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