From 49b7ecc5834112e4732c864d753cdec9bdc5696c Mon Sep 17 00:00:00 2001 From: ocfox Date: Mon, 14 Sep 2026 19:43:32 +0800 Subject: [PATCH] fix(web): add dark mode theme-color meta tag for PWA status bar (#6320) --- web/index.html | 3 ++- web/src/utils/theme.ts | 27 ++++++++++++++++++++++----- web/tests/theme.test.ts | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 web/tests/theme.test.ts diff --git a/web/index.html b/web/index.html index 606be5059..0f2e41db3 100644 --- a/web/index.html +++ b/web/index.html @@ -9,7 +9,8 @@ - + + diff --git a/web/src/utils/theme.ts b/web/src/utils/theme.ts index 2232be921..521b643d2 100644 --- a/web/src/utils/theme.ts +++ b/web/src/utils/theme.ts @@ -175,10 +175,27 @@ const setThemeAttribute = (theme: ResolvedTheme): void => { * Updates the theme-color meta tag to match the current theme background. * This colors the browser/status bar on mobile devices. */ -const updateThemeColorMeta = (theme: ResolvedTheme): void => { - const meta = document.querySelector('meta[name="theme-color"]'); - if (meta) { - meta.content = THEME_COLORS[theme]; +const updateThemeColorMeta = (theme: Theme): void => { + const lightMeta = document.querySelector('meta[name="theme-color"][media*="light"]'); + const darkMeta = document.querySelector('meta[name="theme-color"][media*="dark"]'); + + if (theme === "system") { + if (lightMeta) { + lightMeta.content = THEME_COLORS.default; + } + if (darkMeta) { + darkMeta.content = THEME_COLORS["default-dark"]; + } + return; + } + + const resolvedTheme = resolveTheme(theme); + const color = THEME_COLORS[resolvedTheme]; + if (lightMeta) { + lightMeta.content = color; + } + if (darkMeta) { + darkMeta.content = color; } }; @@ -213,7 +230,7 @@ export const loadTheme = (themeName: string): void => { injectThemeStyle(resolvedTheme); setThemeAttribute(resolvedTheme); - updateThemeColorMeta(resolvedTheme); + updateThemeColorMeta(validTheme); updateColorScheme(resolvedTheme); setStoredTheme(validTheme); // Store original theme preference (not resolved) }; diff --git a/web/tests/theme.test.ts b/web/tests/theme.test.ts new file mode 100644 index 000000000..95db85937 --- /dev/null +++ b/web/tests/theme.test.ts @@ -0,0 +1,33 @@ +import { beforeEach, describe, expect, it } from "vitest"; +import { loadTheme } from "@/utils/theme"; + +describe("theme-color meta tags update", () => { + beforeEach(() => { + document.head.innerHTML = ` + + + `; + localStorage.clear(); + }); + + it("updates meta tags to specific theme color when a non-system theme is loaded", () => { + loadTheme("default-dark"); + + const lightMeta = document.querySelector('meta[name="theme-color"][media*="light"]'); + const darkMeta = document.querySelector('meta[name="theme-color"][media*="dark"]'); + + expect(lightMeta?.content).toBe("#1d1f23"); + expect(darkMeta?.content).toBe("#1d1f23"); + }); + + it("restores adaptive meta tags when system theme is loaded", () => { + loadTheme("paper"); + loadTheme("system"); + + const lightMeta = document.querySelector('meta[name="theme-color"][media*="light"]'); + const darkMeta = document.querySelector('meta[name="theme-color"][media*="dark"]'); + + expect(lightMeta?.content).toBe("#faf9f5"); + expect(darkMeta?.content).toBe("#1d1f23"); + }); +});