Giter VIP home page Giter VIP logo

tourguide.js's Introduction

Tourguide.js

Simple, lightweight library for creating guided tours for your web, apps and more.

A tour guide is a person who provides assistance, information on cultural, historical and contemporary heritage to people on organized tours and individual clients at educational establishments, religious and historical sites, museums, and at venues of other significant interest, attractions sites. [https://en.wikipedia.org/wiki/Tour_guide]

Examples

Fiddle with It

Want to see how it works right away? Try on JSFiddle

React

You can look at some react examples here: React examples.

Install Tourguide.js

npm i tourguidejs

or add it from jsdelivr
https://cdn.jsdelivr.net/npm/[email protected]/tourguide.min.js
https://cdn.jsdelivr.net/npm/[email protected]/tourguide.js

Why do I need Tourguide.js

Every time you build you next awesome web app, you sit back and stare lovingly at your handy-work :) But then inevitably someone comes along asking one and the same inconvenient question: "So, how do I use it?"

You try to explain, but people are just not getting it! You write how-tos, lengthy docs, and step-by-step guides, and yet, nothing seems to be enough.

This is why we built Tourguide.js - a simple yet powerful explainer utility, designed to help you make the reply a little bit less painful. Because, let's face it - picture is worth a 1000 words:

Getting started

There are a few ways you can use Tourguide.js

Fiddle with it

Want to see how it works right away? Try on JSFiddle

CommonJS

Download tourguide.min.js, add it to your project libraries, and then include it on page:

<script src="tourguide.min.js"></script>

ES Module support

If you use ES modules in your project (Webpack, Rollup) import Tourguide.js like so:

import Tourguide from "tourguidejs";

Usage

Before use, Tourguide.js must be instantiated:

var tourguide = new Tourguide({options});
  • root?<string>: root element the tour steps will attach to; default is document.body
  • selector?<string>: if you want to use content based tour approach you can use this option to specify the common selector for the tour steps; default is [data-tour]
  • animationspeed?<number>: speed of all tour built-in animations; default is 120
  • padding?<number>: additional padding to add to step highlighter; default is 5(px)
  • steps?<Array<Step>>: if you choose to take JSON based tour approach provide use this property to provide the data; default is null
  • src?<string>: if you want to load the tour from a remote URL you may specify it here; default is null
  • preloadimages?<boolean>: if you want to preload images, you may set this attribute to true; default is false
  • restoreinitialposition?<boolean>: if you want to restore the scroll position after the tour ended, you may set this attribute to true; default is true
  • colors<Object>: if you want to customize the color schema of this plugin, use the following properties; the object you passed in will be combine with default values. The defaults are:
{
  fontFamily: 'sans-serif',
  fontSize: "14px",
  tooltipWidth: "40vw",
  overlayColor: "rgba(0, 0, 0, 0.5)",
  textColor: "#333",
  accentColor: "#0d6efd",
  focusColor: "auto",
  bulletColor: "auto",
  bulletVisitedColor: "auto",
  bulletCurrentColor: "auto",
  stepButtonCloseColor: "auto",
  stepButtonPrevColor: "auto",
  stepButtonNextColor: "auto",
  stepButtonCompleteColor: "auto",
  backgroundColor: "#fff",
}
  • keyboardNavigation?<Object>: if you want to enable keyboard navigation, use this attribute. each attribute can be number, string or object. If you want to disable the keyboard navigation, just set this option to false.

    {
      "next": "ArrowRight",
      "prev": "ArrowLeft",
      "first": "Home",
      "last": "End",
      "complete": null,
      "stop": "Escape"
    }
  • request?<Object>: if you want to load the tour from a remote URL you may provide request headers here Defaults are:

{
	options: {
		mode: "cors",
		cache: "no-cache",
	},
	headers: {
		"Content-Type": "application/json",
	},
}
  • actionHandlers?<Array<ActionHandler>>: optional array of custom step action handlers (see Handling tour actions for details)
  • contentDecorators?<Array<ContentDecorator>>: optional array of custom step decoration handlers (see Content decorators for details)
  • onStart?<Function>: callback function triggered when the tour starts
  • onStop?<Function>: callback function triggered when tour stops
  • onComplete?<Function>: callback triggered when tour completes
  • onStep?<Function>: callback triggered when a tour step is shown

Once instantiated you can use tourguide instance a several different ways:

Content based approach

Simplest approach is to read the descriptions right off the elements on page. This works best if you are using an MVC approach in your application. Simply add tour descriptions to the HTML elements in your page template:

<button aria-label="Collaborate" data-tour="step: 1; title: Step1; content: Lorem ipsum dolor sit amet">
  Collaborate
</button>

In this mode you can simply use Tourguide.js as-is:

var tourguide = new Tourguide();
tourguide.start();

About step details, See Step section.

JSON based approach

You may also write your own steps definition using JSON notation:

`[`
`  {`
`    "selector": null,`
`    "step": 1,`
`    "title": "Lets take a moment and look around Docsie library",`
`    "content": "Click a button to advance to the next step of this tour.<br/> To stop this tour at any time click a button in the top-right corner.",`
`    "image": "https://somehost.com/image.jpeg"`
`  },`
`  {`
`    "selector": "[data-component=library]:first-of-type",`
`    "step": 2,`
`    "title": "Shelf",`
`    "content": "Just like a real library &lt;mark&gt;Docsie&lt;/mark&gt; starts with &lt;dfn&gt;shelves&lt;/dfn&gt;. Each &lt;dfn&gt;shelf&lt;/dfn&gt; represnts a separate collection of ideas. You may think of them as individual websites, or website sections."`
`  }`
`]`

Once you have the complete JSON description for each of your tour steps you will have to initialize Tourguide.js passing your JSON as steps: property:

var steps = [...];
var tourguide = new Tourguide({steps: steps});
tourguide.start();

About step details, See Step section.

Remote URL approach

You may also want to load the steps remotely. To do so simply provide the target src as one of the Tourguide.js init params:

var tourguide = new Tourguide({src: "https://somedomain.com/tours/guide.json"});
tourguide.start();

About step details, See Step section.

Step

  • selector?<string>: CSS selector used to find the target element (used on JSON based approach and Remote URL approach)
  • step?<number>: tour step sequence number (when using JSON as data source this property may be omitted)
  • title<string>: tour step title
  • content<string>: write the content Both title and content support markdown language and content decorators (see Content decorators for details)
  • image?<url>: tour step illustration
  • width?<number>: step width in pixels (computed automatically by default)
  • height?<number>: step height in pixels (computed automatically by default)
  • layout?<enum>: this property can be horizontal or vertical and causes the tour step to be oriented horizontally or vertically (the default is vertical, works only when image is defined)
  • placement?<enum>: optional hint on where to place a tour step in relation to the step target; may be one of the following:
    • top-start
    • top
    • top-end
    • left
    • right
    • bottom-start
    • bottom
    • bottom-end
  • overlay?<boolean>: when set to false - hides step overlay (the default is true)
  • navigation?<boolean>: when set to false - hides step control buttons (the default is true)
  • actions<Action>: an array of step actions to be rendered in step footer (see Handling tour actions for details)

?* indicates the property is optional*

Content decorators

Both step title and content properties support content decorators defined in the following format:

	title: "Text ... {placeholder} ... more text.",
	content: "Text ... {fontsize,16,text} ... more text."

To render the decorated content you must provide your own custom content decorator when you initialize the tour. To create a custom decorator simply use the provided Tourguide.ContentDecorator class:

	const decorator = new Tourguide.ContentDecorator(
          "decorator",
          function (text, matches, step, context) {
			 ... do something to the text property
            return text;
          }
        );

Decorator class requires two properties:

  • match<string | RegExp>: either a plain string or a RegExp object identifying the decorator in text
  • decoratorFn<Function(text, matches, step, context)>: function decorator will call when match has been found
    • text<string>: full text of the step content
    • matches<Array<Match>>: an array of matches found in content text
	Match {
		match: exact decorator string matched
		start: position in text where the 
		length: total length of the matched string
		properties?: optional array of additional properties 
	}
* `step<Step>`: complete current step object
* `context<Tour>`: complete tour object

Dynamic Username example

Say you would like to make your tour more user friendly and display a name of the current user in one of the tour steps:

{
	title: "Hi {username},"
}

To render an actual user name in place to the decorator placeholder you need to pass the following decorator into the your Tour initialization options:

contentDecorators: [
        new Tourguide.ContentDecorator(
          "username",
          function (text, matches, step, context) {
            let _text = text;
            matches.forEach(match => {
              _text = _text.substring(0, match.start)
                + "User Name"
                + _text.substring(match.start + match.length);
            })
            return _text;
          }
        ),
]

Dynamic font size example

It's also possible to use the same technique to change some aspects of tour step text styling:

{
	"content": "**Click** the {fontsize,16,button} to see the {fontsize,20,result}"
}

In this example the script will match fontsize and parse two variables: 16 and button. You may then use these in your decorator function:

        new Tourguide.ContentDecorator(
          "fontsize",
          function (text, matches, step, context) {
            let _text = text;
            matches.forEach(match => {
              _text = _text.substring(0, match.start)
                + `<span style="font-size:${match.properties[0]}px">${match.properties[1]}</span>`
                + _text.substring(match.start + match.length);
            })
            return _text;
          }
        )

Handling tour actions

Tour actions provide you with an ability to display and handle additional actions in your tour steps. Action object has the following format:

Action {
	label: string;
	action: ActionType | string;
	primary?: boolean;
	[key: string]: any;
}

Passing an array of Action[] into a tour step will result in tour rendering a row of actions in the step footer, where the button label will be the action.label, and tour action may be one of the following:

enum ActionType {
	next, // Advance tour progress by one step
	previous, // Go back to a previous step, if any
	stop // Stop the tour
}

Furthermore you can handle custom actions you may define yourself.

To handle add custom actions to the tour you may use the provided Tourguide.ActionHandler class.

	new Tourguide.ActionHandler(actionName, actionHandlerFN);

Where actionName is the name of your action that must match with the action property to passed in your Action array. For instance if you pass {label: "Custom", action: "custom"} you must then pass the following action handler as part of your tour initialization options

actionHandlers: [
	new Tourguide.ActionHandler(
		"custom",
		function (event, action, context) {
			... do something
		}
	)
]

Link action example

Let's suppose you would like to add a link into a step footer. You may do so by passing the following action into the step object:

{
    "actions": [
      {
        "label": "Go to Google",
        "action": "link",
		 "href": "https://google.ca"
      }
    ]
}

Now to handle this action we need to create a custom handler:

actionHandlers: [
	new Tourguide.ActionHandler(
		"link",
		function (event, action, context) {
			event.preventDefault();
			window.location = action.href;
		}
	)
]

Controlling the tour

Once your tour has started you have several ways to manually control the tour flow:

tourguide.start([step])

Start the tour at any time by calling start(). You may optionally provide the step number to start the tour at a specific step (by default a tour always starts at step 1):

tourguide.start(2)

tourguide.stop()

Stop the tour at any moment by calling stop()

tourguide.next()

Causes tour to go to the next step in the sequence

tourguide.previous()

Causes tour to go to the previous step in the sequence

tourguide.go(step)

Causes tour to go to the step specified

tourguide.go(2)

Additional properties

  • tourguide.currentstep: returns the current step object

  • tourguide.length: return the number of steps on the tour

  • tourguide.steps: returns the tour steps as JSON notation

  • tourguide.hasnext: return true if there are steps remaining in the tour, otherwise returns false

  • tourguide.options: returns Tourguide.js options object

Attaching callbacks

Tourguide.js supports several helpful callbacks to help you better control the tour. You may optionally pass the callback functions into the tour instance at initialization time:

var tourguide = new Tourguide({
  `onStart:function(options){...},`
  `onStop:function(options){...},`
  `onComplete:function(){...},`
  `onStep:function(currentstep, type){...},`
});

onStart

Fires when the guided tour is started. The callback function will receive a single param:

  • options: tour options object

onStop

Fires when the guided tour stops. The callback function will receive a single param:

  • options: tour options object

onComplete

Fires when the guided tour is complete. The callback function will receives no params.

NOTE: onStop is always fired first, before onComplete is fired

onStep

Fires when tour step is activated. The callback function receives two params:

  • currentstep: tour step object

  • type: string representing the current direction of the tor; can be one of: "previous" | "next"

Step object

Each step of the tour is wrapped into a Step class. This allows you to have a direct access to the individual step properties and actions:

  • target: returns the target element step is attached to

  • el: returns the step view element

  • show(): show step element

  • hide(): hide step element

You can obtain the current step object an any time during the tour execution by calling tourguide.currentstep property:

var currentstep = tourguide.currentstep;
var stepTarget = currentstep.target;
var stepView = currentstep.el;

License

Tourguide.js is licensed under BSD 3-Clause "New" or "Revised" License

A permissive license similar to the BSD 2-Clause License, but with a 3rd clause that prohibits others from using the name of the project or its contributors to promote derived products without written consent.

tourguide.js's People

Contributors

dependabot[bot] avatar it-s avatar philippetrounev avatar ramunarasinga avatar syjsdev avatar wesley-nunes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tourguide.js's Issues

Failed to execute 'scrollTo' (Android 6.0)

Error closing tour:
Uncaught TypeError: Failed to execute 'scrollTo' on 'Window': No function was found that matched the signature provided.
tourguide.js

what to do, tell me, please?

Add backward navigation

Describe the solution you'd like

I'd like to be able to go backwards in the tour without clicking on the nav dots.

A left chevon, opposite the one on the right, to go back to the previous slide.

image

I'm getting this error when I was trying to use this in reactjs

./src/tourguidejs/tourguide.esm.js
Line 8:172: Expected an assignment or function call and instead saw an expression no-unused-expressions
Line 8:541: Expected an assignment or function call and instead saw an expression no-unused-expressions
Line 8:3717: Expected an assignment or function call and instead saw an expression no-unused-expressions
Line 8:4877: Expected an assignment or function call and instead saw an expression no-unused-expressions
Line 8:5761: Expected an assignment or function call and instead saw an expression no-unused-expressions
Line 8:7802: Expected an assignment or function call and instead saw an expression no-unused-expressions

Doesn't work on Safari / iPhone / iPad

When I view the tour on my iPad or iPhone in Safari and in Chrome, all positioning is messed up.

It doesn't matter if I do it in my app - or in the JS fiddle example on the main page, all highlighted elements are dead center, and the prompts appear in random locations.

I wish I'd known this before adding it to every section of my app!
123

Highlight styles not being applied

Describe the bug
Set style for highlight not applying styles

To Reproduce
Steps to reproduce the behavior:

  1. initiate new guide
  2. Inspect he active highlight dom element div.guided-tour-step.active > div.guided-tour-step-highlight
  3. The width, height, top and left style props are not set

Expected behavior
Highlight should be set to the target + padding

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: Mac OSX
  • Browser Chrome latest
  • Version 1.1.2

Additional context
Looking at the code seems as thought the values for the styles are being passed as numbers (without the 'px') assigning the style object without the unit of measurement does not work. This does work in 1.0.1, so seems a regression.

Tourguide.ActionHandler is not a constructor

I get an error when trying to create an ActionHandler.

Uncaught TypeError: Tourguide.ActionHandler is not a constructor
at :1:1

const steps = [
{
				content: "some text",
				title: "some title",
				selector: ".account-subitem.my-library",
				step: 6,
				actions: [{
						"label": "Invite members",
						"action": "click",
						"target": ".account-subitem.team"
					},
					{
						"label": "Share",
						"action": "link",
						"href": "/my-account/listmanager/library/"
					}
				]
			}
]
tg = new Tourguide({
			steps: steps,
			actionHandlers: [
				new Tourguide.ActionHandler(
					"link",
					function(event, action, context) {
						event.preventDefault();
						window.location = action.href;
					}
				),
				new Tourguide.ActionHandler(
					"click",
					function(event, action, context) {
						event.preventDefault();
						jQuery(action.target).trigger('click');
					}
				)
			]
		})

I'm obviously doing something wrong.. Anyone care to point me in the right direction?

Don't hide overlay between 2 steps

Hi,

Congrats for this project !

I integrate it on my product, but there is a tiny problem.

When we are jumping from step to step, unactive the overlay and active the overlay and it's like a blinking and it's a deceptive effet.

Can you help me ?

Regards

Michael

Back Navigation button for tourguide popup

Is your feature request related to a problem? Please describe.
navigating back involved clicking on dots that show up in tour popup, it'd be better to have back button

Describe the solution you'd like
Navigate back button should be created

Describe alternatives you've considered

Additional context

Support for Vuejs, Reactjs

Hi, I want use this repo for Vuejs (or Reactjs). Do you support for this? If yes, please provide docs and demo.
Thanks you.

The background is not displayed on safari v14

Describe the bug
The background is not displayed when the selector is null on safari v14.

To Reproduce

[
  {
    "selector": null,
    "step": 1,
    "title": "Tour",
    "content": "Example tour description."
  }
]

Screenshots

  • Current
    Screenshot_1
  • Expected
    Screenshot_2

Desktop (please complete the following information):

  • OS: macOS v11 Big Sur
  • Browser: Safari 14.0.2
  • Version: 0.2.1

Remote JSON src configured tourGuide options as src results in error

Unable to configure remote src as tourGuide options
Provided tourGuide options as 'var tourguideRemoteSrc = new Tourguide({src: "https://jsonplaceholder.typicode.com/users"});', it results in error and doesn't fetch remote json data

To Reproduce
Steps to reproduce the behavior:

  1. configure tourGuide in a project but with remote src providing json in fetch
  2. See error

Expected behavior
JSON fetched should results in steps being created, instead results in error at the time this is written

Desktop (please complete the following information):

  • OS: Mac
  • Browser: Chrome
  • Version 74

Screenshot 2019-05-29 at 9 52 27 PM

Colors doesn't work

Describe the bug
According to the readme you should be able to set colors. But this feature seems to be unimplemented.

To Reproduce
Steps to reproduce the behavior:

  1. create a new tourguide with new Tourguide({colors: {bullet: '#000'}})
  2. the colors are unchanged

Expected behavior
bullet color should be black

Desktop (please complete the following information):

  • Browser chrome

Additional context
There's no mention of colors anywhere in the sourcecode downloaded from npm v0.3.0. Is this a build issue? I can see that it's implemented in the github source

onLoaded callback

Is your feature request related to a problem? Please describe.
When using remote src, if the user starts tour before the plugin initializes, this error happened.
image

Describe the solution you'd like
Need the onLoaded callback which notifies the plugin is ready.

go(step), next(), previous() function is not working as expected.

Describe the bug
I had this issue when I wanted to move to another step before the current step was displayed.

To Reproduce
Steps to reproduce the behavior:

  1. Set animationspeed to 3000 (3 seconds)
  2. Then call the go or next or previous function programmatically before 3 seconds.

Screenshots
image

Additional context
version: v0.2.1

Typo in demo

Describe the bug

There's a typo in the demo

To Reproduce

Go to the demo and go through the tour. The last card says card 4

image

Expected behavior
Should say card 2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.