-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy paththeme.js
54 lines (45 loc) · 1.96 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
43
44
45
46
47
48
49
50
51
52
53
54
document.addEventListener("DOMContentLoaded", () => {
// finding the theme pref using media query.
const themePreferenceDark = window.matchMedia("(prefers-color-scheme: dark)");
// when-ever system theme is changing, we need to update check the theme pref and update the button text.
themePreferenceDark.addEventListener("change", (event) => {
checkThemePreference();
})
const getOSThemePref = () => themePreferenceDark.matches ? "dark" : "light";
const getThemePrefFromLocalStorage = () => {
return localStorage.getItem("theme");
}
const checkThemePreference = () => {
const defaultTheme = getOSThemePref();
const currentTheme = getThemePrefFromLocalStorage();
if (currentTheme === "dark") {
document.body.classList.toggle("dark-theme");
} else if (currentTheme === "light") {
document.body.classList.toggle("light-theme");
}
setButtonText(currentTheme ?? defaultTheme)
}
const setButtonText = (theme) => {
// themeSwitcher.textContent = theme === "dark" ? "Light" : "Dark";
themeSwitcher.innerHTML = "";
const image = document.createElement("img");
image.src = theme === "dark" ? "light.svg" : "dark.svg";
image.id = "theme-icon";
themeSwitcher.appendChild(image);
}
const switchTheme = () => {
let theme;
if (themePreferenceDark.matches) {
document.body.classList.toggle("light-theme");
theme = document.body.classList.contains("light-theme") ? "light" : "dark"
} else {
document.body.classList.toggle("dark-theme");
theme = document.body.classList.contains("dark-theme") ? "dark" : "light"
}
localStorage.setItem("theme", theme);
setButtonText(theme)
}
const themeSwitcher = document.getElementById("theme-switcher");
themeSwitcher.addEventListener("click", switchTheme);
checkThemePreference();
})