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

Fix scroll restoration for layout components #26861

Merged
merged 5 commits into from Apr 26, 2021

Conversation

micha149
Copy link
Contributor

Description

Main focus of this PR is to fix the useScrollRestoration hook to work in layout components which are not re-rendered on a location change.

To be able to check the results I added previously missing tests. For this I addded some generic typings to the hook to be able to use it well typed in the tests on a div element (have a look at #26458).

Documentation

This should not affect the currently documented usage.

Related Issues

Fixes #26458

@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Sep 11, 2020
@micha149 micha149 changed the title Fix scroll restoration on layout Fix scroll restoration for layout components Sep 11, 2020
this is required to match the type of `useRef` which requires the target
element type to be specified.

The hook can be used like the following:
```typescript
const MyComponent: React.FunctionComponent = () => {
  const scrollRestorationProps = useScrollRestoration<HTMLDivElement>(`some-key`)
  return (
    <div
      {...scrollRestorationProps}
      style={{ overflow: `auto` }}
    >
      Test
    </div>
  )
}
```

Fixes: gatsbyjs#26458
Previously, the scroll position was only updated when the using
component was re-rendered. This did not work when the hook is used in
a wrapping Layout component, becaus its persitent over location changes.
Adding the location key to useEffect will cause the scroll position is
updated every time the key changes.
@micha149 micha149 force-pushed the fix-scroll-restoration-on-layout branch from 330ac40 to 987d950 Compare September 11, 2020 07:39
@sidharthachatterjee sidharthachatterjee added status: needs core review Currently awaiting review from Core team member topic: core Relates to Gatsby's core (e.g. page loading, reporter, state machine) and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Sep 14, 2020
@wardpeet wardpeet self-assigned this Sep 25, 2020
@micha149
Copy link
Contributor Author

@wardpeet Would you like to add the hacktoberfest-accepted label? I hope it will count on my contributions even though I have opened this PR in september.

Can you estimate when this PR will be reviewed? I had to add a really nasty workaround to our website to be able to go live and would like to remove it as soon as possible.

@wardpeet
Copy link
Contributor

I don't know if it works that way but i'll review it tomorrow, if not feel free to re-open and re-create it

@micha149
Copy link
Contributor Author

Unfortunately it did not work. Anyway, I don't want to interrupt the review process. Thank you for trying… I am looking forward to the outcome of the review!

Copy link
Contributor

@vladar vladar left a comment

Choose a reason for hiding this comment

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

Thank you for the PR! It looks good to me 💜
Sorry for not coming back to this in time - we are a bit short on time dedicated to PR reviews 😞

@vladar vladar merged commit f57efab into gatsbyjs:master Apr 26, 2021
@vladar vladar added this to To cherry-pick in Release candidate via automation Apr 26, 2021
vladar pushed a commit that referenced this pull request Apr 27, 2021
…ents (#26861)

* add generic type to useScrollRestoration hook

this is required to match the type of `useRef` which requires the target
element type to be specified.

The hook can be used like the following:
```typescript
const MyComponent: React.FunctionComponent = () => {
  const scrollRestorationProps = useScrollRestoration<HTMLDivElement>(`some-key`)
  return (
    <div
      {...scrollRestorationProps}
      style={{ overflow: `auto` }}
    >
      Test
    </div>
  )
}
```

Fixes: #26458

* add tests for useScrollRestoration hook

* fix useScrollResotration to update position on location change

Previously, the scroll position was only updated when the using
component was re-rendered. This did not work when the hook is used in
a wrapping Layout component, becaus its persitent over location changes.
Adding the location key to useEffect will cause the scroll position is
updated every time the key changes.

* lint/ts fixes

Co-authored-by: Vladimir Razuvaev <vladimir.razuvaev@gmail.com>
(cherry picked from commit f57efab)
@vladar vladar moved this from To cherry-pick to Backport PR opened in Release candidate Apr 27, 2021
vladar pushed a commit that referenced this pull request Apr 27, 2021
…ents (#26861) (#31079)

* add generic type to useScrollRestoration hook

this is required to match the type of `useRef` which requires the target
element type to be specified.

The hook can be used like the following:
```typescript
const MyComponent: React.FunctionComponent = () => {
  const scrollRestorationProps = useScrollRestoration<HTMLDivElement>(`some-key`)
  return (
    <div
      {...scrollRestorationProps}
      style={{ overflow: `auto` }}
    >
      Test
    </div>
  )
}
```

Fixes: #26458

* add tests for useScrollRestoration hook

* fix useScrollResotration to update position on location change

Previously, the scroll position was only updated when the using
component was re-rendered. This did not work when the hook is used in
a wrapping Layout component, becaus its persitent over location changes.
Adding the location key to useEffect will cause the scroll position is
updated every time the key changes.

* lint/ts fixes

Co-authored-by: Vladimir Razuvaev <vladimir.razuvaev@gmail.com>
(cherry picked from commit f57efab)

Co-authored-by: Michael van Engelshoven <michael@van-engelshoven.de>
@vladar vladar moved this from Backport PR opened to Backported in Release candidate Apr 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs core review Currently awaiting review from Core team member topic: core Relates to Gatsby's core (e.g. page loading, reporter, state machine)
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

useScrollRestoration TypeScript typings don't work with HTMLDivElement
4 participants