Skip to content

Commit e9a3097

Browse files
committed
fix(scrollParentToElement): should not run if scrollToElement is set to false
1 parent 918ca7a commit e9a3097

File tree

6 files changed

+39
-35
lines changed

6 files changed

+39
-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: 7 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,21 @@ 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");
5151
}

tests/index.test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
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+
} from "./helper";
311

412
describe("intro", () => {
513
beforeEach(() => {

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)