Created
April 8, 2021 14:17
-
-
Save james2doyle/f598c675a3fbe1a346cf3e61b13cb1e8 to your computer and use it in GitHub Desktop.
An example of how to detect dark theme preferences (prefers-color-scheme) using window.matchMedia
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() { | |
const prefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches; | |
const setting = localStorage.getItem('color-schema') || 'auto'; | |
if (setting === 'dark' || (prefersDark && setting !== 'light')) { | |
document.documentElement.classList.toggle('dark', true); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment