Skip to content

Commit 5ea4cc1

Browse files
authored
feat: change theme dynamically when system preferences change (ietf-tools#7956)
1 parent 5581dc7 commit 5ea4cc1

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

client/App.vue

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,24 @@ const appContainer = ref(null)
3030
// Set user theme
3131
// --------------------------------------------------------------------
3232
33-
const desiredTheme = window.localStorage?.getItem('theme')
34-
if (desiredTheme === 'dark') {
35-
siteStore.theme = 'dark'
36-
} else if (desiredTheme === 'light') {
37-
siteStore.theme = 'light'
38-
} else if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
39-
siteStore.theme = 'dark'
40-
} else {
41-
siteStore.theme = 'light'
33+
function updateTheme() {
34+
const desiredTheme = window.localStorage?.getItem('theme')
35+
if (desiredTheme === 'dark') {
36+
siteStore.theme = 'dark'
37+
} else if (desiredTheme === 'light') {
38+
siteStore.theme = 'light'
39+
} else if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
40+
siteStore.theme = 'dark'
41+
} else {
42+
siteStore.theme = 'light'
43+
}
4244
}
4345
46+
updateTheme()
47+
48+
// this change event fires for either light or dark changes
49+
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', updateTheme)
50+
4451
// --------------------------------------------------------------------
4552
// Handle browser resize
4653
// --------------------------------------------------------------------

0 commit comments

Comments
 (0)