Skip to content

Commit

Permalink
feat: set color scheme based on system preference (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
kunish authored Aug 4, 2023
1 parent e588f1f commit cc88300
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ export const App = () => {
[colorScheme],
)

useEffect(() => {
const darkModePreference = window.matchMedia('(prefers-color-scheme: dark)')
const onDarkModeChange = (e: MediaQueryListEvent) => toggleColorScheme(e.matches ? 'dark' : 'light')

darkModePreference.addEventListener('change', onDarkModeChange)

return () => darkModePreference.removeEventListener('change', onDarkModeChange)
}, [toggleColorScheme])

useEffect(() => {
setColorScheme(appState.preferredColorScheme || preferredColorScheme)
}, [setColorScheme, preferredColorScheme, appState.preferredColorScheme])
Expand Down

0 comments on commit cc88300

Please sign in to comment.