Frontend Recipes

Theming

Custom properties, dark mode, and inverse regions.

Every visual decision in the library resolves to a CSS custom property. Components never hold a colour, a size, or a duration of their own — they read tokens, and tokens are overridable.

There is no theme provider, no context, and no JavaScript that needs to run for theming to work. It is CSS custom properties all the way down.

Two themes, one attribute

The token set is defined twice: once on :root and once on [data-theme="dark"].

css
:root {
  --fr-paper: #ffffff;
  --fr-ink: #262626;
  --fr-accent: #047857;
}

[data-theme="dark"] {
  --fr-paper: #0a0a0a;
  --fr-ink: #f5f5f5;
  --fr-accent: #34d399;
}

Switching themes means setting one attribute on the root element:

tsx
document.documentElement.setAttribute("data-theme", "dark");

Because it is an attribute, it scopes. Any subtree can be forced into a theme, which is useful for previewing both at once:

tsx
<div data-theme="dark">
  <Button variant="primary">Always dark, whatever the page is</Button>
</div>
An inverse region inside the page
Inverse regionOn ink

[data-theme="inverse"] is the third built-in set: dark ground, light ink, and the shadow flips to light. Use it for a footer or a deliberate contrast band.

Avoiding the flash

A theme stored in localStorage is only readable after JavaScript runs, which means a flash of the wrong theme on first paint. The docs site solves this with a blocking inline script in <head>, before any content renders:

tsx
// app/layout.tsx
<head>
  <script dangerouslySetInnerHTML={{ __html: themeScript }} />
</head>
ts
const themeScript = `(function(){
  try {
    var stored = localStorage.getItem("fr-theme");
    var dark = window.matchMedia("(prefers-color-scheme: dark)").matches;
    document.documentElement.setAttribute("data-theme", stored || (dark ? "dark" : "light"));
  } catch (e) {}
})();`;

That script is about 200 bytes and eliminates the flash entirely. It is the only script in the docs site that exists purely for theming.

Overriding tokens

Override at any level. A scoped override is usually better than a global one:

css
/* A different brand accent, globally. Emerald is the default; replace the
   three accent tokens together or the hover and soft fills will not follow. */
:root {
  --fr-accent: #1f4d8f;
  --fr-accent-hover: #173c70;
  --fr-accent-soft: #e8eef7;
}

/* Tighter controls in a dense data view */
.dense-view {
  --fr-control-md: 2rem;
  --fr-space-4: 0.75rem;
}

Changing the control height token reflows every button, input, and select inside .dense-view at once, because they all derive their height from it. That is the payoff of tokenising heights rather than hardcoding paddings.

What is safe to override

| Safe | Changes behaviour | Do not | | --- | --- | --- | | Colours | --fr-control-* heights | --fr-radius to a non-zero value, unless you mean it | | Font family | --fr-space-* scale | Individual component class names | | Motion durations | --fr-measure | Internal class structure | | Shadow colour | --fr-focus-width | — |

Adding a third theme

Copy the [data-theme="dark"] block, rename the selector, and adjust. Every token must be supplied, or the new theme will inherit whatever :root has for the ones you miss — which is usually the right fallback, but check each one against your ground colour.