UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

144 lines (143 loc) 6.07 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/>. */ 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; }; import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime"; import { useCallback } from 'react'; import { Spotlight } from './Spotlight'; import { useSpotlightContext } from './SpotlightProvider'; export const SpotlightTour = (props) => { const { setActiveSpotlight, setIsOpen } = useSpotlightContext(); const { spotlights, dismissButtonText = 'Dismiss', backButtonText = 'Back', nextButtonText = 'Next', closeButtonText = 'Close', onAction, } = props; const next = useCallback((index) => { onAction === null || onAction === void 0 ? void 0 : onAction(spotlights[index].target, 'next'); setActiveSpotlight(spotlights[index + 1].target); }, [onAction, spotlights, setActiveSpotlight]); const back = useCallback((index) => { onAction === null || onAction === void 0 ? void 0 : onAction(spotlights[index].target, 'back'); setActiveSpotlight(spotlights[index - 1].target); }, [onAction, spotlights, setActiveSpotlight]); const dismiss = useCallback((index) => { onAction === null || onAction === void 0 ? void 0 : onAction(spotlights[index].target, 'dismiss'); setActiveSpotlight(null); setIsOpen(false); }, [onAction, spotlights, setActiveSpotlight, setIsOpen]); const close = useCallback((index) => { onAction === null || onAction === void 0 ? void 0 : onAction(spotlights[index].target, 'close'); setActiveSpotlight(null); setIsOpen(false); }, [onAction, spotlights, setActiveSpotlight, setIsOpen]); const getActions = useCallback((actions, index) => { if (actions === null || actions === void 0 ? void 0 : actions.length) { return actions; } const isFirst = index === 0; const isLast = index === spotlights.length - 1; const hasMultiple = spotlights.length > 1; const newActions = []; if (!isLast && hasMultiple) { newActions.push({ label: dismissButtonText, fill: 'text', onClick: () => dismiss(index), }); } if (!isFirst && hasMultiple) { newActions.push({ label: backButtonText, fill: 'text', onClick: () => back(index), }); } if (isLast) { newActions.push({ label: closeButtonText, fill: 'outlined', onClick: () => close(index), }); } else { newActions.push({ label: nextButtonText, fill: 'outlined', onClick: () => next(index), }); } return newActions; }, [ spotlights, dismissButtonText, dismiss, backButtonText, back, closeButtonText, close, nextButtonText, next, ]); const getBeforeActions = useCallback((beforeActions, index) => { if (beforeActions) { return beforeActions; } return spotlights.length > 1 ? index + 1 + '/' + spotlights.length : null; }, [spotlights]); const getInitialFocus = useCallback((initialFocus, actions) => { if (initialFocus !== undefined) { return initialFocus; } return actions.length - 1; }, []); const getOnClose = useCallback((onClose, index) => { if (onClose) { return onClose; } return (reason) => { onAction === null || onAction === void 0 ? void 0 : onAction(spotlights[index].target, reason); }; }, [onAction, spotlights]); const getOnOpen = useCallback((onOpen, index) => { if (onOpen) { return onOpen; } return () => { onAction === null || onAction === void 0 ? void 0 : onAction(spotlights[index].target, 'open'); }; }, [onAction, spotlights]); return (_jsx(_Fragment, { children: spotlights.map((spotlight, index) => { const { actions, beforeActions, initialFocus, onClose, onOpen } = spotlight, restSpotlight = __rest(spotlight, ["actions", "beforeActions", "initialFocus", "onClose", "onOpen"]); const newActions = getActions(actions, index); const newBeforeActions = getBeforeActions(beforeActions, index); const newInitialFocus = getInitialFocus(initialFocus, newActions); const newOnClose = getOnClose(onClose, index); const newOnOpen = getOnOpen(onOpen, index); return (_jsx(Spotlight, Object.assign({}, restSpotlight, { actions: newActions, beforeActions: newBeforeActions, initialFocus: newInitialFocus, onClose: newOnClose, onOpen: newOnOpen }), index)); }) })); }; //# sourceMappingURL=SpotlightTour.js.map