-
Notifications
You must be signed in to change notification settings - Fork 10.3k
fix: Several Fixes for Scroll Handling and Restoration #24306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8373529
fix(gatsby-react-router-scroll): Major improvements to scroll handling
blainekasten 23344c0
Finish migration and keep previous compatibility
blainekasten 8c3cbe5
fix lints
blainekasten e00e94a
Update packages/gatsby-react-router-scroll/src/index.ts
blainekasten 722d9ab
Update packages/gatsby-react-router-scroll/src/scroll-container.tsx
blainekasten 9ed5960
Update packages/gatsby-react-router-scroll/src/scroll-handler.tsx
blainekasten 5424060
fix a bug with state popping when saved state is set to 0
blainekasten 7c6f0dc
merge master
blainekasten 15596d6
use scrollTo
blainekasten d114260
deprecate scroll container
blainekasten 5d2ce5b
add deprecation warning
blainekasten b93db89
Update packages/gatsby-react-router-scroll/src/scroll-container.tsx
blainekasten e9958ed
fix lint
blainekasten aa4d48d
remove null usage as it wasnt really used
blainekasten 9cc5396
Fix tests and add more
blainekasten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
89 changes: 0 additions & 89 deletions
89
packages/gatsby-react-router-scroll/src/ScrollBehaviorContext.js
This file was deleted.
Oops, something went wrong.
83 changes: 0 additions & 83 deletions
83
packages/gatsby-react-router-scroll/src/ScrollContainer.js
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,3 @@ | ||
export { ScrollHandler as ScrollContext } from "./scroll-handler" | ||
export { ScrollContainer } from "./scroll-container" | ||
export { useScrollRestoration } from "./use-scroll-restoration" |
96 changes: 96 additions & 0 deletions
96
packages/gatsby-react-router-scroll/src/scroll-container.tsx
This file contains hidden or 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,96 @@ | ||
// TODO: In Gatsby v3, this file should be removed. | ||
// We are deprecating this in V2 in favor of useScrollRestoration | ||
import * as 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, | ||
} | ||
|
||
interface IProps { | ||
scrollKey: string | ||
shouldUpdateScroll?: Function | ||
children: React.ReactNode | ||
} | ||
|
||
interface IPropsWithContextAndLocation extends IProps { | ||
context: SessionStorage | ||
location: HLocation | ||
} | ||
|
||
let hasNotWarnedDeprecation = true | ||
|
||
class ScrollContainerImplementation extends React.Component< | ||
IPropsWithContextAndLocation | ||
> { | ||
constructor(props: IPropsWithContextAndLocation) { | ||
super(props) | ||
|
||
if (process.env.NODE_ENV !== `production` && hasNotWarnedDeprecation) { | ||
hasNotWarnedDeprecation = false | ||
console.log( | ||
`Deprecation Warning: | ||
|
||
Gatsby <ScrollContainer> is deprecated in Gatsby v2 and will be removed in Gatsby v3. | ||
Update to the React hook alternative useScrollRestoration, like this:. | ||
|
||
\`\`\` | ||
import React from 'react'; | ||
import { useScrollRestoration } from 'gatsby-react-router-scroll'; | ||
|
||
function Component() { | ||
const scrollRestoration = useScrollRestoration('${this.props.scrollKey}'); | ||
|
||
return <ul {...scrollRestoration} />; | ||
} | ||
\`\`\` | ||
` | ||
) | ||
} | ||
} | ||
|
||
componentDidMount(): void { | ||
// eslint-disable-next-line react/no-find-dom-node | ||
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 || 0) | ||
} | ||
|
||
render(): React.ReactNode { | ||
return this.props.children | ||
} | ||
} | ||
|
||
export const ScrollContainer = (props: IProps): React.ReactNode => ( | ||
<Location> | ||
{({ location }): React.ReactNode => ( | ||
<ScrollContext.Consumer> | ||
{(context): React.ReactNode => ( | ||
<ScrollContainerImplementation | ||
{...props} | ||
context={context} | ||
location={location} | ||
/> | ||
)} | ||
</ScrollContext.Consumer> | ||
)} | ||
</Location> | ||
) | ||
|
||
ScrollContainer.propTypes = propTypes |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.