Retrofitting a Dark Theme into a Light-Only Nuxt 3 App
How we moved 860+ Tailwind palette classes across 224 Vue files to semantic role tokens without changing a single pixel of light mode, and found 26 accessibility failures we'd been shipping for years.
blog.oleg.stepura.com/bontique-dark-theme-migration
Bontique One is a B2B platform where corporate customers configure gift checks, manage budgets and run multi-step ordering wizards. Like a lot of apps built on a single design system, our Nuxt 3 SPA was designed for a white background and nothing else. There was no dark Figma file, no dark spec, and nothing in the code that said what a color was for. Components just named raw Tailwind CSS v4 shades: bg-white, text-gray-600, border-gray-200. About 860 of them, spread over 224 Vue files.
On paper, a dark theme is a design task. In practice, picking dark colors was the small part. The real work was teaching the codebase the difference between “this is a card surface” and “this is literally white”, while holding one hard rule: light mode must not change by a single pixel.
The result is a stack of PRs (278 files, ) that separates palette values from semantic roles, gets rid of the theme flash on load, guards against regressions with custom lint rules, and, somewhat unexpectedly, exposed 26 contrast failures in the light theme we’d been shipping all along. Most of that code was written by an AI coding agent, and every change went through human code review.
Two shortcuts we didn’t take
Before touching a single component, we wrote an ADR and ruled out two tempting shortcuts.
Inverting the palette. Redefine --color-gray-50 … --color-gray-900 in reverse order and swap --color-white with --color-black. It’s simple, it makes a great screenshot, and it gives you a broken product. White labels on solid blue buttons turn black. Switch knobs go dark. The print preview of a physical gift check goes dark too, even though it shows ink on paper. And from that day on, gray-50 means “almost black”, which poisons every future conversation about color.
dark: variants everywhere. Writing bg-white dark:bg-zinc-900 across all templates doubles the class count from 860 to over 1,700, scatters theme logic over hundreds of files, and is easy to forget in new code. Worse, Tailwind’s dark: variant can’t switch back: a light-only print preview nested inside a dark page still picks up the dark ancestor.
We kept the dark: custom variant defined in CSS, just in case. After the whole migration, the number of dark: classes in the codebase is zero. Semantic roles covered every case.
Two token layers: palette vs. role
One thing was already in good shape: every Tailwind v4 color in our stylesheets was backed by a CSS custom property. We left those palette shades alone and added a second layer on top, 145 semantic role tokens in a separate theme stylesheet:
:root,
[data-theme='light'],
.theme-light {
--bg-surface: var(--color-white);
--bg-surface-sunken: var(--color-gray-50);
--fg: var(--color-black);
--fg-muted: var(--color-gray-600);
--border-default: var(--color-gray-200);
/* … 145 roles … */
color-scheme: light;
}
[data-theme='dark'] {
--bg-surface: #1c1c1f;
--bg-surface-sunken: #141416;
--fg: #ececee;
--fg-muted: #a3a3a8;
--border-default: #38383d;
/* … */
color-scheme: dark;
}
The roles are mapped into Tailwind v4’s per-property namespaces (--background-color-*, --text-color-*, --border-color-*) via @theme inline. Using property-specific namespaces instead of the generic --color-* means Tailwind doesn’t generate nonsense like text-surface. Text roles get an fg prefix (text-fg-muted) so they don’t collide with the font-size scale (text-sm, text-lg).
That gave us one rule, which now doubles as documentation for the whole repo:
- A palette class (
bg-key-500,text-white) means the color is the same in both themes. Solid brand and status fills, white labels on those fills, switch knobs, chart series and permanently dark chrome (toasts, step bars, dark tooltips) stay on the palette. - A role class (
bg-surface,text-fg-muted) means the color follows the theme. Surfaces, borders, neutral text, colored text links and subtle tints move to roles.
Every role’s light value is exactly the palette shade it replaces. So moving a component from text-gray-600 to text-fg-muted changes nothing in light mode, and each PR could be merged, paused or shipped on its own.
The custom property inheritance gotcha
The intuitive way to build this is to declare the roles once on :root (--bg-surface: var(--color-white)) and then override --color-white inside [data-theme='dark']. That doesn’t work. A custom property that references another variable is resolved on the element where it’s declared, and children inherit the computed value, not the reference.
So every theme scope (:root, [data-theme='light'], .theme-light and [data-theme='dark']) has to re-declare all 145 roles. A unit test checks that the light and dark blocks define exactly the same keys. The nice side effect: re-declaring gives us light-only islands for free. Wrap a print preview or a pastel tag in class="theme-light" and every role resets for that subtree, even inside a dark page.
No flash on load, no ripple on toggle
Bontique One is a client-rendered SPA (ssr: false). The server sends an empty HTML shell, so it can’t put data-theme="dark" on the first response. If the theme were set from inside the Vue bundle, dark-mode users would see a white flash on every reload.
We handle it in two places, both before the app boots:
- An inline
<head>script.nuxt.config.tsinlines a small, dependency-free classic script into<head>. It readslocalStorage, resolves'system'viamatchMedia('(prefers-color-scheme: dark)')and setsdata-themeon<html>before the first paint. - A dark SPA loading template. Nuxt’s
app/spa-loading-template.htmlrenders before the main stylesheet arrives, so it got literalhtml[data-theme='dark']rules matching the dark page background (#18181b).
Once the app is mounted, a small theme composable takes over and keeps the theme in sync with OS preference changes and with other tabs through storage events.
Toggling the theme had one more surprise: a visible ripple across the page. Hundreds of elements had transition: color 150ms or background-color transitions, all running slightly out of phase. The fix is to switch transitions off around one forced style recalculation:
root.dataset.themeSwitching = ''
root.dataset.theme = theme.value
getComputedStyle(document.body).getPropertyValue('color') // force a synchronous recalculation
setTimeout(() => {
delete root.dataset.themeSwitching
}, 1)
Together with [data-theme-switching] * { transition: none !important; }, the whole UI now flips in a single frame.
One product decision worth mentioning: light stays the default for existing users. We didn’t want to default to system and quietly switch people to dark overnight.
Rolling it out in small PRs
One giant PR would have been impossible to review and a magnet for merge conflicts, so we split the work into a stack:
| What the PR did | Size |
|---|---|
| Laid the foundation: role tokens, setting the theme before first paint, a theme composable, a theme switch in Storybook | 14 files, |
| Moved the 76 shared UI components to roles | 79 files, |
| Moved the feature screens, marked light-only islands, and added lint rules that block new palette classes | 155 files, |
| Polished the dark draft: elevation, focus rings, SVG fallbacks, and a contrast check in CI | 80 files, |
A small Node codemod did 505 unambiguous swaps across shared components and screens (text-gray-600 → text-fg-muted, border-gray-200 → border-default). The 257 context-dependent uses of white, black and gray-900 were reviewed and decided by hand. Inline SVGs with hard-coded #fff or #000 fills got role variables with hex fallbacks (fill="var(--bg-surface, #fff)"), so they still render correctly outside the app document.
Checking the invariant, and the one time we didn’t
To prove that light mode hadn’t changed, we ran two Storybook instances side by side, baseline and PR branch, and compared computed styles (color, background-color, borders, box-shadow, fill, stroke, outline-color) across all 136 stories.
An invariant is only as strong as how consistently you check it. In the dark-polish PR we darkened the solid success, danger and gray button fills so white labels would pass contrast on a dark page. One commit darkened them in both themes. The Storybook diff hadn’t been re-run on that commit, so it was a human reviewer who caught it. They blocked the PR until the fills were split into theme-aware --fill-success, --fill-danger and --fill-gray tokens.
To keep things clean from here on, the feature-screens PR wires a single palette-to-role map into both ESLint (vue/no-restricted-class) and a custom Stylelint rule. Put text-gray-600 in a template or an @apply block and CI fails with a message naming the role to use instead.
Dark UI details, and 26 contrast failures in light mode
With the plumbing in place, going through real screens in dark mode turned up problems that a token swap can’t solve:
- Elevation without shadows. Black drop shadows disappear on
#18181b. In dark mode,--elevation-cardand panels trade the shadow for a 1px white hairline ring (0 0 0 1px rgb(255 255 255 / 8%)) plus stepped surface lightness. Deep shadows are reserved for floating dialogs and menus. - “Disabled” was really two things.
--fg-disabled(gray-400) was used both for readable placeholders and empty states, and for genuinely disabled form values. We split it into--fg-hint(5.0:1 in dark) and--fg-disabled(2.8:1, deliberately below the readable roles). Both states got clearer. - Softer inverse selections. Flipping selected tabs and page numbers from light’s gray-900 to dark’s gray-100 produced glaring white pills. A dedicated
--bg-selected(#3d3d43) with off-white text reads clearly without blinding anyone.
Final palette sign-off belongs to our designer, so we built a side-by-side role comparison matrix in Storybook, backed by a contrast unit test. The test parses both stylesheets, resolves every var() chain recursively, alpha-blends translucent dark tints over the surface underneath, and asserts WCAG AA ratios. On the way we had to work around a quirk in our color library: it treated modern space-separated strings like rgb(0 0 0 / 5%) as opaque.
Running the test on both themes gave us the biggest surprise of the project. The draft dark palette passed WCAG AA on all 40 checked pairs. The light theme, the one that had been in production for years, passed on only 23. Muted body text (gray-500 on white), hint text at 2.1:1, 500-shade status text and white labels on solid status buttons had all been failing contrast without anyone noticing. We pinned those 26 light-mode shortfalls in the test suite, so nothing can get worse while the designer reviews both palettes together.
What I’d take away
- Separate semantics from swatches early. Even if you only ship one theme today, a thin role layer turns future theming from a 240-file refactor into a single stylesheet change.
- Make the migration invisible. Mapping light roles 1:1 to the existing shades lets you verify every PR mechanically and keep merging alongside normal feature work.
- White is never just one color. In a light-only app,
bg-whitemeans surface, chrome, switch knob and print canvas all at once. Pulling those meanings apart is where the real architectural value is. - Check contrast in CI, for both themes. Numbers in a unit test end subjective color debates, and they’ll almost certainly turn up accessibility debt in your light theme too.
A note on process: we wrote the plan and the ADR with AI help, up front. The code itself was written by an AI coding agent: the implementation, the codemod and the Storybook computed-style checks. Every change went through human code review. That’s what squeezed a 3–4 week estimate into several days.