Best Practices: Token Usage

How FlexPixels tokens actually work — the 12-step color scale, semantic layers, the two-tier hook pattern, and how to use them in your own CSS.

What This Page Is For

This page shows how to use FlexPixels design tokens in practice, with patterns taken from how fp-button and other components consume them. Follow them and your own styles work in light mode, dark mode and any generated theme without extra effort.

The Three-Layer Token Architecture

FlexPixels tokens are organized in three layers. Each layer builds on the one below it.

text
Primitives  →  DNA  →  Semantic
(raw scale)    (system defaults)    (purpose-driven)
  • Primitives: raw values like --fp-font-size-3 or --fp-radius-2 — you rarely use these directly
  • DNA: system-level defaults like --fp-spacing-base — the engine uses these internally
  • Semantic: what you actually reach for — --fp-s-4, --fp-corner-control, --fp-stroke-divider

Always start with semantic tokens. Drop to primitives only when no semantic token fits your use case.

The 12-Step Color Scale

Every color role (primary, secondary, neutral, success, danger, warning, info) generates 12 steps. Each step has a specific purpose:

StepsPurposeExample
1–2Backgrounds (page canvas, subtle cards)var(--fp-neutral-1), var(--fp-neutral-2)
3–5UI elements and interactive statesvar(--fp-primary-3) for inputs, var(--fp-primary-4) for hover, var(--fp-primary-5) for active
6–7Bordersvar(--fp-primary-6) subtle, var(--fp-primary-7) strong
8Interactive objects (icons, links)var(--fp-primary-8)
9Solid surfaces (buttons, badges)var(--fp-primary-9)
10Solid hovervar(--fp-primary-10)
11–12Textvar(--fp-primary-11) secondary, var(--fp-primary-12) headings

Steps 8–11 have a -contrast companion in the default stylesheet that gives you the right foreground colour (the full stylesheet has one for every step):

css
/* The button surface and its text color */
background: var(--fp-primary-9);
color: var(--fp-primary-9-contrast);

Dark mode is automatic: ramp steps such as --fp-neutral-2 carry both a light and a dark value, so you write one rule. Step 9 of a brand role is the brand colour in both schemes, and its -contrast is measured for each.

For brand-coloured text or links on the page background, use --fp-{role}-text — it is the role’s colour, adjusted where needed to stay readable (4.5:1) on the page.

Alpha Tokens

Each color role also generates alpha tokens (a1–a5) — semi-transparent versions of step 9:

TokenOpacityUse case
--fp-primary-a15%Ghost hover tint
--fp-primary-a210%Subtle background, outline hover
--fp-primary-a320%Active/pressed state
--fp-primary-a430%Focus ring
--fp-primary-a545%Badge, chip background

fp-button’s outline variant uses them, simplified here to the primary intent:

css
/* Outline button: transparent at rest, alpha tint on hover and press */
:host([variant="outline"]) {
  --_v-bg: transparent;
  --_v-bg-hover: var(--fp-primary-a2); /* 10% tint */
  --_v-bg-active: var(--fp-primary-a3); /* 20% tint */
}

Semantic Tokens You’ll Use Most

Spacing (--fp-s-*)

12 steps from tight to wide. Use these for padding, margin, and gap:

css
padding: var(--fp-s-2) var(--fp-s-6); /* vertical horizontal */
gap: var(--fp-s-4);

Corners (--fp-corner-*)

TokenUse case
--fp-corner-controlButtons, inputs, small interactive elements
--fp-corner-surfaceCards, panels, content areas
--fp-corner-overlayModals, dialogs, popovers
--fp-corner-circleAvatars, circular badges

Borders (--fp-stroke-*)

TokenUse case
--fp-stroke-controlButtons, inputs
--fp-stroke-dividerSeparators, list dividers
--fp-stroke-strongEmphasized borders
--fp-stroke-accentDecorative accent lines

Pair them with a colour such as --fp-border-subtle-color or --fp-border-strong-color.

Elevation (--fp-elevation-*)

4 levels of box-shadow, from subtle to prominent, with semantic aliases:

css
box-shadow: var(--fp-elevation-rest); /* level 1 — resting surface */
box-shadow: var(--fp-elevation-lifted); /* level 2 — raised, hover */
box-shadow: var(--fp-elevation-overlay); /* level 3 — dropdown */
box-shadow: var(--fp-elevation-modal); /* level 4 — modal */

Typography (--fp-text-*)

Each text role is a set of tokens — family, size, line height, weight and letter-spacing. Apply them together:

css
.body-text {
  font-family: var(--fp-text-body-font-family);
  font-size: var(--fp-text-body-font-size);
  line-height: var(--fp-text-body-line-height);
  font-weight: var(--fp-text-body-font-weight);
  letter-spacing: var(--fp-text-body-letter-spacing);
}

The Two-Tier Hook Pattern

This is the pattern that makes FlexPixels components overridable. Every visual property reads a component hook first, then falls back to the system: a token, or a value derived from one (the hover and active colours are oklch(from var(--fp-primary-9) …)). The basic shape:

text
var(--fp-{component}-{property}, var(--fp-{system-token}))
     ↑ component hook (override)      ↑ system fallback

Here’s how fp-button does it (its colours are simplified to the default primary intent):

css
:host {
  /* Background: component hook → system token */
  --_bg: var(--fp-button-background, var(--fp-primary-9));

  /* Foreground: component hook → contrast token */
  --_fg: var(--fp-button-color, var(--fp-primary-9-contrast));

  /* Border radius: component hook → shape override → semantic token */
  --_radius: var(--fp-button-border-radius, var(--fp-shape-radius, var(--fp-corner-control)));

  /* Border width: component hook → semantic token */
  --_border-width: var(--fp-button-border-width, var(--fp-stroke-control));

  /* Spacing: component hook → the size axis, floored by the corner's inset */
  --_padding-x: var(
    --fp-button-padding-inline,
    max(var(--fp-inset-control), var(--fp-size-inset-inline))
  );
}

The private --_ vars are internal. The --fp-button-* hooks are what consumers set to customize a specific button. The --fp-* system tokens are the global defaults.

Overriding a component

To change just one button’s appearance without affecting others:

css
fp-button.cta {
  --fp-button-background: var(--fp-secondary-9);
  --fp-button-background-hover: var(--fp-secondary-10);
  --fp-button-color: var(--fp-secondary-9-contrast);
}

The system tokens (--fp-primary-9) still apply everywhere else. Only buttons with class cta see the override.

Focus Ring Pattern

Focus rings have their own dedicated tokens. Never hardcode focus styles:

css
[part="surface"]:focus-visible {
  outline: var(--fp-focus-ring-width) solid var(--fp-focus-ring-color);
  outline-offset: var(--fp-focus-ring-offset);
}

Components can override these per-instance:

css
fp-button.custom {
  --fp-button-outline-color: var(--fp-danger-a4);
}

Common Mistakes

Hardcoded colors

css
/* ❌ Ignores the theme */
background: #6366f1;

/* ✅ Follows the theme */
background: var(--fp-primary-9);

Writing dark mode overrides

css
/* ❌ Tokens already handle this */
@media (prefers-color-scheme: dark) {
  .card {
    background: #1e1e2e;
  }
}

/* ✅ One rule, both modes */
.card {
  background: var(--fp-neutral-2);
}

Incomplete typography

css
/* ❌ Missing family, line-height, weight, letter-spacing */
font-size: var(--fp-text-body-font-size);

/* ✅ Apply the whole role */
font-family: var(--fp-text-body-font-family);
font-size: var(--fp-text-body-font-size);
line-height: var(--fp-text-body-line-height);
font-weight: var(--fp-text-body-font-weight);
letter-spacing: var(--fp-text-body-letter-spacing);

Using the wrong color step

css
/* ❌ Step 9 is for solid surfaces; a pale brand colour may be unreadable as text */
color: var(--fp-primary-9);

/* ✅ Steps 11–12 are for text */
color: var(--fp-primary-12);

/* ✅ Or the role's measured text colour, for brand-coloured text */
color: var(--fp-primary-text);

Skipping the contrast token

css
/* ❌ Guessing the foreground color */
background: var(--fp-primary-9);
color: white;

/* ✅ Let the system pick the right contrast */
background: var(--fp-primary-9);
color: var(--fp-primary-9-contrast);

Next Steps