Переписал на vue3
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import { ref, computed, onMounted } from 'vue'
|
||||
|
||||
export function useTheme() {
|
||||
const theme = ref('dark')
|
||||
|
||||
const isLightTheme = computed(() => theme.value === 'light')
|
||||
|
||||
function applyTheme(name) {
|
||||
theme.value = name
|
||||
document.documentElement.className = name
|
||||
localStorage.setItem('theme', name)
|
||||
}
|
||||
|
||||
function toggleTheme() {
|
||||
applyTheme(theme.value === 'dark' ? 'light' : 'dark')
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
const saved = localStorage.getItem('theme')
|
||||
if (saved === 'dark' || saved === 'light') {
|
||||
applyTheme(saved)
|
||||
return
|
||||
}
|
||||
|
||||
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches
|
||||
applyTheme(prefersDark ? 'dark' : 'light')
|
||||
})
|
||||
|
||||
return { theme, isLightTheme, toggleTheme }
|
||||
}
|
||||
Reference in New Issue
Block a user