# Theming

Every color, radius, shadow and duration of the kit is a CSS custom property declared with `@theme default`. Override it in your own `@theme`, on a subtree or on a single instance — components never hardcode a value.

## How tokens work

`theme.css` declares the tokens inside one `@theme default` block. Tailwind compiles them to utilities (`bg-primary`, `text-muted-foreground`, `rounded-button`) and to plain CSS custom properties on `:root`. Because they are _defaults_, anything you declare in a regular `@theme` wins regardless of import order.

Semantic names come in families:

- `--color-<name>` with `-foreground` and `-hover` companions (`primary`, `secondary`, `info`, `success`, `warning`, `error`, `accent`)
- Surfaces and text: `background`, `app`, `subtle`, `muted`, `foreground` …
- `--radius-<component>`, compiled to `rounded-<component>`
- `--shadow-*` for elevation and focus rings, `--ease-*` and `--duration-*` for motion

## Override globally

Redefine tokens in your stylesheet's `@theme` block:

```css title="styles.css"
/* styles.css — after @import '@vigdx/ui/theme.css' */
@theme {
  --color-primary: oklch(0.55 0.2 150);
  --color-primary-hover: oklch(0.5 0.19 150);
  --color-primary-active: oklch(0.45 0.18 150);
  --radius-button: 9999px;
}
```

Without Tailwind, with the precompiled `@vigdx/ui/styles.css`, declare the same custom properties in plain CSS. The file keeps its tokens in the `vigdx` cascade layer, so your declarations outside it win whatever the import order. Unlike an `@theme` override with Tailwind, a `:root` override applies in dark mode too (it outranks the kit's `.dark` palette): re-declare under `.dark` every token dark mode changes, for a brand color the whole family.

```css title="styles.css"
/* styles.css — after @import '@vigdx/ui/styles.css' (no Tailwind) */
:root {
  --color-primary: #1f6f4a;
  --color-primary-hover: #185c3d;
  --color-primary-active: #134a31;
  --color-primary-foreground: #ffffff;
  --radius-button: 9999px;
}

/* :root applies in both schemes: re-declare the whole family for dark mode */
.dark {
  --color-primary: #5fc796;
  --color-primary-hover: #84d5ae;
  --color-primary-active: #a9e2c6;
  --color-primary-foreground: #0b0e14;
}
```

Whichever way you override, keep the [contrast contract](https://vigdx.com/docs/theming.md).

## Override a subtree or one instance

Tokens are custom properties, so they cascade. Set them on any element to re-theme what is inside it — try it:

```html
<!-- Every button inside this section becomes a pill -->
<section style="--radius-button: 9999px">
  <vig-button label="Get started" />
</section>

<!-- One instance -->
<vig-button style="--radius-button: 0" label="Sharp" />
```

```css
.marketing-hero {
  --color-primary: var(--color-accent);
  --color-primary-hover: var(--color-accent-hover);
}
```

> **Warning: Overlays render outside the subtree**
>
> Inline components follow subtree overrides. Body-level overlays (menus, dialogs, tooltips) render outside the subtree and inherit from `:root`.

## Customizing: tokens → parts → class

Reach for the three styling layers in this order:

1. **Tokens.** Most changes are a custom property: a color, a radius, a duration. They apply everywhere the token is used and survive kit updates.
2. **Parts.** Each component marks its meaningful internal elements with a stable `data-part` attribute and the component's `data-scope` — `trigger`, `panel`, `option`, `close`… — and exposes state through `data-state` and `aria-*`. Every component page lists its parts under "Styling hooks". Parts are public API: the CI gate records them, so a part is only removed in a major release.
3. **A host class** for layout around the component (width, margins, grid placement).

```css title="styles.css"
/* styles.css — global CSS, outside any @layer (or in a layer after utilities) */
[data-scope='chip'][data-part='close'] {
  color: var(--color-error);
}

/* Overlay panels render outside the host; the scope still matches them */
[data-scope='select'][data-part='panel'] {
  max-height: 20rem;
}

/* State comes from data-state and aria-* */
[data-scope='accordion'][data-part='trigger'][data-state='open'] {
  font-weight: 700;
}
```

```html
<!-- The same, as a Tailwind arbitrary variant on the host -->
<vig-chip
  class="[&_[data-scope=chip][data-part=close]]:text-error"
  label="Urgent"
  closable
/>
```

Select a part by its `data-scope` and `data-part` together. A descendant selector such as `vig-multi-select [data-part=label]` would also hit the parts of kit components nested inside (the chips of a multi-select), and misses overlay panels, which render outside the host.

No `!important` is needed, as long as the rule is in the right place. The kit's classes are Tailwind utilities, which your build compiles into Tailwind's `@layer utilities`. A rule wins over them, whatever its specificity, when it is in global CSS outside any cascade layer, or in a layer ordered after `utilities`. It loses inside `@layer components` or `@layer base`, in a stylesheet imported with `@import … layer(x)` unless that layer comes after `utilities`, and in a component's own (emulated-encapsulation) styles, which never reach the kit's elements without `::ng-deep`. With the precompiled `@vigdx/ui/styles.css` the kit's utilities are unlayered and scoped to its elements (`.p-2:where([data-scope])`, specificity 0,1,0): a part selector (0,2,0) beats them anywhere, a rule of equal specificity when loaded after `styles.css`, and a rule inside any cascade layer never does. Put part overrides in a global stylesheet:

```css title="styles.css"
/* styles.css — to keep your own rules layered, order an app layer last */
@layer theme, base, components, utilities, app;
@import 'tailwindcss';
@import '@vigdx/ui/theme.css';

@layer app {
  [data-scope='chip'][data-part='close'] {
    color: var(--color-error);
  }
}
```

Overlays that animate (dialog, drawer, menus, popover, command menu, the tree select, cascade select and color picker panels, the image and galleria viewer) mark the animating element with `data-opening` while their entrance plays and `data-closing` while their exit plays, and remove the attribute once the transition settles (two seconds at most). Both are stable hooks, listed with each component's parts: style an entrance or exit with Tailwind's `data-opening:` and `data-closing:` variants, and the test harnesses of `@vigdx/ui/testing` wait on them.

An arbitrary variant such as `[&_[data-scope=chip][data-part=close]]:text-error` stays inside the utilities layer, but its selector (a class plus two attributes) is more specific than the part's own single-class utilities. For hover and focus styles, which the kit also sets with variants, prefer global CSS. The Storybook story “Core / Styling hooks” checks these cases in a real browser, with zero-specificity `:where()` rules.

## Dark mode

`theme.css` ships a dark palette under the `.dark` class: semantic tokens only, with brand and status hues lifted to hold WCAG AA on dark grounds. Put the class on `<html>` — this site does exactly that from the theme switch in the site navigation.

```typescript
// Put .dark on <html> so body-level overlays (menus, dialogs) follow too.
document.documentElement.classList.toggle('dark', prefersDark);
```

```css
/* Make Tailwind's dark: variant follow the same class */
@custom-variant dark (&:where(.dark, .dark *));

/* Re-theme dark mode: declare your own .dark block after the import */
.dark {
  --color-primary: #7aa2ff;
  --color-background: #0f141c;
}
```

## Contrast contract

The components draw tokens on one another: `--color-primary-foreground` on `--color-primary`, a slider fill on a `--color-muted` track. The defaults and the dark palette meet WCAG AA for every such pair; when you override a token, keeping them there is yours to do. Automated audits miss many of these pairs (text over pseudo-elements and translucent tints, colors that only appear on hover), so check them whenever a token changes. The [playground](https://vigdx.com/docs/theming.md) above computes them live for the colors you pick.

- **Text, 4.5:1**: every text color on the surface it sits on, including each `-foreground` on its fill _and_ on the fill's `-hover`, and the status hues as text on `background` (messages, field errors).
- **Text on tints, 4.5:1**: each hue on its own 10% tint over `background` (soft badges, hovered text and outlined buttons), and each `-hover` token on its hue's 15% tint (pressed buttons, whose text takes the `-hover` token). Composite the tint over `background` before measuring; a hue that passes on `background` can fail on its own tint.
- **Non-text, 3:1**: control boundaries (`--color-border-control`) and state indicators: `--color-primary` on `background`, `muted`, `subtle` and `app` (checked fills, slider, progress and knob fills on muted tracks, selection marks), and `--color-rating` on `background` and `muted`.
- **Focus rings** derive from `--color-primary` (and `--color-error` on invalid fields). The offset ring draws a `background` gap and then `primary`, so `primary` needs 3:1 against `background` and against whatever surrounds the control.
- **Forced colors** (Windows High Contrast) replace token colors with system colors and drop box shadows. Every focus ring is paired with an outline and states map to `Highlight`, `GrayText` and `CanvasText`, so no token override can break that mode. Your own CSS around the kit must not convey state with a token color or a shadow alone.
- **Dark mode**: check every pair again under `.dark`. A dark theme on a class of its own, without the kit's `.dark`, must re-declare the status hues and `--color-rating` as well: the light ones give status text about 3.1:1 on a near-black surface.

> **Warning: Example: a stock emerald primary**
>
> Tailwind's `emerald-500` (`#10b981`) as `--color-primary` gives white button text 2.54:1 and a slider fill on `slate-100` 2.32:1, below both minimums. `emerald-700` (`#047857`) holds 5.48:1 and 5.01:1.

## Color tokens

Swatches are live: they follow the theme switch. The numeric scales (`--color-primary-50` … `-950`, `--color-surface-*`, `--color-viz-1` … `-8`) are primitives and keep their values in both schemes.

| Token | Default | Dark | Purpose |
| --- | --- | --- | --- |
| `--color-background` | `#ffffff` | yes | Component surfaces (cards, panels, inputs, overlays) |
| `--color-foreground` | `#151a24` | yes | Primary text on background |
| `--color-app` | `#f7f8fa` | yes | App canvas behind surfaces |
| `--color-subtle` | `#eff1f5` | yes | Lightest neutral fill (hover rows, wells) |
| `--color-muted` | `#e6e9ef` | yes | Neutral fill (tracks, skeletons, disabled fields) |
| `--color-foreground-soft` | `#4b5263` | yes | Secondary text |
| `--color-muted-foreground` | `#626a7a` | yes | Hints, placeholders, meta text |
| `--color-disabled` | `#9aa2b1` | yes | Disabled text and icons |
| `--color-link` | `#1f49c7` | yes | Inline link text |
| `--color-border-subtle` | `#e6e9ef` | yes | Dividers inside surfaces |
| `--color-border` | `#dce0e8` | yes | Default surface border |
| `--color-border-strong` | `#c3c9d5` | yes | Decorative and emphasised edges (panels, secondary buttons) |
| `--color-border-control` | `#6b7384` | yes | Form control boundaries: ≥3:1 non-text contrast |
| `--color-primary` | `#2b5ce6` | yes | Brand color: primary buttons, selection, focus rings |
| `--color-secondary` | `#eff1f5` | yes | Secondary (neutral) buttons and chips |
| `--color-info` | `#2b5ce6` | yes | Informational status |
| `--color-success` | `#11754f` | yes | Success status |
| `--color-warning` | `#945806` | yes | Warning status |
| `--color-error` | `#be3037` | yes | Error status, invalid fields, destructive actions |
| `--color-accent` | `#7c3aed` | yes | Violet for passive indicators (badges and chips only) |
| `--color-backdrop` | `rgb(0 0 0 / 0.5)` |   | Dialog and drawer backdrop |

## Radii

Each component has its own radius token, so you can round buttons without rounding cards.

| Token | Default | Dark | Purpose |
| --- | --- | --- | --- |
| `--radius-button` | `0.375rem` |   | Buttons, select and toggle buttons, paginator, stepper |
| `--radius-input` | `0.375rem` |   | Text fields, selects, file drop zone |
| `--radius-popover` | `var(--radius-input)` |   | Popover and date picker panels |
| `--radius-menu` | `var(--radius-input)` |   | Menus and the option panels of select, multi-select and autocomplete |
| `--radius-card` | `0.5rem` |   | Cards |
| `--radius-dialog` | `0.5rem` |   | Dialogs and drawers |
| `--radius-chip` | `0.25rem` |   | Chips |
| `--radius-badge` | `calc(infinity * 1px)` |   | Badges (pill) |
| `--radius-checkbox` | `0.25rem` |   | Checkboxes |
| `--radius-switch` | `calc(infinity * 1px)` |   | Toggle switch track and thumb |
| `--radius-slider` | `calc(infinity * 1px)` |   | Slider track |
| `--radius-tooltip` | `0.375rem` |   | Tooltips |
| `--radius-message` | `0.375rem` |   | Messages and toasts |
| `--radius-table` | `0.375rem` |   | Table frame |
| `--radius-progress` | `calc(infinity * 1px)` |   | Progress bar track |
| `--radius-skeleton` | `0.25rem` |   | Skeleton blocks |
| `--radius-avatar` | `0.375rem` |   | Square avatars |
| `--radius-link` | `0.125rem` |   | Link focus outline |

## Focus rings

Focus comes in three shapes: the `--shadow-focus` halo on bordered text-entry controls and buttons, the offset `--shadow-focus-ring` on borderless and non-text controls, and the `currentColor` ring inside tinted surfaces. They derive from `--color-primary`, `--color-error` and `--color-background`, so a rebrand recolors them. Use them in your own controls as `focus-visible:shadow-(--shadow-focus-ring)`.

| Token | Default | Dark | Purpose |
| --- | --- | --- | --- |
| `--shadow-focus` | `3px primary halo` | yes | Halo on bordered text-entry controls and buttons |
| `--shadow-focus-error` | `3px error halo` | yes | Halo on invalid text-entry controls |
| `--shadow-focus-ring` | `2px offset + 2px primary` | yes | Offset ring on borderless and non-text controls |
| `--shadow-focus-ring-error` | `2px offset + 2px error` | yes | Offset ring on invalid checkboxes, switches and radios |
| `--shadow-focus-current` | `0 0 0 2px currentColor` |   | Ring inside tinted surfaces (chip remove, message close) |

## Motion

Durations have no utility namespace: use `duration-(--duration-fast)`. Spinners, progress bars and skeletons fall back to static visuals under `prefers-reduced-motion`.

| Token | Default | Dark | Purpose |
| --- | --- | --- | --- |
| `--ease-standard` | `cubic-bezier(0.2, 0, 0, 1)` |   | Default curve for state changes |
| `--ease-out` | `cubic-bezier(0.16, 1, 0.3, 1)` |   | Entering elements |
| `--ease-in` | `cubic-bezier(0.4, 0, 1, 1)` |   | Leaving elements |
| `--duration-fast` | `120ms` |   | Tooltips, popovers, switches |
| `--duration-base` | `180ms` |   | Dialogs, accordions, toasts, floating labels |
| `--duration-slow` | `260ms` |   | Drawers |

## Global side effects

Importing `theme.css` redefines some Tailwind defaults app-wide because the components rely on them: `--text-base`, `--text-sm`, `--text-xs` (a denser 14 / 13 / 12px scale), `--font-sans`, `--font-mono`, the `--shadow-*` scale and `--ease-in` / `--ease-out`. To keep Tailwind's values, restore them in your `@theme`; the kit then uses them too.

```css
@theme {
  --font-sans: ui-sans-serif, system-ui, sans-serif;
  --text-sm: 0.875rem;
  --text-sm--line-height: calc(1.25 / 0.875);
  --text-base: 1rem;
  --text-base--line-height: calc(1.5 / 1);
  /* …and likewise for the other values you rely on */
}
```
