const isDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
console.log(isDarkMode) // Result: True or False
const isDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
const isDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').match;
/* Light mode, Dark mode is below...
Standard is registered on W3C in Media Queries Level 5:
https://drafts.csswg.org/mediaqueries-5/#prefers-color-scheme
*/
@media (prefers-color-scheme: light) {
body {
background-color: white;
color: black;
}
}
/* Dark mode */
@media (prefers-color-scheme: dark) {
body {
background-color: black;
color: white;
}
}
const isDarkMode = () =>
window.matchMedia &&
window.matchMedia("(prefers-color-scheme: dark)").matches;
// Testing
console.log(isDarkMode());