Skip to content
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

docs(gatsby-react-router-scroll): Add documentation for scroll restoration options #25450

Merged
merged 11 commits into from
Jul 10, 2020
41 changes: 41 additions & 0 deletions docs/docs/reach-router-and-gatsby.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The main reasons Gatsby uses `@reach/router` are:
1. Preloading. You can read more about preloading in the docs for the [Gatsby Link API](https://www.gatsbyjs.org/docs/gatsby-link/).
2. The [routing accessibility](https://reach.tech/router/accessibility) it provides.
3. It supports [server rendering](https://reach.tech/router/server-rendering) which helps Gatsby build routed files at build time.
4. Scroll restoration.
Copy link

@AishaBlake AishaBlake Jul 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably have a full sentence with a link to learn more, like the other list items. I wouldn't necessarily expect all (or even most?) of the people reading this know what scroll restoration is. What does using scroll restoration let us do? The link could be to the Reach docs or just the relevant section below.

Suggested change
4. Scroll restoration.
4. It supports [scroll restoration](), which allows Gatsby to better control pages' scroll position.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this change, but I don't really know what to link to. Maybe this? https://reacttraining.com/react-router/web/guides/scroll-restoration

Copy link

@AishaBlake AishaBlake Jul 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry! Yes, that's exactly what I meant by "Reach docs". That was, in no way, clear. 😳

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fwiw that's react router docs. Reach doesn't have a section on scroll restoration

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That... is true. It is the page I was thinking of, regardless. 😅


With Gatsby, you will mostly be using the `<Link />` component provided by the `gatsby` package. The [`<Link />` API docs](https://www.gatsbyjs.org/docs/gatsby-link/) explain the relationship between `gatsby` `<Link />` and `@reach/router` `<Link />` very nicely:

Expand All @@ -20,6 +21,46 @@ With Gatsby, you will mostly be using the `<Link />` component provided by the `

Besides using the [`<Link />` API](https://www.gatsbyjs.org/docs/gatsby-link/) for linking between pages Gatsby generates, you can define your own client-side routes. See the [client only paths example](https://github.com/gatsbyjs/gatsby/tree/master/examples/client-only-paths) on how to use `<Router />` from `@reach/router` to make client routes work seamlessly together with your server routes.

## Scroll Restoration

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 (deprecated) `ScrollContainer` component or `useScrollRestoration` hook in their code to tell Gatsby about scroll containers that we should track and restore.

This is an example of using the ScrollContainer component to render a list of countries in an overflow `ul` element.

```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>
);
}
}
```

Here is an example of using the `useScrollRestoration` hook with the same code.

```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>
);
}
```

## Other resources

- [Reach Router docs](https://reach.tech/router)
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby/cache-dir/gatsby-browser-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Link, {
navigateTo,
parsePath,
} from "gatsby-link"
import { useScrollRestoration } from "gatsby-react-router-scroll"
import PageRenderer from "./public-page-renderer"
import loader from "./loader"

Expand Down Expand Up @@ -107,6 +108,7 @@ export {
push, // TODO replace for v3
replace, // TODO remove replace for v3
navigateTo, // TODO: remove navigateTo for v3
useScrollRestoration,
StaticQueryContext,
StaticQuery,
PageRenderer,
Expand Down
9 changes: 8 additions & 1 deletion packages/gatsby/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ export {
withAssetPrefix,
} from "gatsby-link"

export const useScrollRestoration: (
key: string
) => {
ref: React.MutableRefObject<HTMLElement | undefined>
onScroll(): void
}

export const useStaticQuery: <TData = any>(query: any) => TData

export const parsePath: (path: string) => WindowLocation
Expand All @@ -41,7 +48,7 @@ export const prefetchPathname: (path: string) => void
* export default (props: PageProps) => {
*
* @example
* // When adding types for both pageContext (represended by LocaleLookUpInfo)
* // When adding types for both pageContext (represended by LocaleLookUpInfo)
* // and GraphQL query data (represented by IndexQueryProps)
*
* import {PageProps} from "gatsby"
Expand Down