Skip to content

fix(v2): restore previous scroll position on back button click - #5081

Merged
slorber merged 1 commit into
masterfrom
lex111/iss5076
Jun 29, 2021
Merged

slorber merged 1 commit into
masterfrom
lex111/iss5076

Conversation

@lex111

@lex111 lex111 commented Jun 29, 2021 •

Copy link
Copy Markdown
Contributor

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

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

Copy link
Copy Markdown
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
Copy Markdown
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

netlify Bot commented Jun 29, 2021

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown

⚡️ 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
Copy Markdown

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
Copy Markdown

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

@lex111

lex111 commented Jun 29, 2021

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown

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

@slorber

slorber commented Jun 29, 2021

Copy link
Copy Markdown
Collaborator

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
mrizwanashiq pushed a commit to mrizwanashiq/docusaurus that referenced this pull request Jun 25, 2026
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