Created
March 19, 2022 13:05
-
-
Save AhmetEnesKCC/bb78d112aa2eabb90710219cf15acd41 to your computer and use it in GitHub Desktop.
watch system theme
This file contains 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
const watchSystemTheme = (cb) => { // I will use callback for that | |
// Addlistener to matchmedia for light and dark mode | |
window.matchMedia("(prefers-color-scheme: dark)").addListener(e => { | |
e.matches && cb("dark"); // If it is dark we called the defined callback with dark argument | |
}) | |
window.matchMedia("(prefers-color-scheme: light)").addListener(e => { | |
e.matches && cb("light"); // Else If it is dark we called the defined callback with light argument | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment