Skip to content

Commit 2899dc0

Browse files
committed
Format
1 parent 917ac94 commit 2899dc0

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

public/js/theme.js

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
function changeTheme() {
2-
const element = document.documentElement
3-
const theme = element.classList.contains("dark") ? "light" : "dark"
2+
const element = document.documentElement;
3+
const theme = element.classList.contains("dark") ? "light" : "dark";
44

5-
const css = document.createElement("style")
5+
const css = document.createElement("style");
66

77
css.appendChild(
88
document.createTextNode(
@@ -14,56 +14,58 @@ function changeTheme() {
1414
transition: none !important;
1515
}`,
1616
),
17-
)
17+
);
1818

19-
document.head.appendChild(css)
19+
document.head.appendChild(css);
2020

2121
if (theme === "dark") {
22-
element.classList.add("dark")
22+
element.classList.add("dark");
2323
} else {
24-
element.classList.remove("dark")
24+
element.classList.remove("dark");
2525
}
2626

27-
window.getComputedStyle(css).opacity
27+
window.getComputedStyle(css).opacity;
2828

29-
document.head.removeChild(css)
30-
localStorage.theme = theme
29+
document.head.removeChild(css);
30+
localStorage.theme = theme;
3131
}
3232

3333
function preloadTheme() {
3434
const theme = (() => {
35-
const userTheme = localStorage.theme
35+
const userTheme = localStorage.theme;
3636

3737
if (userTheme === "light" || userTheme === "dark") {
38-
return userTheme
38+
return userTheme;
3939
} else {
40-
return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"
40+
return window.matchMedia("(prefers-color-scheme: dark)").matches
41+
? "dark"
42+
: "light";
4143
}
42-
})()
44+
})();
4345

44-
const element = document.documentElement
46+
const element = document.documentElement;
4547

4648
if (theme === "dark") {
47-
element.classList.add("dark")
49+
element.classList.add("dark");
4850
} else {
49-
element.classList.remove("dark")
51+
element.classList.remove("dark");
5052
}
5153

52-
localStorage.theme = theme
54+
localStorage.theme = theme;
5355
}
5456

5557
window.onload = () => {
5658
function initializeThemeButtons() {
57-
const headerThemeButton = document.getElementById("header-theme-button")
58-
const drawerThemeButton = document.getElementById("drawer-theme-button")
59-
headerThemeButton?.addEventListener("click", changeTheme)
60-
drawerThemeButton?.addEventListener("click", changeTheme)
59+
const headerThemeButton = document.getElementById("header-theme-button");
60+
const drawerThemeButton = document.getElementById("drawer-theme-button");
61+
headerThemeButton?.addEventListener("click", changeTheme);
62+
drawerThemeButton?.addEventListener("click", changeTheme);
6163
}
6264

63-
document.addEventListener("astro:after-swap", initializeThemeButtons)
64-
initializeThemeButtons()
65-
}
65+
document.addEventListener("astro:after-swap", initializeThemeButtons);
66+
initializeThemeButtons();
67+
};
6668

67-
document.addEventListener("astro:after-swap", preloadTheme)
69+
document.addEventListener("astro:after-swap", preloadTheme);
6870

69-
preloadTheme()
71+
preloadTheme();

0 commit comments

Comments
 (0)