Skip to content

Apply stored theme on public routes - #15

Merged
prathamkashyap merged 1 commit into
mainfrom
fix/theme-on-public-routes
Oct 4, 2026
Merged

prathamkashyap merged 1 commit into
mainfrom
fix/theme-on-public-routes

Conversation

@prathamkashyap

Copy link
Copy Markdown
Owner

The Step 6 visual pass found one UX inconsistency worth fixing rather than documenting:
a stored Light preference was ignored on / and /auth.

Cause

The entire theme implementation lived inside ThemeToggle, which is mounted only by AppLayout —
the authenticated shell. So the public routes had nothing applying a theme and always rendered dark.
Worse, even in the app the class was applied in a useEffect after mount, so a Light user got a
frame of Aurora first.

Fix — centralised initialisation, one system

localStorage → application boot → apply stored theme → /  /auth  /dashboard

instead of

AppLayout mounts → theme applied

src/lib/theme.ts now owns the theme values, the storage key, the cycle order, normalisation and
the DOM write. main.tsx calls initTheme() at module scope, before createRoot, so the correct
theme is present at first paint on every route. ThemeToggle imports the same helpers and now only
reflects the current theme and cycles it.

Deliberately not done: no inline duplicate of this logic in index.html. The usual no-flash trick
is a blocking inline script, but that would be a second implementation of the same normalisation free
to drift. Booting before render is enough here, because the app is fully client-rendered — #root is
empty in the HTML, so there is no themed paint before the bundle runs. The flash this removes is the
post-mount one that already existed in the authenticated shell.

Behaviour deliberately unchanged

  • Values are still exactly "starry" | "light"; the key is still athenaeum-theme.
  • Cycle order unchanged; aria-label/title strings unchanged.
  • A stale stored "academia" still normalises to "starry" and is rewritten, and a stale
    theme-academia class is still cleared from the DOM.
  • prefers-reduced-motion is untouched — the theme never consults matchMedia; that stays
    AuroraBackground's job. There is a test asserting exactly that separation.
  • Storage access is wrapped in try/catch. A throw here would happen before render and take the
    whole app down, which is a worse failure than a default theme.

Verified in a real browser, not just jsdom

Route Stored <html> class Body background
/ light theme-light rgb(248, 250, 252) correct
/auth light theme-light rgb(248, 250, 252) correct
/ starry (none) rgb(5, 7, 10) correct
/auth starry (none) rgb(5, 7, 10) correct
/ (unset) (none) rgb(5, 7, 10) correct

Reviewed by eye as well as measured: the light landing page renders as pale blue/lavender over
white with navy text and blue CTAs, and the auth screen as a white card on the same field. The Aurora
layer adapts rather than inverting, so this is a real light theme and not a token swap.

Tests — 17 new, plus 3 repointed

themeApplication.test.tsx (16) exercises behaviour rather than source text, which is what let the
defect through: two-mode recognition, academia migration and write-back, valid values left alone,
nothing written when nothing was stored, Light applied on / and /auth, applied with no
component mounted at all
, stale theme-academia cleared, boot surviving blocked storage,
idempotent application, switching back to Aurora, and the motion-preference separation.

Three assertions in themeTypography.test.ts were reading ThemeToggle's source for strings that
now live in src/lib/theme.ts. They follow the definitions, and I added a fourth asserting there is
exactly one definition — the toggle must not redeclare the key, the order or a classList write, and
boot must go through the shared module with initTheme() before createRoot. That last check needed
matching the calls rather than the identifiers, since both names also appear in the imports.

PROOF.frontend updated 357 → 374 against actual runs.

Check Before After
Frontend 357 / 28 374 / 29
Backend unit / integration / E2E 942 / 101 / 26 unchanged
typecheck / build / lint 0 / 0 / 0 0 / 0 / 0

@prathamkashyap
prathamkashyap merged commit 9bc6029 into main Oct 4, 2026
3 checks passed
@prathamkashyap
prathamkashyap deleted the fix/theme-on-public-routes branch October 4, 2026 17:28
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