-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy paththeme.js
42 lines (34 loc) · 1.35 KB
/
theme.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function switchTheme(isLight) {
const markdownBodyElement = document.getElementById("markdown-body");
const themeIconElement = document.getElementById("theme-icon");
const lightHL = document.getElementById("light-hl");
const darkHL = document.getElementById("dark-hl");
const LS = window.localStorage;
const LSisLight = LS.getItem("MDcatisLight") === "true";
isLight = isLight ?? !LSisLight;
const colorMode = isLight ? "light" : "dark";
const colorIcon = isLight ? "sun" : "moon";
markdownBodyElement.setAttribute("data-color-mode", colorMode);
markdownBodyElement.setAttribute("data-dark-theme", colorMode);
lightHL.rel = isLight ? "stylesheet" : "stylesheet alternate";
darkHL.rel = isLight ? "stylesheet alternate" : "stylesheet";
themeIconElement.setAttribute(
"data-icon",
"octicon:" + colorIcon + "-16"
);
LS.setItem("MDcatisLight", isLight);
}
const themeButton = document.getElementById("theme-button");
const LS = window.localStorage;
const LSisLight = LS.getItem("MDcatisLight") === "true";
themeButton.addEventListener("click", () => {
switchTheme();
});
LS.getItem("MDcatisLight") === null
? switchTheme(
!(
window.matchMedia &&
window.matchMedia("(prefers-color-scheme: dark)").matches
)
)
: switchTheme(LSisLight);