Repository: kamranahmedse/driver.js Branch: master Commit: 4e4d07d481ae Files: 67 Total size: 224.4 KB Directory structure: gitextract_tn0sokzd/ ├── .github/ │ └── FUNDING.yml ├── .gitignore ├── .prettierignore ├── .prettierrc ├── docs/ │ ├── .gitignore │ ├── astro.config.mjs │ ├── package.json │ ├── src/ │ │ ├── components/ │ │ │ ├── Analytics/ │ │ │ │ ├── Analytics.astro │ │ │ │ └── analytics.ts │ │ │ ├── CodeSample.tsx │ │ │ ├── Container.astro │ │ │ ├── DocsHeader.tsx │ │ │ ├── ExampleButton.tsx │ │ │ ├── Examples.astro │ │ │ ├── FeatureMarquee.tsx │ │ │ ├── Features.astro │ │ │ ├── FormHelp.tsx │ │ │ ├── HeroSection.astro │ │ │ ├── OpenSourceLove.astro │ │ │ ├── Sidebar.astro │ │ │ ├── UsecaseItem.astro │ │ │ └── UsecaseList.astro │ │ ├── content/ │ │ │ ├── config.ts │ │ │ └── guides/ │ │ │ ├── animated-tour.mdx │ │ │ ├── api.mdx │ │ │ ├── async-tour.mdx │ │ │ ├── basic-usage.mdx │ │ │ ├── buttons.mdx │ │ │ ├── configuration.mdx │ │ │ ├── confirm-on-exit.mdx │ │ │ ├── installation.mdx │ │ │ ├── migrating-from-0x.mdx │ │ │ ├── popover-position.mdx │ │ │ ├── prevent-destroy.mdx │ │ │ ├── simple-highlight.mdx │ │ │ ├── static-tour.mdx │ │ │ ├── styling-overlay.mdx │ │ │ ├── styling-popover.mdx │ │ │ ├── theming.mdx │ │ │ └── tour-progress.mdx │ │ ├── env.d.ts │ │ ├── layouts/ │ │ │ ├── BaseLayout.astro │ │ │ └── DocsLayout.astro │ │ ├── lib/ │ │ │ ├── github.ts │ │ │ └── guide.ts │ │ └── pages/ │ │ ├── docs/ │ │ │ └── [guideId].astro │ │ └── index.astro │ ├── tailwind.config.cjs │ └── tsconfig.json ├── dts-bundle-generator.config.ts ├── index.html ├── license ├── package.json ├── readme.md ├── src/ │ ├── config.ts │ ├── driver.css │ ├── driver.ts │ ├── emitter.ts │ ├── events.ts │ ├── highlight.ts │ ├── overlay.ts │ ├── popover.ts │ ├── state.ts │ └── utils.ts ├── tests/ │ └── sum.test.ts ├── tsconfig.json └── vite.config.ts ================================================ FILE CONTENTS ================================================ ================================================ FILE: .github/FUNDING.yml ================================================ # These are supported funding model platforms github: [kamranahmedse] ================================================ FILE: .gitignore ================================================ # Logs logs *.log npm-debug.log* yarn-debug.log* yarn-error.log* pnpm-debug.log* lerna-debug.log* node_modules dist dist-ssr *.local coverage # Editor directories and files .vscode/* .history/* !.vscode/extensions.json .idea .DS_Store *.suo *.ntvs* *.njsproj *.sln *.sw? ================================================ FILE: .prettierignore ================================================ .history .vscode coverage dist node_modules ================================================ FILE: .prettierrc ================================================ { "printWidth": 120, "tabWidth": 2, "singleQuote": false, "trailingComma": "es5", "arrowParens": "avoid", "bracketSpacing": true, "useTabs": false, "endOfLine": "auto", "singleAttributePerLine": false, "bracketSameLine": false, "jsxSingleQuote": false, "quoteProps": "as-needed", "semi": true } ================================================ FILE: docs/.gitignore ================================================ # build output dist/ # generated types .astro/ # dependencies node_modules/ # logs npm-debug.log* yarn-debug.log* yarn-error.log* pnpm-debug.log* # environment variables .env .env.production # macOS-specific files .DS_Store ================================================ FILE: docs/astro.config.mjs ================================================ import { defineConfig } from "astro/config"; import tailwind from "@astrojs/tailwind"; import react from "@astrojs/react"; import mdx from "@astrojs/mdx"; import compress from "astro-compress"; // https://astro.build/config export default defineConfig({ build: { format: "file", }, markdown: { shikiConfig: { // theme: "material-theme" theme: "monokai", // theme: 'poimandres' }, }, integrations: [ tailwind(), react(), mdx(), compress({ CSS: false, JS: false, }), ], }); ================================================ FILE: docs/package.json ================================================ { "name": "driver-docs", "type": "module", "version": "0.0.1", "scripts": { "dev": "astro dev", "start": "astro dev", "build": "astro build", "preview": "astro preview", "astro": "astro" }, "dependencies": { "@astrojs/mdx": "^1.0.0", "@astrojs/react": "^3.0.0", "@astrojs/tailwind": "^5.0.0", "@types/react": "^18.2.21", "@types/react-dom": "^18.2.7", "astro": "^3.0.8", "astro-compress": "^2.0.15", "driver.js": "1.3.0", "react": "^18.2.0", "react-dom": "^18.2.0", "react-fast-marquee": "^1.6.0", "tailwindcss": "^3.3.3" }, "devDependencies": { "@tailwindcss/typography": "^0.5.9" } } ================================================ FILE: docs/src/components/Analytics/Analytics.astro ================================================ --- --- ================================================ FILE: docs/src/components/Analytics/analytics.ts ================================================ declare global { interface Window { gtag: any; fireEvent: (props: { action: string; category: string; label?: string; value?: string; }) => void; } } /** * Tracks the event on google analytics * @see https://developers.google.com/analytics/devguides/collection/gtagjs/events * @param props Event properties * @returns void */ window.fireEvent = (props) => { const { action, category, label, value } = props; if (!window.gtag) { console.warn('Missing GTAG - Analytics disabled'); return; } if (import.meta.env.DEV) { console.log('Analytics event fired', props); } window.gtag('event', action, { event_category: category, event_label: label, value: value, }); }; ================================================ FILE: docs/src/components/CodeSample.tsx ================================================ import type { Config, DriveStep, PopoverDOM } from "driver.js"; import { driver } from "driver.js"; import "driver.js/dist/driver.css"; type CodeSampleProps = { heading?: string; config?: Config; highlight?: DriveStep; tour?: DriveStep[]; id?: string; className?: string; children?: any; buttonText?: string; }; export function removeDummyElement() { const el = document.querySelector(".dynamic-el"); if (el) { el.remove(); } } export function mountDummyElement() { const newDiv = (document.querySelector(".dynamic-el") || document.createElement("div")) as HTMLElement; newDiv.innerHTML = "This is a new Element"; newDiv.style.display = "block"; newDiv.style.padding = "20px"; newDiv.style.backgroundColor = "black"; newDiv.style.color = "white"; newDiv.style.fontSize = "14px"; newDiv.style.position = "fixed"; newDiv.style.top = `${Math.random() * (500 - 30) + 30}px`; newDiv.style.left = `${Math.random() * (500 - 30) + 30}px`; newDiv.className = "dynamic-el"; document.body.appendChild(newDiv); } function attachFirstButton(popover: PopoverDOM) { const firstButton = document.createElement("button"); firstButton.innerText = "Go to First"; popover.footerButtons.appendChild(firstButton); firstButton.addEventListener("click", () => { window.driverObj.drive(0); }); } export function CodeSample(props: CodeSampleProps) { const { heading, id, children, buttonText = "Show me an Example", className, config, highlight, tour } = props; if (id === "demo-hook-theme") { config!.onPopoverRender = attachFirstButton; } function onClick() { if (highlight) { const driverObj = driver({ ...config, }); window.driverObj = driverObj; driverObj.highlight(highlight); } else if (tour) { if (id === "confirm-destroy") { config!.onDestroyStarted = () => { if (!driverObj.hasNextStep() || confirm("Are you sure?")) { driverObj.destroy(); } }; } if (id === "logger-events") { config!.onNextClick = () => { console.log("next clicked"); }; config!.onNextClick = () => { console.log("Next Button Clicked"); // Implement your own functionality here driverObj.moveNext(); }; config!.onPrevClick = () => { console.log("Previous Button Clicked"); // Implement your own functionality here driverObj.movePrevious(); }; config!.onCloseClick = () => { console.log("Close Button Clicked"); // Implement your own functionality here driverObj.destroy(); }; } if (tour?.[2]?.popover?.title === "Next Step is Async") { tour[2].popover.onNextClick = () => { mountDummyElement(); driverObj.moveNext(); }; if (tour?.[3]?.element === ".dynamic-el") { tour[3].onDeselected = () => { removeDummyElement(); }; // @ts-ignore tour[4].popover.onPrevClick = () => { mountDummyElement(); driverObj.movePrevious(); }; // @ts-ignore tour[3].popover.onPrevClick = () => { removeDummyElement(); driverObj.movePrevious(); }; } } const driverObj = driver({ ...config, steps: tour, }); window.driverObj = driverObj; driverObj.drive(); } } return (
{heading &&

{heading}

} {children &&
{children}
}
); } ================================================ FILE: docs/src/components/Container.astro ================================================
================================================ FILE: docs/src/components/DocsHeader.tsx ================================================ import { useState } from "react"; import type { CollectionEntry } from "astro:content"; type DocsHeaderProps = { groupedGuides: Record[]>; activeGuideTitle: string; }; export function DocsHeader(props: DocsHeaderProps) { const { groupedGuides, activeGuideTitle } = props; const [isActive, setIsActive] = useState(false); return ( <>
Astro driver.js
{Object.entries(groupedGuides).map(([category, guides]) => (
{category}
{guides.map(guide => ( {guide.data.title} ))}
))}
); } ================================================ FILE: docs/src/components/ExampleButton.tsx ================================================ type ExampleButtonProps = { id: string; onClick: () => void; text: string; }; export function ExampleButton(props: ExampleButtonProps) { const { id, onClick, text } = props; return ( ); } ================================================ FILE: docs/src/components/Examples.astro ================================================ --- import { ExampleButton } from "./ExampleButton"; ---

Examples

Here are just a few examples; find more in the documentation.

and much more ...
================================================ FILE: docs/src/components/FeatureMarquee.tsx ================================================ import React from "react"; import Marquee from "react-fast-marquee"; const featureList = [ "Supports all Major Browsers", "Works on Mobile Devices", "Highly Customizable", "Light-weight", "No dependencies", "Feature Rich", "MIT Licensed", "Written in TypeScript", "Vanilla JavaScript", "Easy to use", "Accessible", "Frameworks Ready", ]; export function FeatureMarquee() { return (

{ featureList.map((featureItem, index) => ( { featureItem }· ))}

); } ================================================ FILE: docs/src/components/Features.astro ================================================ --- import { Earth, Smartphone, Settings, Feather, Code2, Layers, Keyboard } from "lucide-react"; import Container from "./Container.astro"; ---

Nothing else like it

Lightweight with no external dependencies, supports all major browsers and is highly customizable.

Browser Support

Works in all modern browsers including Chrome, IE9+, Safari, Firefox and Opera

Mobile Ready

Works on desktop, tablets and mobile devices

Highly Customizable

Powerful API that allows you to customize it to your needs

Lightweight

Only 5KB minified, compared to other libraries which are typically >12KB minified

No Dependencies

Simple to use with absolutely no external dependencies

Feature Rich

Create powerful feature introductions for your web applications

MIT

MIT License

Free for both personal and commercial use

Keyboard Control

All actions can be controlled via keyboard

ALL

Highlight Anything

Highlight any element on the page

================================================ FILE: docs/src/components/FormHelp.tsx ================================================ import { useEffect } from "react"; import { driver } from "driver.js"; import "driver.js/dist/driver.css"; export function FormHelp() { useEffect(() => { const driverObj = driver({ popoverClass: "driverjs-theme", stagePadding: 0, onDestroyed: () => { (document?.activeElement as any)?.blur(); } }); const nameEl = document.getElementById("name"); const educationEl = document.getElementById("education"); const ageEl = document.getElementById("age"); const addressEl = document.getElementById("address"); const submitEl = document.getElementById("submit-btn"); nameEl!.addEventListener("focus", () => { driverObj.highlight({ element: nameEl!, popover: { title: "Name", description: "Enter your name here", }, }); }); educationEl!.addEventListener("focus", () => { driverObj.highlight({ element: educationEl!, popover: { title: "Education", description: "Enter your education here", }, }); }); ageEl!.addEventListener("focus", () => { driverObj.highlight({ element: ageEl!, popover: { title: "Age", description: "Enter your age here", }, }); }); addressEl!.addEventListener("focus", () => { driverObj.highlight({ element: addressEl!, popover: { title: "Address", description: "Enter your address here", }, }); }); submitEl!.addEventListener("focus", (e) => { e.preventDefault(); driverObj.destroy(); }); }); return <>; } ================================================ FILE: docs/src/components/HeroSection.astro ================================================ --- import Container from "./Container.astro"; ---

driver.js

Lightweight JavaScript library for product tours, highlights, and contextual help to guide users through your product.

Get Started
================================================ FILE: docs/src/components/OpenSourceLove.astro ================================================ --- import Container from "./Container.astro"; import { getFormattedStars } from "../lib/github"; const starCount = getFormattedStars('kamranahmedse/driver.js'); ---

Loved by Many

With millions of downloads, Driver.js is an MIT licensed opensource project and is used by thousands of companies around the world.

================================================ FILE: docs/src/components/Sidebar.astro ================================================ --- import { getCollection, getEntry } from "astro:content"; import { getFormattedStars } from "../lib/github"; import { getAllGuides } from "../lib/guide"; export interface Props { activePath: string; } const { activePath, groupedGuides, activeGuideTitle } = Astro.props; --- ================================================ FILE: docs/src/components/UsecaseItem.astro ================================================ --- export interface Props { title: string; description: string; } const { title, description } = Astro.props; ---

{ title }

{ description }

================================================ FILE: docs/src/components/UsecaseList.astro ================================================ --- import UsecaseItem from "./UsecaseItem.astro"; ---

Due to its extensive API, driver.js can be used for a wide range of use cases.

================================================ FILE: docs/src/content/config.ts ================================================ import { z, defineCollection } from "astro:content"; const guidesCollection = defineCollection({ type: "content", schema: z.object({ groupTitle: z.string(), title: z.string(), sort: z.number(), }), }); export const collections = { guides: guidesCollection, }; ================================================ FILE: docs/src/content/guides/animated-tour.mdx ================================================ --- title: "Animated Tour" groupTitle: "Examples" sort: 2 --- import { CodeSample } from "../../components/CodeSample.tsx"; The following example shows how to create a simple tour with a few steps. Click the button below the code sample to see the tour in action. ================================================ FILE: docs/src/content/guides/api.mdx ================================================ --- title: "API Reference" groupTitle: "Introduction" sort: 4 --- Here is the list of methods provided by `driver` when you initialize it. > **Note:** We have omitted the configuration options for brevity. Please look at the configuration section for the options. Links are provided in the description below. ```javascript import { driver } from "driver.js"; import "driver.js/dist/driver.css"; // Look at the configuration section for the options // https://driverjs.com/docs/configuration#driver-configuration const driverObj = driver({ /* ... */ }); // -------------------------------------------------- // driverObj is an object with the following methods // -------------------------------------------------- // Start the tour using `steps` given in the configuration driverObj.drive(); // Starts at step 0 driverObj.drive(4); // Starts at step 4 driverObj.moveNext(); // Move to the next step driverObj.movePrevious(); // Move to the previous step driverObj.moveTo(4); // Move to the step 4 driverObj.hasNextStep(); // Is there a next step driverObj.hasPreviousStep() // Is there a previous step driverObj.isFirstStep(); // Is the current step the first step driverObj.isLastStep(); // Is the current step the last step driverObj.getActiveIndex(); // Gets the active step index driverObj.getActiveStep(); // Gets the active step configuration driverObj.getPreviousStep(); // Gets the previous step configuration driverObj.getActiveElement(); // Gets the active HTML element driverObj.getPreviousElement(); // Gets the previous HTML element // Is the tour or highlight currently active driverObj.isActive(); // Recalculate and redraw the highlight driverObj.refresh(); // Look at the configuration section for configuration options // https://driverjs.com/docs/configuration#driver-configuration driverObj.getConfig(); driverObj.setConfig({ /* ... */ }); driverObj.setSteps([ /* ... */ ]); // Set the steps // Look at the state section of configuration for format of the state // https://driverjs.com/docs/configuration#state driverObj.getState(); // Look at the DriveStep section of configuration for format of the step // https://driverjs.com/docs/configuration/#drive-step-configuration driverObj.highlight({ /* ... */ }); // Highlight an element driverObj.destroy(); // Destroy the tour ``` ================================================ FILE: docs/src/content/guides/async-tour.mdx ================================================ --- title: "Async Tour" groupTitle: "Examples" sort: 3 --- import { CodeSample } from "../../components/CodeSample.tsx"; You can also have async steps in your tour. This is useful when you want to load some data from the server and then show the tour. ```js import { driver } from "driver.js"; import "driver.js/dist/driver.css"; const driverObj = driver({ showProgress: true, steps: [ { popover: { title: 'First Step', description: 'This is the first step. Next element will be loaded dynamically.' // By passing onNextClick, you can override the default behavior of the next button. // This will prevent the driver from moving to the next step automatically. // You can then manually call driverObj.moveNext() to move to the next step. onNextClick: () => { // .. load element dynamically // .. and then call driverObj.moveNext(); }, }, }, { element: '.dynamic-el', popover: { title: 'Async Element', description: 'This element is loaded dynamically.' }, // onDeselected is called when the element is deselected. // Here we are simply removing the element from the DOM. onDeselected: () => { // .. remove element document.querySelector(".dynamic-el")?.remove(); } }, { popover: { title: 'Last Step', description: 'This is the last step.' } } ] }); driverObj.drive(); ``` > **Note**: By overriding `onNextClick`, and `onPrevClick` hooks you control the navigation of the driver. This means that user won't be able to navigate using the buttons and you will have to either call `driverObj.moveNext()` or `driverObj.movePrevious()` to navigate to the next/previous step. > > You can use this to implement custom logic for navigating between steps. This is also useful when you are dealing with dynamic content and want to highlight the next/previous element based on some logic. > > `onNextClick` and `onPrevClick` hooks can be configured at driver level as well as step level. When configured at the driver level, you control the navigation for all the steps. When configured at the step level, you control the navigation for that particular step only. ================================================ FILE: docs/src/content/guides/basic-usage.mdx ================================================ --- title: "Basic Usage" groupTitle: "Introduction" sort: 2 --- import { CodeSample } from "../../components/CodeSample.tsx"; Once installed, you can import and start using the library. There are several different configuration options available to customize the library. You can find more details about the options in the [configuration section](/docs/configuration). Given below are the basic steps to get started. Here is a simple example of how to create a tour with multiple steps. ```js import { driver } from "driver.js"; import "driver.js/dist/driver.css"; const driverObj = driver({ showProgress: true, steps: [ { element: '.page-header', popover: { title: 'Title', description: 'Description' } }, { element: '.top-nav', popover: { title: 'Title', description: 'Description' } }, { element: '.sidebar', popover: { title: 'Title', description: 'Description' } }, { element: '.footer', popover: { title: 'Title', description: 'Description' } }, ] }); driverObj.drive(); ``` You can pass a single step configuration to the `highlight` method to highlight a single element. Given below is a simple example of how to highlight a single element. ```js import { driver } from "driver.js"; import "driver.js/dist/driver.css"; const driverObj = driver(); driverObj.highlight({ element: '#some-element', popover: { title: 'Title for the Popover', description: 'Description for it', }, }); ``` The same configuration passed to the `highlight` method can be used to create a tour. Given below is a simple example of how to create a tour with a single step. Examples above show the basic usage of the library. Find more details about the configuration options in the [configuration section](/docs/configuration) and the examples in the [examples section](/docs/examples). ================================================ FILE: docs/src/content/guides/buttons.mdx ================================================ --- title: "Popover Buttons" groupTitle: "Examples" sort: 9 --- import { CodeSample } from "../../components/CodeSample.tsx"; You can use the `showButtons` option to choose which buttons to show in the popover. The default value is `['next', 'previous', 'close']`.
> **Note:** When using the `highlight` method to highlight a single element, the only button shown is the `close` button. However, you can use the `showButtons` option to show other buttons as well. But the buttons won't do anything. You will have to use the `onNextClick` and `onPrevClick` callbacks to implement the functionality.
```js import { driver } from "driver.js"; import "driver.js/dist/driver.css"; const driverObj = driver({ showButtons: [ 'next', 'previous', 'close' ], steps: [ { element: '#first-element', popover: { title: 'Popover Title', description: 'Popover Description' } }, { element: '#second-element', popover: { title: 'Popover Title', description: 'Popover Description' } } ] }); driverObj.drive(); ```
## Change Button Text You can also change the text of buttons using `nextBtnText`, `prevBtnText` and `doneBtnText` options.
', prevBtnText: '<--', doneBtnText: 'X', }} tour={[ { element: '#code-sample-3', popover: { title: 'Popover Title', description: 'Popover Description' } }, { element: '#code-sample-3 code', popover: { title: 'Popover Title', description: 'Popover Description' } } ]} id={"code-sample-3"} client:load> ```js import { driver } from "driver.js"; import "driver.js/dist/driver.css"; const driverObj = driver({ nextBtnText: '—›', prevBtnText: '‹—', doneBtnText: '✕', showProgress: true, steps: [ // ... ] }); driverObj.drive(); ```
## Event Handlers You can use the `onNextClick`, `onPreviousClick` and `onCloseClick` callbacks to implement custom functionality when the user clicks on the next and previous buttons. > Please note that when you configure these callbacks, the default functionality of the buttons will be disabled. You will have to implement the functionality yourself. ```js import { driver } from "driver.js"; import "driver.js/dist/driver.css"; const driverObj = driver({ onNextClick:() => { console.log('Next Button Clicked'); // Implement your own functionality here driverObj.moveNext(); }, onPrevClick:() => { console.log('Previous Button Clicked'); // Implement your own functionality here driverObj.movePrevious(); }, onCloseClick:() => { console.log('Close Button Clicked'); // Implement your own functionality here driverObj.destroy(); }, steps: [ // ... ] }); driverObj.drive(); ``` ## Custom Buttons You can add custom buttons using `onPopoverRender` callback. This callback is called before the popover is rendered. In the following example, we are adding a custom button that takes the user to the first step. ```js import { driver } from "driver.js"; import "driver.js/dist/driver.css"; const driverObj = driver({ // Get full control over the popover rendering. // Here we are adding a custom button that takes // user to the first step. onPopoverRender: (popover, { config, state }) => { const firstButton = document.createElement("button"); firstButton.innerText = "Go to First"; popover.footerButtons.appendChild(firstButton); firstButton.addEventListener("click", () => { driverObj.drive(0); }); }, steps: [ // .. ] }); driverObj.drive(); ``` ================================================ FILE: docs/src/content/guides/configuration.mdx ================================================ --- title: "Configuration" groupTitle: "Introduction" sort: 3 --- import { CodeSample } from "../../components/CodeSample.tsx"; Driver.js is built to be highly configurable. You can configure the driver globally, or per step. You can also configure the driver on the fly, while it's running. > Driver.js is written in TypeScript. Configuration options are mostly self-explanatory. Also, if you're using an IDE like WebStorm or VSCode, you'll get autocomplete and documentation for all the configuration options. ## Driver Configuration You can configure the driver globally by passing the configuration object to the `driver` call or by using the `setConfig` method. Given below are some of the available configuration options. ```typescript type Config = { // Array of steps to highlight. You should pass // this when you want to setup a product tour. steps?: DriveStep[]; // Whether to animate the product tour. (default: true) animate?: boolean; // Overlay color. (default: black) // This is useful when you have a dark background // and want to highlight elements with a light // background color. overlayColor?: string; // Whether to smooth scroll to the highlighted element. (default: false) smoothScroll?: boolean; // Whether to allow closing the popover by clicking on the backdrop. (default: true) allowClose?: boolean; // Opacity of the backdrop. (default: 0.5) overlayOpacity?: number; // What to do when the overlay backdrop is clicked. // Possible options are 'close', 'nextStep', or a custom function. (default: 'close') overlayClickBehavior?: "close" | "nextStep" | ((element?: Element, step: DriveStep, options: { config: Config; state: State, driver: Driver }) => void), // Distance between the highlighted element and the cutout. (default: 10) stagePadding?: number; // Radius of the cutout around the highlighted element. (default: 5) stageRadius?: number; // Whether to allow keyboard navigation. (default: true) allowKeyboardControl?: boolean; // Whether to disable interaction with the highlighted element. (default: false) // Can be configured at the step level as well disableActiveInteraction?: boolean; // If you want to add custom class to the popover popoverClass?: string; // Distance between the popover and the highlighted element. (default: 10) popoverOffset?: number; // Array of buttons to show in the popover. Defaults to ["next", "previous", "close"] // for product tours and [] for single element highlighting. showButtons?: AllowedButtons[]; // Array of buttons to disable. This is useful when you want to show some of the // buttons, but disable some of them. disableButtons?: AllowedButtons[]; // Whether to show the progress text in popover. (default: false) showProgress?: boolean; // Template for the progress text. You can use the following placeholders in the template: // - {{current}}: The current step number // - {{total}}: Total number of steps progressText?: string; // Text to show in the buttons. `doneBtnText` // is used on the last step of a tour. nextBtnText?: string; prevBtnText?: string; doneBtnText?: string; // Called after the popover is rendered. // PopoverDOM is an object with references to // the popover DOM elements such as buttons // title, descriptions, body, container etc. onPopoverRender?: (popover: PopoverDOM, options: { config: Config; state: State, driver: Driver }) => void; // Hooks to run before and after highlighting // each step. Each hook receives the following // parameters: // - element: The target DOM element of the step // - step: The step object configured for the step // - options.config: The current configuration options // - options.state: The current state of the driver // - options.driver: Current driver object onHighlightStarted?: (element?: Element, step: DriveStep, options: { config: Config; state: State, driver: Driver }) => void; onHighlighted?: (element?: Element, step: DriveStep, options: { config: Config; state: State, driver: Driver }) => void; onDeselected?: (element?: Element, step: DriveStep, options: { config: Config; state: State, driver: Driver }) => void; // Hooks to run before and after the driver // is destroyed. Each hook receives // the following parameters: // - element: Currently active element // - step: The step object configured for the currently active // - options.config: The current configuration options // - options.state: The current state of the driver // - options.driver: Current driver object onDestroyStarted?: (element?: Element, step: DriveStep, options: { config: Config; state: State, driver: Driver }) => void; onDestroyed?: (element?: Element, step: DriveStep, options: { config: Config; state: State, driver: Driver }) => void; // Hooks to run on button clicks. Each hook receives // the following parameters: // - element: The current DOM element of the step // - step: The step object configured for the step // - options.config: The current configuration options // - options.state: The current state of the driver // - options.driver: Current driver object onNextClick?: (element?: Element, step: DriveStep, options: { config: Config; state: State, driver: Driver }) => void; onPrevClick?: (element?: Element, step: DriveStep, options: { config: Config; state: State, driver: Driver }) => void; onCloseClick?: (element?: Element, step: DriveStep, options: { config: Config; state: State, driver: Driver }) => void; }; ``` > **Note**: By overriding `onNextClick`, and `onPrevClick` hooks you control the navigation of the driver. This means that user won't be able to navigate using the buttons and you will have to either call `driverObj.moveNext()` or `driverObj.movePrevious()` to navigate to the next/previous step. > > You can use this to implement custom logic for navigating between steps. This is also useful when you are dealing with dynamic content and want to highlight the next/previous element based on some logic. > > `onNextClick` and `onPrevClick` hooks can be configured at the step level as well. When configured at the driver level, you control the navigation for all the steps. When configured at the step level, you control the navigation for that particular step only. ## Popover Configuration The popover is the main UI element of Driver.js. It's the element that highlights the target element, and shows the step content. You can configure the popover globally, or per step. Given below are some of the available configuration options. ```typescript type Popover = { // Title and descriptions shown in the popover. // You can use HTML in these. Also, you can // omit one of these to show only the other. title?: string; description?: string; // The position and alignment of the popover // relative to the target element. side?: "top" | "right" | "bottom" | "left"; align?: "start" | "center" | "end"; // Array of buttons to show in the popover. // When highlighting a single element, there // are no buttons by default. When showing // a tour, the default buttons are "next", // "previous" and "close". showButtons?: ("next" | "previous" | "close")[]; // An array of buttons to disable. This is // useful when you want to show some of the // buttons, but disable some of them. disableButtons?: ("next" | "previous" | "close")[]; // Text to show in the buttons. `doneBtnText` // is used on the last step of a tour. nextBtnText?: string; prevBtnText?: string; doneBtnText?: string; // Whether to show the progress text in popover. showProgress?: boolean; // Template for the progress text. You can use // the following placeholders in the template: // - {{current}}: The current step number // - {{total}}: Total number of steps // Defaults to following if `showProgress` is true: // - "{{current}} of {{total}}" progressText?: string; // Custom class to add to the popover element. // This can be used to style the popover. popoverClass?: string; // Hook to run after the popover is rendered. // You can modify the popover element here. // Parameter is an object with references to // the popover DOM elements such as buttons // title, descriptions, body, etc. onPopoverRender?: (popover: PopoverDOM, options: { config: Config; state: State, driver: Driver }) => void; // Callbacks for button clicks. You can use // these to add custom behavior to the buttons. // Each callback receives the following parameters: // - element: The current DOM element of the step // - step: The step object configured for the step // - options.config: The current configuration options // - options.state: The current state of the driver // - options.driver: Current driver object onNextClick?: (element?: Element, step: DriveStep, options: { config: Config; state: State, driver: Driver }) => void onPrevClick?: (element?: Element, step: DriveStep, options: { config: Config; state: State, driver: Driver }) => void onCloseClick?: (element?: Element, step: DriveStep, options: { config: Config; state: State, driver: Driver }) => void } ``` ## Drive Step Configuration Drive step is the configuration object passed to the `highlight` method or the `steps` array of the `drive` method. You can configure the popover and the target element for each step. Given below are some of the available configuration options. ```typescript type DriveStep = { // The target element to highlight. // This can be a DOM element, // a function that returns a DOM Element, or a CSS selector. // If this is a selector, the first matching // element will be highlighted. element?: Element | string | (() => Element); // The popover configuration for this step. // Look at the Popover Configuration section popover?: Popover; // Whether to disable interaction with the highlighted element. (default: false) disableActiveInteraction?: boolean; // Callback when the current step is deselected, // about to be highlighted or highlighted. // Each callback receives the following parameters: // - element: The current DOM element of the step // - step: The step object configured for the step // - options.config: The current configuration options // - options.state: The current state of the driver // - options.driver: Current driver object onDeselected?: (element?: Element, step: DriveStep, options: { config: Config; state: State, driver: Driver }) => void; onHighlightStarted?: (element?: Element, step: DriveStep, options: { config: Config; state: State, driver: Driver }) => void; onHighlighted?: (element?: Element, step: DriveStep, options: { config: Config; state: State, driver: Driver }) => void; } ``` ## State You can access the current state of the driver by calling the `getState` method. It's also passed to the hooks and callbacks. ```typescript type State = { // Whether the driver is currently active or not isInitialized?: boolean; // Index of the currently active step if using // as a product tour and have configured the // steps array. activeIndex?: number; // DOM element of the currently active step activeElement?: Element; // Step object of the currently active step activeStep?: DriveStep; // DOM element that was previously active previousElement?: Element; // Step object of the previously active step previousStep?: DriveStep; // DOM elements for the popover i.e. including // container, title, description, buttons etc. popover?: PopoverDOM; } ``` ================================================ FILE: docs/src/content/guides/confirm-on-exit.mdx ================================================ --- title: "Confirm on Exit" groupTitle: "Examples" sort: 3 --- import { CodeSample } from "../../components/CodeSample.tsx"; You can use the `onDestroyStarted` hook to add a confirmation dialog or some other logic when the user tries to exit the tour. In the example below, upon exit we check if there are any tour steps left and ask for confirmation before we exit. ```js import { driver } from "driver.js"; import "driver.js/dist/driver.css"; const driverObj = driver({ showProgress: true, steps: [ { element: '#confirm-destroy-example', popover: { title: 'Animated Tour Example', description: 'Here is the code example showing animated tour. Let\'s walk you through it.', side: "left", align: 'start' }}, { element: 'code .line:nth-child(1)', popover: { title: 'Import the Library', description: 'It works the same in vanilla JavaScript as well as frameworks.', side: "bottom", align: 'start' }}, { element: 'code .line:nth-child(2)', popover: { title: 'Importing CSS', description: 'Import the CSS which gives you the default styling for popover and overlay.', side: "bottom", align: 'start' }}, { popover: { title: 'Happy Coding', description: 'And that is all, go ahead and start adding tours to your applications.' } } ], // onDestroyStarted is called when the user tries to exit the tour onDestroyStarted: () => { if (!driverObj.hasNextStep() || confirm("Are you sure?")) { driverObj.destroy(); } }, }); driverObj.drive(); ``` > **Note:** By overriding the `onDestroyStarted` hook, you are responsible for calling `driverObj.destroy()` to exit the tour. ================================================ FILE: docs/src/content/guides/installation.mdx ================================================ --- title: "Installation" groupTitle: "Introduction" sort: 1 --- Run one of the following commands to install the package: ```bash # Using npm npm install driver.js # Using pnpm pnpm install driver.js # Using yarn yarn add driver.js ``` Alternatively, you can use CDN and include the script in your HTML file: ```html ``` ## Start Using Once installed, you can import the package in your project. The following example shows how to highlight an element: ```js import { driver } from "driver.js"; import "driver.js/dist/driver.css"; const driverObj = driver(); driverObj.highlight({ element: "#some-element", popover: { title: "Title", description: "Description" } }); ``` ### Note on CDN If you are using the CDN, you will have to use the package from the `window` object: ```js const driver = window.driver.js.driver; const driverObj = driver(); driverObj.highlight({ element: "#some-element", popover: { title: "Title", description: "Description" } }); ``` Continue reading the [Getting Started](/docs/basic-usage) guide to learn more about the package. ================================================ FILE: docs/src/content/guides/migrating-from-0x.mdx ================================================ --- title: "Migrate to 1.x" groupTitle: "Introduction" sort: 6 --- Drivers 1.x is a major release that introduces a new API and a new architecture. This page will help you migrate your code from 0.x to 1.x. > Change in how you import the library ```diff - import Driver from 'driver.js'; - import 'driver.js/dist/driver.min.css'; + import { driver } from 'driver.js'; + import "driver.js/dist/driver.css"; ``` > Change in how you initialize the library ```diff - const driverObj = new Driver(config); - driverObj.setSteps(steps); + // Steps can be passed in the constructor + const driverObj = driver({ + ...config, + steps + }); ``` > Changes in configuration ```diff const config = { - overlayClickNext: false, // Option has been removed - closeBtnText: 'Close', // Option has been removed (close button is now an icon) - scrollIntoViewOptions: {}, // Option has been renamed - opacity: 0.75, + overlayOpacity: 0.75, - className: 'scoped-class', + popoverClass: 'scoped-class', - padding: 10, + stagePadding: 10, - showButtons: false, + showButtons: ['next', 'prev', 'close'], // pass an array of buttons to show - keyboardControl: true, + allowKeyboardControl: true, - onHighlightStarted: (Element) {}, + onHighlightStarted?: (element?: Element, step: DriveStep, options: { config: Config; state: State }) => void; - onHighlighted: (Element) {}, + onHighlighted?: (element?: Element, step: DriveStep, options: { config: Config; state: State }) => void; - onDeselected: (Element) {}, // Called when element has been deselected + onDeselected?: (element?: Element, step: DriveStep, options: { config: Config; state: State }) => void; - onReset: (Element) {}, // Called when overlay is about to be cleared + onDestroyStarted?: (element?: Element, step: DriveStep, options: { config: Config; state: State }) => void; + onDestroyed?: (element?: Element, step: DriveStep, options: { config: Config; state: State }) => void; + onCloseClick?: (element?: Element, step: DriveStep, options: { config: Config; state: State }) => void; - onNext: (Element) => {}, // Called when moving to next step on any step - onPrevious: (Element) => {}, // Called when moving to next step on any step + // By overriding the default onNextClick and onPrevClick, you control the flow of the driver + // Visit for more details: https://driverjs.com/docs/configuration + onNextClick?: (element?: Element, step: DriveStep, options: { config: Config; state: State }) => void; + onPrevClick?: (element?: Element, step: DriveStep, options: { config: Config; state: State }) => void; + // New options added + overlayColor?: string; + stageRadius?: number; + popoverOffset?: number; + disableButtons?: ["next", "prev", "close"]; + showProgress?: boolean; + progressText?: string; + onPopoverRender?: (popover: PopoverDOM, options: { config: Config; state: State }) => void; } ``` > Changes in step and popover definition ```diff const stepDefinition = { popover: { - closeBtnText: 'Close', // Removed, close button is an icon - element: '.some-element', // Required + element: '.some-element', // Optional, if not provided, step will be shown as modal - className: 'popover-class', + popoverClass: string; - showButtons: false, + showButtons: ["next", "previous", "close"]; // Array of buttons to show - title: ''; // Required + title: ''; // Optional - description: ''; // Required + description: ''; // Optional - // position can be left, left-center, left-bottom, top, - // top-center, top-right, right, right-center, right-bottom, - // bottom, bottom-center, bottom-right, mid-center - position: 'left', + // Now you need to specify the side and align separately + side?: "top" | "right" | "bottom" | "left"; + align?: "start" | "center" | "end"; + // New options + showProgress?: boolean; + progressText?: string; + onPopoverRender?: (popover: PopoverDOM, options: { config: Config; state: State }) => void; + onNextClick?: (element?: Element, step: DriveStep, options: { config: Config; state: State }) => void + onPrevClick?: (element?: Element, step: DriveStep, options: { config: Config; state: State }) => void + onCloseClick?: (element?: Element, step: DriveStep, options: { config: Config; state: State }) => void } + // New hook to control the flow of the driver + onDeselected?: (element?: Element, step: DriveStep, options: { config: Config; state: State }) => void; + onHighlightStarted?: (element?: Element, step: DriveStep, options: { config: Config; state: State }) => void; + onHighlighted?: (element?: Element, step: DriveStep, options: { config: Config; state: State }) => void; }; ``` > Changes in API methods. ```diff - driverObj.preventMove(); // async support is built-in, no longer need to call this - activeElement.getCalculatedPosition(); - activeElement.hidePopover(); - activeElement.showPopover(); - activeElement.getNode(); - const isActivated = driverObj.isActivated; + const isActivated = driverObj.isActive(); - driverObj.start(stepNumber = 0); + driverObj.drive(stepNumber = 0); - driverObj.highlight(string|stepDefinition); + driverObj.highlight(stepDefinition) - driverObj.reset(); + driverObj.destroy(); - driverObj.hasHighlightedElement(); + typeof driverObj.getActiveElement() !== 'undefined'; - driverObj.getHighlightedElement(); + driverObj.getActiveElement(); - driverObj.getLastHighlightedElement(); + driverObj.getPreviousElement(); + // New options added + driverObj.moveTo(stepIndex) + driverObj.getActiveStep(); // returns the configured step definition + driverObj.getPreviousStep(); // returns the previous step definition + driverObj.isLastStep(); + driverObj.isFirstStep(); + driverObj.getState(); + driverObj.getConfig(); + driverObj.setConfig(config); + driverObj.refresh(); ``` Please make sure to visit the [documentation](https://driverjs.com/docs/configuration) for more details. ================================================ FILE: docs/src/content/guides/popover-position.mdx ================================================ --- title: "Popover Position" groupTitle: "Examples" sort: 7 --- import { CodeSample } from "../../components/CodeSample.tsx"; You can control the popover position using the `side` and `align` options. The `side` option controls the side of the element where the popover will be shown and the `align` option controls the alignment of the popover with the element. > **Note:** Popover is intelligent enough to adjust itself to fit in the viewport. So, if you set `side` to `left` and `align` to `start`, but the popover doesn't fit in the viewport, it will automatically adjust itself to fit in the viewport. Consider highlighting and scrolling the browser to the element below to see this in action. ```js import { driver } from "driver.js"; import "driver.js/dist/driver.css"; const driverObj = driver(); driverObj.highlight({ element: '#left-start', popover: { title: 'Animated Tour Example', description: 'Here is the code example showing animated tour. Let\'s walk you through it.', side: "left", align: 'start' } }); ```

Use the buttons below to show the popover.

left and align set to start. PS, we can use HTML in the title and descriptions of popover.', side: "left", align: 'start' } }} id={"left-start"} client:load /> left and align set to center. PS, we can use HTML in the title and descriptions of popover.', side: "left", align: 'center' } }} id={"left-start"} client:load /> left and align set to end. PS, we can use HTML in the title and descriptions of popover.', side: "left", align: 'end' } }} id={"left-start"} client:load /> top and align set to start. PS, we can use HTML in the title and descriptions of popover.', side: "top", align: 'start' } }} id={"top-start"} client:load /> top and align set to center. PS, we can use HTML in the title and descriptions of popover.', side: "top", align: 'center' } }} id={"top-start"} client:load /> top and align set to end. PS, we can use HTML in the title and descriptions of popover.', side: "top", align: 'end' } }} id={"top-start"} client:load /> right and align set to start. PS, we can use HTML in the title and descriptions of popover.', side: "right", align: 'start' } }} id={"right-start"} client:load /> right and align set to center. PS, we can use HTML in the title and descriptions of popover.', side: "right", align: 'center' } }} id={"right-start"} client:load /> right and align set to end. PS, we can use HTML in the title and descriptions of popover.', side: "right", align: 'end' } }} id={"right-start"} client:load /> bottom and align set to start. PS, we can use HTML in the title and descriptions of popover.', side: "bottom", align: 'start' } }} id={"bottom-start"} client:load /> bottom and align set to center. PS, we can use HTML in the title and descriptions of popover.', side: "bottom", align: 'center' } }} id={"bottom-start"} client:load /> bottom and align set to end. PS, we can use HTML in the title and descriptions of popover.', side: "bottom", align: 'end' } }} id={"right-start"} client:load />
================================================ FILE: docs/src/content/guides/prevent-destroy.mdx ================================================ --- title: "Prevent Tour Exit" groupTitle: "Examples" sort: 3 --- import { CodeSample } from "../../components/CodeSample.tsx"; You can also prevent the user from exiting the tour using `allowClose` option. This option is useful when you want to force the user to complete the tour before they can exit. In the example below, you won't be able to exit the tour until you reach the last step. ```js import { driver } from "driver.js"; import "driver.js/dist/driver.css"; const driverObj = driver({ showProgress: true, allowClose: false, steps: [ { element: '#prevent-exit', popover: { title: 'Animated Tour Example', description: 'Here is the code example showing animated tour. Let\'s walk you through it.', side: "left", align: 'start' }}, { element: 'code .line:nth-child(1)', popover: { title: 'Import the Library', description: 'It works the same in vanilla JavaScript as well as frameworks.', side: "bottom", align: 'start' }}, { element: 'code .line:nth-child(2)', popover: { title: 'Importing CSS', description: 'Import the CSS which gives you the default styling for popover and overlay.', side: "bottom", align: 'start' }}, { popover: { title: 'Happy Coding', description: 'And that is all, go ahead and start adding tours to your applications.' } } ], }); driverObj.drive(); ``` ================================================ FILE: docs/src/content/guides/simple-highlight.mdx ================================================ --- title: "Simple Highlight" groupTitle: "Examples" sort: 11 --- import { FormHelp } from "../../components/FormHelp.tsx"; import { CodeSample } from "../../components/CodeSample.tsx"; Product tours is not the only usecase for Driver.js. You can use it to highlight any element on the page and show a popover with a description. This is useful for providing contextual help to the user e.g. help the user fill a form or explain a feature. Example below shows how to highlight an element and simply show a popover. Here is the code for above example: ```js const driverObj = driver({ popoverClass: "driverjs-theme", stagePadding: 4, }); driverObj.highlight({ element: "#highlight-me", popover: { side: "bottom", title: "This is a title", description: "This is a description", } }) ``` You can also use it to show a simple modal without highlighting any element. Yet another highlight example.", }, }} client:load /> Here is the code for above example: ```js const driverObj = driver(); driverObj.highlight({ popover: { description: "Yet another highlight example.", } }) ``` Focus on the input below and see how the popover is shown.