Colore JS

Search documentation

Find a function or jump to a section.
v2.0.1

applyThemeToCss

applyThemeToCss rebrands the theme file you already have. Give it your stylesheet and a system from generateSystem, and it rewrites the existing color tokens in place: same variable names, same value format, nothing else in the file touched. It can also add what the theme is missing, such as a dark mode or chart colors.

Example

import { applyThemeToCss, generateSystem } from 'colore-js';

const css = `:root {
  --bg: #ffffff;
  --text: #17212b;
  --brand: #4f46e5;
}`;

const result = applyThemeToCss(css, generateSystem('#0f766e'), { add: true });

console.log(result.css);
:root {
  --bg: #f3f6f5;
  --text: #070707;
  --brand: #0f766e;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #070707;
    --text: #f3f6f5;
  }
}

The brand is exactly the color you gave, and the new dark mode uses a media query because this hand-written theme has no .dark class.

Syntax

applyThemeToCss(css: string, system: ColorSystem, options?: ApplyThemeOptions): ThemeEditResult

Parameters

NameTypeDescription
cssstringThe stylesheet to update.
systemColorSystemA system from generateSystem.
optionsApplyThemeOptionsOptional. See below.

Options

OptionTypeDefaultDescription
onlyThemeGroup[]all groupsUpdate only these token groups. A -foreground token follows its background.
modesThemeMode[]['light', 'dark']Update only these modes.
addbooleanfalseAdd missing tokens, and a dark mode when the file has none.
replacebooleantrueRewrite existing colors. Set it to false with add: true to fill gaps and keep every existing value.

Returns

A ThemeEditResult:

PropertyTypeDescription
cssstringThe whole stylesheet with the edits applied.
changesThemeEditChange[]Each change with its scope, variable, old value from (null when added) and new value to.

Change only the brand

Pass only and modes to keep the change small. Here only the light-mode primary changes, and its paired text is updated with it.

import { applyThemeToCss, generateSystem } from 'colore-js';

const globalsCss = `:root {
  --primary: oklch(0.205 0 0);
  --primary-foreground: oklch(0.985 0 0);
  --muted-foreground: oklch(0.55 0 0);
}`;
const result = applyThemeToCss(globalsCss, generateSystem('#0f766e'), {
  only: ['brand'],
  modes: ['light'],
});

console.log(result.changes);
[
  { scope: ':root', variable: '--primary', from: 'oklch(0.205 0 0)', to: 'oklch(0.511 0.086 186.391)' },
  { scope: ':root', variable: '--primary-foreground', from: 'oklch(0.985 0 0)', to: 'oklch(1 0 0)' }
]

What it recognizes

  • shadcn/ui names such as --primary, --muted-foreground, --sidebar and --chart-1, in v4 OKLCH or v3 HSL channels.
  • Tailwind CSS v4 scales such as --color-brand-500 in @theme.
  • Hand-written names such as --bg, --text, --surface and --on-primary.

Hex, OKLCH, RGB, HSL and bare HSL channel values keep their format. Other formats are written as OKLCH.

What add fills in

  • A dark mode for every light token that differs by mode: a .dark block for shadcn/ui and Tailwind, and @media (prefers-color-scheme: dark) for hand-written themes.
  • chart-1 to chart-5 and destructive for shadcn/ui and Tailwind themes. Add only: ['status'] to add success and warning with their foregrounds.
  • Matching --color-* aliases when a shadcn/ui file declares them in @theme inline.

Check the result with auditCssVariables, or run colore theme "#0f766e" --apply globals.css --add from the CLI to edit, repair and audit in one step.