Skip to content

Instantly share code, notes, and snippets.

@thagxt
Last active January 10, 2020 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thagxt/538a028892597f97b4dea360a33d6f12 to your computer and use it in GitHub Desktop.
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
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