Created
January 4, 2019 20:38
-
-
Save arnog/f907aeed9ff3916c9091de3b8cadabe7 to your computer and use it in GitHub Desktop.
This file contains 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 switchTheme(ev) { | |
// If the alt/option key is pressed, reset to system default | |
if (ev.altKey) { | |
document.getElementsByTagName("BODY")[0].removeAttribute("theme"); | |
return; | |
} | |
const prefersDark = window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches; | |
let theme = document.getElementsByTagName("BODY")[0].getAttribute("theme"); | |
if (theme === "dark") { | |
theme = "light"; | |
} else if (theme === "light") { | |
theme = "dark"; | |
} else { | |
theme = prefersDark ? "light" : "dark"; | |
} | |
document.getElementsByTagName("BODY")[0].setAttribute("theme", theme); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment