Skip to content

Theming

Themes

Create and apply custom var-ui themes with createDesignTheme.

Themes

A theme is a named TypeStyles surface: light/dark token values (via tokens + colorMode), optional data-color-mode pins, optional custom token namespaces (tokens.brand, …), and optional typed recipe restyles via components on createDesignTheme (see Customize).

Theme class names use the configured scope: theme-var-ui-<name> (e.g. theme-var-ui-default).

createDesignTheme modules only appear in CSS if you extract with @typestyles/vite. Variable overrides work on the published @var-ui/core/styles.css file.

Authoring

Built-in default tokens ship from @var-ui/core (tokenValues, defaultThemeClassName). The default theme surface is registered when you import from the package. Use createDesignTheme({ name: '…' }) for custom themes. Additional palette and style themes (forest, rose, AI Glow, Windows 95, …) live as copyable examples in the docs repo under docs/src/themes/.

TS
import { createDesignTheme, type DesignThemePreset } from '@var-ui/core';

// Reusable preset — spread into createDesignTheme
export const forestPreset: DesignThemePreset = {
  tokens: {
    color: {/* light-face color patches */},
    shadow: {/* mode-invariant overrides */},
  },
  colorMode: {
    dark: {
      color: {/* dark-face color patches */},
    },
  },
};

export const forestTheme = createDesignTheme({
  name: 'forest',
  ...forestPreset,
  tokens: {
    /* optional custom namespaces — see Customize */
    brand: { glow: '…' },
  },
});

// Optional recipe restyles — see Customize
// components: { button: ({ tokens: t }) => ({ base: { … } }) },

For a full working forest theme, see docs/src/themes/forest.ts in the var-ui repo.

Color mode

Omit data-color-mode to follow the OS. Set data-color-mode="light" or data-color-mode="dark" on any element to pin that subtree. Import COLOR_MODE_ATTRIBUTE from @var-ui/core if you need the attribute name in code. Global color-scheme rules for the attribute ship with @var-ui/core/styles.

Applying a theme

Wrap the app in DesignSystemProvider and pass the theme object:

TSX
import { DesignSystemProvider } from '@var-ui/react';
import { forestTheme } from './themes/forest';

export function App() {
  return (
    <DesignSystemProvider applyToDocument defaultColorMode="system" customTheme={forestTheme}>
      {/* … */}
    </DesignSystemProvider>
  );
}
  • customTheme — a createDesignTheme result, or a theme class name string.
  • When customTheme is set, it replaces the built-in default surface class on the provider wrapper (it does not stack both).
  • defaultThemeClassName / controlled colorMode — light / dark / system via data-color-mode (independent of which palette theme class is applied). System removes the attribute.

For SPA / full-page apps, pass applyToDocument so the theme class and data-color-mode live on <html> and body picks up tokenized background/text from core document globals.

No flash on reload

React applies a stored colour mode only after it loads, so a reload paints the default mode first. A small inline script in <head> applies the stored mode (and the theme class) before first paint. There is one implementation (@var-ui/core/color-mode-script), and each setup renders it:

  • Vite SPA (no server rendering). Add the varUiColorMode plugin. It injects the script into index.html. Share the settings with the provider so the storage key and default can’t drift:

    TS
    // src/colorMode.ts
    export const COLOR_MODE = { storageKey: 'color-mode', defaultColorMode: 'system' } as const;
    TS
    // vite.config.ts
    import { varUiColorMode } from '@var-ui/core/vite';
    import { defaultThemeClassName } from '@var-ui/core/theme-constants';
    import { COLOR_MODE } from './src/colorMode';
    
    export default defineConfig({
      plugins: [react(), varUiColorMode({ ...COLOR_MODE, themeClassName: defaultThemeClassName })],
    });
    TSX
    <DesignSystemProvider applyToDocument {...COLOR_MODE}>
      …
    </DesignSystemProvider>

    With a custom theme, pass its class name (forestTheme.className) as themeClassName.

  • SSR (Next.js, Remix, …). Render getColorModeInitScript({ storageKey, defaultColorMode, themeClassName }) from @var-ui/react as the first child of <head>: <script dangerouslySetInnerHTML={{ __html: getColorModeInitScript(COLOR_MODE) }} />.

  • Astro. <ThemeScript themeClass={…} storageKey={…} /> from @var-ui/astro renders the same script and carries the mode across view transitions.

  • Plain HTML / another framework. Call getColorModeInitScript from @var-ui/core/color-mode-script at build or render time and inline the result.

Next