-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finish migration and keep previous compatibility
- Loading branch information
1 parent
8373529
commit 23344c0
Showing
7 changed files
with
142 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { ScrollHandler as ScrollContext } from "./scroll-handler" | ||
import { ScrollContainer } from "./scroll-container" | ||
import { useScrollRestoration } from "./use-scroll-restoration" | ||
|
||
export { ScrollContext, ScrollContainer, useScrollRestoration } |
64 changes: 64 additions & 0 deletions
64
packages/gatsby-react-router-scroll/src/scroll-container.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import React from "react" | ||
import ReactDOM from "react-dom" | ||
import PropTypes from "prop-types" | ||
import { ScrollContext } from "./scroll-handler" | ||
import { SessionStorage } from "./session-storage" | ||
import { Location } from "@reach/router" | ||
import { Location as HLocation } from "history" | ||
|
||
const propTypes = { | ||
scrollKey: PropTypes.string.isRequired, | ||
shouldUpdateScroll: PropTypes.func, | ||
children: PropTypes.element.isRequired, | ||
} | ||
|
||
type Props = { | ||
scrollKey: string | ||
shouldUpdateScroll?: Function | ||
children: React.ReactNode | ||
} | ||
|
||
type PropsWithContextAndLocation = Props & { | ||
context: SessionStorage | ||
location: HLocation | ||
} | ||
|
||
class ScrollContainerImplementation extends React.Component< | ||
PropsWithContextAndLocation | ||
> { | ||
componentDidMount() { | ||
const node = ReactDOM.findDOMNode(this) as Element | ||
const { location, scrollKey } = this.props | ||
|
||
if (!node) return | ||
|
||
node.addEventListener("scroll", () => { | ||
this.props.context.save(location, scrollKey, node.scrollTop) | ||
}) | ||
|
||
const position = this.props.context.read(location, scrollKey) | ||
node.scrollTo(0, position) | ||
} | ||
|
||
render() { | ||
return this.props.children | ||
} | ||
} | ||
|
||
export const ScrollContainer = (props: Props) => ( | ||
<Location> | ||
{({ location }) => ( | ||
<ScrollContext.Consumer> | ||
{context => ( | ||
<ScrollContainerImplementation | ||
{...props} | ||
context={context} | ||
location={location} | ||
/> | ||
)} | ||
</ScrollContext.Consumer> | ||
)} | ||
</Location> | ||
) | ||
|
||
ScrollContainer.propTypes = propTypes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
packages/gatsby-react-router-scroll/src/use-scroll-restoration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { ScrollContext } from "./scroll-handler" | ||
import { useRef, useContext, useLayoutEffect } from "react" | ||
import { useLocation } from "@reach/router" | ||
|
||
export function useScrollRestoration(identifier: string) { | ||
const location = useLocation() | ||
const state = useContext(ScrollContext) | ||
const ref = useRef<HTMLElement>() | ||
|
||
useLayoutEffect(() => { | ||
const position = state.read(location, identifier) | ||
ref.current!.scrollTo(0, position) | ||
}, []) | ||
|
||
return { | ||
ref, | ||
onScroll() { | ||
state.save(location, identifier, ref.current!.scrollTop) | ||
}, | ||
} | ||
} |