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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import React, {useRef} from 'react';
import {useHistory} from '@docusaurus/router';
import Translate from '@docusaurus/Translate';
import {useLocationChange} from '@docusaurus/theme-common';

Expand All @@ -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

const handleSkip = (e: React.MouseEvent<HTMLAnchorElement>) => {
e.preventDefault();

Expand All @@ -32,7 +34,7 @@ function SkipToContent(): JSX.Element {
};

useLocationChange(({location}) => {
if (containerRef.current && !location.hash) {
if (containerRef.current && !location.hash && action !== 'POP') {
programmaticFocus(containerRef.current);
}
});
Expand Down