Skip to content

Open-source personal finance manager. Self-hosted, privacy-first.

feat(theme): add a System theme that follows the OS (#1227)

Shayn Hornikf48b8a4631fa
diff --git a/frontend/src/components/app-layout.tsx b/frontend/src/components/app-layout.tsx
index b3eb4f48..95edb0d9 100644
--- a/frontend/src/components/app-layout.tsx
+++ b/frontend/src/components/app-layout.tsx
@@ -43,6 +43,7 @@ import {
EyeOff,
Sun,
Moon,
+ Monitor,
Languages,
KeyRound,
Check,
@@ -62,6 +63,7 @@ import { GlobalChatPanel } from '@/components/global-chat-panel'
import { useFeatureFlags } from '@/hooks/use-feature-flags'
import { Bot, Plus, Search, Sparkles } from 'lucide-react'
import { setThemeBasedOnSystem } from '@/lib/theme-utils'
+import { nextTheme, THEME_LABEL_KEY } from '@/lib/theme-cycle'
import { useLocalAuthEnabled } from '@/hooks/use-local-auth'
import { formatCurrency } from '@/lib/format'
@@ -177,12 +179,11 @@ export function AppLayout() {
}, [user, updateUser])
const userInitial = user?.email?.charAt(0).toUpperCase() ?? '?'
- const resolvedThemeLocal = theme === 'system' ? undefined : theme
- const isDark = resolvedThemeLocal
- ? resolvedThemeLocal === 'dark'
- : typeof window !== 'undefined' &&
- window.matchMedia?.('(prefers-color-scheme: dark)').matches
- const toggleTheme = () => setTheme(isDark ? 'light' : 'dark')
+ // The button shows, and switches to, the next theme in the cycle.
+ const upcomingTheme = nextTheme(theme)
+ const themeButtonLabel = t(THEME_LABEL_KEY[upcomingTheme])
+ const ThemeIcon = { light: Sun, dark: Moon, system: Monitor }[upcomingTheme]
+ const toggleTheme = () => setTheme(upcomingTheme)
const { data: accountsList } = useQuery({
queryKey: ['accounts'],
@@ -239,12 +240,10 @@ export function AppLayout() {
<button
onClick={toggleTheme}
className="text-sidebar-muted hover:text-sidebar-foreground transition-colors p-1"
- title={isDark ? t('settings.themeLight') : t('settings.themeDark')}
- aria-label={
- isDark ? t('settings.themeLight') : t('settings.themeDark')
- }
+ title={themeButtonLabel}
+ aria-label={themeButtonLabel}
>
- {isDark ? <Sun size={18} /> : <Moon size={18} />}
+ <ThemeIcon size={18} />
</button>
{/* AI chat — opens the global slide-over (also reachable via
⌘J). Sits next to the theme toggle so the icon is always
@@ -355,14 +354,10 @@ export function AppLayout() {
'text-sidebar-muted hover:text-sidebar-foreground transition-colors p-1 rounded-md hover:bg-sidebar-accent',
desktopSidebarCollapsed && 'lg:flex lg:h-9 lg:w-9 lg:items-center lg:justify-center lg:p-0 lg:[&>svg]:h-[18px] lg:[&>svg]:w-[18px]',
)}
- title={
- isDark ? t('settings.themeLight') : t('settings.themeDark')
- }
- aria-label={
- isDark ? t('settings.themeLight') : t('settings.themeDark')
- }
+ title={themeButtonLabel}
+ aria-label={themeButtonLabel}
>
- {isDark ? <Sun size={16} /> : <Moon size={16} />}
+ <ThemeIcon size={16} />
</button>
</div>
</div>
diff --git a/frontend/src/components/auth-controls.test.tsx b/frontend/src/components/auth-controls.test.tsx
index 56469829..f5a78d39 100644
--- a/frontend/src/components/auth-controls.test.tsx
+++ b/frontend/src/components/auth-controls.test.tsx
@@ -17,6 +17,8 @@ const state = vi.hoisted(() => ({
is_superuser: false,
preferences: { onboarding_completed: true },
},
+ theme: 'light',
+ setTheme: vi.fn(),
workspace: { id: 'synthetic', name: 'Synthetic workspace', kind: 'personal', role: 'owner' },
auth: {
oidcConfig: vi.fn(),
@@ -72,7 +74,7 @@ vi.mock('@/lib/webauthn', () => ({
startPasskeyRegistration: vi.fn(),
}))
vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn() } }))
-vi.mock('next-themes', () => ({ useTheme: () => ({ theme: 'light', setTheme: vi.fn() }) }))
+vi.mock('next-themes', () => ({ useTheme: () => ({ theme: state.theme, setTheme: state.setTheme }) }))
vi.mock('@/components/collection-selector', () => ({ CollectionSelector: () => null }))
vi.mock('@/components/command-palette', () => ({ CommandPalette: () => null }))
vi.mock('@/components/global-chat-panel', () => ({ GlobalChatPanel: () => null }))
@@ -90,6 +92,7 @@ function openMenu(surface: string) {
beforeEach(() => {
vi.clearAllMocks()
localStorage.removeItem('securo.sidebar.collapsed')
+ state.theme = 'light'
state.user = { ...state.user, is_2fa_enabled: true }
state.auth.oidcConfig.mockResolvedValue({
enabled: true,
@@ -120,6 +123,17 @@ it('collapses the desktop sidebar and persists the preference', async () => {
expect(screen.getByRole('menuitem', { name: /Workspace settings/ })).toBeInTheDocument()
})
+it('offers System after Dark in the theme button (#1203)', async () => {
+ state.theme = 'dark'
+ const { user } = renderWithProviders(<AppLayout />)
+
+ const buttons = screen.getAllByRole('button', { name: t('settings.themeSystem') })
+ expect(buttons.length).toBeGreaterThan(0)
+ await user.click(buttons[0])
+
+ expect(state.setTheme).toHaveBeenCalledWith('system')
+})
+
it('restores a collapsed desktop sidebar from local storage', () => {
localStorage.setItem('securo.sidebar.collapsed', 'true')
diff --git a/frontend/src/components/theme-provider.tsx b/frontend/src/components/theme-provider.tsx
index 207fe1e7..036dfbbb 100644
--- a/frontend/src/components/theme-provider.tsx
+++ b/frontend/src/components/theme-provider.tsx
@@ -3,7 +3,7 @@ import type { ReactNode } from 'react'
export function ThemeProvider({ children }: { children: ReactNode }) {
return (
- <NextThemesProvider attribute="class" defaultTheme="dark" enableSystem={false}>
+ <NextThemesProvider attribute="class" defaultTheme="dark" enableSystem>
{children}
</NextThemesProvider>
)
diff --git a/frontend/src/lib/theme-cycle.test.ts b/frontend/src/lib/theme-cycle.test.ts
new file mode 100644
index 00000000..d0999ba7
--- /dev/null
+++ b/frontend/src/lib/theme-cycle.test.ts
@@ -0,0 +1,17 @@
+import { describe, expect, it } from 'vitest'
+import { nextTheme } from './theme-cycle'
+
+describe('nextTheme', () => {
+ it('steps light → dark → system → light', () => {
+ expect(nextTheme('light')).toBe('dark')
+ expect(nextTheme('dark')).toBe('system')
+ expect(nextTheme('system')).toBe('light')
+ })
+
+ it('treats an unknown or missing theme as the dark default', () => {
+ expect(nextTheme(undefined)).toBe('system')
+ expect(nextTheme('sepia')).toBe('system')
+ expect(nextTheme('__proto__')).toBe('system')
+ expect(nextTheme('toString')).toBe('system')
+ })
+})
diff --git a/frontend/src/lib/theme-cycle.ts b/frontend/src/lib/theme-cycle.ts
new file mode 100644
index 00000000..26917f34
--- /dev/null
+++ b/frontend/src/lib/theme-cycle.ts
@@ -0,0 +1,23 @@
+// The header's theme button steps Light → Dark → System → Light (issue
+// #1203). "System" follows the OS colour scheme and switches with it.
+export type ThemeChoice = 'light' | 'dark' | 'system'
+
+const NEXT: Record<ThemeChoice, ThemeChoice> = {
+ light: 'dark',
+ dark: 'system',
+ system: 'light',
+}
+
+function isThemeChoice(theme: string | undefined): theme is ThemeChoice {
+ return theme !== undefined && Object.hasOwn(NEXT, theme)
+}
+
+export function nextTheme(theme: string | undefined): ThemeChoice {
+ return NEXT[isThemeChoice(theme) ? theme : 'dark']
+}
+
+export const THEME_LABEL_KEY: Record<ThemeChoice, string> = {
+ light: 'settings.themeLight',
+ dark: 'settings.themeDark',
+ system: 'settings.themeSystem',
+}
diff --git a/frontend/src/pages/setup.tsx b/frontend/src/pages/setup.tsx
index 2277a150..eff133d2 100644
--- a/frontend/src/pages/setup.tsx
+++ b/frontend/src/pages/setup.tsx
@@ -19,7 +19,7 @@ import {
} from '@/components/ui/select'
import { AuthBrandPanel } from '@/components/auth-brand-panel'
import { cn } from '@/lib/utils'
-import { Sun, Moon, Globe } from 'lucide-react'
+import { Sun, Moon, Monitor, Globe } from 'lucide-react'
import { ShellLogo } from '@/components/shell-logo'
export default function SetupPage() {
@@ -151,6 +151,19 @@ export default function SetupPage() {
>
<Moon size={14} />
</button>
+ <button
+ type="button"
+ onClick={() => setTheme('system')}
+ title={t('settings.themeSystem')}
+ className={cn(
+ 'p-1.5 rounded transition-colors',
+ theme === 'system'
+ ? 'bg-primary/15 text-primary'
+ : 'text-muted-foreground hover:text-foreground'
+ )}
+ >
+ <Monitor size={14} />
+ </button>
</div>
</div>
</div>