Skip to content

Commit 792e284

Browse files
committed
feature system preference option added
1 parent f4a85c3 commit 792e284

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

β€Ždark-mode-app/src/App.jsx

+10-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ import { Toggle } from "./Components/Toggle/Toggle";
55
function App() {
66
const [isDark, setIsDark] = useState(true);
77

8+
let themePreference = window.matchMedia(
9+
"(prefers-color-scheme: dark)"
10+
)?.matches;
11+
812
const body = document.body;
9-
if (isDark) {
13+
14+
if (isDark && themePreference) {
1015
body.setAttribute("data-theme", "dark");
11-
localStorage.getItem("theme", "dark");
16+
localStorage.getItem("theme");
1217
} else {
1318
body.setAttribute("data-theme", "light");
1419
}
@@ -18,14 +23,14 @@ function App() {
1823
};
1924

2025
useEffect(() => {
21-
localStorage.setItem("theme", isDark ? "dark" : "light");
22-
}, [isDark]);
26+
localStorage.setItem("theme", themePreference ? "dark" : "light");
27+
}, [themePreference]);
2328

2429
return (
2530
<>
2631
<Toggle isChecked={isDark} handleChange={toggleDarkMode} />
2732
<div className="container">
28-
<h1>{isDark ? "Dark Mode 🌝" : "Light Mode 🌞"}</h1>
33+
<h1>{isDark ? "Dark Mode πŸŒ‘" : "Light Mode 🌞"}</h1>
2934
</div>
3035
</>
3136
);

β€Ždark-mode-app/src/Components/Toggle/Toggle.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const Toggle = ({ handleChange, isChecked }) => {
1111
onChange={handleChange}
1212
checked={isChecked}
1313
/>
14-
<label htmlFor="check">Dark Mode</label>
14+
<label htmlFor="check"></label>
1515
</div>
1616
);
1717
};

β€Ždark-mode-app/src/Components/Toggle/toggle.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
border-radius: 10px;
3636
margin-right: 10px;
3737
position: absolute;
38-
left: 0px;
38+
left: 10px;
3939
}
4040

4141
.toggle-container .toggle:checked + label {

0 commit comments

Comments
Β (0)