Get started
Theming
Every component reads its colors from design tokens, so switching light and dark mode or the brand palette restyles everything at once: for the whole page, or for one section.
Build your theme visually
Theme the whole page
Set data-theme (light or dark) and data-brand on the <html> element. Without them, the page is light with the blue brand. Change the attributes at runtime to switch themes; no re-render is needed, since the tokens are plain CSS variables.
<!-- index.html -->
<html lang="en" data-theme="dark" data-brand="purple">
Theme one section
Wrap any part of your app in Theme to give it its own mode or brand. Themes nest, and each prop you leave out is inherited from the next theme outward. Overlays opened from inside (menus, popovers, dialogs) keep the section's theme even though they render in a portal.
Pick a color to use it instead of a preset.
<Theme brand="purple" mode="light" fill>
<Settings />
</Theme>
fill paints the theme's background and text color on the wrapper. Use it when the section's mode differs from the page's, so the content sits on the right surface.
Brand palettes
A brand sets the primary color family: solid fills, hover and pressed states, subtle tints, focus rings, links and selection. Each palette has separate light and dark values, checked for WCAG AA contrast against the surfaces they sit on. Each brand also brings its own accent, a brighter shade of the same hue. Status tones (success, warning, danger, info) stay the same across brands.
Blue brand="blue"
The default. Confident and calm.
Purple brand="purple"
Creative and modern.
Green brand="green"
Emerald, distinct from the success tone.
Gold brand="gold"
Warm, with an amber accent.
Black brand="black"
Monochrome, for editorial looks.
Accent color
The accent is the bright highlight on badges, switches, progress bars, sliders, steppers and the active navigation link. By default each brand uses its own: blue gets a sky blue, purple a lavender, gold an amber, green a mint, and black keeps the original lime. Set accent on Theme (or data-accent on <html>) to choose another: "lime", "brand" to go back to the brand's own, or any hex color. Accents always carry dark text, so a color too dark for that is lightened, keeping its hue.
brand="purple"Newaccent="lime"Newaccent="#ff9f43"New// Each brand's own accent (the default): purple gets a lavender.
<Theme brand="purple">…</Theme>
// The original yellow-green, whatever the brand.
<Theme brand="purple" accent="lime">…</Theme>
// Any hex color. One that's too dark for dark text is lightened.
<Theme brand="purple" accent="#ff9f43">…</Theme>
accentCss() turns a hex color into a stylesheet rule for a named accent, the way brandCss() does for brands.
import { accentCss } from "@vjdesigns/vjdesigns-ui";
// A [data-accent="sunset"] rule with every --accent-* variable for both modes.
// Ship it in your CSS and set <html data-accent="sunset">.
const css = accentCss("sunset", "#ff9f43");
Follow the user's preference
A common setup: start from the operating system's color scheme, let people override it with a toggle, and remember their choice.
import { useEffect, useState } from "react";
type Mode = "light" | "dark";
function initialMode(): Mode {
const saved = localStorage.getItem("theme");
if (saved === "light" || saved === "dark") return saved;
return matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
}
export function useColorMode() {
const [mode, setMode] = useState<Mode>(initialMode);
useEffect(() => {
document.documentElement.dataset.theme = mode;
localStorage.setItem("theme", mode);
}, [mode]);
return [mode, setMode] as const;
}
Read the current theme
useTheme() returns the brand, accent and mode set by the nearest enclosing Theme, for cases where JavaScript needs them, such as picking chart colors.
import { useTheme } from "@vjdesigns/vjdesigns-ui";
function ChartColors() {
// Values from the nearest enclosing <Theme>. All are undefined
// when only the <html> attributes apply.
const { brand, accent, mode } = useTheme();
// ...
}
Add your own brand
Pass any hex color as brand and Theme generates the whole palette for both modes. The hue is kept; if a color fails WCAG AA against the library's surfaces, it's shifted lighter or darker just enough to pass, with a console warning in development. Try it with the color field in the example above, or in the Theme builder.
// One color: used as given in the mode where it passes WCAG AA,
// with a matching shade generated for the other mode.
<Theme brand="#e11d48">
<App />
</Theme>
// Or choose the color for each mode yourself.
<Theme brand={{ light: "#4338ca", dark: "#f472b6" }}>
<App />
</Theme>
To theme the whole page from CSS instead, brandCss() turns a hex color into a stylesheet rule for a named brand.
import { brandCss } from "@vjdesigns/vjdesigns-ui";
// A [data-brand="acme"] rule with every --brand-* variable for both modes.
// Ship it in your CSS and set <html data-brand="acme"> to avoid a flash.
const css = brandCss("acme", "#e11d48");
Or write the palette by hand. Any data-brand value works if you define its palette. Declare the --brand-light-* and --brand-dark-* variables for it, and the library resolves --color-primary and the rest for each mode. Set the attribute on <html> or any element.
/* Your stylesheet, loaded after vjdesigns-ui.css */
[data-brand="acme"] {
--brand-light-primary: #c2410c;
--brand-light-primary-hover: #a3370a;
--brand-light-primary-active: #872d08;
--brand-light-on-primary: #ffffff;
--brand-light-primary-subtle: #fff1e8;
--brand-light-primary-subtle-hover: #ffe2cf;
--brand-light-primary-subtle-text: #872d08;
--brand-light-primary-border: #c2410c;
--brand-light-link-visited: #6b3fa0;
--brand-dark-primary: #fb923c;
--brand-dark-primary-hover: #fca561;
--brand-dark-primary-active: #fdb985;
--brand-dark-on-primary: #1f0b02;
--brand-dark-primary-subtle: #2e1606;
--brand-dark-primary-subtle-hover: #3d1e09;
--brand-dark-primary-subtle-text: #fed7b5;
--brand-dark-primary-border: #fb923c;
--brand-dark-link-visited: #c9a6f2;
}
Check your colors for contrast: text on --brand-*-primary needs 4.5:1 with --brand-*-on-primary, and the primary color needs 3:1 against the page background to work as a focus ring. See Design tokens for every variable.
Theme API
| Prop | Type | Default |
|---|---|---|
brandBrand palette for everything inside: buttons, links, focus rings, selection, and indicators all switch at once. Leave unset to inherit. A preset name, or any hex color ( | ThemeBrand | BrandColor | — |
accentNew in 1.4.10The accent: the energetic highlight on badges, switches, progress bars, sliders, steppers, and active navigation. Leave unset to inherit; with no accent set anywhere, each brand uses its own (a bright shade of its hue).
| ThemeAccent | BrandColor | — |
modeColor mode for everything inside. Leave unset to inherit. | "light""dark" | — |
fillPaint the page background and text color of the theme on this wrapper (useful when | boolean | false |
Also accepts native <div> attributes, plus className, style and ref.