Repository navigation
nav 公共页亮暗主题切换按钮(访客个人 + 三态循环,localStorage 记忆) - #23
Merged
todosdev[bot] merged 4 commits intoNov 4, 2026
Merged
Conversation
added 4 commits
November 4, 2026 18:35
- 新增访客本机偏好层:nav.visitor-theme 记住 system/light/dark,优先级高于站点设置,首帧前生效不闪 - 公共页顶栏加切换按钮(Monitor/Sun/Moon 三档图标,按 跟随系统→浅色→深色 循环) - 补 10 条单测覆盖循环、记忆、覆盖站点默认、非法值与未加载态
- 按钮挪到顶栏按钮组最右:登录/后台按钮出现或消失时不再左右跳动(实测 pending→已登录 x 恒为 1332) - 图标颜色 text-white/90 → text-foreground:浅色档芯片上对比度 2.41:1 → 8.22:1(深色档 15.96:1) - 补测:模块初始化读取本机偏好、PublicPage 三种登录态都渲染且按钮在组内最右、layout effect 落色(改 useEffect 会失败) - 清掉 nextThemeMode 里不可达的 ?? "system",修正 sonner 的主题来源注释
用户已确认:点回「跟随系统」不交回站点默认,所以把这条决定记在 theme.ts (文件头、useVisitorThemeStore、useThemeCycle),避免以后被当成 bug 改回去。
- useSiteThemeMode 过 isThemeMode:后端 settings 表是自由文本,未知档位当 null(回到不猜色), 不再把 undefined 交给 THEME_ICONS/resolveTheme;补 3 条用例,去掉校验后其中 2 条失败 - 登录/后台/退出按钮改 text-foreground:浅色档与主题按钮色调一致(实测两档计算色完全相同) - 顶栏用例不再断言按钮总数,只断言「主题按钮在且是组内最右」
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.
Summary
localStorage,只影响当前浏览器,各访客互不影响。settings.theme站点默认;没点过的访客仍按后台设置显示,后台「站点设置→主题」与后端 settings 表、API 契约一律不动。useLayoutEffect落<html>.dark),并堵住一个由脏数据引入的整页白屏路径。Changes
web/src/lib/theme.tsVISITOR_THEME_KEY = "nav.visitor-theme"、isThemeMode、readVisitorTheme、nextThemeMode、useVisitorThemeStore、useEffectiveThemeMode、useThemeCycle;useResolvedTheme改为走生效档位。useSiteThemeMode()对后端下发的theme做isThemeMode校验,脏值回落到「不猜颜色」,避免THEME_ICONS[mode]取到undefined导致 React 整页抛错(后端 settings 表是自由文本,PUT 只校验写入)。web/src/components/theme-provider.tsx— 改用useEffectiveThemeMode(),其余(useLayoutEffect落类、matchMedia订阅、不猜色行为)不变。web/src/components/theme-toggle.tsx(新)— Monitor / Sun / Moon 三档图标按钮,aria-label+title是「主题:X,点击切换为 Y」,mode为空时不渲染;text-foreground保证浅色档在毛玻璃芯片上可见。web/src/components/public-page.tsx— 顶栏按钮组最右加<ThemeToggle />(探活中/已登录/未登录都显示,且固定在右缘不随登录/后台按钮出现而左右跳动);登录/后台/退出按钮统一改为text-foreground,与主题按钮在浅色档色调一致。web/src/components/ui/sonner.tsx— 仅更新注释,说明 Toaster 主题现在跟随生效档位(本机偏好 > 站点设置)。web/test/theme-toggle.test.tsx(新,66 条中的 18 条)— 覆盖三态循环落盘、刷新恢复(模块初始化路径)、访客偏好覆盖站点默认、跟随系统实时跟随、非法 localStorage 值、脏站点值不崩溃、三种登录态下按钮都在且是组内最后、无首帧闪烁(layout effect 落色,改useEffect会失败)。Testing
pnpm lint(仅 27 条既有 warning,退出码 0)、pnpm format:check、pnpm typecheck、pnpm build、pnpm test(web 66 + worker 102)。light/dark/system并同步切换.dark/color-scheme;刷新后本机偏好保持且覆盖站点默认;逐帧探针证明「有内容的第一帧就已是深色」;Emulation.setEmulatedMedia强制系统深/浅色即时跟随;未登录访客也有按钮;#admin同样跟随本机偏好且无该按钮;直接sqlite3把theme改成"purple"后公共页/后台均正常渲染、无白屏。text-white/90仅 2.4:1)、深色档 15.96:1;顶栏三个按钮计算色在两种档位下完全一致。workflow_dispatch补跑):green,head 与当前提交一致。