Last active
January 10, 2020 20:59
-
-
Save thagxt/538a028892597f97b4dea360a33d6f12 to your computer and use it in GitHub Desktop.
Detect if OS is in dark mode with Vanilla JS and add class to BODY
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
document.addEventListener("DOMContentLoaded", function(){ | |
// Detect dark mode with Vanilla JS and add class to BODY | |
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) { | |
document.body.classList.add("dark-mode"); // your class here | |
} | |
// Turn Dark Mode on/off, manually | |
document.querySelector('.lightsoff').onclick = function() { | |
document.querySelector('body').classList.toggle('dark-mode'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment