11"use client" ;
2- import { useState , useEffect } from "react" ;
2+ import { useState , useEffect } from "react" ;
33
44export function useChangeTheme ( ) {
55 const [ theme , setTheme ] = useState ( "tomorrow" ) ;
@@ -23,16 +23,52 @@ export function useChangeTheme(){
2323
2424} ;
2525export function ThemeToggle ( ) {
26+ const [ isChecked , setIsChecked ] = useState ( false ) ;
27+ useEffect ( ( ) => {
28+ const checkIsDarkSchemePreferred = ( ) =>
29+ window ?. matchMedia ?.( '(prefers-color-scheme:dark)' ) ?. matches ?? false ;
30+ setIsChecked ( checkIsDarkSchemePreferred ( ) ) ;
31+ } , [ ] ) ;
32+
2633 return (
34+ < label className = "flex cursor-pointer gap-2" style = { { marginLeft : "1em" } } >
35+ < svg
36+ xmlns = "http://www.w3.org/2000/svg"
37+ width = "20"
38+ height = "20"
39+ viewBox = "0 0 24 24"
40+ fill = "none"
41+ stroke = "currentColor"
42+ strokeWidth = "2"
43+ strokeLinecap = "round"
44+ strokeLinejoin = "round" >
45+ < circle cx = "12" cy = "12" r = "5" />
46+ < path
47+ d = "M12 1v2M12 21v2M4.2 4.2l1.4 1.4M18.4 18.4l1.4 1.4M1 12h2M21 12h2M4.2 19.8l1.4-1.4M18.4 5.6l1.4-1.4" />
48+ </ svg >
2749 < input
2850 type = "checkbox"
51+ checked = { isChecked }
2952 className = "toggle theme-controller"
30- style = { { marginLeft : "1em" } }
3153 onChange = { ( e ) => {
32- const isDark = e . target . checked ;
33- const theme = isDark ? "dark" : "light" ;
54+ const isdark = e . target . checked ;
55+ setIsChecked ( isdark ) ;
56+ const theme = isdark ? "dark" : "light" ;
3457 document . documentElement . setAttribute ( "data-theme" , theme ) ;
3558 } }
3659 />
60+ < svg
61+ xmlns = "http://www.w3.org/2000/svg"
62+ width = "20"
63+ height = "20"
64+ viewBox = "0 0 24 24"
65+ fill = "none"
66+ stroke = "currentColor"
67+ strokeWidth = "2"
68+ strokeLinecap = "round"
69+ strokeLinejoin = "round" >
70+ < path d = "M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z" > </ path >
71+ </ svg >
72+ </ label >
3773 ) ;
3874}
0 commit comments