2023-07-23 07:27:41 +07:00
|
|
|
const userPref = window.matchMedia("(prefers-color-scheme: light)").matches ? "light" : "dark"
|
|
|
|
const currentTheme = localStorage.getItem("theme") ?? userPref
|
|
|
|
document.documentElement.setAttribute("saved-theme", currentTheme)
|
2023-06-04 02:07:19 +07:00
|
|
|
|
2023-07-03 03:08:29 +07:00
|
|
|
document.addEventListener("nav", () => {
|
2023-06-07 10:58:26 +07:00
|
|
|
const switchTheme = (e: any) => {
|
2023-06-07 09:48:37 +07:00
|
|
|
if (e.target.checked) {
|
2023-07-23 07:27:41 +07:00
|
|
|
document.documentElement.setAttribute("saved-theme", "dark")
|
|
|
|
localStorage.setItem("theme", "dark")
|
|
|
|
} else {
|
|
|
|
document.documentElement.setAttribute("saved-theme", "light")
|
|
|
|
localStorage.setItem("theme", "light")
|
2023-06-07 10:58:26 +07:00
|
|
|
}
|
|
|
|
}
|
2023-06-07 09:48:37 +07:00
|
|
|
|
2023-06-07 10:58:26 +07:00
|
|
|
// Darkmode toggle
|
2023-07-23 07:27:41 +07:00
|
|
|
const toggleSwitch = document.querySelector("#darkmode-toggle") as HTMLInputElement
|
|
|
|
toggleSwitch.removeEventListener("change", switchTheme)
|
|
|
|
toggleSwitch.addEventListener("change", switchTheme)
|
|
|
|
if (currentTheme === "dark") {
|
2023-06-07 09:48:37 +07:00
|
|
|
toggleSwitch.checked = true
|
|
|
|
}
|
2023-09-19 23:25:51 +07:00
|
|
|
|
|
|
|
// Listen for changes in prefers-color-scheme
|
|
|
|
const colorSchemeMediaQuery = window.matchMedia("(prefers-color-scheme: dark)")
|
|
|
|
colorSchemeMediaQuery.addEventListener("change", (e) => {
|
|
|
|
const newTheme = e.matches ? "dark" : "light"
|
|
|
|
document.documentElement.setAttribute("saved-theme", newTheme)
|
|
|
|
localStorage.setItem("theme", newTheme)
|
|
|
|
toggleSwitch.checked = e.matches
|
|
|
|
})
|
2023-06-07 09:48:37 +07:00
|
|
|
})
|