Skip to content

Commit 9fb0282

Browse files
committed
Remove unnecessary id attribute on step tooltip containers.
Follows up on the discussion [here](#282 (comment)).
1 parent b8014d7 commit 9fb0282

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ const myTour = new Shepherd.Tour(options);
124124

125125
- `steps`: An array of Step instances to initialize the tour with
126126
- `defaultStepOptions`: Default options for Steps created through `addStep`
127+
- `tourName`: An optional "name" for the tour. This will be appended to the the tour's
128+
dynamically generated `id` property -- which is also set on the `body` element as the `data-shepherd-active-tour` attribute whenever the tour becomes active.
127129
- `confirmCancel`: If true, will issue a window.confirm before cancelling
128130
- `confirmCancelMessage`: The message to display in the confirm dialog
129131

src/js/step.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ export class Step extends Evented {
190190
_createTooltipContent() {
191191
const content = document.createElement('div');
192192
const classes = this.options.classes || '';
193-
const element = createFromHTML(`<div class='${classes}' data-shepherd-step-id='${this.id}' id="step-${this.options.id}-${uniqueId()}"}>`);
193+
const element = createFromHTML(`<div class="${classes}" data-shepherd-step-id="${this.id}">`);
194194
const header = document.createElement('header');
195195

196196
if (this.options.title) {

src/js/tour.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@ import { bindMethods } from './bind.js';
55
import tippy from 'tippy.js';
66
import { defaults as tooltipDefaults } from './utils/tooltip-defaults';
77

8+
/**
9+
* Creates incremented ID for each newly created tour
10+
*
11+
* @private
12+
* @return {Number} The unique id for the tour
13+
*/
14+
const uniqueId = (function() {
15+
let id = 0;
16+
return function() {
17+
return ++id;
18+
};
19+
})();
20+
821
const Shepherd = new Evented();
922

1023
/**
@@ -17,6 +30,9 @@ export class Tour extends Evented {
1730
* @param {Object} options The options for the tour
1831
* @param {Object} options.defaultStepOptions Default options for Steps created through `addStep`
1932
* @param {Step[]} options.steps An array of Step instances to initialize the tour with
33+
* @param {string} options.tourName An optional "name" for the tour. This will be appended to the the tour's
34+
* dynamically generated `id` property -- which is also set on the `body` element as the `data-shepherd-active-tour` attribute
35+
* whenever the tour becomes active.
2036
* @returns {Tour}
2137
*/
2238
constructor(options = {}) {
@@ -43,6 +59,7 @@ export class Tour extends Evented {
4359
});
4460

4561
this._setTooltipDefaults();
62+
this._setTourID();
4663

4764
return this;
4865
}
@@ -119,7 +136,7 @@ export class Tour extends Evented {
119136
this.trigger(event);
120137

121138
Shepherd.activeTour = null;
122-
document.body.classList.remove('shepherd-active');
139+
this._removeBodyAttrs();
123140
this.trigger('inactive', { tour: this });
124141
}
125142

@@ -260,7 +277,7 @@ export class Tour extends Evented {
260277
* @private
261278
*/
262279
_setupActiveTour() {
263-
document.body.classList.add('shepherd-active');
280+
this._addBodyAttrs();
264281
this.trigger('active', { tour: this });
265282

266283
Shepherd.activeTour = this;
@@ -291,6 +308,24 @@ export class Tour extends Evented {
291308
this._setupActiveTour();
292309
}
293310
}
311+
312+
_setTourID() {
313+
const tourName = this.options.tourName || 'tour';
314+
const uuid = uniqueId();
315+
316+
this.id = `${tourName}--${uuid}`;
317+
}
318+
319+
_addBodyAttrs() {
320+
document.body.setAttribute('data-shepherd-active-tour', this.id);
321+
document.body.classList.add('shepherd-active');
322+
}
323+
324+
_removeBodyAttrs() {
325+
document.body.removeAttribute('data-shepherd-active-tour');
326+
document.body.classList.remove('shepherd-active');
327+
}
328+
294329
}
295330

296331
export { Shepherd };

test/unit/test.tour.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ describe('Tour | Top-Level Class', function() {
6767
assert.equal(tippySpy.callCount, 1);
6868
assert.equal(tippySpy.calledWith(tooltipDefaults), true);
6969
});
70+
71+
it('generates a unique `id` property, optionally based upon the `tourName` option', function() {
72+
const instance1 = new Shepherd.Tour();
73+
const instance2 = new Shepherd.Tour({ tourName: 'select-avatar'});
74+
75+
assert.equal(instance1.id.startsWith('tour--'), true);
76+
assert.equal(instance2.id.startsWith('select-avatar--'), true);
77+
78+
const uniqueId1 = instance1.id.split('--')[1];
79+
const uniqueId2 = instance2.id.split('--')[1];
80+
81+
assert.notEqual(uniqueId1, uniqueId2);
82+
});
7083
});
7184

7285
describe('methods', () => {
@@ -142,6 +155,14 @@ describe('Tour | Top-Level Class', function() {
142155

143156
assert.equal(instance, Shepherd.activeTour);
144157
});
158+
159+
it('adds the current tour\'s "id" property to the body as a data attribute', function() {
160+
instance.id = 'test-id';
161+
instance.start();
162+
163+
assert.equal(document.body.hasAttribute('data-shepherd-active-tour'), true);
164+
assert.equal(document.body.getAttribute('data-shepherd-active-tour'), 'test-id');
165+
});
145166
});
146167

147168
describe('.getCurrentStep()', function() {

0 commit comments

Comments
 (0)