Skip to content

Commit 17bbd42

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

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

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: 34 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
/**
@@ -43,6 +56,7 @@ export class Tour extends Evented {
4356
});
4457

4558
this._setTooltipDefaults();
59+
this._setTourID();
4660

4761
return this;
4862
}
@@ -119,7 +133,7 @@ export class Tour extends Evented {
119133
this.trigger(event);
120134

121135
Shepherd.activeTour = null;
122-
document.body.classList.remove('shepherd-active');
136+
this._removeBodyAttrs();
123137
this.trigger('inactive', { tour: this });
124138
}
125139

@@ -260,7 +274,7 @@ export class Tour extends Evented {
260274
* @private
261275
*/
262276
_setupActiveTour() {
263-
document.body.classList.add('shepherd-active');
277+
this._addBodyAttrs();
264278
this.trigger('active', { tour: this });
265279

266280
Shepherd.activeTour = this;
@@ -291,6 +305,24 @@ export class Tour extends Evented {
291305
this._setupActiveTour();
292306
}
293307
}
308+
309+
_setTourID() {
310+
const tourName = this.options.tourName || 'tour';
311+
const uuid = uniqueId();
312+
313+
this.id = `${tourName}--${uuid}`;
314+
}
315+
316+
_addBodyAttrs() {
317+
document.body.setAttribute('data-shepherd-active-tour', this.id);
318+
document.body.classList.add('shepherd-active');
319+
}
320+
321+
_removeBodyAttrs() {
322+
document.body.removeAttribute('data-shepherd-active-tour', this.id);
323+
document.body.classList.remove('shepherd-active');
324+
}
325+
294326
}
295327

296328
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)