File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -4,27 +4,26 @@ import { getAssetUrl } from "../utils/getAssetUrl";
44const STORAGE_KEY = "theme" ;
55
66export default function ThemeSwitcher ( ) {
7- const [ theme , setTheme ] = useState < "light" | "dark" > ( "light" ) ;
8-
9- useEffect ( ( ) => {
7+ const [ theme , setTheme ] = useState < "light" | "dark" > ( ( ) => {
108 const savedTheme = localStorage . getItem ( STORAGE_KEY ) as
119 | "light"
1210 | "dark"
1311 | null ;
1412
1513 if ( savedTheme === "dark" || savedTheme === "light" ) {
16- setTheme ( savedTheme ) ;
1714 document . documentElement . classList . toggle ( "dark" , savedTheme === "dark" ) ;
15+ return savedTheme ;
1816 } else {
1917 // First visit → follow system
2018 const systemPrefersDark = window . matchMedia (
2119 "(prefers-color-scheme: dark)" ,
2220 ) . matches ;
2321 const initialTheme = systemPrefersDark ? "dark" : "light" ;
24- setTheme ( initialTheme ) ;
22+
2523 document . documentElement . classList . toggle ( "dark" , systemPrefersDark ) ;
24+ return initialTheme ;
2625 }
27- } , [ ] ) ;
26+ } ) ;
2827
2928 useEffect ( ( ) => {
3029 document . documentElement . classList . toggle ( "dark" , theme === "dark" ) ;
You can’t perform that action at this time.
0 commit comments