Skip to content

refactor(ui): unify dark mode across Ladle and components - #20

Merged
Ardnived merged 9 commits into
mainfrom
refactor/unify-dark-mode
Jun 30, 2026
Merged

Ardnived merged 9 commits into
mainfrom
refactor/unify-dark-mode

Conversation

@Ardnived

@Ardnived Ardnived commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Unifies dark mode so a single signal — the root <html> class — drives NextUI, Tailwind, and the Mapbox basemap, and lets Ladle's own theme toggle re-theme the entire story canvas.

  • useTheme now observes the root class (via useSyncExternalStore + MutationObserver) instead of giving each component its own useState plus a per-render class write. A theme change made anywhere (ThemeSwitch, the Ladle toggle) propagates to every consumer — including the map basemap, which previously could not follow an externally-set theme.
  • Ladle's light/dark/auto toggle drives the whole preview canvas; the per-story <StorySection theme="dark"> "Dark Surface" blocks (18 stories) are removed along with the theme prop on StorySection, whose chrome is now expressed in NextUI semantic tokens so it reads in both themes.

Changes

  • src/hooks/use-theme.ts — reactive, root-class-derived theme; exports applyTheme() (the single class-writing seam) and initTheme() (startup restore). No more dependency-less per-render class write.
  • src/main.tsx, src/Widget.tsx — call initTheme() before first paint (no flash); App.tsx no longer inits.
  • .ladle/components.tsx — decorator maps useLadleContext().globalState.theme onto the root class through applyTheme() (resolves auto via matchMedia, tracked live); canvas defers its background to Ladle's own theme.
  • src/components/ladle/StorySection.tsx — drops the theme prop and all theme === 'dark' branches; semantic-token chrome; theme-aware neutral/gradient backgrounds retained.
  • 18 *.stories.tsx — removed "Dark Surface" sections; reworded 3 docstrings; one text-gray-500 → text-default-500.
  • src/components/organisms/Mapbox/layers.ts — comments documenting why the white cluster count and debug-only #888 bounds line need no theming.
  • tailwind.config.js — fix the dark theme so the custom brand palette (orange/teal) actually applies in dark; EventDetails month-label gets a dark brand surface in dark mode so its text stays legible.

Verification

  • Lint: ✓ (pnpm lint)
  • Types: ✓ (pnpm typecheck)
  • Unit: ✓ 79 passed / 19 files (pnpm test:run)
  • Build: ✓ (pnpm build)
  • Ladle build: ✓ (pnpm ladle:build)

Manual / visual verification

Drove the running Ladle (Playwright MCP), toggling the theme on representative stories in both light and dark:

  1. EventPanel / EventDetails — with the brand palette now applied in dark, the "JUL" date strip (dark:bg-primary-900), borders, highlighted contact icon, chips, and the "Register" button all read in both themes.
  2. Map — basemap flips bidirectionally with the toggle (light style ↔ dark style); this was the previously-broken behavior.
  3. EventsList — items, chips, distances, and dark:hover:bg-default-100 hover all legible in dark.
  4. Chip — palette matrix (primary = orange, secondary = teal) and StorySection chrome (titles, grid headers, dividers, inContext rule) legible in both themes.

Notes for reviewer

  • Fixed the dark-palette config bug. tailwind.config.js nested the dark theme's colors under NextUI's extend (which takes a base-theme name string, not an object), so the custom dark palette never applied and dark mode used NextUI's default blue/purple. Moved colors to the theme level (mirroring the light theme); dark now renders the brand orange/teal.
  • That exposed the one real change-feat(components): add ladle previews + standardize component library #5 component fix: our *-100 shades are fixed light tints, so once the palette applied, the EventDetails month-label bg-primary-100 strip would have rendered light-on-light in dark — given a dark brand surface there (dark:bg-primary-900). EventItem/ListItem hover, border-primary-100, the highlighted contact icon, and the Mapbox layers were all re-verified legible in dark with the brand palette and need no change.
  • Per reviewer feedback, the Ladle canvas no longer forces bg-background; it defers to Ladle's own theme background.
  • A /code-review (max effort, with verification) and a 4-angle /simplify pass were run over the branch — no correctness bugs. From the /review pass, localStorage access in useTheme is now wrapped so the toggle survives sandboxed-iframe embeds / blocked storage (with a co-located test).

Closes #18

🤖 Generated with Claude Code

Ardnived and others added 6 commits June 30, 2026 15:50
useTheme now derives the theme by observing the <html> class with
useSyncExternalStore (MutationObserver), instead of per-component
useState plus a dependency-less effect that rewrote the class on every
render. The root class becomes the single source of truth that NextUI,
Tailwind, and the Mapbox basemap all read, so a change made anywhere
(ThemeSwitch, the Ladle toggle) propagates to every consumer.

Add initTheme() to apply the persisted/default theme to the root class
once at startup, wired into App's mount effect (standalone + widget).

Refs #18

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The decorator now reads useLadleContext().globalState.theme and maps it
onto the root light/dark class (resolving 'auto' against the OS, tracked
live). Since useTheme observes that class, flipping Ladle's toggle
re-themes every story including the Mapbox basemap. Drop the forced
`light` class and the hardcoded `bg-white`/`text-gray-900` canvas in
favor of `bg-background text-foreground`.

Refs #18

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Dark mode is now driven by Ladle's own theme toggle (whole canvas), so
the per-story `<StorySection theme="dark" title="Dark Surface">` blocks
are redundant. Remove them from all 18 stories, drop the orphaned
`text-white` override and now-unused imports, reword the three docstrings
that referenced the dark surface, and swap a stray `text-gray-500` for
the semantic `text-default-500` in the EventTime story.

Refs #18

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Remove the `theme` prop and all `theme === 'dark'` branches now that the
canvas theme comes from Ladle's toggle. Re-express chrome with NextUI
semantic tokens (text-foreground / text-default-600 / text-default-500 /
border-divider / border-default-200) so it reads in both themes, and make
the neutral (bg-default-100) and brand-teal gradient context backgrounds
theme-aware.

Refs #18

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Visual verification (Ladle, both themes) confirmed the white cluster
count and the debug-only #888 bounds line read on both basemaps once the
basemap follows the theme toggle, so they're left as-is with a comment
explaining why.

Refs #18

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Post-review refinements:
- use-theme: drop premature useCallback wrappers; export a single shared
  applyTheme() seam for writing the root class.
- Ladle decorator reuses applyTheme() instead of duplicating the
  class-toggle logic.
- Move initTheme() out of App's mount effect to the entry points
  (main.tsx module scope, Widget.tsx body) so the persisted theme is set
  before first paint — no flash — matching the existing pre-render setup
  in those files.
- Let the Ladle story canvas fall back to Ladle's own theme background
  (drop the forced bg-background; keep text-foreground) per feedback.

Refs #18

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 30, 2026 •

Copy link
Copy Markdown

Deploying atlas-legacy with  Cloudflare Pages  Cloudflare Pages

Latest commit: b847ee5
Status: ✅  Deploy successful!
Preview URL: https://bd21b993.syatlas.pages.dev
Branch Preview URL: https://refactor-unify-dark-mode.syatlas.pages.dev

View logs

Ardnived and others added 3 commits June 30, 2026 16:46
localStorage can throw (not just be absent) in sandboxed iframes and
some privacy modes — relevant since this ships as an embeddable widget.
Wrap reads/writes so applyTheme still updates the root class when storage
is unavailable (the choice just isn't persisted), and fix initTheme's
guard, which previously only handled a missing localStorage, not one that
throws on access. Adds a co-located node-only test for applyTheme/initTheme.

Refs #18

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The gradient background is a fixed dark teal; without a pinned text color
its children inherited text-foreground, which is dark in light mode —
dark-on-dark. Pin white text so the (currently unused) gradient surface
reads in both themes, matching its comment.

Refs #18

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
NextUI's themes.<name>.extend takes a base-theme *name* (a string), so
nesting `colors` under `dark.extend` silently dropped the custom dark
palette — dark mode fell back to NextUI's default blue/purple instead of
the brand orange/teal. Move colors to the theme level (mirroring the
light theme) and drop the invalid borderColor/backgroundColor keys (never
read by NextUI).

This exposes that our `*-100` shades are fixed light tints, so the
EventDetails month-label `bg-primary-100` strip would render light-on-
light in dark; give it a dark brand surface there (dark:bg-primary-900)
so its text stays high-contrast. Verified both themes in Ladle.

Refs #18

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@Ardnived
Ardnived merged commit 2c9cf76 into main Jun 30, 2026
3 checks passed
@Ardnived
Ardnived deleted the refactor/unify-dark-mode branch June 30, 2026 16:13
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.

refactor(ui): unify dark mode across Ladle and components

1 participant