PWA
This commit is contained in:
parent
9cde9b5cf7
commit
f3927b97a4
21 changed files with 8672 additions and 202 deletions
53
src/components/theme-provider/index.ts
Normal file
53
src/components/theme-provider/index.ts
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
import { computed, onMounted, ref, watch } from 'vue'
|
||||
|
||||
type Theme = 'dark' | 'light' | 'system'
|
||||
|
||||
const useTheme = () => {
|
||||
const theme = ref<Theme>('dark')
|
||||
const systemTheme = ref<'dark' | 'light'>('light')
|
||||
|
||||
const updateSystemTheme = () => {
|
||||
systemTheme.value = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
|
||||
}
|
||||
|
||||
const currentTheme = computed(() => {
|
||||
return theme.value === 'system' ? systemTheme.value : theme.value
|
||||
})
|
||||
|
||||
const applyTheme = () => {
|
||||
if (currentTheme.value === 'dark') {
|
||||
document.documentElement.classList.add('dark')
|
||||
} else {
|
||||
document.documentElement.classList.remove('dark')
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
const stored = localStorage.getItem('ui-theme')
|
||||
if (stored && (stored === 'dark' || stored === 'light' || stored === 'system')) {
|
||||
theme.value = stored as Theme
|
||||
}
|
||||
|
||||
updateSystemTheme()
|
||||
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', updateSystemTheme)
|
||||
applyTheme()
|
||||
})
|
||||
|
||||
watch(currentTheme, () => {
|
||||
applyTheme()
|
||||
})
|
||||
|
||||
const setTheme = (newTheme: Theme) => {
|
||||
theme.value = newTheme
|
||||
localStorage.setItem('ui-theme', newTheme)
|
||||
}
|
||||
|
||||
return {
|
||||
theme,
|
||||
setTheme,
|
||||
systemTheme,
|
||||
currentTheme
|
||||
}
|
||||
}
|
||||
|
||||
export { useTheme }
|
||||
Loading…
Add table
Add a link
Reference in a new issue