@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
122 lines (121 loc) • 5.67 kB
JavaScript
/**
*
* 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 { OutlinedButton } from '../outlined-button';
import { TextButton } from '../text-button';
import { Spotlight } from './Spotlight';
import { useSpotlightContext } from './SpotlightProvider';
export const SpotlightTour = ({ spotlights, dismissButtonLabel = 'Dismiss', backButtonLabel = 'Back', nextButtonLabel = 'Next', closeButtonLabel = 'Close', onAction, }) => {
const { setActiveSpotlight, setIsOpen } = useSpotlightContext();
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((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(_jsx(TextButton, { onClick: () => dismiss(index), children: dismissButtonLabel }));
}
if (!isFirst && hasMultiple) {
newActions.push(_jsx(TextButton, { onClick: () => back(index), children: backButtonLabel }));
}
if (isLast) {
newActions.push(_jsx(OutlinedButton, { onClick: () => close(index), children: closeButtonLabel }));
}
else {
newActions.push(_jsx(OutlinedButton, { onClick: () => next(index), children: nextButtonLabel }));
}
return newActions;
}, [
spotlights,
dismissButtonLabel,
dismiss,
backButtonLabel,
back,
closeButtonLabel,
close,
nextButtonLabel,
next,
]);
const getBeforeActions = useCallback((index, beforeActions) => {
if (beforeActions !== undefined && beforeActions !== null) {
return beforeActions;
}
return spotlights.length > 1 ? `${index + 1}/${spotlights.length}` : null;
}, [spotlights]);
const getOnClose = 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 = useCallback((index, onOpen) => {
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(index, actions);
const newBeforeActions = getBeforeActions(index, beforeActions);
const newOnClose = getOnClose(index, onClose);
const newOnOpen = getOnOpen(index, onOpen);
return (_jsx(Spotlight, Object.assign({}, restSpotlight, { actions: newActions, beforeActions: newBeforeActions, initialFocus: initialFocus, onClose: newOnClose, onOpen: newOnOpen }), index));
}) }));
};
//# sourceMappingURL=SpotlightTour.js.map