feat: 添加客户端主题切换并优化导出状态

This commit is contained in:
Nixevol
2026-07-05 04:14:17 +08:00
parent 09a68ad0df
commit e8e514f6dd
10 changed files with 273 additions and 6 deletions
+46
View File
@@ -0,0 +1,46 @@
import { computed, ref } from 'vue';
export type ThemeMode = 'light' | 'dark' | 'auto';
const KEY = 'artveris_client_theme';
const modes: ThemeMode[] = ['light', 'dark', 'auto'];
const media = window.matchMedia('(prefers-color-scheme: dark)');
const systemDark = ref(media.matches);
media.addEventListener('change', (event) => {
systemDark.value = event.matches;
});
function getInitialMode(): ThemeMode {
const saved = localStorage.getItem(KEY) as ThemeMode | null;
return saved && modes.includes(saved) ? saved : 'auto';
}
const mode = ref<ThemeMode>(getInitialMode());
const isDark = computed(() => mode.value === 'dark' || (mode.value === 'auto' && systemDark.value));
function applyTheme() {
const root = document.documentElement;
if (mode.value === 'auto') {
root.removeAttribute('data-theme');
} else {
root.setAttribute('data-theme', mode.value);
}
root.style.colorScheme = isDark.value ? 'dark' : 'light';
}
export function setThemeMode(next: ThemeMode) {
mode.value = next;
localStorage.setItem(KEY, next);
applyTheme();
}
export function initTheme() {
applyTheme();
}
export function useTheme() {
return { mode, isDark, setThemeMode };
}
initTheme();