Skip to content

Instantly share code, notes, and snippets.

@zidell
Created May 19, 2020 14:05
Show Gist options
  • Select an option

  • Save zidell/717247167bd4f2e1b0532d9a1443d346 to your computer and use it in GitHub Desktop.

Select an option

Save zidell/717247167bd4f2e1b0532d9a1443d346 to your computer and use it in GitHub Desktop.
function userTheme() {
window.__THEME_MODE = 'light';
if (!!window.matchMedia) {
window.__THEME_MODE = window.matchMedia("(prefers-color-scheme: dark)").matches ? 'dark' : 'auto';
}
document.getElementsByTagName('html')[0].classList[window.__THEME_MODE === 'dark' ? 'add' : 'remove']('darkmode');
}
if (!!window.matchMedia) {
['light', 'dark'].forEach(mode => {
window.matchMedia(`(prefers-color-scheme: ${mode})`).addListener(e => {
if(!!e.matches) {
userTheme();
}
});
});
}
userTheme();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment