Skip to content

Commit 523519f

Browse files
committed
Allow step element to be a HTMLElement
1 parent 886f5fe commit 523519f

File tree

6 files changed

+26
-16
lines changed

6 files changed

+26
-16
lines changed

demo/scripts/demo.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ document.addEventListener("DOMContentLoaded", function () {
33

44
const tourSteps = [
55
{
6-
element: '#driver-demo-head',
6+
element: document.getElementById('driver-demo-head'),
77
popover: {
88
title: 'Before we start',
99
description: 'This is just one use-case, make sure to check out the rest of the docs below.',

index.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,9 @@ <h4>Step Definition</h4>
292292
<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
293293
you pass to <code>highlight</code> method</p>
294294
<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
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
298298
description: 'Description', // Body of the popover
299299
showButtons: false, // Do not show control buttons in footer
300300
closeBtnText: 'Close', // Text on the close button for this step

readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ Here are the set of options that you can pass while defining steps `defineSteps`
182182

183183
```javascript
184184
const stepDefinition = {
185-
element: '#some-item', // Query selector for the item to be highlighted
185+
element: '#some-item', // Query selector string or Node to be highlighted
186186
stageBackground: '#ffffff', // This will override the one set in driver
187187
popover: { // There will be no popover if empty or not given
188188
title: 'Title', // Title on the popover

src/common/utils.js

+8
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,11 @@ export const getStyleProperty = (element, propertyName, prefixVendor = false) =>
4747
return propertyValue && propertyValue.toLowerCase ? propertyValue.toLowerCase() : propertyValue;
4848
};
4949

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

+12-10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
SHOULD_OUTSIDE_CLICK_NEXT,
1818
} from './common/constants';
1919
import Stage from './core/stage';
20+
import { isDomElement } from './common/utils';
2021

2122
/**
2223
* Plugin class that drives the plugin
@@ -248,10 +249,6 @@ export default class Driver {
248249
this.steps = [];
249250

250251
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-
255252
const element = this.prepareElementFromStep(step, steps, index);
256253
if (!element) {
257254
return;
@@ -272,21 +269,26 @@ export default class Driver {
272269
* @private
273270
*/
274271
prepareElementFromStep(currentStep, allSteps = [], index = 0) {
275-
let querySelector = '';
276272
let elementOptions = {};
273+
let querySelector = currentStep;
277274

278-
// If it is just a query selector string
279-
if (typeof currentStep === 'string') {
280-
querySelector = currentStep;
281-
} else {
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) {
282284
querySelector = currentStep.element;
283285
elementOptions = {
284286
...this.options,
285287
...currentStep,
286288
};
287289
}
288290

289-
const domElement = this.document.querySelector(querySelector);
291+
const domElement = isDomElement(querySelector) ? querySelector : this.document.querySelector(querySelector);
290292
if (!domElement) {
291293
console.warn(`Element to highlight ${querySelector} not found`);
292294
return null;

types/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ declare module 'driver.js' {
138138
/**
139139
* Query selector representing the DOM Element
140140
*/
141-
element: string;
141+
element: string | HTMLElement | Node;
142142

143143
/**
144144
* Color of stage when this step is active

0 commit comments

Comments
 (0)