11'use client' ;
2+
23import { usePathname } from 'next/navigation' ;
34import 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 */
9499export function useHash ( ) {
95- const { hash } = React . useContext ( HashContext ) ;
100+ const { hash } = React . useContext ( NavigationStatusContext ) ;
96101
97102 return hash ;
98103}
99104
100105export function useIsNavigating ( ) {
101- const { isNavigating : hasBeenClicked } = React . useContext ( HashContext ) ;
106+ const { isNavigating : hasBeenClicked } = React . useContext ( NavigationStatusContext ) ;
102107 return hasBeenClicked ;
103108}
0 commit comments