feat: change theme dynamically when system preferences change (#7956)
This commit is contained in:
parent
5581dc79e1
commit
5ea4cc13bb
|
@ -30,17 +30,24 @@ const appContainer = ref(null)
|
||||||
// Set user theme
|
// Set user theme
|
||||||
// --------------------------------------------------------------------
|
// --------------------------------------------------------------------
|
||||||
|
|
||||||
const desiredTheme = window.localStorage?.getItem('theme')
|
function updateTheme() {
|
||||||
if (desiredTheme === 'dark') {
|
const desiredTheme = window.localStorage?.getItem('theme')
|
||||||
siteStore.theme = 'dark'
|
if (desiredTheme === 'dark') {
|
||||||
} else if (desiredTheme === 'light') {
|
siteStore.theme = 'dark'
|
||||||
siteStore.theme = 'light'
|
} else if (desiredTheme === 'light') {
|
||||||
} else if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
|
siteStore.theme = 'light'
|
||||||
siteStore.theme = 'dark'
|
} else if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
|
||||||
} else {
|
siteStore.theme = 'dark'
|
||||||
siteStore.theme = 'light'
|
} else {
|
||||||
|
siteStore.theme = 'light'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateTheme()
|
||||||
|
|
||||||
|
// this change event fires for either light or dark changes
|
||||||
|
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', updateTheme)
|
||||||
|
|
||||||
// --------------------------------------------------------------------
|
// --------------------------------------------------------------------
|
||||||
// Handle browser resize
|
// Handle browser resize
|
||||||
// --------------------------------------------------------------------
|
// --------------------------------------------------------------------
|
||||||
|
|
Loading…
Reference in a new issue