-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
scroll-handler.tsx
111 lines (92 loc) · 2.82 KB
/
scroll-handler.tsx
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
102
103
104
105
106
107
108
109
110
111
import * as React from "react"
import { LocationContext } from "@reach/router"
import PropTypes from "prop-types"
import { SessionStorage } from "./session-storage"
export const ScrollContext = React.createContext<SessionStorage>(
new SessionStorage()
)
ScrollContext.displayName = `GatsbyScrollContext`
type ShouldUpdateScrollFn = (
prevRouterProps: LocationContext | undefined,
routerProps: LocationContext
) => boolean
type ShouldUpdateScroll = undefined | ShouldUpdateScrollFn
export class ScrollHandler extends React.Component<
LocationContext & { shouldUpdateScroll: ShouldUpdateScroll }
> {
static propTypes = {
shouldUpdateScroll: PropTypes.func,
children: PropTypes.element.isRequired,
location: PropTypes.object.isRequired,
}
_stateStorage: SessionStorage = new SessionStorage()
scrollListener = (): void => {
const { key } = this.props.location
if (key) {
this._stateStorage.save(this.props.location, key, window.scrollY)
}
}
componentDidMount(): void {
window.addEventListener(`scroll`, this.scrollListener)
let scrollPosition
const { key, hash } = this.props.location
if (key) {
scrollPosition = this._stateStorage.read(this.props.location, key)
}
if (scrollPosition) {
this.windowScroll(scrollPosition, undefined)
} else if (hash) {
this.scrollToHash(decodeURI(hash), undefined)
}
}
componentWillUnmount(): void {
window.removeEventListener(`scroll`, this.scrollListener)
}
componentDidUpdate(prevProps: LocationContext): void {
const { hash, key } = this.props.location
let scrollPosition
if (key) {
scrollPosition = this._stateStorage.read(this.props.location, key)
}
if (hash && scrollPosition === 0) {
this.scrollToHash(decodeURI(hash), prevProps)
} else {
this.windowScroll(scrollPosition, prevProps)
}
}
windowScroll = (
position: number,
prevProps: LocationContext | undefined
): void => {
if (this.shouldUpdateScroll(prevProps, this.props)) {
window.scrollTo(0, position)
}
}
scrollToHash = (
hash: string,
prevProps: LocationContext | undefined
): void => {
const node = document.getElementById(hash.substring(1))
if (node && this.shouldUpdateScroll(prevProps, this.props)) {
node.scrollIntoView()
}
}
shouldUpdateScroll = (
prevRouterProps: LocationContext | undefined,
routerProps: LocationContext
): boolean => {
const { shouldUpdateScroll } = this.props
if (!shouldUpdateScroll) {
return true
}
// Hack to allow accessing this._stateStorage.
return shouldUpdateScroll.call(this, prevRouterProps, routerProps)
}
render(): React.ReactNode {
return (
<ScrollContext.Provider value={this._stateStorage}>
{this.props.children}
</ScrollContext.Provider>
)
}
}