Detect preferred color scheme

This commit is contained in:
silverpill 2023-04-15 20:10:48 +00:00
parent eb73a7cf37
commit 302872d4ac
2 changed files with 14 additions and 2 deletions

View file

@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## [Unreleased] ## [Unreleased]
### Added
- Detect preferred color scheme.
## [1.21.0] - 2023-04-12 ## [1.21.0] - 2023-04-12
### Changed ### Changed

View file

@ -9,7 +9,15 @@ enum Theme {
Dark = "dark", Dark = "dark",
} }
const currentTheme = ref(Theme.Light) function defaultTheme(): Theme {
if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
return Theme.Dark
} else {
return Theme.Light
}
}
const currentTheme = ref(defaultTheme())
export function useTheme() { export function useTheme() {
@ -36,7 +44,7 @@ export function useTheme() {
const { ensureCurrentUser } = useCurrentUser() const { ensureCurrentUser } = useCurrentUser()
const currentUser = ensureCurrentUser() const currentUser = ensureCurrentUser()
const clientConfig = currentUser.client_config[APP_NAME] || {} const clientConfig = currentUser.client_config[APP_NAME] || {}
const theme = clientConfig.theme || Theme.Light const theme = clientConfig.theme || defaultTheme()
setTheme(theme as Theme) setTheme(theme as Theme)
} }