readme.md 14.0 KB
Newer Older
K
Kamran Ahmed 已提交
1
<h1 align="center"><img height="150" src="./demo/images/driver.png" /><br> Driver.js</h1>
2

K
Kamran Ahmed 已提交
3
<p align="center">
K
Kamran Ahmed 已提交
4
  <a href="https://github.com/kamranahmedse/driver.js/blob/master/license">
K
Kamran Ahmed 已提交
5
    <img src="https://img.shields.io/badge/License-MIT-yellow.svg" />
K
Kamran Ahmed 已提交
6 7
  </a>
  <a href="https://npmjs.org/package/driver.js">
K
Kamran Ahmed 已提交
8 9 10
    <img src="https://img.shields.io/npm/v/driver.js.svg" alt="version" />
  </a>
  <a href="https://npmjs.org/package/driver.js">
K
Kamran Ahmed 已提交
11
    <img src="https://img.shields.io/npm/dt/driver.js.svg" alt="downloads" />
K
Kamran Ahmed 已提交
12 13
  </a>
</p>
14

K
Kamran Ahmed 已提交
15
<p align="center">
K
Kamran Ahmed 已提交
16 17
  <b>Powerful, highly customizable vanilla JavaScript engine to drive the user's focus across the page</b></br>
  <sub>No external dependencies, supports all major browsers and highly customizable <sub>
K
Kamran Ahmed 已提交
18
</p>
19

K
Kamran Ahmed 已提交
20 21 22 23 24
<br />

* **Simple**: is simple to use and has no external dependency at all
* **Highly customizable**: has a powerful API and can be used however you want
* **Highlight anything**: highlight any (literally any) element on page
K
Kamran Ahmed 已提交
25
* **Feature introductions**: create powerful feature introductions for your web applications
K
Kamran Ahmed 已提交
26 27 28 29 30 31 32
* **Focus shifters**: add focus shifters for users
* **User friendly**: Everything is controllable by keyboard
* **Consistent behavior**: usable across all browsers (including in-famous IE)
* **MIT Licensed**: free for personal and commercial use

![](./demo/images/split.png)

K
Kamran Ahmed 已提交
33
For Usage and Examples, [have a look at demo](http://kamranahmed.info/driver.js)
34

K
Kamran Ahmed 已提交
35 36
## So, yet another tour library?

K
Kamran Ahmed 已提交
37
**No**, it is not. **Tours are just one of the many use-cases**. Driver.js can be used wherever you need some sort of overlay for the page; some common usecases could be: e.g. dimming the background when user is interacting with some component i.e. [the way Facebook does](https://i.imgur.com/Q3PzaKkr.png) when you try to create a post, using it as a focus shifter to bring user's attention to some component on page, or using it to simulate those "Turn off the Lights" widgets that you might have seen on video players online, etc.
K
Kamran Ahmed 已提交
38

39
Driver.js is written in Vanilla JS, has zero dependencies and is highly customizable. It has several options allowing you to manipulate how it behaves and also **provides you the hooks** to manipulate the elements as they are highlighted, about to be highlighted, or deselected.
K
Kamran Ahmed 已提交
40

41 42
## Installation

43
You can install it using `yarn` or `npm`, whatever you prefer.
K
Kamran Ahmed 已提交
44

K
Kamran Ahmed 已提交
45
```bash
46
yarn add driver.js
K
Kamran Ahmed 已提交
47
npm install driver.js
48
```
K
Kamran Ahmed 已提交
49
Or include it using CDN. If you want a specific version, put it as `driver.js@0.5` in the name
K
Kamran Ahmed 已提交
50
```html
K
Kamran Ahmed 已提交
51
<script src="https://unpkg.com/driver.js/dist/driver.min.js"></script>
M
martinst1 已提交
52
<link rel="stylesheet" href="https://unpkg.com/driver.js/dist/driver.min.css">
K
Kamran Ahmed 已提交
53
```
54

55
Or grab the code from `dist` directory and include it directly.
56 57 58 59

```html
<link rel="stylesheet" href="/dist/driver.min.css">
<script src="/dist/driver.min.js"></script>
K
Kamran Ahmed 已提交
60 61 62 63 64 65
```

![](./demo/images/split.png)

## Usage and Demo

K
Kamran Ahmed 已提交
66 67 68 69 70 71 72 73
If you are using some sort of module bundler, import the library and the CSS file

```javascript
import Driver from 'driver.js';
import 'driver.js/dist/driver.min.css';
```
otherwise use the `script` and `link` tags to import the JavaScript and CSS files.

K
Kamran Ahmed 已提交
74
Demos and many more usage examples can be found [in the docs page](http://kamranahmed.info/driver.js).
K
Kamran Ahmed 已提交
75

K
Kamran Ahmed 已提交
76
### Highlighting Single Element – [Demo](http://kamranahmed.info/driver.js#single-element-no-popover)
K
Kamran Ahmed 已提交
77

78
You can highlight a single element by simply passing the selector.
K
Kamran Ahmed 已提交
79 80 81 82 83

```javascript
const driver = new Driver();
driver.highlight('#create-post');
```
84
A real world usage example for this is: using it to dim the background and highlight the required element e.g. the way Facebook does it when creating a post.
K
Kamran Ahmed 已提交
85

K
Kamran Ahmed 已提交
86
### Highlight and Popover – [Demo](http://kamranahmed.info/driver.js#single-element-with-popover)
K
Kamran Ahmed 已提交
87

88
You can show additional details beside the highlighted element using the popover.
K
Kamran Ahmed 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102

```javascript
const driver = new Driver();
driver.highlight({
  element: '#some-element',
  popover: {
    title: 'Title for the Popover',
    description: 'Description for it',
  }
});
```

Also, `title` and `description` can have HTML as well.

K
Kamran Ahmed 已提交
103
### Positioning the Popover – [Demo](http://kamranahmed.info/driver.js#single-element-with-popover-position)
K
Kamran Ahmed 已提交
104

105
By default, driver automatically finds the suitable position for the popover and displays it. You can override it using `position` property.
K
Kamran Ahmed 已提交
106 107 108 109 110 111 112 113

```javascript
const driver = new Driver();
driver.highlight({
  element: '#some-element',
  popover: {
    title: 'Title for the Popover',
    description: 'Description for it',
K
Kamran Ahmed 已提交
114 115
    // position can be left, left-center, left-bottom, top,
    // top-center, top-right, right, right-center, right-bottom,
116
    // bottom, bottom-center, bottom-right, mid-center
K
Kamran Ahmed 已提交
117
    position: 'left',
K
Kamran Ahmed 已提交
118 119 120 121
  }
});
```

K
Kamran Ahmed 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
You can also add offset to the popover position by using the `offset` property

```javascript
const driver = new Driver();
driver.highlight({
  element: '#some-element',
  popover: {
    title: 'Title for the Popover',
    description: 'Description for it',
    position: 'bottom',
    // Will show it 20 pixels away from the actual position of popover
    // You may also provide the negative values
    offset: 20,
  }
});
```

K
Kamran Ahmed 已提交
139
### Creating Feature Introductions – [Demo](http://kamranahmed.info/driver.js)
K
Kamran Ahmed 已提交
140

P
Prayag Verma 已提交
141
Feature introductions are helpful when onboarding new users and giving them an idea about different parts of the application; you can create them seamlessly with Driver. Define the steps and call the `start` when you want to start presenting. User will be able to control the steps using the keyboard or using the buttons on popovers.
K
Kamran Ahmed 已提交
142 143 144 145 146 147 148 149 150

```javascript
const driver = new Driver();

// Define the steps for introduction
driver.defineSteps([
  {
    element: '#first-element-introduction',
    popover: {
151
      className: 'first-step-popover-class',
K
Kamran Ahmed 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
      title: 'Title on Popover',
      description: 'Body of the popover',
      position: 'left'
    }
  },
  {
    element: '#second-element-introduction',
    popover: {
      title: 'Title on Popover',
      description: 'Body of the popover',
      position: 'top'
    }
  },
  {
    element: '#third-element-introduction',
    popover: {
      title: 'Title on Popover',
      description: 'Body of the popover',
      position: 'right'
    }
  },
]);

// Start the introduction
driver.start();
```
178
You can also hide the buttons and control the introductions programmatically by using the API methods listed below.
K
Kamran Ahmed 已提交
179 180 181

![](./demo/images/split.png)

K
Kamran Ahmed 已提交
182
### Asynchronous Actions – [Demo](http://kamranahmed.info/driver.js)
K
Kamran Ahmed 已提交
183

K
Kamran Ahmed 已提交
184
For any asynchronous actions between the transition steps, you may delay the execution till the action completes. All you have to do is stop the transition using `driver.preventMove()` in your `onNext` or `onPrevious` callbacks and initiate it manually using `driver.moveNext()`. Here is a sample implementation where it will stop at the second step for four seconds and then move on to the next step.
K
Kamran Ahmed 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

```javascript
const driver = new Driver();

// Define the steps for introduction
driver.defineSteps([
  {
    element: '#first-element-introduction',
    popover: {
      title: 'Title on Popover',
      description: 'Body of the popover',
      position: 'left'
    }
  },
  {
    element: '#second-element-introduction',
    popover: {
      title: 'Title on Popover',
      description: 'Body of the popover',
      position: 'top'
    },
    onNext: () => {
K
Kamran Ahmed 已提交
207
      // Prevent moving to the next step
K
Kamran Ahmed 已提交
208
      driver.preventMove();
K
Kamran Ahmed 已提交
209 210 211 212 213 214
      
      // Perform some action or create the element to move to
      // And then move to that element
      setTimeout(() => {
        driver.moveNext();
      }, 4000);
K
Kamran Ahmed 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    }
  },
  {
    element: '#third-element-introduction',
    popover: {
      title: 'Title on Popover',
      description: 'Body of the popover',
      position: 'right'
    }
  },
]);

// Start the introduction
driver.start();
```
You can also hide the buttons and control the introductions programmatically by using the API methods listed below.

![](./demo/images/split.png)

K
Kamran Ahmed 已提交
234
## API
K
Kamran Ahmed 已提交
235

236
Driver comes with several options that you can manipulate to make Driver behave as you like
K
Kamran Ahmed 已提交
237 238 239

### Driver Definition

240
Here are the options that Driver understands:
K
Kamran Ahmed 已提交
241 242 243

```javascript
const driver = new Driver({
244
  className: 'scoped-class',        // className to wrap driver.js popover
245
  animate: true,                    // Whether to animate or not
K
Kamran Ahmed 已提交
246 247
  opacity: 0.75,                    // Background opacity (0 means only popovers and without overlay)
  padding: 10,                      // Distance of element from around the edges
K
Kamran Ahmed 已提交
248
  allowClose: true,                 // Whether the click on overlay should close or not
K
Kamran Ahmed 已提交
249
  overlayClickNext: false,          // Whether the click on overlay should move next
K
Kamran Ahmed 已提交
250 251
  doneBtnText: 'Done',              // Text on the final button
  closeBtnText: 'Close',            // Text on the close button for this step
252
  stageBackground: '#ffffff',       // Background color for the staged behind highlighted element
K
Kamran Ahmed 已提交
253 254 255
  nextBtnText: 'Next',              // Next button text for this step
  prevBtnText: 'Previous',          // Previous button text for this step
  showButtons: false,               // Do not show control buttons in footer
256
  keyboardControl: true,            // Allow controlling through keyboard (escape to close, arrow keys to move)
257
  scrollIntoViewOptions: {},        // We use `scrollIntoView()` when possible, pass here the options for it if you want any
258 259 260 261
  onHighlightStarted: (Element) => {}, // Called when element is about to be highlighted
  onHighlighted: (Element) => {},      // Called when element is fully highlighted
  onDeselected: (Element) => {},       // Called when element has been deselected
  onReset: (Element) => {},            // Called when overlay is about to be cleared
K
Kamran Ahmed 已提交
262
  onNext: (Element) => {},                    // Called when moving to next step on any step
D
Diego de Fabregas 已提交
263
  onPrevious: (Element) => {},                // Called when moving to previous step on any step
K
Kamran Ahmed 已提交
264 265
});
```
K
Kamran Ahmed 已提交
266
Note that all the button options that you provide in the driver definition can be overridden for a specific step by giving them in the step definition
K
Kamran Ahmed 已提交
267 268 269

### Step Definition

270
Here are the set of options that you can pass while defining steps `defineSteps` or the object that you pass to `highlight` method:
K
Kamran Ahmed 已提交
271 272 273

```javascript
const stepDefinition = {
274
  element: '#some-item',        // Query selector string or Node to be highlighted
275
  stageBackground: '#ffffff',   // This will override the one set in driver
K
Kamran Ahmed 已提交
276
  popover: {                    // There will be no popover if empty or not given
277
    className: 'popover-class', // className to wrap this specific step popover in addition to the general className in Driver options
K
Kamran Ahmed 已提交
278
    title: 'Title',             // Title on the popover
K
Kamran Ahmed 已提交
279
    description: 'Description', // Body of the popover
K
Kamran Ahmed 已提交
280 281 282 283 284
    showButtons: false,         // Do not show control buttons in footer
    doneBtnText: 'Done',        // Text on the last button
    closeBtnText: 'Close',      // Text on the close button
    nextBtnText: 'Next',        // Next button text
    prevBtnText: 'Previous',    // Previous button text
285 286
  },
  onNext: () => {},             // Called when moving to next step from current step
D
Diego de Fabregas 已提交
287
  onPrevious: () => {},         // Called when moving to previous step from current step
K
Kamran Ahmed 已提交
288 289 290
};
```

291
For example, here is how it would look when highlighting a single element:
K
Kamran Ahmed 已提交
292 293 294 295 296 297

```javascript
const driver = new Driver(driverOptions);
driver.highlight(stepDefinition);
```

298
And this is how it would look when creating a step by step guide:
K
Kamran Ahmed 已提交
299 300 301 302 303 304 305 306 307 308 309

```javascript
const driver = new Driver(driverOptions);
driver.defineSteps([
    stepDefinition1,
    stepDefinition2,
    stepDefinition3,
    stepDefinition4,
]);
```

K
Kamran Ahmed 已提交
310 311
### API Methods

312
Below are the set of methods that are available:
K
Kamran Ahmed 已提交
313 314 315 316

```javascript
const driver = new Driver(driverOptions);

K
Kamran Ahmed 已提交
317 318 319 320 321 322 323
// Checks if the driver is active or not
if (driver.isActivated) {
    console.log('Driver is active');
}

// In case of the steps guide, you can call below methods
driver.defineSteps([ stepDefinition1, stepDefinition2, stepDefinition3 ]);
K
Kamran Ahmed 已提交
324
driver.start(stepNumber = 0);  // Starts driving through the defined steps
K
Kamran Ahmed 已提交
325 326 327 328 329
driver.moveNext();             // Moves to next step in the steps list
driver.movePrevious();         // Moves to previous step in the steps list
driver.hasNextStep();          // Checks if there is next step to move to
driver.hasPreviousStep();      // Checks if there is previous step to move to

K
Kamran Ahmed 已提交
330 331 332 333
// Prevents the current move. Useful in `onNext` or `onPrevious` if you want to
// perform some asynchronous task and manually move to next step
driver.preventMove();

K
Kamran Ahmed 已提交
334 335 336
// Highlights the element using query selector or the step definition
driver.highlight(string|stepDefinition);

K
Kamran Ahmed 已提交
337 338 339
// Reposition the popover and highlighted element
driver.refresh();

K
Kamran Ahmed 已提交
340 341
// Resets the overlay and clears the screen
driver.reset();
K
Kamran Ahmed 已提交
342

K
Kamran Ahmed 已提交
343 344 345 346 347
// Additionally you can pass a boolean parameter
// to clear immediately and not do the animations etc
// Could be useful when you, let's say, want to run
// a different instance of driver while one was running
driver.reset(clearImmediately = false);
K
Kamran Ahmed 已提交
348 349 350 351 352

// Checks if there is any highlighted element
if(driver.hasHighlightedElement()) {
    console.log('There is an element highlighted');
}
K
Kamran Ahmed 已提交
353 354

// Gets the currently highlighted element on screen
K
Kamran Ahmed 已提交
355
// It would be an instance of `/src/core/element.js`
K
Kamran Ahmed 已提交
356
const activeElement = driver.getHighlightedElement();
K
Kamran Ahmed 已提交
357 358

// Gets the last highlighted element, would be an instance of `/src/core/element.js`
K
Kamran Ahmed 已提交
359
const lastActiveElement = driver.getLastHighlightedElement();
K
Kamran Ahmed 已提交
360

361 362 363
activeElement.getCalculatedPosition(); // Gets screen co-ordinates of the active element
activeElement.hidePopover();           // Hide the popover
activeElement.showPopover();           // Show the popover
K
Kamran Ahmed 已提交
364 365 366 367

activeElement.getNode();  // Gets the DOM Element behind this element
```

K
Kamran Ahmed 已提交
368 369
![](./demo/images/split.png)

370
**Note –** Do not forget to add `e.stopPropagation()` to the `click` binding that triggers driver.
K
Kamran Ahmed 已提交
371

K
Kamran Ahmed 已提交
372 373
![](./demo/images/split.png)

K
Kamran Ahmed 已提交
374 375
## Contributions

376
Feel free to submit pull requests, create issues or spread the word.
K
Kamran Ahmed 已提交
377

K
Kamran Ahmed 已提交
378 379
## Sponsored By

380
Thanks to [BrowserStack](https://browserstack.com) for sponsoring the compatibility testing needs.
K
Kamran Ahmed 已提交
381

K
Kamran Ahmed 已提交
382
[![BrowserStack](./demo/images/browserstack.png)](https://www.browserstack.com)
K
Kamran Ahmed 已提交
383

K
Kamran Ahmed 已提交
384 385 386 387
## License

MIT &copy; [Kamran Ahmed](https://twitter.com/kamranahmedse)

K
Kamran Ahmed 已提交
388