File tree Expand file tree Collapse file tree 3 files changed +50
-2
lines changed Expand file tree Collapse file tree 3 files changed +50
-2
lines changed Original file line number Diff line number Diff line change @@ -74,7 +74,11 @@ export default [
74
74
} ,
75
75
} ,
76
76
{
77
- files : [ 'src/generators/legacy-html/assets/*.js' ] ,
77
+ files : [
78
+ 'src/generators/legacy-html/assets/*.js' ,
79
+ 'src/generators/web/hooks/*' ,
80
+ 'src/generators/web/components/*' ,
81
+ ] ,
78
82
languageOptions : { globals : { ...globals . browser } } ,
79
83
} ,
80
84
] ;
Original file line number Diff line number Diff line change 1
1
import NavBar from '@node-core/ui-components/Containers/NavBar' ;
2
2
import NodejsLogo from '@node-core/ui-components/Common/NodejsLogo' ;
3
+ import ThemeToggle from '@node-core/ui-components/Common/ThemeToggle' ;
4
+
5
+ import { useTheme } from '../hooks/useTheme.mjs' ;
3
6
4
7
const MDXNavBar = ( ) => {
8
+ const [ theme , toggleTheme ] = useTheme ( ) ;
9
+
5
10
return (
6
11
< NavBar
7
12
pathname = "/docs/latest/api/"
@@ -40,7 +45,10 @@ const MDXNavBar = () => {
40
45
} ,
41
46
] }
42
47
>
43
- < p > some child</ p >
48
+ < ThemeToggle
49
+ onClick = { toggleTheme }
50
+ aria-label = { `Switch to ${ theme === 'light' ? 'dark' : 'light' } theme` }
51
+ />
44
52
</ NavBar >
45
53
) ;
46
54
} ;
Original file line number Diff line number Diff line change
1
+ import { useState , useEffect } from 'react' ;
2
+
3
+ /**
4
+ * This hook provides theme management functionality
5
+ */
6
+ export const useTheme = ( ) => {
7
+ const [ theme , setTheme ] = useState ( 'light' ) ;
8
+
9
+ useEffect ( ( ) => {
10
+ // Get initial theme from HTML or fallback to system preference
11
+ const htmlElement = document . documentElement ;
12
+ const currentTheme = htmlElement . getAttribute ( 'data-theme' ) ;
13
+
14
+ const initialTheme =
15
+ currentTheme ||
16
+ localStorage . getItem ( 'theme' ) ||
17
+ ( window . matchMedia ( '(prefers-color-scheme: dark)' ) . matches
18
+ ? 'dark'
19
+ : 'light' ) ;
20
+
21
+ setTheme ( initialTheme ) ;
22
+ htmlElement . setAttribute ( 'data-theme' , initialTheme ) ;
23
+ } , [ ] ) ;
24
+
25
+ /**
26
+ * Toggles the theme between 'light' and 'dark'.
27
+ */
28
+ const toggleTheme = ( ) => {
29
+ const newTheme = theme === 'light' ? 'dark' : 'light' ;
30
+ setTheme ( newTheme ) ;
31
+ document . documentElement . setAttribute ( 'data-theme' , newTheme ) ;
32
+ localStorage . setItem ( 'theme' , newTheme ) ;
33
+ } ;
34
+
35
+ return [ theme , toggleTheme ] ;
36
+ } ;
You can’t perform that action at this time.
0 commit comments