Skip to content

Commit

Permalink
fix(v2): do not focus on skip link if page refreshed (facebook#4797)
Browse files Browse the repository at this point in the history
* fix(v2): do not focus on skip link if page refreshed

* rename ref

Co-authored-by: slorber <[email protected]>
  • Loading branch information
lex111 and slorber authored May 18, 2021
1 parent ab19070 commit 0360364
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
* LICENSE file in the root directory of this source tree.
*/

import React, {useRef, useEffect} from 'react';
import React, {useRef} from 'react';
import Translate from '@docusaurus/Translate';
import {useLocation} from '@docusaurus/router';
import {useChangeRoute} from '@docusaurus/theme-common';

import styles from './styles.module.css';

function programmaticFocus(el: HTMLElement) {
Expand All @@ -18,8 +19,6 @@ function programmaticFocus(el: HTMLElement) {

function SkipToContent(): JSX.Element {
const containerRef = useRef<HTMLDivElement>(null);
const location = useLocation();

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

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

useEffect(() => {
if (!location.hash && containerRef.current) {
useChangeRoute(() => {
if (containerRef.current) {
programmaticFocus(containerRef.current);
}
}, [location.pathname]);
});

return (
<div ref={containerRef}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import {useState, useCallback, useEffect, useRef} from 'react';
import {useLocation} from '@docusaurus/router';
import useScrollPosition from '@theme/hooks/useScrollPosition';
import {useChangeRoute} from '@docusaurus/theme-common';
import type {useHideableNavbarReturns} from '@theme/hooks/useHideableNavbar';

const useHideableNavbar = (hideOnScroll: boolean): useHideableNavbarReturns => {
Expand Down Expand Up @@ -55,13 +56,13 @@ const useHideableNavbar = (hideOnScroll: boolean): useHideableNavbarReturns => {
[navbarHeight, isFocusedAnchor],
);

useEffect(() => {
useChangeRoute(() => {
if (!hideOnScroll) {
return;
}

setIsNavbarVisible(true);
}, [location.pathname]);
});

useEffect(() => {
if (!hideOnScroll) {
Expand Down
2 changes: 2 additions & 0 deletions packages/docusaurus-theme-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export {useTitleFormatter} from './utils/generalUtils';

export {usePluralForm} from './utils/usePluralForm';

export {useChangeRoute} from './utils/useChangeRoute';

export {
useDocsPreferredVersion,
useDocsPreferredVersionByPluginId,
Expand Down
21 changes: 21 additions & 0 deletions packages/docusaurus-theme-common/src/utils/useChangeRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {useRef, useEffect} from 'react';
import {useLocation} from '@docusaurus/router';

export function useChangeRoute(onRouteChange: () => void): void {
const {pathname} = useLocation();
const latestPathnameRef = useRef(pathname);

useEffect(() => {
if (pathname !== latestPathnameRef.current) {
latestPathnameRef.current = pathname;
onRouteChange();
}
}, [pathname, latestPathnameRef, onRouteChange]);
}

0 comments on commit 0360364

Please sign in to comment.