Skip to content

Instantly share code, notes, and snippets.

@joelmichael
Created September 3, 2021 01:27
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 joelmichael/bd6dab612ca9d062cf44bc81ace6f209 to your computer and use it in GitHub Desktop.
Save joelmichael/bd6dab612ca9d062cf44bc81ace6f209 to your computer and use it in GitHub Desktop.
Auto dark mode detection
document.addEventListener('DOMContentLoaded', function(event) {
var changeColorScheme = function() {
var html = document.querySelector('html');
if (this.matches)
html.classList.add('dark');
else
html.classList.remove('dark');
}
var mm = window.matchMedia('(prefers-color-scheme: dark)');
mm.addEventListener('change', changeColorScheme);
changeColorScheme.call(mm);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment