forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrollManagementMixin.js
More file actions
69 lines (54 loc) · 1.88 KB
/
ScrollManagementMixin.js
File metadata and controls
69 lines (54 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import React from 'react';
import { Actions } from 'history';
import { canUseDOM, setWindowScrollPosition } from './DOMUtils';
var { func } = React.PropTypes;
function getCommonAncestors(routes, otherRoutes) {
return routes.filter(route => otherRoutes.indexOf(route) !== -1);
}
function shouldUpdateScrollPosition(state, prevState) {
var { location, routes } = state;
var { location: prevLocation, routes: prevRoutes } = prevState;
// When an onEnter hook uses transition.to to redirect
// on the initial load prevLocation is null, so assume
// we don't want to update the scroll position.
if (prevLocation === null)
return false;
// Don't update scroll position if only the query has changed.
if (location.pathname === prevLocation.pathname)
return false;
// Don't update scroll position if any of the ancestors
// has `ignoreScrollPosition` set to `true` on the route.
var sharedAncestors = getCommonAncestors(routes, prevRoutes);
if (sharedAncestors.some(route => route.ignoreScrollBehavior))
return false;
return true;
}
function updateWindowScrollPosition(action, scrollX, scrollY) {
if (canUseDOM) {
if (action === Actions.POP) {
setWindowScrollPosition(scrollX, scrollY);
} else {
setWindowScrollPosition(0, 0);
}
}
}
var ScrollManagementMixin = {
propTypes: {
shouldUpdateScrollPosition: func.isRequired,
updateScrollPosition: func.isRequired
},
getDefaultProps() {
return {
shouldUpdateScrollPosition,
updateScrollPosition: updateWindowScrollPosition
};
},
componentDidUpdate(prevProps, prevState) {
var { location } = this.state;
if (location && this.props.shouldUpdateScrollPosition(this.state, prevState)) {
var { action, scrollX, scrollY } = location;
this.props.updateScrollPosition(action, scrollX || 0, scrollY || 0);
}
}
};
export default ScrollManagementMixin;