Guide your users through a tour of your app.
npm
npm install shepherd.js --save
yarn
yarn add shepherd.js
Note: Eager is now Cloudflare Apps
We will eventually make this work again, but it probably currently does not.
First create a new Tour
instance for your tour:
const tour = new Shepherd.Tour({
defaults: {
classes: 'shepherd-theme-arrows',
scrollTo: true
}
});
The defaults
option allows you to specify any options which should be applied
to all this tour's steps by default.
Next, add your steps:
tour.addStep('example-step', {
text: 'This step is attached to the bottom of the <code>.example-css-selector</code> element.',
attachTo: '.example-css-selector bottom',
classes: 'example-step-extra-class',
buttons: [
{
text: 'Next',
action: tour.next
}
]
});
Finally, to start the tour, just call start
on your Tour
instance:
tour.start();
Shepherd exposes a single object onto the window, Shepherd
.
That global object fires several events to let you link up actions with events occurring in any tour:
Shepherd.on(eventName, handler, [context])
: Bind an eventShepherd.off(eventName, [handler])
: Unbind an eventShepherd.once(eventName, handler, [context])
: Bind just the next instance of an event
The global Shepherd fires the following events whenever a Tour
instance fires them. It adds to the object passed to the
event handlers a tour
key pointing to the instance which fired the event:
complete
cancel
hide
show
start
active
inactive
The global Shepherd
includes a property which is always set to the currently active tour, or null if there is no active tour:
Shepherd.activeTour
You create a Tour
object for each tour you'd like to create.
Tour's constructor accepts a hash of options:
const myTour = new Shepherd.Tour(options);
steps
: An array of Step instances to initialize the tour withdefaults
: Default options for Steps created throughaddStep
addStep(id, options)
: Creates a new Step object with options, and returns theStep
instance it created. If you'd like you can also just pass an options hash which includesid
as a key. If the options hash doesn't include anid
, one will be generated. You can also pass an existingStep
instance rather thanoptions
, but note that Shepherd does not support a Step being attached to multiple Tours.getById(id)
: Return a step with a specific idnext()
: Advance to the next step, in the order they were addedback()
: Show the previous step, in the order they were addedcancel()
: Trigger cancel on the current step, hiding it without advancinghide()
: Hide the current stepshow([id])
: Show the step specified by id (if it's a string), or index (if it's a number) provided. Defaults to the first step.start()
: Show the first step and begin the tourgetCurrentStep()
: Returns the currently shown stepon(eventName, handler, [context])
: Bind an eventoff(eventName, [handler])
: Unbind an eventonce(eventName, handler, [context])
: Bind just the next instance of an event
complete
: Triggered when the last step is advancedcancel
hide
show
: Triggered with a hash of thestep
and theprevious
stepstart
Steps are instances of the Step object. They are generally created by the Tour::addStep
method, which returns the Step
instance it
created.
text
: The text in the body of the step. It can be one of four types:- HTML string
- Array of HTML strings
HTMLElement
object- Function to be executed when the step is built. It must return one the three options above.
title
: The steps title. It becomes anh3
at the top of the step.attachTo
: What element the step should be attached to on the page. It can either be a string of the form"element on"
, or an object with those properties. For example:".some #element left"
, or{element: '.some #element', on: 'left'}
. If you use the object syntax,element
can also be a DOM element. If you don't specify anattachTo
the element will appear in the middle of the screen.beforeShowPromise
: A function that returns a promise. When the promise resolves, the rest of theshow
code for the step will execute.classes
: Extra classes to add to the step.shepherd-theme-arrows
will give you our theme.buttons
: An array of buttons to add to the step. By default we add a Next button which triggersnext()
, set this to false to disable. Each button in the array is an object of the format:text
: The HTML text of the buttonclasses
: Extra classes to apply to the<a>
action
: A function executed when the button is clicked onevents
: A hash of events to bind onto the button, for example{'mouseover': function(){}}
. Adding a click event toevents
when you already have anaction
specified is not supported. You can useevents
to skip steps or navigate to specific steps, with something like:
events: { click: function() { return Shepherd.activeTour.show('some_step_name'); } }
advanceOn
: An action on the page which should advance shepherd to the next step. It can be of the form"selector event"
, or an object with those properties. For example:".some-element click"
, or{selector: '.some-element', event: 'click'}
. It doesn't have to be an event inside the tour, it can be any event fired on any element on the page. You can also always manually advance the Tour by callingmyTour.next()
.renderLocation
: AnHTMLElement
or selector string of the element you want the tour step to render in. Most of the time, you will not need to pass anything, and it will default todocument.body
, but this is needed for<dialog>
and might as well support passing anything.showCancelLink
: Should a cancel "✕" be shown in the header of the step?scrollTo
: Should the element be scrolled to when this step is shown?scrollToHandler
: A function that lets you override the defaultscrollTo
behavior and define a custom action to do the scrolling, and possibly other logic.when
: You can define show, hide, etc events inside when. For example:
when: {
show: function() {
window.scrollTo(0, 0);
}
}
popperOptions
: Extra options to pass to popper.js
show()
: Show this stephide()
: Hide this stepcancel()
: Hide this step and trigger thecancel
eventcomplete()
: Hide this step and trigger thecomplete
eventscrollTo()
: Scroll to this step's elementisOpen()
: Returns true if the step is currently showndestroy()
: Remove the elementon(eventName, handler, [context])
: Bind an eventoff(eventName, [handler])
: Unbind an eventonce(eventName, handler, [context])
: Bind just the next instance of an event
before-show
show
before-hide
hide
complete
cancel
destroy
Please note that complete
and cancel
are only ever triggered if you call the associated methods in your code.
You can use the advanceOn
option, or the Next button, to advance steps. If you would like however to have a step advance on a
complex user action, you can do the following:
const myStep = myTour.addStep('my-step', options);
yourApp.on('some-event', () => {
if (myStep.isOpen()){
Shepherd.activeTour.next();
}
});
It's strongly recommended that you use some sort of event mediator to connect your app's actions with Shepherd, to prevent
having to sprinkle Shepherd code throughout your codebase, and to keep things loosely coupled. You can create a basic
mediator if need be using the Evented
object which is provided with Shepherd:
const mediator = new Shepherd.Evented();
You can then trigger events in one part of your app:
mediator.trigger('user-create');
And listen for them in other areas:
mediator.on('user-create', () => {});
IE9+ and all modern browsers
Here we showcase some of the awesome libraries built using Shepherd.
Ember addon for the site tour library Shepherd
SimplePlanner uses Shepherd to help new users get familiar with its collaborative scheduling approach. You do need to sign up via OAuth or email to see the scheduling tour. Check out the Envato Tuts+ Startup Series on its codebase which describes how Simple Planner was built.
If you have a cool open-source library built on Shepherd, PR this doc.