feat: 添加客户端主题切换并优化导出状态
This commit is contained in:
@@ -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();
|
||||
Reference in New Issue
Block a user