Merge branch 'IbrahimCSAE-feat/interactive-onboarding-guides'

This commit is contained in:
Ibrahim committed 2024-10-04 12:53:33 -04:00
commit 579a592812
20 files changed
+846 -4

No files matched your search

@@ -0,0 +1,48 @@
.shepherd-header {
@apply !bg-popover !w-[100%] !p-0;
}
.shepherd-title {
@apply !text-highlight !w-[100%] !break-words !text-lg !leading-[1.5];
}
.shepherd-content {
@apply flex flex-col gap-[8px] p-[12px];
}
.shepherd-element {
@apply !bg-popover !max-w-[260px];
}
.shepherd-text {
@apply text-foreground !w-[100%] p-0 text-base leading-normal;
}
.shepherd-footer {
@apply !w-[100%] p-0;
}
.shepherd-button {
@apply !inline-flex !h-[36px] !min-w-[62px] !flex-row !items-center !justify-center !gap-[5px] !whitespace-nowrap !rounded !bg-[#348cfd] !px-[10px] !text-center !font-sans !text-[14px] !leading-[1.2] !text-white !outline-none !transition !duration-300 !ease-in-out focus:!outline-none;
}
.shepherd-button.shepherd-button-secondary {
@apply !bg-transparent !text-[#348cfd];
}
.shepherd-arrow::before {
@apply !bg-popover !h-[30px] !w-[30px];
}
.shepherd-element[data-popper-placement^='left'] > .shepherd-arrow {
right: 3px !important;
top: 6px !important;
}
.shepherd-element[data-popper-placement^='top'] > .shepherd-arrow {
bottom: 2px !important;
}
.shepherd-modal-overlay-container.shepherd-modal-is-visible {
@apply !opacity-70;
}
@@ -0,0 +1,57 @@
import { useEffect } from 'react';
import { useShepherd } from 'react-shepherd';
import { StepOptions, TourOptions } from 'shepherd.js';
import { useLocation } from 'react-router';
import 'shepherd.js/dist/css/shepherd.css';
import './Onboarding.css';
import { hasTourBeenShown, markTourAsShown, defaultShowHandler, middleware } from './utilities';
const Onboarding = () => {
const Shepherd = useShepherd();
const location = useLocation();
const tours = window.config.tours as Array<{
id: string;
route: string;
tourOptions: TourOptions;
steps: StepOptions[];
}>;
/**
* Show the tour if it hasn't been shown yet based on the current route.
* Constructs a tour instance and adds steps to it based on the matching tour.
*/
useEffect(() => {
if (!tours) {
return;
}
const matchingTour = tours.find(tour => tour.route === location.pathname);
if (!matchingTour || hasTourBeenShown(matchingTour.id)) {
return;
}
const tourInstance = new Shepherd.Tour({
...matchingTour.tourOptions,
defaultStepOptions: {
...matchingTour.tourOptions?.defaultStepOptions,
floatingUIOptions: matchingTour.tourOptions?.defaultStepOptions?.floatingUIOptions || {
middleware,
},
when: {
...matchingTour.tourOptions?.defaultStepOptions?.when,
show:
matchingTour.tourOptions?.defaultStepOptions?.when?.show ||
(() => defaultShowHandler(Shepherd)),
},
},
});
matchingTour.steps.forEach(step => tourInstance.addStep(step));
tourInstance.start();
markTourAsShown(matchingTour.id);
}, [Shepherd, tours, location.pathname]);
return null;
};
export { Onboarding };
@@ -0,0 +1,3 @@
import { Onboarding } from './Onboarding';
export { Onboarding };
@@ -0,0 +1,91 @@
import { ShepherdBase } from 'shepherd.js';
import { offset, flip, shift, detectOverflow } from '@floating-ui/dom';
/**
* Retrieves the list of tours that have been shown from localStorage.
* @returns {string[]} An array of tour IDs that have been shown.
*/
const getShownTours = () => JSON.parse(localStorage.getItem('shownTours')) || [];
/**
* Checks if a specific tour has been shown.
* @param {string} tourId - The ID of the tour to check.
* @returns {boolean} True if the tour has been shown, false otherwise.
*/
const hasTourBeenShown = (tourId: string) => getShownTours().includes(tourId);
/**
* Marks a specific tour as shown by adding it to localStorage.
* @param {string} tourId - The ID of the tour to mark as shown.
* @returns {void}
*/
const markTourAsShown = (tourId: string) => {
const shownTours = getShownTours();
if (!shownTours.includes(tourId)) {
shownTours.push(tourId);
localStorage.setItem('shownTours', JSON.stringify(shownTours));
}
};
/**
* Default handler for the 'show' event in Shepherd steps.
* Adds a progress indicator to the footer of the current step.
*
* @param {ShepherdBase} Shepherd - The Shepherd.js instance.
* @returns {void}
*/
const defaultShowHandler = (Shepherd: ShepherdBase) => {
const currentStep = Shepherd.activeTour?.getCurrentStep();
if (currentStep) {
const progress = document.createElement('span');
progress.className = 'shepherd-progress text-base text-muted-foreground';
progress.innerText = `${Shepherd.activeTour?.steps.indexOf(currentStep) + 1}/${Shepherd.activeTour?.steps.length}`;
progress.style.position = 'absolute';
progress.style.left = '13px';
progress.style.bottom = '20px';
progress.style.zIndex = '1';
const footer = currentStep?.getElement()?.querySelector('.shepherd-footer');
footer?.appendChild(progress);
}
};
/**
* Custom middleware for adjusting Shepherd step positioning when overflowing.
*
* @type {object}
* @property {string} name - The name of the middleware.
* @property {function} fn - The function that adjusts the position of the step when overflowing.
*/
const customMiddleware = {
name: 'customOverflowMiddleware',
async fn(state) {
const overflow = await detectOverflow(state, {
boundary: document.querySelector('body'),
padding: 24,
});
const xAdjustment =
overflow.left > 0 ? overflow.left : overflow.right > 0 ? -overflow.right : 0;
const yAdjustment =
overflow.top > 0 ? overflow.top : overflow.bottom > 0 ? -overflow.bottom : 0;
return {
x: state.x + xAdjustment,
y: state.y + yAdjustment,
};
},
};
/**
* Default Floating UI middleware for positioning steps in Shepherd.js.
* Includes offset, shift, flip, and custom overflow middleware.
*
* @type {Array<object>}
*/
const middleware = [offset(15), shift(), flip(), customMiddleware];
export { hasTourBeenShown, markTourAsShown, middleware, defaultShowHandler };
+2
View File
@@ -67,6 +67,7 @@ import {
DropdownMenuSubTrigger,
DropdownMenuRadioGroup,
} from './DropdownMenu';
import { Onboarding } from './Onboarding';
export {
Button,
@@ -145,4 +146,5 @@ export {
DropdownMenuSubContent,
DropdownMenuSubTrigger,
DropdownMenuRadioGroup,
Onboarding,
};