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(v2): restore previous scroll position on back button click #5081

Merged
merged 1 commit into from Jun 29, 2021

Conversation

lex111
Copy link
Contributor

@lex111 lex111 commented Jun 29, 2021

Motivation

Quick attempt to fix #5076

Currently, after changing the location, we focus on the skip link to focus on that link at first. However, this has the side effect that when the user clicks on the browser's back button, then the previous scroll position is lost. To avoid this we can focus on the skip link only when moving forward in browser history.

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

Scroll a bit on any doc page, then go to another page, then press on back button in the browser, as result is the previous scroll position should be preserved.

Related PRs

(If this PR adds or changes functionality, please take some time to update the docs at https://github.com/facebook/docusaurus, and link to your PR here.)

@lex111 lex111 added the pr: bug fix This PR fixes a bug in a past release. label Jun 29, 2021
@lex111 lex111 requested a review from slorber as a code owner June 29, 2021 10:06
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Jun 29, 2021
@@ -19,6 +20,7 @@ function programmaticFocus(el: HTMLElement) {

function SkipToContent(): JSX.Element {
const containerRef = useRef<HTMLDivElement>(null);
const {action} = useHistory();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perhaps action.history should be part of location object in useLocationChange hook?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not really needed until we have a 2nd use-case to access history.action so as you want. Will merge it in current state for now

@netlify
Copy link

netlify bot commented Jun 29, 2021

✔️ [V2]

🔨 Explore the source changes: 57d9b20

🔍 Inspect the deploy log: https://app.netlify.com/sites/docusaurus-2/deploys/60daf0971a16b20007f38bde

😎 Browse the preview: https://deploy-preview-5081--docusaurus-2.netlify.app

@github-actions
Copy link

⚡️ Lighthouse report for the changes in this PR:

Category Score
🟠 Performance 83
🟢 Accessibility 98
🟢 Best practices 100
🟢 SEO 100
🟢 PWA 95

Lighthouse ran on https://deploy-preview-5081--docusaurus-2.netlify.app/

@github-actions
Copy link

Size Change: 0 B

Total Size: 601 kB

ℹ️ View Unchanged
Filename Size
website/build/assets/css/styles.********.css 87.7 kB
website/build/assets/js/main.********.js 415 kB
website/build/blog/2017/12/14/introducing-docusaurus/index.html 66.5 kB
website/build/docs/introduction/index.html 235 B
website/build/index.html 31.3 kB

compressed-size-action

@Andrew-web-coder
Copy link

I would like to have scroll position restored when navigating both ways (back and forward), because ... that is how webpages work.

@lex111
Copy link
Contributor Author

lex111 commented Jun 29, 2021

It also will work correctly if you move forward in history. You can check this behavior on the preview site, it should work.

@Andrew-web-coder
Copy link

Yes, indeed, thank you so much for your work!

@slorber
Copy link
Collaborator

slorber commented Jun 29, 2021

Thanks @lex111 ! that seems to work fine

@slorber slorber merged commit 7fe2a98 into master Jun 29, 2021
@slorber slorber deleted the lex111/iss5076 branch August 17, 2021 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: bug fix This PR fixes a bug in a past release.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Scroll position is lost when navigating the page (Chrome)
4 participants