Skip to content

Commit 3b85c5d

Browse files
committed
feat: add prefers-color-scheme
1 parent 8c0faa1 commit 3b85c5d

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/demo/js/toggle-dark.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
1+
const icon = document.querySelector(".darkmode i");
2+
if (window.matchMedia("(prefers-color-scheme: dark)").matches == true) {
3+
darkmode();
4+
} else {
5+
lightmode();
6+
}
7+
18
function toggleTheme() {
2-
const icon = document.querySelector(".darkmode i");
3-
/* dark mode on */
49
if (document.body.getAttribute("data-theme") !== "dark") {
5-
icon.className = "gg-sun";
6-
setCookie("darkmode", "on", 9999);
7-
document.body.setAttribute("data-theme", "dark");
8-
}
9-
/* dark mode off */
10-
else {
11-
icon.className = "gg-moon";
12-
setCookie("darkmode", "off", 9999);
13-
document.body.removeAttribute("data-theme");
10+
/* dark mode on */
11+
darkmode();
12+
} else {
13+
/* dark mode off */
14+
lightmode();
1415
}
1516
}
1617

18+
function darkmode() {
19+
icon.className = "gg-sun";
20+
setCookie("darkmode", "on", 9999);
21+
document.body.setAttribute("data-theme", "dark");
22+
}
23+
24+
function lightmode() {
25+
icon.className = "gg-moon";
26+
setCookie("darkmode", "off", 9999);
27+
document.body.removeAttribute("data-theme");
28+
}
29+
1730
function setCookie(cname, cvalue, exdays) {
1831
var d = new Date();
1932
d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);

0 commit comments

Comments
 (0)