Repository navigation
ONE-448: 主题切换生效(settings.theme 三档 + system 跟随) - #17
Merged
Merged
Conversation
- 新增 web/src/lib/theme.ts:三档解析(system 用 matchMedia 实时解析)+ 系统外观 store,不落 localStorage - 新增 web/src/components/theme-provider.tsx:按站点数据 settings.theme 在绘制前切换 html.dark / color-scheme,公开页与管理面同一来源 - main.tsx 挂载 ThemeProvider - sonner.tsx 去掉悬空的 next-themes useTheme,改用解析后的显式 light/dark - 后台保存设置(含 JSON 导入)后用 PUT 响应就地刷新 store,主题保存即生效
按评审意见修复: - nav-store.patchAdminSettings 改为原子刷新 adminData / publicData / privateData 三片存活切片 (原实现只刷 adminData:登出后主题回退、返回公开页站点标题/背景图不生效) - theme.useSiteThemeMode 真源优先级补 privateData:adminData > privateData > publicData (已登录刷新时 bootstrapSession 先拉 /api/private,不再依赖延迟的 /api/public) - theme-provider 补 matchMedia 环境防御(nit) 按族排查额外发现并修复,同属「settings 写回」这一类: - loadPublic 是第 3 个 settings 写回入口,而后端 /api/public 是 public, max-age=60, 返回公开页可能拿旧副本把新设置盖回去:本地写新设置后下一轮 loadPublic 显式绕过缓存, 并丢弃「写新之前发出、写新之后才回」的旧响应(api.getPublic 增加可选 cache 参数)
This was referenced Nov 6, 2026
todosdev Bot
added a commit
that referenced
this pull request
Nov 6, 2026
…d-ad0342ed16e8 nav 链接编辑支持修改所属分类(下拉 + categoryId,沿 #17 搬家口径)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ONE-448 阶段 3:主题切换生效(settings.theme 三档 + system 跟随)。中等档,只改 web/,不碰 worker/。
改了什么
web/src/lib/theme.ts(新增)system用matchMedia('(prefers-color-scheme: dark)')实时解析成 light/dark;resolved是派生值,不落 localStorage;导出useSiteThemeMode / useResolvedTheme与一个只装系统外观的 zustand storeweb/src/components/theme-provider.tsx(新增)settings.theme→documentElement.classList.toggle("dark")+style.colorScheme;订阅matchMedia的 change 事件(system 实时跟随);用useLayoutEffect在绘制前完成切换web/src/main.tsx<ThemeProvider>(包住<App />)web/src/components/ui/sonner.tsxnext-themesuseTheme,Toaster 的 theme 传解析结果的显式值(未解析时回退system)web/src/lib/nav-store.tspatchAdminSettings(settings):用PUT /api/admin/settings的响应就地刷新adminData.settingsweb/src/components/admin/settings-panels.tsxpatchAdminSettings,主题保存即生效(仍有loadAdmin兜底)为什么不用 next-themes Provider:它是「访客级偏好 + localStorage 持久化 + 首屏内联脚本读 storage」的模型,与单描述冻结的契约(站点级设置、
settings.theme唯一真源、不得引入 localStorage 第二真源)冲突,因此只用已在本栈里的 zustand + matchMedia 做了等效实现。next-themes依赖本身此单不动(删除会整份重写pnpm-lock.yaml,超出本单范围,建议后续单独清理)。行为
dark→ 加.dark、color-scheme: dark;light→ 移除;system→ 跟系统,并在系统外观切换时不刷新即时跟随。settings.theme(后台存在时优先取后台数据,因为它刚被写新),两侧表现一致。document,不拿 light/dark 猜。自验记录
生产构建产物 + 真实 Chrome(playwright 驱动,后端
/api/*由本地假服务按场景返回)跑 11 条断言,11/11 通过:本地 CI 等价命令全绿:
pnpm lint(退出 0,仅 28 条既有 warning,未新增)、pnpm format:check、pnpm typecheck、pnpm build、pnpm test(97 passed)。未验证 / 已知限制:
/api/admin/settings返回前的「加载中…」占位仍用浅色底 → 深色站点会有一次约百毫秒的浅色占位(这是「不落 localStorage」的必然代价,主题值只能来自服务端)。prefers-color-scheme切换。