fix(web): add dark mode theme-color meta tag for PWA status bar (#6320)

This commit is contained in:
ocfox
2026-09-14 19:43:32 +08:00
committed by GitHub
parent 2e33e7a561
commit 49b7ecc583
3 changed files with 57 additions and 6 deletions
+2 -1
View File
@@ -9,7 +9,8 @@
<link rel="icon" type="image/webp" href="/logo.webp" />
<link rel="manifest" href="/site.webmanifest" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<meta name="theme-color" content="#faf9f5" />
<meta name="theme-color" media="(prefers-color-scheme: light)" content="#faf9f5" />
<meta name="theme-color" media="(prefers-color-scheme: dark)" content="#1d1f23" />
<meta name="mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="default" />
+22 -5
View File
@@ -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<HTMLMetaElement>('meta[name="theme-color"]');
if (meta) {
meta.content = THEME_COLORS[theme];
const updateThemeColorMeta = (theme: Theme): void => {
const lightMeta = document.querySelector<HTMLMetaElement>('meta[name="theme-color"][media*="light"]');
const darkMeta = document.querySelector<HTMLMetaElement>('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)
};
+33
View File
@@ -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 = `
<meta name="theme-color" media="(prefers-color-scheme: light)" content="#faf9f5" />
<meta name="theme-color" media="(prefers-color-scheme: dark)" content="#1d1f23" />
`;
localStorage.clear();
});
it("updates meta tags to specific theme color when a non-system theme is loaded", () => {
loadTheme("default-dark");
const lightMeta = document.querySelector<HTMLMetaElement>('meta[name="theme-color"][media*="light"]');
const darkMeta = document.querySelector<HTMLMetaElement>('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<HTMLMetaElement>('meta[name="theme-color"][media*="light"]');
const darkMeta = document.querySelector<HTMLMetaElement>('meta[name="theme-color"][media*="dark"]');
expect(lightMeta?.content).toBe("#faf9f5");
expect(darkMeta?.content).toBe("#1d1f23");
});
});