Skip to content

feat(ui): let the theme follow the operating system again - #13462

Open
conreo wants to merge 2 commits into
paperclipai:masterfrom
conreo:feat/system-theme-preference
Open

conreo wants to merge 2 commits into
paperclipai:masterfrom
conreo:feat/system-theme-preference

Conversation

@conreo

@conreo conreo commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Thinking Path

  • The board has two appearance states, light and dark, reached through one toggle, and every activation stores an explicit choice in localStorage.
  • hasStoredTheme() then reports true for the rest of the install, so ThemeProvider stops listening to prefers-color-scheme and no control can put that behaviour back. A two-state toggle cannot express "follow the system".
  • The state the board starts in is therefore unreachable after the first click, which is the opposite of what the bootstrap script in index.html does on a fresh install.
  • The gap is visible outside the board on Android: an installed app paints its status bar from the manifest theme_color while the status-bar icon colour follows the operating system scheme. A board stuck on dark while the phone is light draws dark icons on a dark bar, so the status bar reads as broken rather than as a theme mismatch.
  • Adding system as a third preference restores the initial behaviour, keeps the inline bootstrap script and this provider in agreement, and needs no new widget.

Linked Issues or Issue Description

No public issue tracks this. Described here following the issue template.

Subsystem affected

The UI theme model: ui/src/context/ThemeContext.tsx, ui/src/components/ThemeToggle.tsx, and their tests. No server, schema, API, or contract change.

Problem or motivation

Theme is "light" | "dark" and toggleTheme() always behaves as an explicit choice, so hasStoredTheme() is true from then on and the prefers-color-scheme effect never attaches again. A person who prefers dark at night and light by day cannot get both, and a person who wants the board to match the device has no way back to that after one click. The initial, OS-following behaviour is the one the app advertises by default, so losing it permanently is surprising.

The user-visible consequence on Android is the status bar. Chrome paints it from the manifest theme_color, which cannot vary by scheme (color_scheme_dark is still only a W3C proposal), while the icon colour comes from the OS scheme. When the board and the OS disagree, the icons can be the same colour as the bar (Chromium 559396686 covers the dynamic theme-color path for installed apps). Keeping board and OS in agreement is the fix available today.

Proposed solution

  • Add "system" to the preference union. theme stays the resolved "light" | "dark" so existing consumers (MarkdownBody, applyTheme) are unchanged, and a new themePreference exposes the choice for a control that needs to show it.
  • Store system as the absence of the key, which is exactly what "follow the OS" already means to the bootstrap script in index.html, so the two cannot disagree and no storage format change is needed.
  • Extend the canonical ThemeToggle to walk light → dark → system. Its label already names the destination ("Switch to dark mode"), so it becomes "Switch to system theme" on the extra step: the third state is reachable without a second control on the two surfaces that render it.

Alternatives considered

  • Always follow the OS and drop the explicit choice. Rejected: it removes a preference people use deliberately.
  • A three-way segmented control in a settings page. Rejected for this change: it adds a widget and a surface, while the existing single control can express all three states. Worth revisiting if the theme model grows again.
  • Persisting the literal string "system". Rejected: the bootstrap script would have to learn a third stored value, and two places would then decide what "system" means.
  • Setting the manifest theme_color to a mid-tone so it reads acceptably under both schemes. Rejected: it degrades both schemes to hide a mismatch that this change removes at the source.

Roadmap alignment

ROADMAP.md does not plan this work.

Additional context

ThemeToggle is documented as the "canonical theme-toggle widget" whose "label, icon, and toggle behaviour stay in sync as the theme model evolves", so the model change lands in the one component by design. The two-state label contract is preserved for the first two steps; only the step after dark is new.

What Changed

  • ui/src/context/ThemeContext.tsx: ThemePreference is "light" | "dark" | "system"; theme is resolved for the document while themePreference records the choice; system stores no key; the OS listener attaches exactly while the preference is system; toggleTheme() walks light → dark → system.
  • ui/src/components/ThemeToggle.tsx: the label and icon come from a single table of the three states, naming the state the control moves to (Moon, then Monitor, then Sun).
  • ui/src/context/ThemeContext.test.tsx: adds coverage for returning from an explicit choice to the system preference, and for the three-stop toggle; the existing tests for OS-following and for the stored-choice case are unchanged.
  • ui/src/components/ThemeToggle.test.tsx: the mock supplies the new field, and each of the three states asserts its label.

Verification

  • vitest run src/context/ThemeContext.test.tsx src/components/ThemeToggle.test.tsx src/lib/pwa-install-mode.test.ts src/lib/pwa-display-mode.test.ts in ui: 4 files, 18 tests passed.
  • pnpm check:token-gates: all four gates clean (colour literals, arbitrary bracket values, raw font-size, legacy hsl(var())).
  • pnpm --filter @paperclipai/ui typecheck (tsc -b): no errors.
  • Behaviour worth checking by hand: choose light, choose dark, choose system, reload — the board follows the device again, and localStorage holds no paperclip.theme key while the preference is system.

Risks

  • Low and UI-only. No server, schema, API, or stored-contract change; the only persisted value is still a plain "light" or "dark" string, and absence still means "follow the OS".
  • The toggle gains a third stop. A person who only wanted light or dark now passes through system on the way back, which one more click resolves; the label names the destination at each step so the stop is not silent.
  • Anyone who stored an explicit choice keeps it. Nothing migrates a stored value, and no existing user's appearance changes until they act.
  • A consumer reading theme gets the same resolved "light" | "dark" as before, so no rendering path changes.

Model Used

  • Provider: DeepSeek. Model ID: deepseek-v4-flash. The model ran as a tool-using coding agent (shell, file edits, test runs) inside a coding harness. The harness did not report a context-window size or a reasoning mode.

Checklist

  • I have included a thinking path that traces from project context to this change
  • I have specified the model used (with version and capability details)
  • I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work
  • I have searched GitHub for duplicate or related PRs and issues and linked them above
  • I have either (a) linked existing issues with Fixes: / Closes / Refs # OR (b) described the issue in-PR following the relevant issue template — no public issue exists, so this is described in-PR
  • I have not referenced internal/instance-local Paperclip issues or links (only public GitHub #NNN / github.com/paperclipai/paperclip URLs)
  • My branch name describes the change and contains no internal ticket id or instance-derived details
  • I have run tests locally and they pass (4 files, 18 tests)
  • I have added or updated tests where applicable
  • I have updated relevant documentation to reflect my changes — the theme model is documented by the component comment and the new test names
  • I have considered and documented any risks above
  • All Paperclip CI gates are green — running on this commit
  • Greptile is 5/5 — awaiting the review run on this commit
  • I will address all Greptile and reviewer comments before requesting merge

The theme had two states, `light` and `dark`, and every toggle stored an
explicit choice. `hasStoredTheme()` then reports true for the rest of the
install, so after a person touched the control once the board stopped
following the device appearance and no control could put it back: a
two-state toggle cannot express "follow the system".

That costs more than a preference. On Android an installed app paints its
status bar from the manifest `theme_color` while the status-bar icon colour
comes from the operating system scheme. A board showing dark while the phone
is light therefore renders dark icons on a dark bar, which reads as an
unreadable status bar rather than as a theme mismatch.

Add `system` as a third preference. It is stored as the absence of the key,
which is what "follow the OS" already meant to the bootstrap script in
`index.html`, so a reload keeps following the system and the inline script
and this provider cannot disagree. The single canonical control walks
light -> dark -> system, and its label names the state it moves to, so the
third state is reachable without adding a widget to the two surfaces that
render it.
@greptile-apps

greptile-apps Bot commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium risk] Theme system now supports following the operating system.

The PR appears safe to merge, with the theme model, bootstrap behavior, consumers, and tests consistently updated.

Summary

The PR restores an explicit system-following theme preference while preserving resolved light/dark values for existing consumers.

  • Adds a three-state light → dark → system theme cycle.
  • Keeps system preference compatible with the bootstrap script by representing it as an absent storage key.
  • Synchronizes system-theme transitions with the current OS value to avoid stale rendering.
  • Updates the canonical toggle and affected tests and mocks for the new preference field.
  • Aligns bootstrap behavior with the provider when storage access fails.

Reviews (4) · Last reviewed commit: "fix(ui): keep every theme consumer and t..."

Comment thread ui/src/components/ThemeToggle.tsx Resolved
Comment thread ui/src/context/ThemeContext.tsx Resolved
Comment thread ui/src/context/ThemeContext.tsx Resolved
@conreo

conreo commented Sep 15, 2026

Copy link
Copy Markdown
Contributor Author

Addressed all three findings in ee8a71c8.

  • P1 — incomplete mocks. Correct. Auth.test.tsx and SidebarAccountMenu.test.tsx mocked useTheme without themePreference, so TOGGLE_TARGETS[themePreference] was undefined and those suites crashed as soon as they rendered the toggle. Both mocks now supply the field. vitest run over the two theme suites plus those two files: 4 files, 23 tests passed.
  • P2 — system theme briefly stale. Also correct. systemTheme stops tracking the OS while an explicit theme is selected, so switching to system rendered one frame from a stale value. The switch now calls readSystemTheme() itself and applies both updates in a single render. A test switches to system after the OS changed while a stored dark theme was active, and asserts the first render already shows the OS value.
  • P2 — bootstrap divergence. Correct as well. index.html forced dark when localStorage threw, while this provider read the same failure as "follow the system", so first paint and the first render could disagree. Both now follow the OS, keeping dark only as the fallback when the environment cannot report a preference.

Also re-verified: pnpm check:token-gates all gates clean, tsc -b clean, and the full ui suite (597 files, 6255 tests) with one unrelated flake in CompanySettings.test.tsx that passes in isolation on this branch and on clean master.

Three findings from review of the system-theme change:

- `ThemeToggle` indexes the state table with `themePreference`, so the mocks
  in `Auth.test.tsx` and `SidebarAccountMenu.test.tsx` crashed the suites that
  render it. Both mocks now supply the field.
- `systemTheme` stops tracking the OS while an explicit theme is selected, so
  switching to `system` rendered one frame of a stale theme. The switch now
  reads the OS itself and applies both updates in one render.
- `index.html` forced dark when `localStorage` threw, while the provider read
  the same failure as "follow the system". Both sides now follow the OS, so
  the initial class cannot disagree with the first render.
@commitperclip

commitperclip Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

@greptileai

This branch has not been deployed

No deployments
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.

1 participant