add darkmode detection

This commit is contained in:
Tealk 2021-03-20 13:18:48 +01:00
parent 6cecff3cc3
commit 313c8b1db0
No known key found for this signature in database
GPG Key ID: 5FE349ABC863E7F9
1 changed files with 7 additions and 0 deletions

View File

@ -53,3 +53,10 @@ function resetTheme() {
localStorage.removeItem("darkSwitch");
}
}
if (window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches) {
document.body.setAttribute("data-theme", "dark");
}
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", (e) => {
const newColorScheme = e.matches ? document.body.setAttribute("data-theme", "dark") : document.body.removeAttribute("data-theme");
});