Skip to content

Commit 8c3cbe5

Browse files
committed
fix lints
1 parent 23344c0 commit 8c3cbe5

File tree

4 files changed

+50
-33
lines changed

4 files changed

+50
-33
lines changed

packages/gatsby-react-router-scroll/src/scroll-container.tsx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,45 @@ const propTypes = {
1212
children: PropTypes.element.isRequired,
1313
}
1414

15-
type Props = {
15+
interface IProps {
1616
scrollKey: string
1717
shouldUpdateScroll?: Function
1818
children: React.ReactNode
1919
}
2020

21-
type PropsWithContextAndLocation = Props & {
21+
interface IPropsWithContextAndLocation extends IProps {
2222
context: SessionStorage
2323
location: HLocation
2424
}
2525

2626
class ScrollContainerImplementation extends React.Component<
27-
PropsWithContextAndLocation
27+
IPropsWithContextAndLocation
2828
> {
29-
componentDidMount() {
29+
componentDidMount(): void {
30+
// eslint-disable-next-line react/no-find-dom-node
3031
const node = ReactDOM.findDOMNode(this) as Element
3132
const { location, scrollKey } = this.props
3233

3334
if (!node) return
3435

35-
node.addEventListener("scroll", () => {
36+
node.addEventListener(`scroll`, () => {
3637
this.props.context.save(location, scrollKey, node.scrollTop)
3738
})
3839

3940
const position = this.props.context.read(location, scrollKey)
4041
node.scrollTo(0, position)
4142
}
4243

43-
render() {
44+
render(): React.ReactNode {
4445
return this.props.children
4546
}
4647
}
4748

48-
export const ScrollContainer = (props: Props) => (
49+
export const ScrollContainer = (props: IProps): React.ReactNode => (
4950
<Location>
50-
{({ location }) => (
51+
{({ location }): React.ReactNode => (
5152
<ScrollContext.Consumer>
52-
{context => (
53+
{(context): React.ReactNode => (
5354
<ScrollContainerImplementation
5455
{...props}
5556
context={context}

packages/gatsby-react-router-scroll/src/scroll-handler.tsx

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,42 @@ export class ScrollHandler extends React.Component<
2525

2626
_stateStorage: SessionStorage = new SessionStorage()
2727

28-
scrollListener = () => {
28+
scrollListener = (): void => {
2929
const { key } = this.props.location
3030

31-
this._stateStorage.save(this.props.location, key, window.scrollY)
31+
if (key) {
32+
this._stateStorage.save(this.props.location, key, window.scrollY)
33+
}
3234
}
3335

34-
componentDidMount() {
36+
componentDidMount(): void {
3537
window.addEventListener(`scroll`, this.scrollListener)
38+
let scrollPosition
39+
const { key, hash } = this.props.location
40+
41+
if (key) {
42+
scrollPosition = this._stateStorage.read(this.props.location, key)
43+
}
3644

37-
const scrollPosition = this._stateStorage.read(
38-
this.props.location,
39-
this.props.location.key
40-
)
4145
if (scrollPosition) {
4246
this.windowScroll(scrollPosition, undefined)
43-
} else if (this.props.location.hash) {
44-
this.scrollToHash(decodeURI(this.props.location.hash), undefined)
47+
} else if (hash) {
48+
this.scrollToHash(decodeURI(hash), undefined)
4549
}
4650
}
4751

48-
componentWillUnmount() {
52+
componentWillUnmount(): void {
4953
window.removeEventListener(`scroll`, this.scrollListener)
5054
}
5155

5256
componentDidUpdate(prevProps: LocationContext): void {
53-
const { hash } = this.props.location
57+
const { hash, key } = this.props.location
58+
let scrollPosition
59+
60+
if (key) {
61+
scrollPosition = this._stateStorage.read(this.props.location, key)
62+
}
5463

55-
const scrollPosition = this._stateStorage.read(
56-
this.props.location,
57-
this.props.location.key
58-
)
5964
if (scrollPosition) {
6065
this.windowScroll(scrollPosition, prevProps)
6166
} else if (hash) {
@@ -96,7 +101,7 @@ export class ScrollHandler extends React.Component<
96101
return shouldUpdateScroll.call(this, prevRouterProps, routerProps)
97102
}
98103

99-
render() {
104+
render(): React.ReactNode {
100105
return (
101106
<ScrollContext.Provider value={this._stateStorage}>
102107
{this.props.children}

packages/gatsby-react-router-scroll/src/session-storage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class SessionStorage {
2828
}
2929
}
3030

31-
save(location: Location, key: string, value: number) {
31+
save(location: Location, key: string, value: number): void {
3232
const stateKey = this.getStateKey(location, key)
3333
const storedValue = JSON.stringify(value)
3434

@@ -50,7 +50,7 @@ export class SessionStorage {
5050
}
5151
}
5252

53-
getStateKey(location: Location, key: string) {
53+
getStateKey(location: Location, key: string): string {
5454
const locationKey = location.key || location.pathname
5555
const stateKeyBase = `${STATE_KEY_PREFIX}${locationKey}`
5656
return key === null || typeof key === `undefined`

packages/gatsby-react-router-scroll/src/use-scroll-restoration.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,31 @@ import { ScrollContext } from "./scroll-handler"
22
import { useRef, useContext, useLayoutEffect } from "react"
33
import { useLocation } from "@reach/router"
44

5-
export function useScrollRestoration(identifier: string) {
5+
interface IScrollRestorationProps {
6+
ref: React.MutableRefObject<HTMLElement | undefined>
7+
onScroll(): void
8+
}
9+
10+
export function useScrollRestoration(
11+
identifier: string
12+
): IScrollRestorationProps {
613
const location = useLocation()
714
const state = useContext(ScrollContext)
815
const ref = useRef<HTMLElement>()
916

10-
useLayoutEffect(() => {
11-
const position = state.read(location, identifier)
12-
ref.current!.scrollTo(0, position)
17+
useLayoutEffect((): void => {
18+
if (ref.current) {
19+
const position = state.read(location, identifier)
20+
ref.current.scrollTo(0, position)
21+
}
1322
}, [])
1423

1524
return {
1625
ref,
17-
onScroll() {
18-
state.save(location, identifier, ref.current!.scrollTop)
26+
onScroll(): void {
27+
if (ref.current) {
28+
state.save(location, identifier, ref.current.scrollTop)
29+
}
1930
},
2031
}
2132
}

0 commit comments

Comments
 (0)