mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-25 15:07:17 +02:00
fix(v2): ignore hash changes in useChangeRoute hook (#5023)
* fix(v2): ignore hash changes in useChangeRoute hook * refactor and introduce useLocationChange hook Co-authored-by: slorber <lorber.sebastien@gmail.com>
This commit is contained in:
parent
41eaa690ee
commit
8bda3b2dbf
8 changed files with 73 additions and 37 deletions
|
@ -167,6 +167,10 @@ declare module '@docusaurus/router' {
|
||||||
// eslint-disable-next-line import/no-extraneous-dependencies
|
// eslint-disable-next-line import/no-extraneous-dependencies
|
||||||
export * from 'react-router-dom';
|
export * from 'react-router-dom';
|
||||||
}
|
}
|
||||||
|
declare module '@docusaurus/history' {
|
||||||
|
// eslint-disable-next-line import/no-extraneous-dependencies
|
||||||
|
export * from 'history';
|
||||||
|
}
|
||||||
|
|
||||||
declare module '@docusaurus/useDocusaurusContext' {
|
declare module '@docusaurus/useDocusaurusContext' {
|
||||||
export default function (): any;
|
export default function (): any;
|
||||||
|
|
|
@ -7,7 +7,11 @@
|
||||||
|
|
||||||
import React, {useState, useCallback, useEffect, useRef, memo} from 'react';
|
import React, {useState, useCallback, useEffect, useRef, memo} from 'react';
|
||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
import {useThemeConfig, isSamePath} from '@docusaurus/theme-common';
|
import {
|
||||||
|
useThemeConfig,
|
||||||
|
isSamePath,
|
||||||
|
usePrevious,
|
||||||
|
} from '@docusaurus/theme-common';
|
||||||
import useUserPreferencesContext from '@theme/hooks/useUserPreferencesContext';
|
import useUserPreferencesContext from '@theme/hooks/useUserPreferencesContext';
|
||||||
import useLockBodyScroll from '@theme/hooks/useLockBodyScroll';
|
import useLockBodyScroll from '@theme/hooks/useLockBodyScroll';
|
||||||
import useWindowSize, {windowSizes} from '@theme/hooks/useWindowSize';
|
import useWindowSize, {windowSizes} from '@theme/hooks/useWindowSize';
|
||||||
|
@ -25,14 +29,6 @@ import styles from './styles.module.css';
|
||||||
|
|
||||||
const MOBILE_TOGGLE_SIZE = 24;
|
const MOBILE_TOGGLE_SIZE = 24;
|
||||||
|
|
||||||
function usePrevious(value) {
|
|
||||||
const ref = useRef(value);
|
|
||||||
useEffect(() => {
|
|
||||||
ref.current = value;
|
|
||||||
}, [value]);
|
|
||||||
return ref.current;
|
|
||||||
}
|
|
||||||
|
|
||||||
const isActiveSidebarItem = (item, activePath) => {
|
const isActiveSidebarItem = (item, activePath) => {
|
||||||
if (item.type === 'link') {
|
if (item.type === 'link') {
|
||||||
return isSamePath(item.href, activePath);
|
return isSamePath(item.href, activePath);
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
import React, {useRef} from 'react';
|
import React, {useRef} from 'react';
|
||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
import Translate from '@docusaurus/Translate';
|
import Translate from '@docusaurus/Translate';
|
||||||
import {useChangeRoute} from '@docusaurus/theme-common';
|
import {useLocationChange} from '@docusaurus/theme-common';
|
||||||
|
|
||||||
import styles from './styles.module.css';
|
import styles from './styles.module.css';
|
||||||
|
|
||||||
|
@ -32,8 +32,8 @@ function SkipToContent(): JSX.Element {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
useChangeRoute(() => {
|
useLocationChange(({location}) => {
|
||||||
if (containerRef.current) {
|
if (containerRef.current && !location.hash) {
|
||||||
programmaticFocus(containerRef.current);
|
programmaticFocus(containerRef.current);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
import {useState, useCallback, useEffect, useRef} from 'react';
|
import {useState, useCallback, useEffect, useRef} from 'react';
|
||||||
import {useLocation} from '@docusaurus/router';
|
import {useLocation} from '@docusaurus/router';
|
||||||
import useScrollPosition from '@theme/hooks/useScrollPosition';
|
import useScrollPosition from '@theme/hooks/useScrollPosition';
|
||||||
import {useChangeRoute} from '@docusaurus/theme-common';
|
import {useLocationChange} from '@docusaurus/theme-common';
|
||||||
import type {useHideableNavbarReturns} from '@theme/hooks/useHideableNavbar';
|
import type {useHideableNavbarReturns} from '@theme/hooks/useHideableNavbar';
|
||||||
|
|
||||||
const useHideableNavbar = (hideOnScroll: boolean): useHideableNavbarReturns => {
|
const useHideableNavbar = (hideOnScroll: boolean): useHideableNavbarReturns => {
|
||||||
|
@ -56,8 +56,8 @@ const useHideableNavbar = (hideOnScroll: boolean): useHideableNavbarReturns => {
|
||||||
[navbarHeight, isFocusedAnchor],
|
[navbarHeight, isFocusedAnchor],
|
||||||
);
|
);
|
||||||
|
|
||||||
useChangeRoute(() => {
|
useLocationChange((locationChangeEvent) => {
|
||||||
if (!hideOnScroll) {
|
if (!hideOnScroll || locationChangeEvent.location.hash) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,9 @@ export {useTitleFormatter} from './utils/generalUtils';
|
||||||
|
|
||||||
export {usePluralForm} from './utils/usePluralForm';
|
export {usePluralForm} from './utils/usePluralForm';
|
||||||
|
|
||||||
export {useChangeRoute} from './utils/useChangeRoute';
|
export {useLocationChange} from './utils/useLocationChange';
|
||||||
|
|
||||||
|
export {usePrevious} from './utils/usePrevious';
|
||||||
|
|
||||||
export {
|
export {
|
||||||
useDocsPreferredVersion,
|
useDocsPreferredVersion,
|
||||||
|
|
|
@ -1,21 +0,0 @@
|
||||||
/**
|
|
||||||
* 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,0 +1,37 @@
|
||||||
|
/**
|
||||||
|
* 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 {useEffect, useRef} from 'react';
|
||||||
|
import {useLocation} from '@docusaurus/router';
|
||||||
|
import {Location} from '@docusaurus/history';
|
||||||
|
import {usePrevious} from './usePrevious';
|
||||||
|
|
||||||
|
type LocationChangeEvent = {
|
||||||
|
location: Location;
|
||||||
|
previousLocation: Location | undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
type OnLocationChange = (locationChangeEvent: LocationChangeEvent) => void;
|
||||||
|
|
||||||
|
export function useLocationChange(onLocationChange: OnLocationChange): void {
|
||||||
|
const location = useLocation();
|
||||||
|
const previousLocation = usePrevious(location);
|
||||||
|
const isFirst = useRef<boolean>(true);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
// Prevent first effect to trigger the listener on mount
|
||||||
|
if (isFirst.current) {
|
||||||
|
isFirst.current = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
onLocationChange({
|
||||||
|
location,
|
||||||
|
previousLocation,
|
||||||
|
});
|
||||||
|
}, [location]);
|
||||||
|
}
|
18
packages/docusaurus-theme-common/src/utils/usePrevious.ts
Normal file
18
packages/docusaurus-theme-common/src/utils/usePrevious.ts
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
/**
|
||||||
|
* 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';
|
||||||
|
|
||||||
|
export function usePrevious<T>(value: T): T | undefined {
|
||||||
|
const ref = useRef<T>();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
ref.current = value;
|
||||||
|
});
|
||||||
|
|
||||||
|
return ref.current;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue