readme.md 9.3 KB
Newer Older
K
Kamran Ahmed 已提交
1
<h1 align="center"><img 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
    <img src="https://badge.fury.io/js/driver.js.svg" alt="version" />
K
Kamran Ahmed 已提交
9 10 11 12 13
  </a>
  <a href="http://twitter.com/kamranahmedse">
    <img src="https://img.shields.io/badge/author-kamranahmedse-blue.svg" />
  </a>
</p>
14

K
Kamran Ahmed 已提交
15
<p align="center">
K
Kamran Ahmed 已提交
16
  <b>Powerful yet light-weight, vanilla JavaScript engine to drive the user's focus across the page</b></br>
K
Kamran Ahmed 已提交
17 18
  <sub>Only ~4kb, no external dependency, supports all major browsers and highly customizable <sub>
</p>
19

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

* **Simple**: is simple to use and has no external dependency at all
* **Light-weight**: ~4kb in size, vanilla JavaScript and no external dependency
* **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 已提交
26
* **Feature introductions**: create powerful feature introductions for your web applications
K
Kamran Ahmed 已提交
27 28 29 30 31 32 33
* **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)

34 35 36 37
For Usage and Examples, [have a look at demo](http://kamranahmed.info/driver)

## Installation

K
Kamran Ahmed 已提交
38 39 40
You can install it using `yarn` or `npm`, whatever you prefer

```sh
41
yarn add driver.js
K
Kamran Ahmed 已提交
42
npm install driver.js
43 44
```

K
Kamran Ahmed 已提交
45
Or grab the code from `dist` directory and include it directly
46 47 48 49

```html
<link rel="stylesheet" href="/dist/driver.min.css">
<script src="/dist/driver.min.js"></script>
K
Kamran Ahmed 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
```

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

## Usage and Demo

Demos and many more usage examples can be found [through the docs page](http://kamranahmed.info/driver).

### Highlighting Single Element – [Demo](http://kamranahmed.info/driver#single-element-no-popover)

If all you want is just highlight a single element, you can do that simply by passing the selector

```javascript
const driver = new Driver();
driver.highlight('#create-post');
```
A real world usage example for this could be using it to dim the background and highlight the required element e.g. the way facebook does it on creating a post.

K
Kamran Ahmed 已提交
68
### Highlight and Popover – [Demo](http://kamranahmed.info/driver#single-element-with-popover)
K
Kamran Ahmed 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

You can show additional details beside the highlighted element using the popover

```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.

### Positioning the Popover – [Demo](http://kamranahmed.info/driver#single-element-with-popover-position)

By default, driver automatically finds the suitable position for the popover and displays it, you can override it using `position` property

```javascript
const driver = new Driver();
driver.highlight({
  element: '#some-element',
  popover: {
    title: 'Title for the Popover',
    description: 'Description for it',
    position: 'left', // can be `top`, `left`, `right`, `bottom`
  }
});
```

### Creating Feature Introductions – [Demo](http://kamranahmed.info/driver)

K
Kamran Ahmed 已提交
103
Feature introductions are helpful in onboarding new users and giving them idea about different parts of the application, you can create them seemlessly with driver. Define the steps and call the `start` when you want to start presenting. User will be able to control the steps using keyboard or using the buttons on popovers.
K
Kamran Ahmed 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

```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'
    }
  },
  {
    element: '#third-element-introduction',
    popover: {
      title: 'Title on Popover',
      description: 'Body of the popover',
      position: 'right'
    }
  },
]);

// Start the introduction
driver.start();
```
K
Kamran Ahmed 已提交
139 140 141 142
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 已提交
143
## API
K
Kamran Ahmed 已提交
144 145 146 147 148 149 150 151 152

Driver comes with several options that you can manipulate to make driver behave as you may like

### Driver Definition

Here are the options that Driver understands

```javascript
const driver = new Driver({
153
  animate: true,                    // Whether to animate or not
K
Kamran Ahmed 已提交
154 155
  opacity: 0.75,                    // Background opacity (0 means only popovers and without overlay)
  padding: 10,                      // Distance of element from around the edges
K
Kamran Ahmed 已提交
156
  allowClose: true,                 // Whether the click on overlay should close or not
K
Kamran Ahmed 已提交
157 158 159 160 161
  doneBtnText: 'Done',              // Text on the final button
  closeBtnText: 'Close',            // Text on the close button for this step
  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
162
  scrollIntoViewOptions: {},        // We use `scrollIntoView()` when possible, pass here the options for it if you want any
K
Kamran Ahmed 已提交
163
  onHighlightStarted: (Element) {}, // Called when element is about to be highlighted
K
Kamran Ahmed 已提交
164 165
  onHighlighted: (Element) {},      // Called when element is fully highlighted
  onDeselected: (Element) {},       // Called when element has been deselected
K
Kamran Ahmed 已提交
166 167
});
```
K
Kamran Ahmed 已提交
168
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 已提交
169 170 171 172 173 174 175

### Step Definition

Here are the set of options that you can pass while defining steps `defineSteps` or the object that you pass to `highlight` method

```javascript
const stepDefinition = {
K
Kamran Ahmed 已提交
176 177 178
  element: '#some-item',        // Query selector for the item to be highlighted
  popover: {                    // There will be no popover if empty or not given
    title: 'Title',             // Title on the popover
K
Kamran Ahmed 已提交
179
    description: 'Description', // Body of the popover
K
Kamran Ahmed 已提交
180 181 182 183 184
    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
K
Kamran Ahmed 已提交
185 186 187 188
  }
};
```

K
Kamran Ahmed 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
For example, here is how it would look when highlighting a single element

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

And this is how it would look when creating a step by step guide

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

K
Kamran Ahmed 已提交
208 209 210 211 212 213 214
### API Methods

Below are the set of methods that are available to you

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

K
Kamran Ahmed 已提交
215 216 217 218 219 220 221
// 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 已提交
222
driver.start(stepNumber = 0);  // Starts driving through the defined steps
K
Kamran Ahmed 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
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

// Highlights the element using query selector or the step definition
driver.highlight(string|stepDefinition);

// Resets the overlay and clears the screen
driver.reset();

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

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

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

K
Kamran Ahmed 已提交
246
activeElement.getScreenCoordinates(); // Gets screen co-ordinates of the active element
K
Kamran Ahmed 已提交
247 248
activeElement.hidePopover();          // Hide the popover
activeElement.showPopover();          // Show the popover
K
Kamran Ahmed 已提交
249 250 251 252

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

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

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

K
Kamran Ahmed 已提交
257 258
## Todo

A
aloupfor 已提交
259
- [X] Single element highlighting
K
Kamran Ahmed 已提交
260 261 262 263 264 265 266 267 268
- [X] Popovers on the highlighted elements
- [X] Add smooth transition on changing highlighted elements
- [X] Multi-step Journey Definitions
- [X] Make it controllable by keyboard
- [X] Bring highlighted element to viewport
- [ ] Add type definitions
- [ ] Create wrappers for Angular and React
- [ ] Write tests

K
Kamran Ahmed 已提交
269 270 271 272
## Contributions

Feel free to submit pull requests, create issues or spread the word

K
Kamran Ahmed 已提交
273 274 275 276
## License

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

K
Kamran Ahmed 已提交
277