Created
July 10, 2020 23:28
-
-
Save semul/84a9e4d2e53671a3a57e3a632be9b904 to your computer and use it in GitHub Desktop.
Get prefers color scheme by pure JavaScript
This file contains hidden or 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
// Get instant color scheme | |
window.matchMedia( '(prefers-color-scheme: dark)' ).addEventListener( 'change', e => { | |
let mode = e.matches ? "dark" : "light"; | |
if ( mode == "dark" ){ | |
console.log('Now Dark'); | |
}else{ | |
console.log('Now Light'); | |
} | |
} ); | |
// Get current color scheme | |
if ( window.matchMedia && window.matchMedia( '(prefers-color-scheme: dark)' ).matches ) { | |
console.log( 'Hello Dark' ); | |
} else { | |
console.log( 'Hello Light' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment