UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

126 lines (125 loc) 6.13 kB
/** * * Copyright (c) "Neo4j" * Neo4j Sweden AB [http://neo4j.com] * * This file is part of Neo4j. * * Neo4j is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ "use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SpotlightTour = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = require("react"); const outlined_button_1 = require("../outlined-button"); const text_button_1 = require("../text-button"); const Spotlight_1 = require("./Spotlight"); const SpotlightProvider_1 = require("./SpotlightProvider"); const SpotlightTour = ({ spotlights, dismissButtonLabel = 'Dismiss', backButtonLabel = 'Back', nextButtonLabel = 'Next', closeButtonLabel = 'Close', onAction, }) => { const { setActiveSpotlight, setIsOpen } = (0, SpotlightProvider_1.useSpotlightContext)(); const next = (0, react_1.useCallback)((index) => { onAction === null || onAction === void 0 ? void 0 : onAction(spotlights[index].target, 'next'); setActiveSpotlight(spotlights[index + 1].target); }, [onAction, spotlights, setActiveSpotlight]); const back = (0, react_1.useCallback)((index) => { onAction === null || onAction === void 0 ? void 0 : onAction(spotlights[index].target, 'back'); setActiveSpotlight(spotlights[index - 1].target); }, [onAction, spotlights, setActiveSpotlight]); const dismiss = (0, react_1.useCallback)((index) => { onAction === null || onAction === void 0 ? void 0 : onAction(spotlights[index].target, 'dismiss'); setActiveSpotlight(null); setIsOpen(false); }, [onAction, spotlights, setActiveSpotlight, setIsOpen]); const close = (0, react_1.useCallback)((index) => { onAction === null || onAction === void 0 ? void 0 : onAction(spotlights[index].target, 'close'); setActiveSpotlight(null); setIsOpen(false); }, [onAction, spotlights, setActiveSpotlight, setIsOpen]); const getActions = (0, react_1.useCallback)((index, actions) => { if (actions !== undefined && actions !== null) { return actions; } const isFirst = index === 0; const isLast = index === spotlights.length - 1; const hasMultiple = spotlights.length > 1; const newActions = []; if (!isLast && hasMultiple) { newActions.push((0, jsx_runtime_1.jsx)(text_button_1.TextButton, { onClick: () => dismiss(index), children: dismissButtonLabel })); } if (!isFirst && hasMultiple) { newActions.push((0, jsx_runtime_1.jsx)(text_button_1.TextButton, { onClick: () => back(index), children: backButtonLabel })); } if (isLast) { newActions.push((0, jsx_runtime_1.jsx)(outlined_button_1.OutlinedButton, { onClick: () => close(index), children: closeButtonLabel })); } else { newActions.push((0, jsx_runtime_1.jsx)(outlined_button_1.OutlinedButton, { onClick: () => next(index), children: nextButtonLabel })); } return newActions; }, [ spotlights, dismissButtonLabel, dismiss, backButtonLabel, back, closeButtonLabel, close, nextButtonLabel, next, ]); const getBeforeActions = (0, react_1.useCallback)((index, beforeActions) => { if (beforeActions !== undefined && beforeActions !== null) { return beforeActions; } return spotlights.length > 1 ? `${index + 1}/${spotlights.length}` : null; }, [spotlights]); const getOnClose = (0, react_1.useCallback)((index, onClose) => { if (onClose) { return onClose; } return (reason) => { onAction === null || onAction === void 0 ? void 0 : onAction(spotlights[index].target, reason); }; }, [onAction, spotlights]); const getOnOpen = (0, react_1.useCallback)((index, onOpen) => { if (onOpen) { return onOpen; } return () => { onAction === null || onAction === void 0 ? void 0 : onAction(spotlights[index].target, 'open'); }; }, [onAction, spotlights]); return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: spotlights.map((spotlight, index) => { const { actions, beforeActions, initialFocus, onClose, onOpen } = spotlight, restSpotlight = __rest(spotlight, ["actions", "beforeActions", "initialFocus", "onClose", "onOpen"]); const newActions = getActions(index, actions); const newBeforeActions = getBeforeActions(index, beforeActions); const newOnClose = getOnClose(index, onClose); const newOnOpen = getOnOpen(index, onOpen); return ((0, jsx_runtime_1.jsx)(Spotlight_1.Spotlight, Object.assign({}, restSpotlight, { actions: newActions, beforeActions: newBeforeActions, initialFocus: initialFocus, onClose: newOnClose, onOpen: newOnOpen }), index)); }) })); }; exports.SpotlightTour = SpotlightTour; //# sourceMappingURL=SpotlightTour.js.map