Persist themes with user default
This commit is contained in:
parent
753863585d
commit
62621e7fe6
@ -9,25 +9,28 @@ const theme_icons = {
|
|||||||
'dark': IconMoonStars,
|
'dark': IconMoonStars,
|
||||||
}
|
}
|
||||||
|
|
||||||
const currentTheme = ref('light')
|
let storedTheme = localStorage.getItem('theme')
|
||||||
|
if (!storedTheme) {
|
||||||
|
storedTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
|
||||||
|
}
|
||||||
|
|
||||||
|
const currentTheme = ref(storedTheme)
|
||||||
|
document.documentElement.setAttribute('data-bs-theme', storedTheme)
|
||||||
|
|
||||||
async function click(ev) {
|
async function click(ev) {
|
||||||
ev.preventDefault()
|
ev.preventDefault()
|
||||||
|
|
||||||
|
let newTheme = 'light'
|
||||||
if (currentTheme.value == 'light') {
|
if (currentTheme.value == 'light') {
|
||||||
currentTheme.value = 'dark'
|
newTheme = 'dark'
|
||||||
} else {
|
|
||||||
currentTheme.value = 'light'
|
|
||||||
}
|
}
|
||||||
document.documentElement.setAttribute('data-bs-theme', currentTheme.value)
|
|
||||||
|
currentTheme.value = newTheme
|
||||||
|
document.documentElement.setAttribute('data-bs-theme', newTheme)
|
||||||
|
localStorage.setItem('theme', newTheme)
|
||||||
}
|
}
|
||||||
|
|
||||||
const currentIcon = computed(() => {
|
const currentIcon = computed(() => theme_icons[currentTheme.value])
|
||||||
let icon = theme_icons[currentTheme.value]
|
|
||||||
//console.log(currentTheme.value)
|
|
||||||
//console.log(icon)
|
|
||||||
return icon
|
|
||||||
})
|
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user