Skip to content

A local-first personal finance app

[AI] Fix desktop PWA title bar theme color (#8181)

PaolinPaperin8c188b572a9d
diff --git a/packages/desktop-client/src/components/FinancesApp.tsx b/packages/desktop-client/src/components/FinancesApp.tsx
index 5fa051a502..beb0120e73 100644
--- a/packages/desktop-client/src/components/FinancesApp.tsx
+++ b/packages/desktop-client/src/components/FinancesApp.tsx
@@ -88,7 +88,7 @@ function RouterBehaviors() {
export function FinancesApp() {
const { isNarrowWidth } = useResponsive();
- useMetaThemeColor(isNarrowWidth ? theme.mobileViewTheme : undefined);
+ useMetaThemeColor(theme.mobileViewTheme);
const location = useLocation();
const dispatch = useDispatch();
diff --git a/packages/desktop-client/src/components/manager/ManagementApp.tsx b/packages/desktop-client/src/components/manager/ManagementApp.tsx
index cc27afae12..d9a17a21d4 100644
--- a/packages/desktop-client/src/components/manager/ManagementApp.tsx
+++ b/packages/desktop-client/src/components/manager/ManagementApp.tsx
@@ -2,7 +2,6 @@ import React, { useEffect } from 'react';
import { Trans } from 'react-i18next';
import { Navigate, Route, Routes } from 'react-router';
-import { useResponsive } from '@actual-app/components/hooks/useResponsive';
import { Text } from '@actual-app/components/text';
import { theme } from '@actual-app/components/theme';
import { tokens } from '@actual-app/components/tokens';
@@ -65,10 +64,7 @@ function Version() {
}
export function ManagementApp() {
- const { isNarrowWidth } = useResponsive();
- useMetaThemeColor(
- isNarrowWidth ? theme.mobileConfigServerViewTheme : undefined,
- );
+ useMetaThemeColor(theme.mobileConfigServerViewTheme);
const files = useSelector(state => state.budgetfiles.allFiles);
const isLoading = useSelector(state => state.app.loadingText !== null);
diff --git a/packages/desktop-client/src/hooks/useMetaThemeColor.test.ts b/packages/desktop-client/src/hooks/useMetaThemeColor.test.ts
index b5f726b760..a4b867e9d2 100644
--- a/packages/desktop-client/src/hooks/useMetaThemeColor.test.ts
+++ b/packages/desktop-client/src/hooks/useMetaThemeColor.test.ts
@@ -1,4 +1,4 @@
-import { renderHook } from '@testing-library/react';
+import { act, renderHook, waitFor } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { usePreferredDarkTheme, useTheme } from '#style/theme';
@@ -6,6 +6,7 @@ import { usePreferredDarkTheme, useTheme } from '#style/theme';
import { useMetaThemeColor } from './useMetaThemeColor';
const DEFAULT_THEME_COLOR = '#5c3dbb';
+const originalMatchMedia = window.matchMedia;
vi.mock('#style/theme', () => ({
useTheme: vi.fn(),
@@ -40,6 +41,11 @@ beforeEach(() => {
afterEach(() => {
vi.clearAllMocks();
+ Object.defineProperty(window, 'matchMedia', {
+ configurable: true,
+ writable: true,
+ value: originalMatchMedia,
+ });
});
describe('useMetaThemeColor', () => {
@@ -118,5 +124,46 @@ describe('useMetaThemeColor', () => {
rerender();
expect(document.body.style.backgroundColor).toBe('rgb(187, 187, 187)');
});
+
+ it('re-runs effect when system color scheme changes', async () => {
+ let matches = false;
+ const listeners = new Set<() => void>();
+ Object.defineProperty(window, 'matchMedia', {
+ configurable: true,
+ value: vi.fn().mockImplementation((query: string) => ({
+ get matches() {
+ return matches;
+ },
+ media: query,
+ onchange: null,
+ addEventListener: vi.fn((event: string, listener: () => void) => {
+ if (event === 'change') {
+ listeners.add(listener);
+ }
+ }),
+ removeEventListener: vi.fn((event: string, listener: () => void) => {
+ if (event === 'change') {
+ listeners.delete(listener);
+ }
+ }),
+ dispatchEvent: vi.fn(),
+ })),
+ });
+
+ setCssVar('--color-mobileViewTheme', '#111');
+ renderHook(() => useMetaThemeColor('var(--color-mobileViewTheme)'));
+ expect(document.body.style.backgroundColor).toBe('rgb(17, 17, 17)');
+ expect(listeners.size).toBe(1);
+
+ setCssVar('--color-mobileViewTheme', '#222');
+ matches = true;
+ act(() => {
+ listeners.forEach(listener => listener());
+ });
+
+ await waitFor(() => {
+ expect(document.body.style.backgroundColor).toBe('rgb(34, 34, 34)');
+ });
+ });
});
});
diff --git a/packages/desktop-client/src/hooks/useMetaThemeColor.ts b/packages/desktop-client/src/hooks/useMetaThemeColor.ts
index 991095725e..b189227553 100644
--- a/packages/desktop-client/src/hooks/useMetaThemeColor.ts
+++ b/packages/desktop-client/src/hooks/useMetaThemeColor.ts
@@ -1,4 +1,4 @@
-import { useEffect } from 'react';
+import { useEffect, useState } from 'react';
import { usePreferredDarkTheme, useTheme } from '#style/theme';
@@ -8,11 +8,13 @@ const DEFAULT_THEME_COLOR = '#5c3dbb';
/**
* Sets the theme-color meta tag (for browsers that use it) and document.body
* background-color (for Safari 26+, which derives status bar tint from body).
- * Re-runs when theme changes so the status bar follows the app theme.
+ * Re-runs when theme or system color scheme changes so the status bar follows
+ * the app theme.
*/
export function useMetaThemeColor(color?: string) {
const [activeTheme] = useTheme();
const [darkThemePreference] = usePreferredDarkTheme();
+ const systemColorScheme = useSystemColorScheme();
useEffect(() => {
if (!color) return;
@@ -23,7 +25,42 @@ export function useMetaThemeColor(color?: string) {
ensureThemeColorMetaTag();
setThemeColorMetaContent(resolved);
document.body.style.backgroundColor = resolved;
- }, [color, activeTheme, darkThemePreference]);
+ }, [color, activeTheme, darkThemePreference, systemColorScheme]);
+}
+
+function useSystemColorScheme() {
+ const [systemColorScheme, setSystemColorScheme] = useState(() =>
+ getSystemColorScheme(),
+ );
+
+ useEffect(() => {
+ if (!window.matchMedia) return;
+
+ const darkThemeMediaQuery = window.matchMedia(
+ '(prefers-color-scheme: dark)',
+ );
+ const handleColorSchemeChange = () => {
+ setSystemColorScheme(darkThemeMediaQuery.matches ? 'dark' : 'light');
+ };
+
+ darkThemeMediaQuery.addEventListener('change', handleColorSchemeChange);
+ handleColorSchemeChange();
+
+ return () => {
+ darkThemeMediaQuery.removeEventListener(
+ 'change',
+ handleColorSchemeChange,
+ );
+ };
+ }, []);
+
+ return systemColorScheme;
+}
+
+function getSystemColorScheme() {
+ return window.matchMedia?.('(prefers-color-scheme: dark)').matches
+ ? 'dark'
+ : 'light';
}
function ensureThemeColorMetaTag() {
diff --git a/upcoming-release-notes/fix-pwa-titlebar-theme-color.md b/upcoming-release-notes/fix-pwa-titlebar-theme-color.md
new file mode 100644
index 0000000000..02f33f944f
--- /dev/null
+++ b/upcoming-release-notes/fix-pwa-titlebar-theme-color.md
@@ -0,0 +1,6 @@
+---
+category: Bugfixes
+authors: [PaolinPaperin]
+---
+
+Fix the installed web app title bar color to follow the active app theme.