-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathScrollManager.js
101 lines (84 loc) · 3.26 KB
/
ScrollManager.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// import StateStorage from '@catamphetamine/farce/StateStorage';
// import StateStorage from '@catamphetamine/farce/lib/esm/StateStorage.js';
// import StateStorage from '@catamphetamine/farce/lib/cjs/StateStorage.js';
import { StateStorage } from '@catamphetamine/farce';
import React, { useRef, useEffect, useMemo, useCallback, createContext } from 'react';
// import ScrollBehavior from 'scroll-behavior';
import ScrollBehavior from '../scroll-behavior/index.js';
const STORAGE_NAMESPACE = '@@scroll';
export const ScrollContext = createContext(null);
const defaultCreateScrollBehavior = config => new ScrollBehavior(config);
// Rewrote `<ScrollManager/>` from `found-scroll` in React hooks.
// Also fixed React strict mode bug.
// /~https://github.com/4Catalyzer/found-scroll/issues/382
export default function ScrollManager({
renderArgs,
shouldUpdateScroll: shouldUpdateScrollProperty,
createScrollBehavior = defaultCreateScrollBehavior,
children
}) {
const { router, location } = renderArgs;
const prevRenderArgs = useRef(null);
const scrollBehaviorRef = useRef();
const scrollBehavior = scrollBehaviorRef.current;
const getCurrentLocation = useCallback(() => {
return location;
}, [location]);
const getCurrentLocationRef = useRef();
getCurrentLocationRef.current = getCurrentLocation;
const shouldUpdateScroll = useCallback((prevRenderArgs, renderArgs) => {
if (!shouldUpdateScrollProperty) {
return true;
}
// A hack to allow access to `ScrollBehavior` internals (e.g. `stateStorage`).
return shouldUpdateScrollProperty.call(scrollBehavior, prevRenderArgs, renderArgs);
}, [scrollBehavior]);
const registerScrollElement = useCallback((key, element) => {
scrollBehavior.registerElement(key, element, shouldUpdateScroll, renderArgs);
return () => {
scrollBehavior.unregisterElement(key);
};
}, [
shouldUpdateScroll,
scrollBehavior,
renderArgs
]);
const scrollContext = useMemo(() => ({
scrollBehavior,
registerScrollElement
}), [
scrollBehavior,
registerScrollElement
]);
useEffect(() => {
const scrollBehavior = createScrollBehavior({
addNavigationListener: router.addNavigationListener,
stateStorage: new StateStorage(router, STORAGE_NAMESPACE),
getCurrentLocation: () => getCurrentLocationRef.current(),
shouldUpdateScroll: (prevRenderArgs, renderArgs) => shouldUpdateScroll(prevRenderArgs, renderArgs)
});
scrollBehaviorRef.current = scrollBehavior;
return () => {
scrollBehavior.stop();
}
}, []);
useEffect(() => {
const scrollBehavior = scrollBehaviorRef.current;
const prevLocation = prevRenderArgs.current && prevRenderArgs.current.location;
if (renderArgs.location === prevLocation || !(renderArgs.elements || renderArgs.error)) {
// If the location hasn't actually changed, or if we're in a global
// pending state, don't update the scroll position.
return;
}
scrollBehavior.updateScroll(prevRenderArgs.current, renderArgs);
prevRenderArgs.current = renderArgs;
});
// return (
// <ScrollContext.Provider value={scrollContext}>
// {children}
// </ScrollContext.Provider>
// );
return React.createElement(ScrollContext.Provider, {
value: scrollContext
}, children);
}