[PR #7026] feat(frontend): add a follow-system theme mode #6590

Open
opened 2026-11-02 04:09:54 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/7026
Author: @ridersaying
Created: 10/29/2026
Status: 🔄 Open

Base: main ← Head: main


📝 Commits (1)

  • 97378bd feat(frontend): add a follow-system theme mode

📊 Changes

20 files changed (+393 additions, -82 deletions)

View changed files

📝 frontend/src/components/command-palette/CommandPalette.tsx (+16 -15)
📝 frontend/src/hooks/useTheme.tsx (+130 -11)
📝 frontend/src/layouts/AppSidebar.tsx (+24 -21)
📝 frontend/src/pages/login/LoginPage.tsx (+18 -21)
📝 frontend/src/test/storybook-theme.test.tsx (+3 -0)
➕ frontend/src/test/theme-system.test.tsx (+132 -0)
📝 internal/web/translation/ar-EG.json (+5 -1)
📝 internal/web/translation/en-US.json (+5 -1)
📝 internal/web/translation/es-ES.json (+5 -1)
📝 internal/web/translation/fa-IR.json (+5 -1)
📝 internal/web/translation/id-ID.json (+5 -1)
📝 internal/web/translation/ja-JP.json (+5 -1)
📝 internal/web/translation/pt-BR.json (+5 -1)
📝 internal/web/translation/ru-RU.json (+5 -1)
📝 internal/web/translation/sk-SK.json (+5 -1)
📝 internal/web/translation/tr-TR.json (+5 -1)
📝 internal/web/translation/uk-UA.json (+5 -1)
📝 internal/web/translation/vi-VN.json (+5 -1)
📝 internal/web/translation/zh-CN.json (+5 -1)
📝 internal/web/translation/zh-TW.json (+5 -1)

📄 Description

Summary

Adds a third theme mode, system, so the panel can follow the OS light/dark preference instead of forcing an explicit choice. The theme switch now cycles light → dark → ultra dark → system, and in system mode the panel repaints whenever the desktop scheme changes, without a reload.

Why

The panel only offered light, dark and ultra dark, stored as a boolean in dark-mode. Anyone running a scheduled dark mode (macOS auto appearance, GNOME night light, Windows auto dark) has to flip the switch twice a day by hand, and the panel never matches the rest of the desktop.

I searched the issue tracker and the PR list first — prefers-color-scheme, "auto theme", "theme follow system" and "system dark" return nothing, and no open or closed PR implements it. #6827 (token-based theme customisation) and #6949 (accent colours) are the nearest neighbours and neither touches the mode itself. So this is a new feature rather than a fix to something existing.

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Documentation
  • Tests only
  • Build / CI / tooling
  • Other

Areas affected

  • Frontend (UI / panel pages)
  • Backend (API endpoints, login, settings)
  • Xray config generation
  • Subscription (share links / Clash / JSON)
  • Statistics / traffic counters
  • Database / migrations
  • Install / upgrade script
  • Docker image
  • Multi-node (sub-nodes)
  • Telegram bot

How was this tested?

Unit / component (jsdom)

npx vitest run --project components src/test/theme-system.test.tsx — 4 tests, each written to fail on a specific regression:

  • honours the legacy dark-mode boolean when theme-mode was never written — a stored dark-mode=false with no theme-mode must open light, so existing users keep their theme through the upgrade.
  • follows the OS scheme in system mode — flipping the matchMedia listener must repaint body class and color-scheme without a remount.
  • keeps an explicit mode pinned when the OS scheme changes — an explicit light/dark choice must ignore OS changes; this is the regression that would make the feature annoying rather than useful.
  • cycles light → dark → ultra dark → system → light — pins the cycle order, including that ultra dark is dropped on the way out.

src/test/storybook-theme.test.tsx needed one change: localStorage.clear() between cases, because the provider now seeds from storage at mount instead of a module-level snapshot, so a toggle in one case leaked into the next.

Real browser, against a built panel

The branch was built into a linux/amd64 binary and served a real panel; Chromium drove the login page with CDP colour-scheme emulation:

step aria-label color-scheme body class
fresh profile 主题: 深色 dark dark
click 1 主题: 极致深色 dark dark
click 2 主题: 跟随系统 light light
OS → dark, no reload 主题: 跟随系统 dark dark
OS → light, no reload 主题: 跟随系统 light light
click 3 主题: 浅色 light light
reload 主题: 深色 dark dark

Rows 3–5 are the point of the change: same tab, same session, only the emulated OS scheme changing.

Three platforms

npm run typecheck, npm run lint, npm run format:check and npx vite build were run on macOS (Node 26), Windows 11 (Node 26) and Ubuntu (Node 24): all clean, no warnings. The full component suite (100 files, 501 tests) passes locally.

Screenshots / recordings

The toggle sits next to the docs and donate buttons in the sidebar brand row and in the login toolbar, and shows a desktop icon in system mode; the command palette action (Ctrl/Cmd+K → Theme) is titled with the current mode too.

I have full-size captures of each state and can attach them here if you want them in the review — tell me and I will upload.

Breaking changes

None.

  • dark-mode (existing) and isUltraDarkThemeEnabled are still written on every change, so older panel builds, user scripts and anything reading the boolean keep working.
  • theme-mode is new and optional; when absent, the stored boolean seeds it, so nobody's theme changes after the upgrade.
  • useTheme() keeps its existing shape (isDark, isUltra, toggleTheme, toggleUltra, antdThemeConfig) and adds mode, systemDark, setMode, cycleTheme. Existing consumers (HostsPage, NodesPage, ApiDocsPage, Sparkline, …) needed no changes.
  • New i18n keys are menu.themeLight, menu.themeDark, menu.themeUltraDark, menu.themeSystem, translated in all 14 locales. i18n-dead-keys.test.ts passes, so no locale is missing them.

Checklist

  • I tested the change locally and confirmed the described behavior.
  • I added or updated tests for the new behavior (when applicable).
  • go build ./... passes locally, and the linux/amd64 binary built from this branch is running on a production panel.
  • For frontend changes: npm run lint, npm run typecheck, and npm run build pass.

Note on the third item: no Go file is touched by this PR, so the Go test suite was not run.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/7026 **Author:** [@ridersaying](https://github.com/ridersaying) **Created:** 10/29/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `main` --- ### 📝 Commits (1) - [`97378bd`](https://github.com/MHSanaei/3x-ui/commit/97378bd17d80ea7f4f58c4333f6f595120b51d0d) feat(frontend): add a follow-system theme mode ### 📊 Changes **20 files changed** (+393 additions, -82 deletions) <details> <summary>View changed files</summary> 📝 `frontend/src/components/command-palette/CommandPalette.tsx` (+16 -15) 📝 `frontend/src/hooks/useTheme.tsx` (+130 -11) 📝 `frontend/src/layouts/AppSidebar.tsx` (+24 -21) 📝 `frontend/src/pages/login/LoginPage.tsx` (+18 -21) 📝 `frontend/src/test/storybook-theme.test.tsx` (+3 -0) ➕ `frontend/src/test/theme-system.test.tsx` (+132 -0) 📝 `internal/web/translation/ar-EG.json` (+5 -1) 📝 `internal/web/translation/en-US.json` (+5 -1) 📝 `internal/web/translation/es-ES.json` (+5 -1) 📝 `internal/web/translation/fa-IR.json` (+5 -1) 📝 `internal/web/translation/id-ID.json` (+5 -1) 📝 `internal/web/translation/ja-JP.json` (+5 -1) 📝 `internal/web/translation/pt-BR.json` (+5 -1) 📝 `internal/web/translation/ru-RU.json` (+5 -1) 📝 `internal/web/translation/sk-SK.json` (+5 -1) 📝 `internal/web/translation/tr-TR.json` (+5 -1) 📝 `internal/web/translation/uk-UA.json` (+5 -1) 📝 `internal/web/translation/vi-VN.json` (+5 -1) 📝 `internal/web/translation/zh-CN.json` (+5 -1) 📝 `internal/web/translation/zh-TW.json` (+5 -1) </details> ### 📄 Description ## Summary Adds a third theme mode, `system`, so the panel can follow the OS light/dark preference instead of forcing an explicit choice. The theme switch now cycles light → dark → ultra dark → system, and in `system` mode the panel repaints whenever the desktop scheme changes, without a reload. ## Why The panel only offered light, dark and ultra dark, stored as a boolean in `dark-mode`. Anyone running a scheduled dark mode (macOS auto appearance, GNOME night light, Windows auto dark) has to flip the switch twice a day by hand, and the panel never matches the rest of the desktop. I searched the issue tracker and the PR list first — `prefers-color-scheme`, "auto theme", "theme follow system" and "system dark" return nothing, and no open or closed PR implements it. #6827 (token-based theme customisation) and #6949 (accent colours) are the nearest neighbours and neither touches the mode itself. So this is a new feature rather than a fix to something existing. ## Type of change - [ ] Bug fix - [x] New feature - [ ] Refactoring (no behavior change) - [ ] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [x] Frontend (UI / panel pages) - [ ] Backend (API endpoints, login, settings) - [ ] Xray config generation - [ ] Subscription (share links / Clash / JSON) - [ ] Statistics / traffic counters - [ ] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [ ] Multi-node (sub-nodes) - [ ] Telegram bot ## How was this tested? **Unit / component (jsdom)** `npx vitest run --project components src/test/theme-system.test.tsx` — 4 tests, each written to fail on a specific regression: - `honours the legacy dark-mode boolean when theme-mode was never written` — a stored `dark-mode=false` with no `theme-mode` must open light, so existing users keep their theme through the upgrade. - `follows the OS scheme in system mode` — flipping the `matchMedia` listener must repaint body class and `color-scheme` without a remount. - `keeps an explicit mode pinned when the OS scheme changes` — an explicit light/dark choice must ignore OS changes; this is the regression that would make the feature annoying rather than useful. - `cycles light → dark → ultra dark → system → light` — pins the cycle order, including that ultra dark is dropped on the way out. `src/test/storybook-theme.test.tsx` needed one change: `localStorage.clear()` between cases, because the provider now seeds from storage at mount instead of a module-level snapshot, so a toggle in one case leaked into the next. **Real browser, against a built panel** The branch was built into a linux/amd64 binary and served a real panel; Chromium drove the login page with CDP colour-scheme emulation: | step | aria-label | `color-scheme` | body class | |---|---|---|---| | fresh profile | 主题: 深色 | dark | dark | | click 1 | 主题: 极致深色 | dark | dark | | click 2 | 主题: 跟随系统 | light | light | | OS → dark, no reload | 主题: 跟随系统 | dark | dark | | OS → light, no reload | 主题: 跟随系统 | light | light | | click 3 | 主题: 浅色 | light | light | | reload | 主题: 深色 | dark | dark | Rows 3–5 are the point of the change: same tab, same session, only the emulated OS scheme changing. **Three platforms** `npm run typecheck`, `npm run lint`, `npm run format:check` and `npx vite build` were run on macOS (Node 26), Windows 11 (Node 26) and Ubuntu (Node 24): all clean, no warnings. The full component suite (100 files, 501 tests) passes locally. ## Screenshots / recordings The toggle sits next to the docs and donate buttons in the sidebar brand row and in the login toolbar, and shows a desktop icon in `system` mode; the command palette action (`Ctrl/Cmd+K` → Theme) is titled with the current mode too. I have full-size captures of each state and can attach them here if you want them in the review — tell me and I will upload. ## Breaking changes None. - `dark-mode` (existing) and `isUltraDarkThemeEnabled` are still written on every change, so older panel builds, user scripts and anything reading the boolean keep working. - `theme-mode` is new and optional; when absent, the stored boolean seeds it, so nobody's theme changes after the upgrade. - `useTheme()` keeps its existing shape (`isDark`, `isUltra`, `toggleTheme`, `toggleUltra`, `antdThemeConfig`) and adds `mode`, `systemDark`, `setMode`, `cycleTheme`. Existing consumers (`HostsPage`, `NodesPage`, `ApiDocsPage`, `Sparkline`, …) needed no changes. - New i18n keys are `menu.themeLight`, `menu.themeDark`, `menu.themeUltraDark`, `menu.themeSystem`, translated in all 14 locales. `i18n-dead-keys.test.ts` passes, so no locale is missing them. ## Checklist - [x] I tested the change locally and confirmed the described behavior. - [x] I added or updated tests for the new behavior (when applicable). - [x] `go build ./...` passes locally, and the linux/amd64 binary built from this branch is running on a production panel. - [x] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. Note on the third item: no Go file is touched by this PR, so the Go test suite was not run. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#6590
No description provided.