Skip to content

A local-first personal finance app

fix(ios): restore status bar color on iOS 26.2 (Safari 26) (#6983)

Matiss Janis Aboltinsc031d9aa4fe9
diff --git a/packages/desktop-client/src/hooks/useMetaThemeColor.test.ts b/packages/desktop-client/src/hooks/useMetaThemeColor.test.ts
new file mode 100644
index 0000000000..6f578862e9
--- /dev/null
+++ b/packages/desktop-client/src/hooks/useMetaThemeColor.test.ts
@@ -0,0 +1,122 @@
+import { renderHook } from '@testing-library/react';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { useMetaThemeColor } from './useMetaThemeColor';
+
+import { usePreferredDarkTheme, useTheme } from '@desktop-client/style/theme';
+
+const DEFAULT_THEME_COLOR = '#5c3dbb';
+
+vi.mock('@desktop-client/style/theme', () => ({
+ useTheme: vi.fn(),
+ usePreferredDarkTheme: vi.fn(),
+}));
+
+function getThemeColorMeta() {
+ const meta = document.querySelector('meta[name="theme-color"]');
+ return meta?.getAttribute('content') ?? null;
+}
+
+function removeThemeColorMeta() {
+ document.querySelector('meta[name="theme-color"]')?.remove();
+}
+
+function setCssVar(name: string, value: string) {
+ document.documentElement.style.setProperty(name, value);
+}
+
+function clearCssVar(name: string) {
+ document.documentElement.style.removeProperty(name);
+}
+
+beforeEach(() => {
+ vi.mocked(useTheme).mockReturnValue(['light', vi.fn()]);
+ vi.mocked(usePreferredDarkTheme).mockReturnValue(['dark', vi.fn()]);
+ document.body.style.backgroundColor = '';
+ document.documentElement.style.backgroundColor = '';
+ removeThemeColorMeta();
+ clearCssVar('--color-mobileViewTheme');
+});
+
+afterEach(() => {
+ vi.clearAllMocks();
+});
+
+describe('useMetaThemeColor', () => {
+ describe('when color is undefined', () => {
+ it('does not set theme-color meta tag', () => {
+ renderHook(() => useMetaThemeColor(undefined));
+ expect(getThemeColorMeta()).toBeNull();
+ });
+
+ it('does not set body background-color', () => {
+ renderHook(() => useMetaThemeColor(undefined));
+ expect(document.body.style.backgroundColor).toBe('');
+ });
+ });
+
+ describe('when color is a literal hex value', () => {
+ it('sets theme-color meta content and body background to that color', () => {
+ renderHook(() => useMetaThemeColor('#1a2b3c'));
+ expect(getThemeColorMeta()).toBe('#1a2b3c');
+ // jsdom normalizes assigned hex to rgb()
+ expect(document.body.style.backgroundColor).toBe('rgb(26, 43, 60)');
+ });
+
+ it('creates theme-color meta tag if missing', () => {
+ expect(getThemeColorMeta()).toBeNull();
+ renderHook(() => useMetaThemeColor('#abc'));
+ expect(getThemeColorMeta()).toBe('#abc');
+ });
+ });
+
+ describe('when color is a CSS var()', () => {
+ it('resolves the var via getComputedStyle and sets meta and body', () => {
+ setCssVar('--color-mobileViewTheme', ' #fedcba ');
+ const getComputedStyleSpy = vi.spyOn(window, 'getComputedStyle');
+
+ renderHook(() => useMetaThemeColor('var(--color-mobileViewTheme)'));
+
+ expect(getComputedStyleSpy).toHaveBeenCalledWith(
+ document.documentElement,
+ );
+ expect(getThemeColorMeta()).toBe('#fedcba');
+ expect(document.body.style.backgroundColor).toBe('rgb(254, 220, 186)');
+ });
+
+ it('uses default when resolved var is empty', () => {
+ renderHook(() => useMetaThemeColor('var(--color-mobileViewTheme)'));
+
+ expect(getThemeColorMeta()).toBe(DEFAULT_THEME_COLOR);
+ expect(document.body.style.backgroundColor).toBe('rgb(92, 61, 187)');
+ });
+ });
+
+ describe('theme reactivity', () => {
+ it('re-runs effect when activeTheme changes', () => {
+ setCssVar('--color-mobileViewTheme', '#111');
+ const { rerender } = renderHook(() =>
+ useMetaThemeColor('var(--color-mobileViewTheme)'),
+ );
+ expect(document.body.style.backgroundColor).toBe('rgb(17, 17, 17)');
+
+ setCssVar('--color-mobileViewTheme', '#222');
+ vi.mocked(useTheme).mockReturnValue(['dark', vi.fn()]);
+ rerender();
+ expect(document.body.style.backgroundColor).toBe('rgb(34, 34, 34)');
+ });
+
+ it('re-runs effect when darkThemePreference changes', () => {
+ setCssVar('--color-mobileViewTheme', '#aaa');
+ const { rerender } = renderHook(() =>
+ useMetaThemeColor('var(--color-mobileViewTheme)'),
+ );
+ expect(document.body.style.backgroundColor).toBe('rgb(170, 170, 170)');
+
+ setCssVar('--color-mobileViewTheme', '#bbb');
+ vi.mocked(usePreferredDarkTheme).mockReturnValue(['midnight', vi.fn()]);
+ rerender();
+ expect(document.body.style.backgroundColor).toBe('rgb(187, 187, 187)');
+ });
+ });
+});
diff --git a/packages/desktop-client/src/hooks/useMetaThemeColor.ts b/packages/desktop-client/src/hooks/useMetaThemeColor.ts
index 1ce2a49e21..1bf57e13ad 100644
--- a/packages/desktop-client/src/hooks/useMetaThemeColor.ts
+++ b/packages/desktop-client/src/hooks/useMetaThemeColor.ts
@@ -1,26 +1,57 @@
-// @ts-strict-ignore
import { useEffect } from 'react';
+import { usePreferredDarkTheme, useTheme } from '@desktop-client/style/theme';
+
const VAR_STRING_REGEX = /^var\((--.*)\)$/;
+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.
+ */
export function useMetaThemeColor(color?: string) {
+ const [activeTheme] = useTheme();
+ const [darkThemePreference] = usePreferredDarkTheme();
+
useEffect(() => {
- if (color) {
- setThemeColor(getPropertyValueFromVarString(color));
- }
- }, [color]);
+ if (!color) return;
+
+ const resolved =
+ getPropertyValueFromVarString(color) || DEFAULT_THEME_COLOR;
+
+ ensureThemeColorMetaTag();
+ setThemeColorMetaContent(resolved);
+ document.body.style.backgroundColor = resolved;
+ }, [color, activeTheme, darkThemePreference]);
+}
+
+function ensureThemeColorMetaTag() {
+ const metaTags = document.getElementsByTagName('meta');
+ const existing = [...metaTags].find(tag => tag.name === 'theme-color');
+ if (!existing) {
+ const meta = document.createElement('meta');
+ meta.name = 'theme-color';
+ meta.setAttribute('content', DEFAULT_THEME_COLOR);
+ document.head.appendChild(meta);
+ }
}
-function setThemeColor(color: string) {
+function setThemeColorMetaContent(color: string) {
const metaTags = document.getElementsByTagName('meta');
const themeTag = [...metaTags].find(tag => tag.name === 'theme-color');
- themeTag.setAttribute('content', color);
+ if (themeTag) {
+ themeTag.setAttribute('content', color);
+ }
}
function getPropertyValueFromVarString(varString: string) {
- return VAR_STRING_REGEX.test(varString)
+ if (!VAR_STRING_REGEX.test(varString)) return varString;
+ const match = varString.match(VAR_STRING_REGEX);
+ return match
? window
.getComputedStyle(document.documentElement)
- .getPropertyValue(varString.match(VAR_STRING_REGEX)[1])
+ .getPropertyValue(match[1])
+ .trim()
: varString;
}
diff --git a/upcoming-release-notes/6983.md b/upcoming-release-notes/6983.md
new file mode 100644
index 0000000000..677f28242f
--- /dev/null
+++ b/upcoming-release-notes/6983.md
@@ -0,0 +1,6 @@
+---
+category: Bugfixes
+authors: [MatissJanis]
+---
+
+Fix status bar color synchronization on iOS Safari by enhancing theme color management.