Skip to content

fix(theme): add 'system' as a first-class theme preference#575

Open
eliran-ops wants to merge 1 commit into
mainfrom
feat/fix-theme-shortcut-not-synced-to-preferences-page
Open

fix(theme): add 'system' as a first-class theme preference#575
eliran-ops wants to merge 1 commit into
mainfrom
feat/fix-theme-shortcut-not-synced-to-preferences-page

Conversation

@eliran-ops
Copy link
Copy Markdown
Contributor

@eliran-ops eliran-ops commented Apr 29, 2026

Summary

SKY-823 bug 13: the keyboard shortcut `t` toggles light/dark theme but the radar-hub Preferences page Theme selector still shows `System`. Linear: SKY-823.

Root cause

The stored `Theme` type was `'dark' | 'light'` only — there was no value that meant "follow the OS", so the radar-hub-web Preferences page exposed a `System` option that didn't correspond to anything in the app's state. The shortcut and the selector therefore could not stay in sync.

Approach

Extends the theme contract so the bug becomes fixable from both sides:

New pure helpers in `packages/k8s-ui/src/utils/theme.ts`:

Symbol Purpose
`Theme = 'dark' | 'light' | 'system'` Stored preference
`EffectiveTheme = 'dark' | 'light'` What's actually applied
`resolveEffectiveTheme(theme, prefersLight)` Pure resolver
`isTheme(value)` Type guard for storage / HTTP reads
`nextThemeForToggle(effective)` Always returns explicit value

`nextThemeForToggle` deliberately writes a deterministic dark/light when the keyboard shortcut fires — staying on `'system'` would let a future OS theme change silently undo the user's deliberate choice. 7 unit tests pin the contract.

`web/src/context/ThemeContext.tsx`:

  • `useTheme()` now returns `{ theme, effectiveTheme, setTheme, toggleTheme }`.
  • Tracks `prefers-color-scheme` continuously so `'system'` updates `effectiveTheme` immediately.
  • Re-exports `Theme` / `EffectiveTheme` so existing imports keep working.

Consumer updates: `CodeViewer`, `LogsViewer`, `WorkloadLogsViewer`, and the topbar `ThemeToggle` icon/tooltip now use `effectiveTheme` so they always reflect what's actually visible.

Cross-repo follow-up

The radar-hub Preferences `Theme` selector can now bind to `theme` directly: when the keyboard shortcut fires, the selector updates from 'System' to 'Dark' or 'Light' in lockstep. That part is a one-liner change in `radar-hub-web` — out of scope for this PR.

Test plan

  • `cd packages/k8s-ui && npm test` — all passing (7 new tests in `theme.test.ts`)
  • `cd web && npm run tsc` — clean
  • `cd web && npm run build` — clean

Made with Cursor


Note

Medium Risk
Touches global theming state, persistence, and OS media-query handling; mistakes could cause incorrect theme application or preference drift across sessions/devices.

Overview
Fixes theme preference handling by making system a first-class stored value and separating stored theme from derived effectiveTheme (dark/light), with shared pure helpers (resolveEffectiveTheme, isTheme, nextThemeForToggle) added to @skyhook-io/k8s-ui/utils and covered by new unit tests.

Updates ThemeContext to persist/consume system, continuously track OS prefers-color-scheme, apply the resolved effectiveTheme to the document, and ensure the keyboard toggle always writes an explicit dark/light value; UI consumers (topbar toggle, logs viewers, CodeViewer) now use effectiveTheme so icons/tooltips and forced-dark rendering match what’s actually displayed.

Reviewed by Cursor Bugbot for commit a03367c. Bugbot is set up for automated code reviews on this repo. Configure here.

SKY-823 bug 13: the keyboard shortcut 't' toggles light/dark theme but
the Preferences page Theme selector still shows 'System'. Root cause:
the stored Theme type was `'dark' | 'light'` only — there was no value
that meant "follow the OS", so the radar-hub-web Preferences page
exposed a 'System' option that didn't correspond to anything in the
app's state. The shortcut and the selector therefore could not stay in
sync.

This change extends the theme contract so the bug becomes fixable from
both sides:

- New pure helpers in `packages/k8s-ui/src/utils/theme.ts`:
  - `Theme = 'dark' | 'light' | 'system'` and `EffectiveTheme = 'dark' | 'light'`
  - `resolveEffectiveTheme(theme, prefersLight)` — pure resolver
  - `isTheme(value)` — type guard for storage / HTTP reads
  - `nextThemeForToggle(effective)` — always returns an explicit value;
    the keyboard shortcut writes a deterministic dark/light so a
    subsequent OS change can't silently undo the user's deliberate
    choice. 7 unit tests pin the contract.

- `web/src/context/ThemeContext.tsx`:
  - Re-exports `Theme` / `EffectiveTheme` from k8s-ui so existing
    imports keep working.
  - `useTheme()` now returns `{ theme, effectiveTheme, setTheme,
    toggleTheme }`. `theme` is the stored preference (may be 'system');
    `effectiveTheme` is what's currently applied to the document.
  - Tracks the OS `prefers-color-scheme` media query continuously so
    'system' updates `effectiveTheme` immediately.
  - The server settings round-trip uses `isTheme` to narrow values
    safely, including the new 'system'.

- All in-repo consumers updated to use `effectiveTheme` where they
  want the concrete current value (CodeViewer, LogsViewer,
  WorkloadLogsViewer, ThemeToggle button icon/tooltip).

The radar-hub Preferences "Theme" selector can now bind to `theme`
directly: when the keyboard shortcut fires, the selector updates from
'System' to 'Dark' or 'Light' in lockstep.

Linear: SKY-823
Made-with: Cursor
@eliran-ops
Copy link
Copy Markdown
Contributor Author

@nadaverell @hisco — ready for review. No outstanding cursor findings. Cursor Bugbot SUCCESS on current HEAD.

@eliran-ops
Copy link
Copy Markdown
Contributor Author

Visual test

Tested locally on gke_koalabackend_us-east1-b_nonprod-cluster-us-east1.

Default (light) theme rendered

Empty localStorage on first load. htmlClass="" (light is default with no class):
light

Press t → toggles to dark with explicit stored value ✓

After single keypress:

  • document.documentElement.className === "dark" (effective theme applied)
  • localStorage = {"radar-theme":"dark"} - deterministic explicit value, not "system".

This is the nextThemeForToggle contract: keyboard toggle never leaves storage on 'system', so a future OS-theme change can't silently undo the user's deliberate choice. Page renders in dark mode:
dark

'system' first-class storage value

system

OS reports prefers-color-scheme: light. The Theme type now accepts 'system', and the resolver is exercised; the path that surfaces a System selector in the UI lives in radar-hub-web's Preferences page (per PR description), so the cross-repo follow-up needs verification there.

Verdict on what I could exercise in OSS

Toggle path verified end-to-end: keypress → effective theme applied → explicit value persisted. That's the load-bearing OSS-side fix for the t shortcut staying in sync with whatever Preferences UI binds to it. The radar-hub-web Preferences System-option binding is the one-line cross-repo follow-up the PR description calls out.

@eliran-ops
Copy link
Copy Markdown
Contributor Author

@nadaverell a reminder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants