kamrify commited on
Commit
e492ddc
·
1 Parent(s): 4c1e8d6

Allow step element to be a HTMLElement

Browse files
Files changed (6) hide show
  1. demo/scripts/demo.js +1 -1
  2. index.html +3 -3
  3. readme.md +1 -1
  4. src/common/utils.js +8 -0
  5. src/index.js +12 -10
  6. types/index.d.ts +1 -1
demo/scripts/demo.js CHANGED
@@ -3,7 +3,7 @@ document.addEventListener("DOMContentLoaded", function () {
3
 
4
  const tourSteps = [
5
  {
6
- element: '#driver-demo-head',
7
  popover: {
8
  title: 'Before we start',
9
  description: 'This is just one use-case, make sure to check out the rest of the docs below.',
 
3
 
4
  const tourSteps = [
5
  {
6
+ element: document.getElementById('driver-demo-head'),
7
  popover: {
8
  title: 'Before we start',
9
  description: 'This is just one use-case, make sure to check out the rest of the docs below.',
index.html CHANGED
@@ -292,9 +292,9 @@ driver.highlight({
292
  <p>Here are the set of options that you can pass in each step i.e. an item in array of steps or the object that
293
  you pass to <code>highlight</code> method</p>
294
  <pre><code class="javascript">const stepDefinition = {
295
- element: '#some-item', // Query selector for the item to be highlighted
296
- popover: { // There will be no popover if empty or not given
297
- title: 'Title', // Title on the popover
298
  description: 'Description', // Body of the popover
299
  showButtons: false, // Do not show control buttons in footer
300
  closeBtnText: 'Close', // Text on the close button for this step
 
292
  <p>Here are the set of options that you can pass in each step i.e. an item in array of steps or the object that
293
  you pass to <code>highlight</code> method</p>
294
  <pre><code class="javascript">const stepDefinition = {
295
+ element: '#some-item', // Query selector string or Node to be highlighted
296
+ popover: { // There will be no popover if empty or not given
297
+ title: 'Title', // Title on the popover
298
  description: 'Description', // Body of the popover
299
  showButtons: false, // Do not show control buttons in footer
300
  closeBtnText: 'Close', // Text on the close button for this step
readme.md CHANGED
@@ -182,7 +182,7 @@ Here are the set of options that you can pass while defining steps `defineSteps`
182
 
183
  ```javascript
184
  const stepDefinition = {
185
- element: '#some-item', // Query selector for the item to be highlighted
186
  stageBackground: '#ffffff', // This will override the one set in driver
187
  popover: { // There will be no popover if empty or not given
188
  title: 'Title', // Title on the popover
 
182
 
183
  ```javascript
184
  const stepDefinition = {
185
+ element: '#some-item', // Query selector string or Node to be highlighted
186
  stageBackground: '#ffffff', // This will override the one set in driver
187
  popover: { // There will be no popover if empty or not given
188
  title: 'Title', // Title on the popover
src/common/utils.js CHANGED
@@ -47,3 +47,11 @@ export const getStyleProperty = (element, propertyName, prefixVendor = false) =>
47
  return propertyValue && propertyValue.toLowerCase ? propertyValue.toLowerCase() : propertyValue;
48
  };
49
 
 
 
 
 
 
 
 
 
 
47
  return propertyValue && propertyValue.toLowerCase ? propertyValue.toLowerCase() : propertyValue;
48
  };
49
 
50
+ /**
51
+ * Checks if the passed element is dom object or not
52
+ * @param element
53
+ * @returns {boolean}
54
+ */
55
+ export const isDomElement = function (element) {
56
+ return element && typeof element === 'object' && 'nodeType' in element;
57
+ };
src/index.js CHANGED
@@ -17,6 +17,7 @@ import {
17
  SHOULD_OUTSIDE_CLICK_NEXT,
18
  } from './common/constants';
19
  import Stage from './core/stage';
 
20
 
21
  /**
22
  * Plugin class that drives the plugin
@@ -248,10 +249,6 @@ export default class Driver {
248
  this.steps = [];
249
 
250
  steps.forEach((step, index) => {
251
- if (!step.element || typeof step.element !== 'string') {
252
- throw new Error(`Element (query selector string) missing in step ${index}`);
253
- }
254
-
255
  const element = this.prepareElementFromStep(step, steps, index);
256
  if (!element) {
257
  return;
@@ -272,13 +269,18 @@ export default class Driver {
272
  * @private
273
  */
274
  prepareElementFromStep(currentStep, allSteps = [], index = 0) {
275
- let querySelector = '';
276
  let elementOptions = {};
 
277
 
278
- // If it is just a query selector string
279
- if (typeof currentStep === 'string') {
280
- querySelector = currentStep;
281
- } else {
 
 
 
 
 
282
  querySelector = currentStep.element;
283
  elementOptions = {
284
  ...this.options,
@@ -286,7 +288,7 @@ export default class Driver {
286
  };
287
  }
288
 
289
- const domElement = this.document.querySelector(querySelector);
290
  if (!domElement) {
291
  console.warn(`Element to highlight ${querySelector} not found`);
292
  return null;
 
17
  SHOULD_OUTSIDE_CLICK_NEXT,
18
  } from './common/constants';
19
  import Stage from './core/stage';
20
+ import { isDomElement } from './common/utils';
21
 
22
  /**
23
  * Plugin class that drives the plugin
 
249
  this.steps = [];
250
 
251
  steps.forEach((step, index) => {
 
 
 
 
252
  const element = this.prepareElementFromStep(step, steps, index);
253
  if (!element) {
254
  return;
 
269
  * @private
270
  */
271
  prepareElementFromStep(currentStep, allSteps = [], index = 0) {
 
272
  let elementOptions = {};
273
+ let querySelector = currentStep;
274
 
275
+ // If the `currentStep` is step definition
276
+ // then grab the options and element from the definition
277
+ const isStepDefinition = typeof currentStep !== 'string' && !isDomElement(currentStep);
278
+
279
+ if (!currentStep || (isStepDefinition && !currentStep.element)) {
280
+ throw new Error(`Element is required in step ${index}`);
281
+ }
282
+
283
+ if (isStepDefinition) {
284
  querySelector = currentStep.element;
285
  elementOptions = {
286
  ...this.options,
 
288
  };
289
  }
290
 
291
+ const domElement = isDomElement(querySelector) ? querySelector : this.document.querySelector(querySelector);
292
  if (!domElement) {
293
  console.warn(`Element to highlight ${querySelector} not found`);
294
  return null;
types/index.d.ts CHANGED
@@ -138,7 +138,7 @@ declare module 'driver.js' {
138
  /**
139
  * Query selector representing the DOM Element
140
  */
141
- element: string;
142
 
143
  /**
144
  * Color of stage when this step is active
 
138
  /**
139
  * Query selector representing the DOM Element
140
  */
141
+ element: string | HTMLElement | Node;
142
 
143
  /**
144
  * Color of stage when this step is active