Skip to content

Instantly share code, notes, and snippets.

@AhmetEnesKCC
Created March 19, 2022 13:05
Show Gist options
  • Save AhmetEnesKCC/bb78d112aa2eabb90710219cf15acd41 to your computer and use it in GitHub Desktop.
Save AhmetEnesKCC/bb78d112aa2eabb90710219cf15acd41 to your computer and use it in GitHub Desktop.
watch system theme
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