Created
September 3, 2021 01:27
-
-
Save joelmichael/bd6dab612ca9d062cf44bc81ace6f209 to your computer and use it in GitHub Desktop.
Auto dark mode detection
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(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