File tree Expand file tree Collapse file tree
packages/ui-components/src/contexts Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import { getLocale } from 'next-intl/server';
66
77import BaseLayout from '#site/layouts/Base' ;
88import { IBM_PLEX_MONO , OPEN_SANS } from '#site/next.fonts' ;
9+ import { HashProvider } from '#site/providers/hashProvider' ;
910import { ThemeProvider } from '#site/providers/themeProvider' ;
1011
1112import type { FC , PropsWithChildren } from 'react' ;
@@ -29,9 +30,11 @@ const RootLayout: FC<PropsWithChildren> = async ({ children }) => {
2930 >
3031 < body suppressHydrationWarning >
3132 < NextIntlClientProvider >
32- < ThemeProvider >
33- < BaseLayout > { children } </ BaseLayout >
34- </ ThemeProvider >
33+ < HashProvider >
34+ < ThemeProvider >
35+ < BaseLayout > { children } </ BaseLayout >
36+ </ ThemeProvider >
37+ </ HashProvider >
3538 </ NextIntlClientProvider >
3639
3740 < a
Original file line number Diff line number Diff line change 1+ 'use client' ;
2+
3+ import { useSyncExternalStore } from 'react' ;
4+
5+ const listeners = new Set < ( ) => void > ( ) ;
6+ let isListening = false ;
7+
8+ const onHashChange = ( ) => {
9+ listeners . forEach ( rerender => rerender ( ) ) ;
10+ } ;
11+
12+ const subscribe = ( callback : ( ) => void ) => {
13+ listeners . add ( callback ) ;
14+ if ( ! isListening && typeof window !== 'undefined' ) {
15+ window . addEventListener ( 'hashchange' , onHashChange ) ;
16+ isListening = true ;
17+ }
18+ return ( ) => {
19+ listeners . delete ( callback ) ;
20+ if ( listeners . size === 0 && typeof window !== 'undefined' ) {
21+ window . removeEventListener ( 'hashchange' , onHashChange ) ;
22+ isListening = false ;
23+ }
24+ } ;
25+ } ;
26+
27+ const getSnapshot = ( ) =>
28+ typeof window !== 'undefined' ? window . location . hash . slice ( 1 ) : '' ;
29+ const getServerSnapshot = ( ) => '' ;
30+
31+ const useHash = ( ) => {
32+ const hash = useSyncExternalStore ( subscribe , getSnapshot , getServerSnapshot ) ;
33+
34+ const setHash = ( newHash : string ) => {
35+ if ( typeof window !== 'undefined' ) {
36+ window . history . replaceState ( null , '' , `#${ newHash } ` ) ;
37+ onHashChange ( ) ;
38+ }
39+ } ;
40+
41+ return [ hash , setHash ] as const ;
42+ } ;
43+
44+ export default useHash ;
Original file line number Diff line number Diff line change 1+ 'use client' ;
2+
3+ import { HashContext } from '@node-core/ui-components/contexts/HashContext' ;
4+
5+ import useHash from '#site/hooks/useHash' ;
6+
7+ import type { FC , PropsWithChildren } from 'react' ;
8+
9+ export const HashProvider : FC < PropsWithChildren > = ( { children } ) => {
10+ const [ hash , setHash ] = useHash ( ) ;
11+
12+ return < HashContext value = { { hash, setHash } } > { children } </ HashContext > ;
13+ } ;
Original file line number Diff line number Diff line change 1+ 'use client' ;
2+
3+ import { createContext , use } from 'react' ;
4+
5+ type HashContextType = {
6+ hash : string ;
7+ setHash : ( newHash : string ) => void ;
8+ } ;
9+
10+ export const HashContext = createContext < HashContextType > ( {
11+ hash : '' ,
12+ setHash : ( ) => { } ,
13+ } ) ;
14+
15+ export const useHashContext = ( ) => use ( HashContext ) ;
You can’t perform that action at this time.
0 commit comments