fix: dark mode

This commit is contained in:
2025-08-06 15:20:08 +08:00
parent aa20b6d7e6
commit 690e9bdbf4
2 changed files with 23 additions and 1 deletions

View File

@@ -34,6 +34,7 @@ class AppState {
// 初始化主题
const savedTheme = localStorage.getItem('theme') || this.detectSystemTheme();
console.log('初始化主题:', savedTheme);
this.setTheme(savedTheme);
// 监听系统主题变化
@@ -60,6 +61,7 @@ class AppState {
setTheme(theme) {
document.documentElement.setAttribute('data-theme', theme);
console.log('设置主题:', theme, '到HTML元素');
// 更新主题切换按钮文本
const themeToggle = document.querySelector('.theme-toggle');
@@ -67,6 +69,27 @@ class AppState {
themeToggle.setAttribute('aria-label',
theme === 'light' ? '切换到暗色主题' : '切换到亮色主题');
}
// 确保CSS变量应用到根元素
const root = document.documentElement;
if (theme === 'dark') {
root.style.setProperty('--bg-primary', '#0f172a');
root.style.setProperty('--bg-secondary', '#1e293b');
root.style.setProperty('--bg-tertiary', '#334155');
root.style.setProperty('--text-primary', '#f8fafc');
root.style.setProperty('--text-secondary', '#e2e8f0');
} else {
root.style.setProperty('--bg-primary', '#ffffff');
root.style.setProperty('--bg-secondary', '#f8fafc');
root.style.setProperty('--bg-tertiary', '#f1f5f9');
root.style.setProperty('--text-primary', '#0f172a');
root.style.setProperty('--text-secondary', '#475569');
}
// 强制触发重绘
document.documentElement.style.display = 'none';
document.documentElement.offsetHeight; // 触发重排
document.documentElement.style.display = '';
}
// 侧边栏管理

View File

@@ -28,7 +28,6 @@
<!-- 主题切换 -->
<button class="theme-toggle"
aria-label="切换主题"
onclick="toggleTheme()"
title="切换明暗主题">
<svg class="sun-icon" width="20" height="20" fill="currentColor" viewBox="0 0 24 24">
<path d="M12 2.25a.75.75 0 01.75.75v2.25a.75.75 0 01-1.5 0V3a.75.75 0 01.75-.75zM7.5 12a4.5 4.5 0 119 0 4.5 4.5 0 01-9 0zM18.894 6.166a.75.75 0 00-1.06-1.06l-1.591 1.59a.75.75 0 101.06 1.061l1.591-1.59zM21.75 12a.75.75 0 01-.75.75h-2.25a.75.75 0 010-1.5H21a.75.75 0 01.75.75zM17.834 18.894a.75.75 0 001.06-1.06l-1.59-1.591a.75.75 0 10-1.061 1.06l1.59 1.591zM12 18a.75.75 0 01.75.75V21a.75.75 0 01-1.5 0v-2.25A.75.75 0 0112 18zM7.758 17.303a.75.75 0 00-1.061-1.06l-1.591 1.59a.75.75 0 001.06 1.061l1.591-1.59zM6 12a.75.75 0 01-.75.75H3a.75.75 0 010-1.5h2.25A.75.75 0 016 12zM6.697 7.757a.75.75 0 001.06-1.06l-1.59-1.591a.75.75 0 00-1.061 1.06l1.59 1.591z"/>