Skip to content

Instantly share code, notes, and snippets.

@james2doyle
Created April 8, 2021 14:17
Show Gist options
  • Save james2doyle/f598c675a3fbe1a346cf3e61b13cb1e8 to your computer and use it in GitHub Desktop.
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
(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