Hopscotch

Contents

What is Hopscotch?

Hopscotch is a framework to make it easy for developers to add product tours to their pages. Hopscotch accepts a tour JSON object as input and provides an API for the developer to control rendering the tour display and managing the tour progress.

Features

Event Callbacks

Callbacks for tour events: onStart, onEnd, onShow, onNext, onPrev, onClose, onError

Multi-page tours

Take your tour across pages! Hopscotch saves state using HTML5 sessionStorage if available and falls back to using cookies as a default.

i18n support

Create tours in all the languages of the world. Hopscotch supports customizing the language of your tour controls.

Lightweight Callouts

Create single instance callouts for those times when one is enough.

General Usage

To get started using the Hopscotch framework, simply include hopscotch.css and hopscotch.js on your page. This will load the hopscotch object into the global window object for you.



  <html>

    <head>

      <title>My First Hopscotch Tour</title>

      <link rel="stylesheet" href="css/hopscotch.css"></link>

    </head>

    <body>

      <h1 id="header">My First Hopscotch Tour</h1>

      <div id="content">

        <p>Content goes here...</p>

      </div>

      <script src="js/hopscotch.js"></script>

      <script src="js/my_first_tour.js"></script> <!-- define and start your tour in this file -->

    </body>

  </html>

      

Then in your my_first_tour.js file, define and start your tour.



  // Define the tour!

  var tour = {

    id: "hello-hopscotch",

    steps: [

      {

        title: "My Header",

        content: "This is the header of my page.",

        target: "header",

        placement: "right"

      },

      {

        title: "My content",

        content: "Here is where I put my content.",

        target: document.querySelector("#content p"),

        placement: "bottom"

      }

    ]

  };



  // Start the tour!

  hopscotch.startTour(tour);

      

That's all there is to it!

Defining a Tour

A Hopscotch tour consists of a tour id, an array of tour steps defined as JSON objects, and a number of tour-specific options. The tour id is simply a unique identifier string. The simplest tour consists of just an id string and an array of one or more steps.

Basic step options

The step options below are the most basic options.

Note that title and content are both optional only because you can choose to have a step with only a title, only content, or both title and content.

This is an example of a tour defined with only basic steps.



  {

    id: {STRING - id of the tour},

    steps: [

      {

        // MANDATORY

        target: STRING/ELEMENT - id of the target DOM element or DOM element itself,

        placement: STRING - ["top", "bottom", "right", "left"]

        // OPTIONAL

        title: STRING - step title,

        content: STRING - step content

      },

      ...

    ]

  };

          


  {

    id: "welcome_tour",

    steps: [

      {

        target: "header",

        placement: "bottom",

        title: "This is the navigation menu",

        content: "Use the links here to get around on our site!"

      },

      {

        target: "profile-pic",

        placement: "right",

        title: "Your profile picture",

        content: "Upload a profile picture here."

      },

      {

        target: "inbox",

        placement: "bottom",

        title: "Your inbox",

        content: "Messages from other users will appear here."

      }

    ]

  }

          

IMPORTANT -- title and content are set using element.innerHTML. This allows the inclusion of very basic markup like links and lists. However, it also allows the inclusion of malicious script injections when used improperly. It is highly recommended to never show user-generated content in a Hopscotch tour. If it is absolutely necessary, you must properly escape the input, as always.

All step options

The comprehensive list of step options are listed below:

Mandatory

Optional

Setting tour options

Tour options can be specified either through the tour JSON object, or through a call to hopscotch.configure(). These options apply to the entire tour. In cases where there is both a value specified in the tour options and in a step definition, (e.g. "showPrevButton") the step definition takes priority. When multiple callbacks are defined in both step and tour options, step callbacks are invoked before tour-wide callbacks.

API Methods

The Hopscotch framework comes with a simple set of API calls with which you can run and manage tours:

Defining callbacks

Hopscotch has several events to which you can assign callbacks. These events include start, end, next, prev, show, close, error. For the next, prev, and show events, you can assign callbacks within step definitions as well as in the tour itself.

There are two ways to define event callbacks:

Function literals

If you are specifying your tour as an object literal in Javascript, you can provide a function literal as the value of your callback. This would look like the following:



  var tour = {

    id: 'myTour',

    steps: [

      {

        target: 'firststep',

        placement: 'bottom',

        title: 'My First Step',

        onNext: function() {

          $('#firststep').hide();

        }

      }

    ],

    onStart: function() {

      $('#article').addClass('selected');

    }

  };

      

Callback Helpers

In some situations, you may want to specify your tour in JSON. This may be because you are dynamically creating a tour on the server. Since functions are not valid JSON, specifying a function literal will not work. Instead, you can use Hopscotch helpers to specify your callback. Using helpers will look something like the following.

First register the helper with Hopscotch.



  hopscotch.registerHelper('selectArticle', function() {

    $('#article').addClass('selected');

  });

      

An example with an argument passed in:



  hopscotch.registerHelper('fillText', function(textFieldId, textStr) {

    document.getElementById(textFieldId).value = textStr;

  });

      

Then, when you define your tour, you specify the callback as an array of the following form: [helperId, arg, arg, ...]. For example:



  {

    id: "myTour",

    steps: [

      {

        target: "firststep",

        placement: "bottom",

        title: "My First Step",

        onNext: ["fillText", "searchField", "Example search query"]

      }

    ],

    onStart: ["selectArticle"]

  }

      

In the above example, since the onStart callback has no arguments, it could be defined as a simple string "selectArticle" instead of being wrapped in a one-element array.

To specify several helpers for a certain event:



  {

    id: "myTour",

    steps: [

      ...

    ],

    onStart: [["fillText", "searchField", "Example search query"], "selectArticle"]

  }

      

Callbacks will be invoked in the order that they are specified.

To remove a helper, simply call hopscotch.unregisterHelper('myHelperId').

Tour Example



  {

    id: "hello-hopscotch",

    steps: [

      {

        target: "hopscotch-title",

        title: "Welcome to Hopscotch!",

        content: "Hey there! This is an example Hopscotch tour. There will be plenty of time to read documentation and sample code, but let's just take some time to see how Hopscotch actually works.",

        placement: "bottom",

        arrowOffset: 60

      },

      {

        target: document.querySelectorAll("#general-use-desc code")[1],

        title: "Where to begin",

        content: "At the very least, you'll need to include these two files in your project to get started.",

        placement: "right",

        yOffset: -20

      },

      {

        target: "my-first-tour-file",

        placement: "top",

        title: "Define and start your tour",

        content: "Once you have Hopscotch on your page, you're ready to start making your tour! The biggest part of your tour definition will probably be the tour steps."

      },

      {

        target: "start-tour",

        placement: "right",

        title: "Starting your tour",

        content: "After you've created your tour, pass it in to the startTour() method to start it.",

        yOffset: -25

      },

      {

        target: "basic-options",

        placement: "left",

        title: "Basic step options",

        content: "These are the most basic step options: <b>target</b>, <b>title</b>, <b>content</b>, and <b>placement</b>. For some steps, they may be all you need.",

        arrowOffset: 100,

        yOffset: -80

      },

      {

        target: "api-methods",

        placement: "top",

        title: "Hopscotch API methods",

        content: "Control your tour programmatically using these methods.",

      },

      {

        target: "demo-tour",

        placement: "top",

        title: "This tour's code",

        content: "This is the JSON for the current tour! Pretty simple, right?",

      },

      {

        target: "hopscotch-title",

        placement: "bottom",

        title: "You're all set!",

        content: "Now go and build some great tours!",

      }

    ],

    showPrevButton: true,

    scrollTopMargin: 100

  }

      

Hopscotch Callouts

Sometimes all you need is a simple callout. You can use Hopscotch Callouts to achieve this.



  var calloutMgr = hopscotch.getCalloutManager();

  calloutMgr.createCallout({

    id: 'attach-icon',

    target: 'attach-btn',

    placement: 'bottom',

    title: 'Now you can share images &amp; files!',

    content: 'Share a project you\'re proud of, a photo from a recent event, or an interesting presentation.'

  });

      

Callouts come with the same options available as tour steps, so you can specify things like width, placement, offsets, and z-index. The most important difference between tour steps and callouts is that you must supply an id when creating a callout for later reference.

All management of callouts is done through the Hopscotch Callout Manager. The Callout Manager's job is pretty simple and comes with only a handful of API methods.