-
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.
docs(gatsby-react-router-scroll): Add documentation for scroll restor…
…ation options (#25450) * docs(gatsby-react-router-scroll): Add documentation for scroll restoration options * feat(gatsby): export useScrollRestoration from main package * Update reach-router-and-gatsby.md * Update packages/gatsby/index.d.ts Co-authored-by: Aisha Blake <aisha@gatsbyjs.com> * Rearrange scroll restoration docs * chore: format * Add links to scroll restoration page * Fix doc link spelling * Update docs/docs/scroll-restoration.md Co-authored-by: Aisha Blake <aisha@gatsbyjs.com> Co-authored-by: Aisha Blake <aisha.g.blake@gmail.com> Co-authored-by: gatsbybot <mathews.kyle+gatsbybot@gmail.com>
- Loading branch information
1 parent
64685c3
commit 73ebb1e
Showing
7 changed files
with
61 additions
and
3 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 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
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,43 @@ | ||
--- | ||
title: Scroll Restoration | ||
--- | ||
|
||
Scroll restoration refers to the [`scrollRestoration`](https://developer.mozilla.org/en-US/docs/Web/API/History/scrollRestoration) property on the [`History`](https://developer.mozilla.org/en-US/docs/Web/API/History) API. This property allows restoring a user's scroll position when navigating to a new page. | ||
|
||
Gatsby will handle scroll restoration for you in most cases. However, when you render containers that have their own scroll values, those scroll positions are typically lost between page transitions. To solve that, users can use the `useScrollRestoration` hook or the (deprecated) `ScrollContainer` component in their code to tell Gatsby about scroll containers that we should track and restore. | ||
|
||
Here is an example of using the `useScrollRestoration` hook to render a list of countries in an overflow `ul` element. | ||
|
||
```jsx | ||
import { useScrollRestoration } from "gatsby"; | ||
import countryList from "../utils/country-list"; | ||
|
||
export default function PageComponent() { | ||
const ulScrollRestoration = useScrollRestoration(`page-component-ul-list`) | ||
|
||
return ( | ||
<ul style={{ height: 200, overflow: `auto` }} {...ulScrollRestoration}> | ||
{countryList.map(country => <li>{country}</li>) | ||
</ul> | ||
); | ||
} | ||
``` | ||
This is an example of using the (deprecated) `ScrollContainer` component with the same code. | ||
```jsx | ||
import { ScrollContainer } from "gatsby-react-router-scroll"; | ||
import countryList from "../utils/country-list"; | ||
|
||
export default class PageComponent extends React.Component { | ||
render() { | ||
return ( | ||
<ScrollContainer key="page-component-ul-list"> | ||
<ul style={{ height: 200, overflow: `auto` }}> | ||
{countryList.map(country => <li>{country}</li>) | ||
</ul> | ||
</ScrollContainer> | ||
); | ||
} | ||
} | ||
``` |
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
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