Skip to content

Commit 2a52314

Browse files
authored
Merge pull request #1 from KefirNot/master
WBS-2116 scroll for elementId implemented
2 parents cd59e34 + 7fbae36 commit 2a52314

File tree

1 file changed

+46
-30
lines changed

1 file changed

+46
-30
lines changed

src/main.js

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,52 @@
11
const storage = [];
22

3-
module.exports = ({ history, reset = true, limit = 20 }) => ({
4-
start: () => {
5-
if(history.action === 'PUSH') {
6-
storage.push([
7-
window.scrollX,
8-
window.scrollY
9-
]);
3+
module.exports = ({ history, elementId, reset = true, limit = 20 }) => {
4+
const $el = elementId ? document.getElementById(elementId) : window;
5+
6+
return {
7+
start: () => {
8+
if(history.action === 'PUSH') {
9+
if($el) {
10+
if(elementId) {
11+
storage.push([
12+
$el.scrollLeft,
13+
$el.scrollTop
14+
]);
15+
} else {
16+
storage.push([
17+
$el.scrollX,
18+
$el.scrollY
19+
]);
20+
}
21+
}
1022

11-
if(storage.length > limit) {
12-
delete storage.shift();
23+
if(storage.length > limit) {
24+
delete storage.shift();
25+
}
1326
}
14-
}
15-
},
16-
render: () => {
17-
const { location } = history;
18-
const state = location.state || {};
19-
const { resetScroll, resetOnAction = 'PUSH'} = state;
20-
let position = [0, 0];
27+
},
28+
render: () => {
29+
const { location } = history;
30+
const state = location.state || {};
31+
const { resetScroll, resetOnAction = 'PUSH'} = state;
32+
let position = [0, 0];
2133

22-
switch (history.action) {
23-
case 'POP':
24-
if(storage.length && !(resetScroll && resetOnAction === 'POP')) {
25-
position = storage.pop();
26-
}
27-
window.scrollTo(...position);
28-
break;
29-
case 'PUSH':
30-
if(reset || (resetScroll && resetOnAction === 'PUSH')) {
31-
window.scrollTo(...position);
32-
}
33-
break;
34+
switch (history.action) {
35+
case 'POP':
36+
if(storage.length && !(resetScroll && resetOnAction === 'POP')) {
37+
position = storage.pop();
38+
}
39+
40+
$el && $el.scrollTo(...position);
41+
42+
break;
43+
case 'PUSH':
44+
if(reset || (resetScroll && resetOnAction === 'PUSH')) {
45+
$el && $el.scrollTo(...position);
46+
}
47+
48+
break;
49+
}
3450
}
35-
}
36-
});
51+
};
52+
};

0 commit comments

Comments
 (0)