Skip to content

Instantly share code, notes, and snippets.

@bryceadams
Last active July 25, 2022 07:04
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 bryceadams/503a5cac996cce1187086c01d3fb7aec to your computer and use it in GitHub Desktop.
Save bryceadams/503a5cac996cce1187086c01d3fb7aec to your computer and use it in GitHub Desktop.
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)') {
var windowPrefersDark = window.matchMedia('(prefers-color-scheme: dark)');
var systemColorMode = windowPrefersDark.matches ? 'dark' : 'light';
// need to check in case doesn't exist like with Safari 13.x
if (
'addEventListener' in windowPrefersDark &&
typeof windowPrefersDark.addEventListener === 'function'
) {
windowPrefersDark.addEventListener('change', (event) => {
systemColorMode = event.matches ? 'dark' : 'light';
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment