Created
July 3, 2025 19:06
-
-
Save wchargin/155b8ad072bf1b403d5b5a3eab22a39b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(() => { | |
function setDark(dark) { | |
document.documentElement.classList.toggle("dark", dark); | |
} | |
const mql = window.matchMedia("(prefers-color-scheme: dark)"); | |
function updateDark() { | |
const fromLocalStorage = localStorage.getItem("theme"); | |
if (fromLocalStorage != null) { | |
setDark(fromLocalStorage === "dark"); | |
return; | |
} | |
setDark(mql.matches); | |
} | |
mql.addEventListener("change", updateDark); | |
window.addEventListener("storage", updateDark); | |
updateDark(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment