### Create and Start a Shepherd Tour
Source: https://github.com/shipshapecode/shepherd/blob/main/shepherd.js/test/cypress/dummy/index.html
Initialize a new Shepherd tour with default options and add a step. The tour is then started to guide the user.
```javascript
const tour = new Shepherd.Tour({
defaultStepOptions: {
classes: 'shadow-md bg-purple-dark',
scrollTo: true
}
});
tour.addStep({
title: 'Example Shepherd',
text: 'Creating a Shepherd is easy too! Just create ...',
attachTo: {
element: '.hero-example',
on: 'bottom'
},
advanceOn: {
selector: '.docs-link',
event: 'click'
},
id: 'example'
});
tour.start();
```
```javascript
import Shepherd from '../../../dist/js/shepherd.mjs'; window.Shepherd = Shepherd;
```
--------------------------------
### Create and Start a Basic Tour
Source: https://github.com/shipshapecode/shepherd/blob/main/shepherd.js/dummy/index.html
Initialize a new Shepherd tour with default options and add a single step. This example demonstrates basic tour creation and starting the tour.
```javascript
const tour = new Shepherd.Tour({
defaultStepOptions: {
classes: 'shadow-md bg-purple-dark',
scrollTo: true
}
});
tour.addStep({
title: 'Example Shepherd',
text: 'Creating a Shepherd is easy too! Just create ...',
attachTo: {
element: '.hero-example',
on: 'bottom'
},
advanceOn: {
selector: '.docs-link',
event: 'click'
},
id: 'example'
});
tour.start();
```
--------------------------------
### Start the Tour
Source: https://github.com/shipshapecode/shepherd/blob/main/docs-src/src/content/docs/guides/usage.md
Initiate the tour by calling the `start` method on the Tour instance. This will display the first step according to the tour's configuration.
```javascript
tour.start();
```
--------------------------------
### Basic Tour Initialization and Step Addition
Source: https://github.com/shipshapecode/shepherd/blob/main/docs-src/src/content/docs/guides/usage.md
Demonstrates how to import Shepherd, create a new Tour instance with default options, add a step, and start the tour.
```APIDOC
## Basic Tour Initialization and Step Addition
### Description
This example shows the fundamental steps to get started with Shepherd.js: importing the library, creating a tour instance with custom options, defining a step, and initiating the tour.
### Method
N/A (JavaScript execution)
### Endpoint
N/A (Client-side JavaScript)
### Parameters
N/A
### Request Example
```javascript
import Shepherd from 'shepherd.js';
const tour = new Shepherd.Tour({
useModalOverlay: true,
defaultStepOptions: {
classes: 'shadow-md bg-purple-dark',
scrollTo: true
}
});
tour.addStep({
id: 'example-step',
text: 'This step is attached to the bottom of the .example-css-selector element.',
attachTo: {
element: '.example-css-selector',
on: 'bottom'
},
classes: 'example-step-extra-class',
buttons: [
{
text: 'Next',
action: tour.next
}
]
});
tour.start();
```
### Response
N/A (Tour is initiated and displayed on the page)
```
--------------------------------
### Install Shepherd.js with pnpm
Source: https://github.com/shipshapecode/shepherd/blob/main/docs-src/src/content/docs/guides/install.mdx
Use pnpm to install Shepherd.js, a fast, disk-space-efficient package manager.
```bash
pnpm add shepherd.js
```
--------------------------------
### Install react-shepherd
Source: https://github.com/shipshapecode/shepherd/blob/main/packages/react/README.md
Install the react-shepherd package using npm.
```bash
npm install --save react-shepherd
```
--------------------------------
### Install Shepherd.js with bun
Source: https://github.com/shipshapecode/shepherd/blob/main/docs-src/src/content/docs/guides/install.mdx
Use bun, a fast all-in-one JavaScript runtime, to add Shepherd.js to your project.
```bash
bun add shepherd.js
```
--------------------------------
### Tour Instance Methods
Source: https://github.com/shipshapecode/shepherd/blob/main/docs-src/src/content/docs/guides/usage.md
Details methods available on a Tour instance, such as adding, removing, starting, and controlling steps.
```APIDOC
## Tour Instance Methods
### Description
This section outlines the key methods available on a `Tour` instance for managing the tour's lifecycle and steps.
### Methods
- `tour.addStep(stepOptions)`: Adds a new step to the tour.
- `tour.removeStep(stepId)`: Removes a step from the tour by its ID.
- `tour.start()`: Starts the tour.
- `tour.next()`: Advances to the next step.
- `tour.back()`: Goes back to the previous step.
- `tour.cancel()`: Cancels the tour.
- `tour.complete()`: Marks the tour as complete.
- `tour.show(stepId)`: Shows a specific step by its ID.
- `tour.hide()`: Hides the current step.
- `tour.destroy()`: Destroys the tour instance and cleans up.
### Request Example
```javascript
// Assuming 'tour' is an initialized Shepherd.Tour instance
// Add a step
tour.addStep({
id: 'step-2',
text: 'This is the second step.',
attachTo: { element: '#element-id', on: 'right' },
buttons: [
{ text: 'Back', action: tour.back },
{ text: 'Finish', action: tour.complete }
]
});
// Remove a step
tour.removeStep('example-step');
// Start the tour
tour.start();
// Navigate to the next step
tour.next();
// Cancel the tour
tour.cancel();
```
### Response
N/A (Methods control tour behavior and state)
```
--------------------------------
### Install Shepherd.js with npm
Source: https://github.com/shipshapecode/shepherd/blob/main/docs-src/src/content/docs/guides/install.mdx
Use npm to install Shepherd.js as a dependency for your project.
```bash
npm install shepherd.js --save
```
--------------------------------
### Install Shepherd.js with yarn
Source: https://github.com/shipshapecode/shepherd/blob/main/docs-src/src/content/docs/guides/install.mdx
Use yarn to add Shepherd.js to your project's dependencies.
```bash
yarn add shepherd.js
```
--------------------------------
### Starlight Project Commands
Source: https://github.com/shipshapecode/shepherd/blob/main/docs-src/README.md
Common npm commands for managing your Starlight project. Use `npm run dev` to start a local development server and `npm run build` to create a production build.
```bash
npm install
```
```bash
npm run dev
```
```bash
npm run build
```
```bash
npm run preview
```
```bash
npm run astro ...
```
```bash
npm run astro -- --help
```
--------------------------------
### Get Step and Tour Information
Source: https://context7.com/shipshapecode/shepherd/llms.txt
Access tour and step state information for conditional logic and debugging. This includes getting steps by ID, the current step, and checking the tour's active status.
```javascript
// Get step by ID
const welcomeStep = tour.getById('welcome');
console.log('Welcome step:', welcomeStep);
// Get current step
const current = tour.getCurrentStep();
if (current) {
console.log('Current step ID:', current.id);
console.log('Step is open:', current.isOpen());
console.log('Step element:', current.getElement());
console.log('Target element:', current.getTarget());
console.log('Step tour:', current.getTour());
}
// Check tour status
console.log('Tour is active:', tour.isActive());
console.log('Total steps:', tour.steps.length);
// Access active tour globally
if (Shepherd.activeTour) {
console.log('Active tour ID:', Shepherd.activeTour.id);
console.log('Active tour current step:', Shepherd.activeTour.getCurrentStep()?.id);
}
```
--------------------------------
### Control Tour Navigation and State
Source: https://context7.com/shipshapecode/shepherd/llms.txt
Use these methods to start, navigate, show specific steps, cancel, complete, and check the status of a tour. Ensure the tour instance is available before calling these methods.
```javascript
// Start the tour
tour.start();
```
```javascript
// Navigate programmatically
document.getElementById('next-btn').addEventListener('click', () => {
tour.next();
});
```
```javascript
document.getElementById('back-btn').addEventListener('click', () => {
tour.back();
});
```
```javascript
// Show a specific step by ID
document.getElementById('jump-to-feature').addEventListener('click', () => {
tour.show('feature-highlight');
});
```
```javascript
// Show a specific step by index
tour.show(2); // Shows the third step (0-indexed)
```
```javascript
// Cancel the tour
document.getElementById('exit-tour').addEventListener('click', () => {
tour.cancel();
});
```
```javascript
// Complete the tour manually
document.getElementById('finish-tour').addEventListener('click', () => {
tour.complete();
});
```
```javascript
// Check if tour is active
if (tour.isActive()) {
console.log('Tour is currently running');
}
```
```javascript
// Get current step
const currentStep = tour.getCurrentStep();
console.log('Current step ID:', currentStep?.id);
```
```javascript
// Hide current step without ending tour
tour.hide();
```
--------------------------------
### Usage via Provider/Context in React
Source: https://github.com/shipshapecode/shepherd/blob/main/packages/react/README.md
Demonstrates how to use react-shepherd with the ShepherdJourneyProvider and useShepherd hook to create and start a tour dynamically. Ensure newSteps is imported from its respective file.
```tsx
import { Component, useContext } from 'react';
import { ShepherdJourneyProvider, useShepherd } from 'react-shepherd';
import newSteps from './steps';
const tourOptions = {
defaultStepOptions: {
cancelIcon: {
enabled: true
}
},
useModalOverlay: true
};
function Button() {
const Shepherd = useShepherd();
const tour = new Shepherd.Tour({
...tourOptions,
steps: newSteps
});
return (
);
}
export default function App() {
return (
.example-css-selector element.',
attachTo: {
element: '.example-css-selector',
on: 'bottom'
},
classes: 'example-step-extra-class',
buttons: [
{
text: 'Next',
action: tour.next
}
]
});
```
--------------------------------
### Handle Modal Shown Event
Source: https://github.com/shipshapecode/shepherd/blob/main/docs-src/src/content/docs/guides/usage.md
Use `beforeShowPromise` to delay the step display until an asynchronous operation completes. This example waits for a Bootstrap modal's `shown.bs.modal` event before resolving the promise.
```javascript
beforeShowPromise: function() {
return new Promise(function(resolve) {
$('#my-bootstrap-modal').on('shown.bs.modal', function () {
resolve();
});
});
},
```
--------------------------------
### Initialize Astro Blog Project
Source: https://github.com/shipshapecode/shepherd/blob/main/landing/README.md
Use this command to scaffold a new Astro project using the blog template.
```sh
npm create astro@latest -- --template blog
```
--------------------------------
### Tour Instance API
Source: https://github.com/shipshapecode/shepherd/blob/main/docs-src/src/content/docs/guides/usage.md
Explains how to create Tour instances and lists the available configuration options for the Tour constructor.
```APIDOC
## Tour Instance API
### Description
This section details the creation of `Tour` instances and the various options that can be passed to the constructor to customize tour behavior.
### Method
`new Shepherd.Tour(options)`
### Endpoint
N/A (Client-side JavaScript constructor)
### Parameters
#### Request Body (Tour Options)
- **classPrefix** (string) - Optional - Prefix for generated class names.
- **confirmCancel** (boolean | function) - Optional - If true, prompts confirmation before cancelling. If a function, it's called and its return value determines cancellation.
- **confirmCancelMessage** (string) - Optional - Message displayed in the confirmation dialog.
- **defaultStepOptions** (object) - Optional - Default options applied to all steps.
- **exitOnEsc** (boolean) - Optional - Enables/disables exiting the tour with the Escape key (defaults to true).
- **keyboardNavigation** (boolean) - Optional - Enables/disables navigation using arrow keys (defaults to true).
- **stepsContainer** (HTMLElement) - Optional - A container element for steps; defaults to `document.body`.
- **modalContainer** (HTMLElement) - Optional - A container element for the modal overlay; defaults to `document.body`.
- **steps** (array) - Optional - An array of step options objects or Step instances to initialize the tour with.
- **tourName** (string) - Optional - An optional name for the tour, appended to its ID.
- **useModalOverlay** (boolean) - Optional - Determines if a modal overlay is used (defaults to true).
### Request Example
```javascript
const myTour = new Shepherd.Tour({
classPrefix: 'my-tour-prefix',
confirmCancel: true,
confirmCancelMessage: 'Are you sure you want to exit this tour?',
defaultStepOptions: {
classes: 'custom-step-class',
scrollTo: false
},
exitOnEsc: false,
keyboardNavigation: true,
steps: [
// Array of step options objects or Step instances
],
tourName: 'Welcome Tour',
useModalOverlay: false
});
```
### Response
N/A (Returns a `Tour` instance
```
--------------------------------
### Initialize a Basic Shepherd Tour
Source: https://github.com/shipshapecode/shepherd/blob/main/shepherd.js/dev/index.html
Create a simple tour instance with default step options and a single step.
```javascript
const tour = new Shepherd.Tour({ defaultStepOptions: { classes: 'shadow-md bg-purple-dark', scrollTo: true } }); tour.addStep({ title: 'Example Shepherd', text: 'Creating a Shepherd is easy too! Just create ...', attachTo: { element: '.hero-example', on: 'bottom' }, advanceOn: { selector: '.docs-link', event: 'click' }, id: 'example' }); tour.start();
```
--------------------------------
### Initialize a Full Shepherd Tour
Source: https://github.com/shipshapecode/shepherd/blob/main/shepherd.js/dev/index.html
Configure a comprehensive tour with multiple steps, modal overlays, and custom button actions.
```javascript
import Shepherd from '/js/shepherd.mjs'; const tour = new Shepherd.Tour({ defaultStepOptions: { cancelIcon: { enabled: true } }, useModalOverlay: true, steps: [ { text: ` Shepherd is a JavaScript library for guiding users through your app. It uses Floating UI, another open source library, to render dialogs for each tour "step".
Among many things, Floating UI makes sure your steps never end up off screen or cropped by an overflow. (Try resizing your browser to see what we mean.)
`, attachTo: { element: '.hero-welcome', on: 'bottom' }, classes: 'shepherd-step-element shepherd-transparent-text first-step', buttons: [ { action() { return this.cancel(); }, classes: 'shepherd-button-secondary', text: 'Exit' }, { action() { return this.next(); }, classes: 'shepherd-button-example-primary', text: 'Next' } ], id: 'welcome' }, { title: 'Including', text: 'Including Shepherd is easy! Just include shepherd.js. The styles are bundled with the JS.', attachTo: { element: '.hero-including', on: 'bottom' }, buttons: [ { action() { return this.back(); }, classes: 'shepherd-button-secondary', text: 'Back' }, { action() { return this.next(); }, classes: 'shepherd-button-example-primary', text: 'Next' } ], id: 'including', classes: 'shepherd-step-element second-step' }, { title: 'Example Shepherd', text: 'Creating a Shepherd is easy too! Just create Shepherd and add as many steps as you want. Check out the documentation to learn more.', attachTo: { element: '.hero-example', on: 'bottom' }, buttons: [ { action() { return this.back(); }, classes: 'shepherd-button-secondary', text: 'Back' }, { action() { return this.next(); }, classes: 'shepherd-button-example-primary', text: 'Next' } ], id: 'example', classes: 'shepherd-step-element third-step' }, { title: 'Learn more', text: 'Star Shepherd on Github so you remember it for your next project', attachTo: { element: '.hero-followup', on: 'left' }, buttons: [ { action() { return this.back(); }, classes: 'shepherd-button-secondary', text: 'Back' }, { action() { return this.next(); }, classes: 'shepherd-button-example-primary', text: 'Done' } ], id: 'followup', classes: 'shepherd-step-element fourth-step' } ] }); tour.start(); ``` -------------------------------- ### Create a Tour Instance with Options Source: https://github.com/shipshapecode/shepherd/blob/main/docs-src/src/content/docs/guides/usage.md Instantiate a new Shepherd Tour object by passing a configuration object to the `Shepherd.Tour` constructor. This allows for customization of tour behavior and appearance. ```javascript const myTour = new Shepherd.Tour(options); ``` -------------------------------- ### Add the final step to complete the tour Source: https://context7.com/shipshapecode/shepherd/llms.txt Define the last step of the tour, which provides a 'Finish' button to conclude the guided experience. ```javascript tour.addStep({ id: 'final-step', title: 'You\'re All Set!', text: 'You now know the basics. Click \'Finish\' to complete the tour.', buttons: [ { text: 'Finish', action: function() { this.complete(); } } ] }); ``` -------------------------------- ### Create a New Tour Instance Source: https://github.com/shipshapecode/shepherd/blob/main/docs-src/src/content/docs/guides/usage.md Initialize a new Shepherd Tour instance. The `useModalOverlay` option enables a modal overlay, and `defaultStepOptions` allows setting common configurations for all steps, such as styling and scroll behavior. ```javascript const tour = new Shepherd.Tour({ useModalOverlay: true, defaultStepOptions: { classes: 'shadow-md bg-purple-dark', scrollTo: true } }); ``` -------------------------------- ### Create a new Shepherd.js Tour instance Source: https://context7.com/shipshapecode/shepherd/llms.txt Initialize a new Shepherd.js tour with various configuration options such as modal overlay, keyboard navigation, and confirmation messages. ```javascript import Shepherd from 'shepherd.js'; const tour = new Shepherd.Tour({ useModalOverlay: true, exitOnEsc: true, keyboardNavigation: true, confirmCancel: true, confirmCancelMessage: 'Are you sure you want to exit the tour?', defaultStepOptions: { classes: 'shadow-md bg-purple-dark', scrollTo: { behavior: 'smooth', block: 'center' }, cancelIcon: { enabled: true, label: 'Close Tour' } } }); // Tour is ready to have steps added console.log('Tour created with ID:', tour.id); ``` -------------------------------- ### Initialize Shepherd Tour with Steps Source: https://github.com/shipshapecode/shepherd/blob/main/shepherd.js/dummy/index.html This code initializes a Shepherd tour with multiple steps, each configured with text, attachment points, buttons, and unique IDs. It demonstrates advanced step configuration and tour initialization. ```javascript import Shepherd from '../dist/js/shepherd.mjs'; window.Shepherd = Shepherd; const shepherd = new Shepherd.Tour({ defaultStepOptions: { cancelIcon: { enabled: true } }, useModalOverlay: true }); const steps = [ { text: `Shepherd is a JavaScript library for guiding users through your app. It uses Floating UI, another open source library, to render dialogs for each tour "step".
Among many things, Floating UI makes sure your steps never end up off screen or cropped by an overflow. (Try resizing your browser to see what we mean.)
`, attachTo: { element: '.hero-welcome', on: 'bottom' }, classes: 'shepherd-step-element shepherd-transparent-text first-step', buttons: [ { action: shepherd.cancel, classes: 'shepherd-button-secondary', text: 'Exit' }, { action: shepherd.next, classes: 'shepherd-button-example-primary', text: 'Next' } ], id: 'welcome' }, { title: 'Including', text: 'Including Shepherd is easy! Just include shepherd.js. The styles are bundled with the JS.', attachTo: { element: '.hero-including', on: 'bottom' }, buttons: [ { action: shepherd.back, classes: 'shepherd-button-secondary', text: 'Back' }, { action: shepherd.next, classes: 'shepherd-button-example-primary', text: 'Next' } ], id: 'including', classes: 'shepherd-step-element second-step' }, { title: 'Example Shepherd', text: 'Creating a Shepherd is easy too! Just create Shepherd and add as many steps as you want. Check out the documentation to learn more.', attachTo: { element: '.hero-example', on: 'bottom' }, buttons: [ { action: shepherd.back, classes: 'shepherd-button-secondary', text: 'Back' }, { action: shepherd.next, classes: 'shepherd-button-example-primary', text: 'Next' } ], id: 'example', classes: 'shepherd-step-element third-step' }, { title: 'Learn more', text: 'Star Shepherd on Github so you remember it for your next project', attachTo: { element: '.hero-followup', on: 'left' }, buttons: [ { action: shepherd.back, classes: 'shepherd-button-secondary', text: 'Back' }, { action: shepherd.next, classes: 'shepherd-button-example-primary', text: 'Done' } ], id: 'followup', classes: 'shepherd-step-element fourth-step' } ]; shepherd.addSteps(steps); shepherd.start(); ``` -------------------------------- ### Create a New Starlight Project Source: https://github.com/shipshapecode/shepherd/blob/main/docs-src/README.md Use this command to create a new Astro project with the Starlight template. This sets up the basic structure for your documentation site. ```bash npm create astro@latest -- --template starlight ``` -------------------------------- ### Starlight Project Structure Source: https://github.com/shipshapecode/shepherd/blob/main/docs-src/README.md Understand the default directory layout for a Starlight project. Key directories include `src/content/docs/` for Markdown files and `public/` for static assets. ```bash .\n├── public/\n├── src/\n│ ├── assets/\n│ ├── content/\n│ │ ├── docs/\n│ │ └── config.ts\n│ └── env.d.ts\n├── astro.config.mjs\n├── package.json\n└── tsconfig.json ``` -------------------------------- ### View Project Directory Structure Source: https://github.com/shipshapecode/shepherd/blob/main/landing/README.md Visual representation of the standard Astro project file layout. ```text ├── public/ ├── src/ │ ├── components/ │ ├── content/ │ ├── layouts/ │ └── pages/ ├── astro.config.mjs ├── README.md ├── package.json └── tsconfig.json ``` -------------------------------- ### Add multiple steps using addSteps() Source: https://context7.com/shipshapecode/shepherd/llms.txt Define an array of step configurations and add them all to the tour at once using the `addSteps()` method. ```javascript const tourSteps = [ { id: 'step-1', title: 'Navigation Menu', text: 'Use this menu to navigate between different sections.', attachTo: { element: '.nav-menu', on: 'bottom' }, buttons: [ { text: 'Next', action: function() { this.next(); } } ] }, { id: 'step-2', title: 'Search Feature', text: 'Search for anything using this search bar.', attachTo: { element: '#search-input', on: 'bottom' }, buttons: [ { text: 'Back', secondary: true, action: function() { this.back(); } }, { text: 'Next', action: function() { this.next(); } } ] }, { id: 'step-3', title: 'User Profile', text: 'Access your profile settings here.', attachTo: { element: '.user-profile', on: 'left' }, buttons: [ { text: 'Back', secondary: true, action: function() { this.back(); } }, { text: 'Complete', action: function() { this.complete(); } } ] } ]; tour.addSteps(tourSteps); tour.start(); ``` -------------------------------- ### Initialize Shepherd Library Source: https://github.com/shipshapecode/shepherd/blob/main/shepherd.js/test/cypress/examples/destroying-elements.html Imports the Shepherd module and attaches it to the global window object for access. ```javascript import Shepherd from '../../../dist/js/shepherd.mjs'; window.Shepherd = Shepherd; ``` -------------------------------- ### React Context Pattern for Shepherd Tours Source: https://context7.com/shipshapecode/shepherd/llms.txt Set up Shepherd tour as a React context for app-wide access to the same tour instance. Ensure the hook is used within the provider. ```javascript import React, { createContext, useContext } from 'react'; import Shepherd from 'shepherd.js'; const ShepherdTourContext = createContext(null); const tourInstance = new Shepherd.Tour({ useModalOverlay: true, defaultStepOptions: { cancelIcon: { enabled: true }, scrollTo: true } }); export function ShepherdTourProvider({ children }) { return (