20260814173126
This commit is contained in:
1 parent
68de86aee9
commit
b6f9a4c912
17 files changed
+1210
-544
No files matched your search
@@ -1,96 +0,0 @@
|
||||
import { ref } from 'vue'
|
||||
|
||||
const MODE_STORAGE_KEY = 'fms-theme'
|
||||
const COLOR_STORAGE_KEY = 'fms-color-theme'
|
||||
|
||||
export const colorThemes = Object.freeze([
|
||||
{ value: 'neutral', label: '中性', color: 'oklch(0.205 0 0)' },
|
||||
{ value: 'blue', label: '蓝色', color: 'oklch(0.546 0.245 262.881)' },
|
||||
{ value: 'green', label: '绿色', color: 'oklch(0.527 0.154 150.069)' },
|
||||
{ value: 'violet', label: '紫色', color: 'oklch(0.496 0.265 301.924)' },
|
||||
{ value: 'orange', label: '橙色', color: 'oklch(0.57 0.2 49.5)' },
|
||||
{ value: 'red', label: '红色', color: 'oklch(0.506 0.232 25.7)' },
|
||||
{ value: 'pink', label: '粉色', color: 'oklch(0.523 0.214 349.4)' },
|
||||
{ value: 'teal', label: '青色', color: 'oklch(0.563 0.144 180)' },
|
||||
])
|
||||
|
||||
const theme = ref('light')
|
||||
const colorTheme = ref('neutral')
|
||||
let initialized = false
|
||||
|
||||
function isColorTheme(value) {
|
||||
return colorThemes.some((item) => item.value === value)
|
||||
}
|
||||
|
||||
function applyTheme() {
|
||||
if (typeof document === 'undefined') return
|
||||
|
||||
document.documentElement.dataset.theme = theme.value
|
||||
document.documentElement.dataset.colorTheme = colorTheme.value
|
||||
document.documentElement.style.colorScheme = theme.value
|
||||
}
|
||||
|
||||
function readStorage(key) {
|
||||
try {
|
||||
return window.localStorage.getItem(key)
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
function writeStorage(key, value) {
|
||||
try {
|
||||
window.localStorage.setItem(key, value)
|
||||
} catch {
|
||||
// The selected theme still applies when storage is unavailable.
|
||||
}
|
||||
}
|
||||
|
||||
function initializeTheme() {
|
||||
if (initialized || typeof window === 'undefined') return
|
||||
|
||||
initialized = true
|
||||
const storedTheme = readStorage(MODE_STORAGE_KEY)
|
||||
const storedColorTheme = readStorage(COLOR_STORAGE_KEY)
|
||||
|
||||
theme.value = storedTheme === 'dark' ? 'dark' : 'light'
|
||||
colorTheme.value = isColorTheme(storedColorTheme) ? storedColorTheme : 'neutral'
|
||||
applyTheme()
|
||||
}
|
||||
|
||||
function setTheme(value) {
|
||||
theme.value = value === 'dark' ? 'dark' : 'light'
|
||||
applyTheme()
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
writeStorage(MODE_STORAGE_KEY, theme.value)
|
||||
}
|
||||
}
|
||||
|
||||
function setColorTheme(value) {
|
||||
colorTheme.value = isColorTheme(value) ? value : 'neutral'
|
||||
applyTheme()
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
writeStorage(COLOR_STORAGE_KEY, colorTheme.value)
|
||||
}
|
||||
}
|
||||
|
||||
function toggleTheme() {
|
||||
setTheme(theme.value === 'dark' ? 'light' : 'dark')
|
||||
}
|
||||
|
||||
initializeTheme()
|
||||
|
||||
export function useTheme() {
|
||||
initializeTheme()
|
||||
|
||||
return {
|
||||
theme,
|
||||
colorTheme,
|
||||
colorThemes,
|
||||
setTheme,
|
||||
setColorTheme,
|
||||
toggleTheme,
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user