Skip to content

Theming

Theming

Themes and direction

import { WaveProvider } from '@mortenbrudvik/waveui';

export function Shell({ children }: { children: React.ReactNode }) {
  return (
    <WaveProvider theme="dark" dir="rtl">
      {children}
      {/* Providers nest in any order: a light panel inside a dark app works. */}
      <WaveProvider theme="light">{children}</WaveProvider>
    </WaveProvider>
  );
}
  • theme: 'light' (default), 'dark' or 'high-contrast'. dir: 'ltr' (default) or 'rtl'. portalContainer: where overlays render (default document.body).
  • A nested provider inherits every prop it omits (theme, dir, portalContainer, motion, applyStylesToPortals; inputDefaults and tokens merge key by key) from the enclosing provider, so the light panel above stays right-to-left. portalContainer={null} sends a nested subtree's overlays to document.body.
  • The provider renders <div class="wave-root wave-<theme>" dir data-wave-theme> with Wave's wave:bg-background wave:text-foreground wave:font-wave wave:text-body-1; a className you pass wins (bg-wave-card replaces the background, text-wave-body-2 the type size).
  • Portaled overlays (Dialog, Drawer, Popover, Menu, listboxes, Tooltip, Toaster) render in a wave-portal wrapper that carries the provider's theme classes, direction and font, and its tokens overrides unless applyStylesToPortals={false} (see Brand colors and token overrides).
  • inputDefaults: { size?, appearance? }, the default size and appearance of the text inputs and pickers below it (see Sizes and appearances). A nested provider merges its own defined keys over the enclosing provider's, so <WaveProvider inputDefaults={{ size: 'small' }}> inside an app with appearance: 'filled-darker' gives small filled-darker fields; omitting the prop inherits the enclosing value.
  • useWaveTheme() returns { theme, dir, themeClassName, portalContainer, inputDefaults, motion, tokens, tokenStyle, applyStylesToPortals, hasProvider } (outside a provider inputDefaults and tokens are {}, tokenStyle is undefined and applyStylesToPortals is true).

Theme classes. The tokens are selected by wave-light (also :root), wave-dark and wave-high-contrast, and the provider adds no other theme class; getThemeClassName(theme) returns a theme's class. Target these classes in your CSS. Tailwind 4's default dark: variant follows prefers-color-scheme, not a class, and Wave does not define a class-based one; to have your dark: utilities follow a dark provider, key the variant on Wave's class: @custom-variant dark (&:where(.wave-dark, .wave-dark *));. color-scheme is set on the three wave-* theme classes only, never on :root, so a page without a provider keeps its own scrollbar and form-control scheme.

The page outside the provider. The page's scrollbar and its canvas (the overscroll area and the space below short content) take their colours from <html>, which a provider never touches. To match them, put the theme class on <html> as well, which sets color-scheme there; in a server-rendered layout that is <html className={getThemeClassName(theme)}> (safe in a Server Component). color-scheme gives the canvas the browser's default colour for the scheme; for the theme's own background, add html { background-color: var(--wave-background); } to your CSS.

Following the operating system. theme takes one of the named themes, so resolve the OS setting in your app and pass the result:

import * as React from 'react';
import { WaveProvider, getThemeClassName, type WaveTheme } from '@mortenbrudvik/waveui';

const DARK = '(prefers-color-scheme: dark)';

function subscribe(onChange: () => void) {
  const query = window.matchMedia(DARK);
  query.addEventListener('change', onChange);
  return () => query.removeEventListener('change', onChange);
}

function useSystemTheme(): WaveTheme {
  const dark = React.useSyncExternalStore(
    subscribe,
    () => window.matchMedia(DARK).matches,
    () => false, // the server renders light
  );
  return dark ? 'dark' : 'light';
}

export function Shell({ children }: { children: React.ReactNode }) {
  const theme = useSystemTheme();
  // The page outside the provider: its scrollbar and canvas.
  React.useLayoutEffect(() => {
    const themeClass = getThemeClassName(theme);
    document.documentElement.classList.add(themeClass);
    return () => document.documentElement.classList.remove(themeClass);
  }, [theme]);
  return <WaveProvider theme={theme}>{children}</WaveProvider>;
}

A server cannot know the setting, so a server-rendered page paints light first and switches to dark after hydration on a dark system. <meta name="color-scheme" content="light dark"> in the document head at least draws the scrollbar and canvas in the system's scheme from the start (the theme class on <html> takes over once it is set).

Brand colors and token overrides

A brand color in three lines: createBrandTokens builds the 16 brand-ramp tokens from one color, and WaveProvider tokens applies them.

import { WaveProvider, createBrandTokens } from '@mortenbrudvik/waveui';

const brand = createBrandTokens('#c43e00'); // once, at module scope
export const App = ({ children }: { children: React.ReactNode }) => <WaveProvider tokens={brand}>{children}</WaveProvider>;
  • Contrast holds. Each generated stop keeps the luminance of the default ramp's stop and takes its hue and colorfulness from the seed, so every text, fill, stroke and focus-ring pair of the light and dark themes stays above the floors the themes are tested against (4.5:1 for text, 3:1 for strokes and focus rings), whatever the seed. The cost: stop 80 (the light primary) is the seed's hue at the default's lightness, so a light seed gives a darker button. For exact brand colors pass a full ramp (createBrandTokens({ 10: '#…', …, 160: '#…' }), a BrandRamp), which is returned unchanged and checked in development: each pair the themes read from it that fails its contrast minimum logs a warning. The high-contrast theme ignores the brand.

  • Any token. tokens takes any token by its camelCase name, the CSS variable without --wave- (primaryForeground, brand80, paletteRedTint, shadow8, fontFamilyMonospace, zOverlay); TypeScript refuses a misspelled one. A value is a finite number or a string without ;, {, } or url( whose quotes, parentheses, square brackets and comments are balanced, with no ! or backslash outside strings and comments (anything else is skipped with a development warning, so a value can neither add a declaration of its own to the server-rendered style nor swallow the declarations after it); it is trimmed, as React's server renderer trims it, so the server and the client render the same overrides. The provider writes them as inline custom properties on its root (also in the server HTML) and on every portal of its subtree, so its dialogs, menus, popovers, tooltips and toasts match it; applyStylesToPortals={false} keeps them off the portals, an enclosing provider's overrides (a brand ramp) included; theme and direction stay.

  • Nesting. A nested provider merges its tokens over the enclosing ones key by key, also when it sets another theme: set a semantic override in the provider that sets the theme it was chosen for. tokens={null} resets the subtree to the stylesheet's tokens: it clears the inherited overrides for the theme's own tokens and for the overlays, and writes the library's own value of each theme-independent token (the ramps, fonts, stacking, motion) that an enclosing provider overrode. A nested accent:

    <WaveProvider tokens={brand}>
      <AppHeader />
      {/* This panel, and the dialogs and menus it opens, use a crimson primary. */}
      <WaveProvider theme="light" tokens={{ primary: '#c4314b', primaryHover: '#a9283f', primaryPressed: '#7a1c2d' }}>
        <CampaignPanel />
      </WaveProvider>
    </WaveProvider>
    
  • Tokens in JavaScript. waveTokens holds every token as the var() that reads it, typed (waveTokens.primary is 'var(--wave-primary)'), for inline styles and charts; chartPalette lists the 12 palette fills in the order a chart gives them to its series. An SVG chart takes the values as they are: <Bar dataKey="north" fill={waveTokens[chartPalette[0]]} /> is var(--wave-palette-blue) and follows the theme. A canvas chart resolves them with getComputedStyle; colors never tell series apart on their own, so give every chart a second encoding (a pattern, a marker, a label). The guide's "Charts with WaveUI" chapter has the recipes, and Storybook's Guides/Charts runs them with Recharts and Chart.js.

  • Server Components. createBrandTokens, getTokenStyle, waveTokens and chartPalette are server-safe. getTokenStyle(overrides) returns the inline style the provider applies, for an element no provider renders: in a server-rendered layout, <html style={getTokenStyle(createBrandTokens('#c43e00'))}> brands the whole page, portals included, since the ramp is theme-independent (a semantic token such as primary belongs in tokens, because every theme class below <html> declares it again).

  • Strict CSP. Under a Content Security Policy whose style-src blocks inline styles, the server-rendered overrides do not apply and hydration does not apply them again: ship them in a stylesheet there (below), and pass no tokens (nor getTokenStyle), since the overlays and the subtrees rendered in the client would still take them and leave the page half branded.

Customizing tokens

Every color is a runtime CSS variable prefixed --wave-. The color utilities of your Tailwind build (on ./tailwind or ./tokens) and of ./utilities use the same name with wave- after the utility: --wave-muted-foreground is text-wave-muted-foreground, --wave-primary is bg-wave-primary, border-wave-primary, ring-wave-primary, and so on.

Override a token for one theme by targeting its theme selector, never :root alone for a token a theme declares: every provider root carries its theme class, which declares the theme's tokens again. With ./styles, load your CSS after Wave's (the selectors have the same specificity); on the Tailwind path any unlayered rule, and any rule in a layer after theme (@layer base), wins over Wave's theme layer. A provider's tokens win over these rules on the elements that carry them (an inline style beats a stylesheet rule on the same element).

/* All light surfaces */
:root,
.wave-light {
  --wave-muted-foreground: #5c5c5c;
}

/* Dark theme only */
.wave-dark {
  --wave-primary: #7fb8f7;
}

Re-branding. The primary, hover, pressed, focus-ring and selected colors of the light and dark themes are built from the brand ramp (--wave-brand-10 … --wave-brand-160, declared on :root), so overriding the ramp re-themes them: light uses brand-80/70/40 for primary/hover/pressed, brand-80 for the focus ring, brand-160/150/140 for the selected surface and its hover and pressed states and brand-60 for selected text; dark uses brand-110/120/90, brand-100 for the focus ring, brand-20/40/10 and brand-110. High contrast uses fixed colors. --wave-info is a fixed color and does not follow the ramp. A ramp written in CSS is not checked: check its contrast against --wave-primary-foreground (white in light, black in dark) and the selection pairs, or generate it with createBrandTokens (above).

:root {
  --wave-brand-40: #3b1a6b;
  --wave-brand-60: #53258f;
  --wave-brand-70: #5f2aa3;
  --wave-brand-80: #6b2fb3;
  --wave-brand-160: #f3edfb;
}

Only the --wave-* names. The 0.4 variable names, the ramps (--brand-80, --grey-38, …) and the semantic colors (--primary, --background, --border, --ring, …), are neither read nor defined: an override of one has no effect and a read such as outline: 2px solid var(--ring) resolves to nothing. Use the --wave-* names (--wave-brand-80, --wave-ring).

Every color token's value in each theme is listed in the README'sColor tokens section on GitHub.