File tree Expand file tree Collapse file tree
frontend/components/Navbar Expand file tree Collapse file tree Original file line number Diff line number Diff line change 33import { useEffect , useState } from "react" ;
44
55export default function LightDarkToggle ( ) {
6- const [ theme , setTheme ] = useState < string | null > (
7- typeof window !== "undefined" ? localStorage . theme : "light" ,
8- ) ;
6+ const [ theme , setTheme ] = useState < string | null > ( null ) ;
97
108 function changeTheme ( ) {
119 if ( theme === "dark" || theme == undefined ) {
@@ -18,9 +16,14 @@ export default function LightDarkToggle() {
1816 }
1917
2018 useEffect ( ( ) => {
21- if ( theme === undefined ) {
22- setTheme ( "dark" ) ;
23- localStorage . setItem ( "theme" , "dark" ) ;
19+ if ( theme === null ) {
20+ let ls = localStorage . getItem ( "theme" ) ;
21+ if ( ls ) {
22+ setTheme ( ls )
23+ } else {
24+ setTheme ( "dark" ) ;
25+ localStorage . setItem ( "theme" , "dark" ) ;
26+ }
2427 }
2528 if ( theme ) {
2629 document . body . setAttribute ( "data-bs-theme" , theme ) ;
@@ -30,7 +33,7 @@ export default function LightDarkToggle() {
3033 return (
3134 < div className = "float-left text-center items-center mr-5 " >
3235 < button className = "btn" data-testid = "light-dark" onClick = { changeTheme }
33- title = { theme === "light" ? "Switch to Dark Mode" : "Switch to Light Mode" } >
36+ title = { theme === "light" ? "Switch to Dark Mode" : "Switch to Light Mode" } >
3437 < i className = "bi bi-lamp-fill" > </ i >
3538 </ button >
3639 </ div >
You can’t perform that action at this time.
0 commit comments