Skip to content

Commit 14c7ff2

Browse files
committed
renaming
1 parent b8d612c commit 14c7ff2

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

packages/gitbook/src/components/RootLayout/RootLayoutClientContexts.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type React from 'react';
55
import { TranslateContext } from '@/intl/client';
66
import type { TranslationLanguage } from '@/intl/translations';
77
import { TooltipProvider } from '@radix-ui/react-tooltip';
8-
import { HashProvider } from '../hooks';
8+
import { NavigationStatusProvider } from '../hooks';
99
import { LoadingStateProvider } from '../primitives/LoadingStateProvider';
1010

1111
/**
@@ -20,9 +20,9 @@ export function RootLayoutClientContexts(props: {
2020
return (
2121
<TranslateContext.Provider value={language}>
2222
<TooltipProvider delayDuration={200}>
23-
<HashProvider>
23+
<NavigationStatusProvider>
2424
<LoadingStateProvider>{children}</LoadingStateProvider>
25-
</HashProvider>
25+
</NavigationStatusProvider>
2626
</TooltipProvider>
2727
</TranslateContext.Provider>
2828
);

packages/gitbook/src/components/hooks/useHash.tsx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
'use client';
2+
23
import { usePathname } from 'next/navigation';
34
import React from 'react';
45

5-
export const HashContext = React.createContext<{
6+
export const NavigationStatusContext = React.createContext<{
67
hash: string | null;
78
/**
89
* Updates the hash value from the URL provided here.
910
* It will then be used by the `useHash` hook.
1011
* URL can be relative or absolute.
1112
*/
12-
updateHashFromUrl: (href: string) => void;
13+
onNavigationClick: (href: string) => void;
1314
/**
1415
* Indicates if a link has been clicked recently.
1516
* Becomes true after a click and resets to false when pathname changes.
@@ -19,7 +20,7 @@ export const HashContext = React.createContext<{
1920
isNavigating: boolean;
2021
}>({
2122
hash: null,
22-
updateHashFromUrl: () => {},
23+
onNavigationClick: () => {},
2324
isNavigating: false,
2425
});
2526

@@ -30,7 +31,7 @@ function getHash(): string | null {
3031
return window.location.hash.slice(1);
3132
}
3233

33-
export const HashProvider: React.FC<React.PropsWithChildren> = ({ children }) => {
34+
export const NavigationStatusProvider: React.FC<React.PropsWithChildren> = ({ children }) => {
3435
const [hash, setHash] = React.useState<string | null>(getHash);
3536
const [isNavigating, setIsNavigating] = React.useState(false);
3637
const timeoutRef = React.useRef<number | null>(null);
@@ -58,7 +59,7 @@ export const HashProvider: React.FC<React.PropsWithChildren> = ({ children }) =>
5859
};
5960
}, []);
6061

61-
const updateHashFromUrl = React.useCallback((href: string) => {
62+
const onNavigationClick = React.useCallback((href: string) => {
6263
const url = new URL(
6364
href,
6465
typeof window !== 'undefined' ? window.location.origin : 'http://localhost'
@@ -78,10 +79,14 @@ export const HashProvider: React.FC<React.PropsWithChildren> = ({ children }) =>
7879
}, []);
7980

8081
const memoizedValue = React.useMemo(
81-
() => ({ hash, updateHashFromUrl, isNavigating }),
82-
[hash, updateHashFromUrl, isNavigating]
82+
() => ({ hash, onNavigationClick, isNavigating }),
83+
[hash, onNavigationClick, isNavigating]
84+
);
85+
return (
86+
<NavigationStatusContext.Provider value={memoizedValue}>
87+
{children}
88+
</NavigationStatusContext.Provider>
8389
);
84-
return <HashContext.Provider value={memoizedValue}>{children}</HashContext.Provider>;
8590
};
8691

8792
/**
@@ -92,12 +97,12 @@ export const HashProvider: React.FC<React.PropsWithChildren> = ({ children }) =>
9297
* Since we have a single Link component that handles all links, we can use a context to share the hash.
9398
*/
9499
export function useHash() {
95-
const { hash } = React.useContext(HashContext);
100+
const { hash } = React.useContext(NavigationStatusContext);
96101

97102
return hash;
98103
}
99104

100105
export function useIsNavigating() {
101-
const { isNavigating: hasBeenClicked } = React.useContext(HashContext);
106+
const { isNavigating: hasBeenClicked } = React.useContext(NavigationStatusContext);
102107
return hasBeenClicked;
103108
}

packages/gitbook/src/components/primitives/Link.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import React from 'react';
66
import { tcls } from '@/lib/tailwind';
77
import { SiteExternalLinksTarget } from '@gitbook/api';
88
import { type TrackEventInput, useTrackEvent } from '../Insights';
9-
import { HashContext } from '../hooks';
9+
import { NavigationStatusContext } from '../hooks';
1010
import { isExternalLink } from '../utils/link';
1111
import { type DesignTokenName, useClassnames } from './StyleProvider';
1212

@@ -72,7 +72,7 @@ export const Link = React.forwardRef(function Link(
7272
) {
7373
const { href, prefetch, children, insights, classNames, className, ...domProps } = props;
7474
const { externalLinksTarget } = React.useContext(LinkSettingsContext);
75-
const { updateHashFromUrl } = React.useContext(HashContext);
75+
const { onNavigationClick } = React.useContext(NavigationStatusContext);
7676
const trackEvent = useTrackEvent();
7777
const forwardedClassNames = useClassnames(classNames || []);
7878
const isExternal = isExternalLink(href);
@@ -81,7 +81,7 @@ export const Link = React.forwardRef(function Link(
8181
const onClick = (event: React.MouseEvent<HTMLAnchorElement>) => {
8282
const isExternalWithOrigin = isExternalLink(href, window.location.origin);
8383
if (!isExternal) {
84-
updateHashFromUrl(href);
84+
onNavigationClick(href);
8585
}
8686

8787
if (insights) {

0 commit comments

Comments
 (0)