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

Do not scroll to top if the URL pathname has not changed #813

Merged
merged 6 commits into from
Mar 8, 2022
Merged

Conversation

frandiox
Copy link
Contributor

@frandiox frandiox commented Mar 4, 2022

Description

Closes #810

The scroll should only be reset when the pathname changes. Or is there any use case where this is different?

  • This also fixes a bug where scroll position was reset to the top after closing mobile menu.

Before submitting the PR, please make sure you do the following:

  • Read the Contributing Guidelines
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123)
  • Update docs in this repository according to your change
  • Run yarn changeset add if this PR cause a version bump based on Keep a Changelog and adheres to Semantic Versioning

@frandiox frandiox requested a review from a team March 4, 2022 12:04
@cartogram
Copy link
Contributor

Comparing this branch to main it's an improvement, but I'm still seeing some unnecessary (and probably unrelated) scroll jumps.

Screen.Recording.2022-03-04.at.15.13.44.mov

@frandiox
Copy link
Contributor Author

frandiox commented Mar 7, 2022

@cartogram Apparently, the mobile menu was triggering a different reset by default.
Also, I noticed opening + closing the menu was resetting the scroll to the top (different bug). This should now be fixed 👍

Copy link
Contributor

@jplhomer jplhomer left a comment

Choose a reason for hiding this comment

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

Something worries me about referencing and mutating module-scoped variables in useEffect — maybe related to concurrent rendering (Suspense) or general rules of hooks in React. But if this solves the issue, then let's :shipit:

…ller chunks (#832)

* Change RouterClient to be a shared component to reduce server bundle size

* Change ServerStateProvider to be a shared component and avoid extra bundling in client build

* Update changeset
window.scrollTo(0, 0);
}

currentPath = serverState.pathname;
}, [pending]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the effect dependency include serverState.pathname?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as I understand, pending already changes every time the server state is updated since the whole thing is a React transition. Do we still need to add serverState.pathname in that case? 🤔

@blittle
Copy link
Contributor

blittle commented Mar 7, 2022

Something worries me about referencing and mutating module-scoped variables in useEffect — maybe related to concurrent rendering (Suspense) or general rules of hooks in React. But if this solves the issue, then let's :shipit:

@jplhomer I think it's fine. A more "react" way would be to use useRef, but that would also make the code more confusing. But because there is only one MobileNavigation component, just using a global module variable is simple.

@blittle
Copy link
Contributor

blittle commented Mar 7, 2022

I might add a e2e test for this. I know I fixed this back in October when I first joined. We've since regressed again with the same issue. :(

@frandiox
Copy link
Contributor Author

frandiox commented Mar 8, 2022

About mutating the module variable, this should only happen in the browser and we only support 1 instance of these components. The problem with having these variables within React is that mutations were triggering extra refreshes and breaking hydration. We'd need to wrap it all in startTransition and it seemed more complicated than this 🤔

@frandiox frandiox merged commit b1b959c into main Mar 8, 2022
@frandiox frandiox deleted the fd-scroll branch March 8, 2022 05:47
@github-actions github-actions bot mentioned this pull request Mar 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Router scrolls to top whenever server state is finished being updated.
5 participants