11import React from "react" ;
2- import { useMemo } from "react" ;
2+ import { useMemo , useLayoutEffect , useState } from "react" ;
33import { useLocation } from "react-router-dom" ;
44import logoWide from "src/assets/svg/logo-wide.svg" ;
55import logoSquare from "src/assets/svg/logo-square.svg" ;
@@ -12,7 +12,7 @@ import { useAppSelector } from "src/redux/store";
1212import { stripLanguageCodeFromHRef } from "src/utils/website-language" ;
1313import { useSearchModal } from "src/utils/search-modal" ;
1414import { Language , LANGUAGES } from "@dzcode.io/models/dist/language" ;
15-
15+ import { themeChange } from "theme-change" ;
1616export interface TopBarProps {
1717 version : string ;
1818 links : Array < { localeKey : DictionaryKeys < "navbar-section" > ; href : string } > ;
@@ -41,6 +41,27 @@ export function TopBar({ version, links }: TopBarProps): JSX.Element {
4141
4242 const { localize } = useLocale ( ) ;
4343
44+ const [ isDark , setIsDark ] = useState ( ( ) => {
45+ const savedTheme = localStorage . getItem ( "theme" ) ;
46+ const isDark = savedTheme === "dark" ;
47+ return savedTheme ? isDark : true ; // default should be dark
48+ } ) ;
49+
50+ useLayoutEffect ( ( ) => {
51+ themeChange ( true ) ;
52+ const theme = localStorage . getItem ( "theme" ) ;
53+ if ( theme ) {
54+ document . documentElement . setAttribute ( "data-theme" , theme ) ;
55+ }
56+ } , [ ] ) ;
57+
58+ function toggleTheme ( ) {
59+ const newTheme = isDark ? "light" : "dark" ;
60+ setIsDark ( ! isDark ) ;
61+ localStorage . setItem ( "theme" , newTheme ) ;
62+ document . documentElement . setAttribute ( "data-theme" , newTheme ) ;
63+ }
64+
4465 return (
4566 < div className = "bg-neutral" >
4667 < div className = "m-auto flex max-w-7xl flex-row gap-4 p-4 items-center" >
@@ -113,10 +134,12 @@ export function TopBar({ version, links }: TopBarProps): JSX.Element {
113134 < path d = "M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z" > </ path >
114135 </ svg >
115136 < input
137+ onClick = { toggleTheme }
116138 id = "theme-toggle"
117139 type = "checkbox"
118140 value = "dzcodeLight"
119141 className = "theme-controller toggle"
142+ checked = { ! isDark }
120143 />
121144 < svg
122145 xmlns = "http://www.w3.org/2000/svg"
0 commit comments