Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

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

ocfox49b7ecc58341
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 @@
<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" />
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<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)
};
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 = `
+ <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");
+ });
+});