File tree 3 files changed +12
-7
lines changed
3 files changed +12
-7
lines changed Original file line number Diff line number Diff line change @@ -5,10 +5,15 @@ import { Toggle } from "./Components/Toggle/Toggle";
5
5
function App ( ) {
6
6
const [ isDark , setIsDark ] = useState ( true ) ;
7
7
8
+ let themePreference = window . matchMedia (
9
+ "(prefers-color-scheme: dark)"
10
+ ) ?. matches ;
11
+
8
12
const body = document . body ;
9
- if ( isDark ) {
13
+
14
+ if ( isDark && themePreference ) {
10
15
body . setAttribute ( "data-theme" , "dark" ) ;
11
- localStorage . getItem ( "theme" , "dark" ) ;
16
+ localStorage . getItem ( "theme" ) ;
12
17
} else {
13
18
body . setAttribute ( "data-theme" , "light" ) ;
14
19
}
@@ -18,14 +23,14 @@ function App() {
18
23
} ;
19
24
20
25
useEffect ( ( ) => {
21
- localStorage . setItem ( "theme" , isDark ? "dark" : "light" ) ;
22
- } , [ isDark ] ) ;
26
+ localStorage . setItem ( "theme" , themePreference ? "dark" : "light" ) ;
27
+ } , [ themePreference ] ) ;
23
28
24
29
return (
25
30
< >
26
31
< Toggle isChecked = { isDark } handleChange = { toggleDarkMode } />
27
32
< div className = "container" >
28
- < h1 > { isDark ? "Dark Mode π " : "Light Mode π" } </ h1 >
33
+ < h1 > { isDark ? "Dark Mode π " : "Light Mode π" } </ h1 >
29
34
</ div >
30
35
</ >
31
36
) ;
Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ export const Toggle = ({ handleChange, isChecked }) => {
11
11
onChange = { handleChange }
12
12
checked = { isChecked }
13
13
/>
14
- < label htmlFor = "check" > Dark Mode </ label >
14
+ < label htmlFor = "check" > </ label >
15
15
</ div >
16
16
) ;
17
17
} ;
Original file line number Diff line number Diff line change 35
35
border-radius : 10px ;
36
36
margin-right : 10px ;
37
37
position : absolute;
38
- left : 0 px ;
38
+ left : 10 px ;
39
39
}
40
40
41
41
.toggle-container .toggle : checked + label {
You canβt perform that action at this time.
0 commit comments