Skip to content

Commit 1a7bc3a

Browse files
authored
Merge pull request usablica#1124 from usablica/fixing-scrollToParent
fix(scrollParentToElement): should not run if scrollToElement is set to false
2 parents 918ca7a + e0fdfc6 commit 1a7bc3a

File tree

6 files changed

+124
-35
lines changed

6 files changed

+124
-35
lines changed

src/core/showElement.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import setShowElement from "../util/setShowElement";
22
import scrollParentToElement from "../util/scrollParentToElement";
3-
import getScrollParent from "../util/getScrollParent";
43
import addClass from "../util/addClass";
54
import scrollTo from "../util/scrollTo";
65
import exitIntro from "./exitIntro";
@@ -106,13 +105,8 @@ export default function _showElement(targetElement) {
106105
oldtooltipContainer.style.opacity = 0;
107106
oldtooltipContainer.style.display = "none";
108107

109-
// scroll to element
110-
scrollParent = getScrollParent(targetElement.element);
111-
112-
if (scrollParent !== document.body) {
113-
// target is within a scrollable element
114-
scrollParentToElement(scrollParent, targetElement.element);
115-
}
108+
// if the target element is within a scrollable element
109+
scrollParentToElement.call(self, targetElement);
116110

117111
// set new position to helper layer
118112
setHelperLayerPosition.call(self, oldHelperLayer);
@@ -224,13 +218,8 @@ export default function _showElement(targetElement) {
224218
"box-shadow": `0 0 1px 2px rgba(33, 33, 33, 0.8), rgba(33, 33, 33, ${self._options.overlayOpacity.toString()}) 0 0 0 5000px`,
225219
});
226220

227-
// scroll to element
228-
scrollParent = getScrollParent(targetElement.element);
229-
230-
if (scrollParent !== document.body) {
231-
// target is within a scrollable element
232-
scrollParentToElement(scrollParent, targetElement.element);
233-
}
221+
// target is within a scrollable element
222+
scrollParentToElement.call(self, targetElement);
234223

235224
//set new position to helper layer
236225
setHelperLayerPosition.call(self, helperLayer);

src/util/scrollParentToElement.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1+
import getScrollParent from "./getScrollParent";
2+
13
/**
24
* scroll a scrollable element to a child element
35
*
4-
* @param Element parent
5-
* @param Element element
6-
* @return Null
6+
* @param {Object} targetElement
77
*/
8-
export default function scrollParentToElement(parent, { offsetTop }) {
9-
parent.scrollTop = offsetTop - parent.offsetTop;
8+
export default function scrollParentToElement(targetElement) {
9+
const element = targetElement.element;
10+
11+
if (!this._options.scrollToElement) return;
12+
13+
const parent = getScrollParent(element);
14+
15+
if (parent === document.body) return;
16+
17+
parent.scrollTop = element.offsetTop - parent.offsetTop;
1018
}

src/util/scrollTo.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import elementInViewport from "./elementInViewport";
55
* To change the scroll of `window` after highlighting an element
66
*
77
* @api private
8-
* @method _scrollTo
98
* @param {String} scrollTo
109
* @param {Object} targetElement
1110
* @param {Object} tooltipLayer

tests/helper.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
export function find(selector) {
2-
if (typeof selector === 'string') {
2+
if (typeof selector === "string") {
33
return document.querySelector(selector);
44
}
55

66
if (selector instanceof Element) {
77
return selector;
88
}
99

10-
throw Error('invalid selector');
10+
throw Error("invalid selector");
1111
}
1212

1313
export function content(selector) {
@@ -31,21 +31,34 @@ export function className(selector) {
3131
}
3232

3333
export function skipButton() {
34-
return find('.introjs-skipbutton');
34+
return find(".introjs-skipbutton");
3535
}
3636

3737
export function nextButton() {
38-
return find('.introjs-nextbutton');
38+
return find(".introjs-nextbutton");
3939
}
4040

4141
export function prevButton() {
42-
return find('.introjs-prevbutton');
42+
return find(".introjs-prevbutton");
4343
}
4444

4545
export function doneButton() {
46-
return find('.introjs-donebutton');
46+
return find(".introjs-donebutton");
4747
}
4848

4949
export function tooltipText() {
50-
return find('.introjs-tooltiptext');
50+
return find(".introjs-tooltiptext");
51+
}
52+
53+
/**
54+
* @returns {Element}
55+
*/
56+
export function appendDummyElement(name, text, style) {
57+
const el = document.createElement(name || "p");
58+
el.innerHTML = text || "hello world";
59+
el.style = style;
60+
61+
document.body.appendChild(el);
62+
63+
return el;
5164
}

tests/index.test.js

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
import introJs from "../src";
2-
import {content, className, skipButton, nextButton, prevButton, doneButton, tooltipText} from "./helper";
2+
import {
3+
content,
4+
className,
5+
skipButton,
6+
nextButton,
7+
prevButton,
8+
doneButton,
9+
tooltipText,
10+
appendDummyElement,
11+
} from "./helper";
312

413
describe("intro", () => {
514
beforeEach(() => {
@@ -131,4 +140,75 @@ describe("intro", () => {
131140
"introjs-relativePosition"
132141
);
133142
});
143+
144+
test("should highlight the target element", () => {
145+
const p = appendDummyElement();
146+
147+
introJs()
148+
.setOptions({
149+
steps: [
150+
{
151+
intro: "step one",
152+
element: document.querySelector("p"),
153+
},
154+
],
155+
})
156+
.start();
157+
158+
expect(p.className).toContain("introjs-showElement");
159+
expect(p.className).toContain("introjs-relativePosition");
160+
});
161+
162+
test("should not highlight the target element if queryString is incorrect", () => {
163+
const p = appendDummyElement();
164+
165+
introJs()
166+
.setOptions({
167+
steps: [
168+
{
169+
intro: "step one",
170+
element: document.querySelector("div"),
171+
},
172+
],
173+
})
174+
.start();
175+
176+
expect(p.className).not.toContain("introjs-showElement");
177+
expect(className(".introjs-showElement")).toContain(
178+
"introjsFloatingElement"
179+
);
180+
});
181+
182+
test("should not add relativePosition if target element is fixed or absolute", () => {
183+
const absolute = appendDummyElement(
184+
"h1",
185+
"hello world",
186+
"position: absolute"
187+
);
188+
const fixed = appendDummyElement("h2", "hello world", "position: fixed");
189+
190+
const intro = introJs();
191+
intro
192+
.setOptions({
193+
steps: [
194+
{
195+
intro: "step one",
196+
element: document.querySelector("h1"),
197+
},
198+
{
199+
intro: "step two",
200+
element: document.querySelector("h2"),
201+
},
202+
],
203+
})
204+
.start();
205+
206+
expect(absolute.className).toContain("introjs-showElement");
207+
expect(absolute.className).not.toContain("introjs-relativePosition");
208+
209+
intro.nextStep();
210+
211+
expect(fixed.className).toContain("introjs-showElement");
212+
expect(fixed.className).not.toContain("introjs-relativePosition");
213+
});
134214
});

tests/util/addClass.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ describe("addClass", () => {
1717
});
1818

1919
test("should append when element is SVG", () => {
20-
const el = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
21-
el.setAttribute('class', "firstClass");
20+
const el = document.createElementNS("http://www.w3.org/2000/svg", "svg");
21+
el.setAttribute("class", "firstClass");
2222

2323
addClass(el, "secondClass");
2424

25-
expect(el.getAttribute('class')).toBe("firstClass secondClass");
25+
expect(el.getAttribute("class")).toBe("firstClass secondClass");
2626
});
2727

2828
test("should not append duplicate classNames to svg elements", () => {
29-
const el = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
30-
el.setAttribute('class', "firstClass");
29+
const el = document.createElementNS("http://www.w3.org/2000/svg", "svg");
30+
el.setAttribute("class", "firstClass");
3131

3232
addClass(el, "firstClass");
3333

34-
expect(el.getAttribute('class')).toBe("firstClass");
34+
expect(el.getAttribute("class")).toBe("firstClass");
3535
});
3636

3737
test("should not append duplicate classNames to elements", () => {

0 commit comments

Comments
 (0)