Jake Howard
37cc1979be
darkreader is pretty big, and this is nice, simple and fast. Not to mention has much less JS
18 lines
457 B
JavaScript
18 lines
457 B
JavaScript
const DARK_MODE_CLASS = "dark-mode";
|
|
const body = document.getElementsByTagName("body")[0];
|
|
|
|
const matchesDarkMode = window.matchMedia("(prefers-color-scheme: dark)");
|
|
|
|
function handleDarkMode(darkMode) {
|
|
if (darkMode) {
|
|
body.classList.add(DARK_MODE_CLASS);
|
|
} else {
|
|
body.classList.remove(DARK_MODE_CLASS);
|
|
}
|
|
}
|
|
|
|
matchesDarkMode.addEventListener("change", (event) =>
|
|
handleDarkMode(event.matches)
|
|
);
|
|
|
|
handleDarkMode(matchesDarkMode.matches);
|