Changing the built-in tokens
This page is for maintainers of this repo, who change what @mainset/ui-core ships by
default. Product teams should override tokens instead, see
Theming with CSS; no fork needed.
Where each value lives
| What | Source (edit this) | Generated? |
|---|---|---|
| palette, tones, neutral scheme colors | packages/ui-core/src/tokens/color.tokens.mts |
yes, via codegen |
| spacing primitives, scales, layout gaps | packages/ui-core/src/tokens/spacing.tokens.mts |
yes |
| radius, border widths | packages/ui-core/src/tokens/border.tokens.mts |
yes |
| breakpoints, containers, z-index | packages/ui-core/src/tokens/layout.tokens.mts |
yes |
| how derived tone roles are mixed (hover, subtle…) | TONE_DERIVATION__TOKENS in color.tokens.mts |
yes |
| the derivation itself (oklab / oklch) | packages/ui-core/src/theme/derive.mts, color.mts |
shipped as JS too |
| fonts, motion, focus ring, opacity | packages/ui-core/src/styles/foundation.scss |
no (Sass) |
typeface files (@font-face) and fallbacks |
packages/ui-core/src/styles/fonts.scss, styles/fonts/ |
no (Sass) |
| control sizes and radius | packages/ui-core/src/elements/control/_control.tokens.scss |
no (Sass) |
TS tokens are the single source of truth: pnpm codegen (run by build and dev) writes
src/styles/_generated.scss, and Sass turns it into custom properties and classes. Never edit
_generated.scss by hand.
Workflow
cd packages/ui-core
pnpm codegen # regenerate _generated.scss after a TS token change
pnpm build # build + minify + browser-support guard
pnpm codegen:check # fails if the committed generated file is stale (CI)Commit the regenerated _generated.scss together with the token change.
Recipes
The palette generator
SCALE__SOURCES in color.tokens.mts generates every scale (gray, ink, violet, orange)
in OKLCH, through generatePalette() (src/theme/palette.mts):
SCALE_RAMP__TOKENS: one lightness per step for every hue, so step N has about the same contrast everywhere, and the share of the scale's peak chroma per step (low at the ends).- A source is a
hue+ peakchroma, or ananchorcolor pinned exactly at astep(its hue and chroma carry across the scale):violet: { anchor: 'rgb(139 20 253)', step: 500 }. - Output is
rgb()(the 2021 baseline has nooklch()); a too-vivid step is reduced to the most vivid sRGB color of its lightness and hue.
Change a brand color
Change the scale's anchor (or hue / chroma). The whole scale and every tone on it, derived roles included, are recomputed:
// color.tokens.mts
const SCALE__SOURCES = {
violet: { anchor: 'rgb(124 58 237)', step: 500 },
…
};Check contrast
src/theme/contrast.test.mts fails when a built-in text or UI color drops below WCAG 2 (4.5 text,
3 borders and focus rings) on any surface it sits on. pnpm contrast:report prints every pair
with its WCAG ratio and APCA Lc (advisory, a perceptual second opinion) after a palette change.
Point a tone at another color
const TONE__TOKENS = {
primary: {
light: { solid: 'teal-700', 'on-solid': 'gray-50' },
dark: { solid: 'teal-500', 'on-solid': 'gray-950' },
},
…
};Add the palette color first (a scale in SCALE__SOURCES, or a literal in PALETTE__TOKENS). Only solid and on-solid are
required; text and accent are optional and default to solid (light) or a lighter solid
(dark). Check on-solid contrast: at least 4.5:1 for small text.
Add a tone
- Add the name to
TONE__OPTIONSand a light + dark definition toTONE__TOKENS(thesatisfiescheck fails until both schemes exist). pnpm codegen && pnpm build.
That alone generates .ms-tone-{tone}, the --ms-color-{tone}-* roles in both schemes, the
bgColor / txtColor / brdColor classes, and the TS types (ElementColor, so srfTheme,
bgColor etc. autocomplete it in React). Every tone adds CSS to the foundation, which every
bundle ships, so report the size change in the PR.
Add a neutral semantic token
Add a key to both schemes in SCHEME__TOKENS (e.g. 'bg-overlay': 'gray-900'). It becomes
--ms-color-bg-overlay, light in full and dark only where it differs.
Change the derivation
TONE_DERIVATION__TOKENS in color.tokens.mts sets how far solid is mixed toward
black/white/canvas for each role, per scheme. text and accent aren't set by an amount but by
a WCAG contrast target, each on the surfaces it sits on:
text(4.5:1) on the scheme'sbg-muted(its lightest quiet fill: stripes, menus) and on the tone's ownsubtlefill;accent(3:1: borders, focus, display text) on the page (bg-default) and cards (bg-raised).
Each is solid with its OKLCH lightness moved just far enough, chroma kept as far as sRGB holds
it. Lightening for dark mode also turns the hue toward yellow (tintHueShift, degrees per unit
of lightness): a violet leans to magenta, a blue to azure, so a tint stays as vivid as the brand
instead of turning pastel. Darkening keeps the hue (a darker yellow would turn muddy green). So
the brand color stays itself wherever it has the contrast: #8b14fd is the text in light, the
accent in both schemes, and its dark text is #b668ff. The derivation runs in deriveToneRoles()
(src/theme/derive.mts), which both codegen (the shipped CSS) and createTheme() (runtime
themes) call, so the two can't drift. Codegen writes the computed values into
$scheme-token-map in _generated.scss; Sass only prints them.
A derivation change is a visual change for every product: compare dist/esm/styles/foundation.css
before and after, and report it in the PR.
Constraints
- Browser baseline (2021): Chrome 88, Firefox 78, Safari 14.1. No
@layer,color-mix(),:has(), container queries or media range syntax in shipped CSS; the build guard fails otherwise. That's why derived colors are precomputed. - Two layers per component: mechanics in
{element}-normalize.scss(no colors, typography, radius, shadows or motion: a test enforces it), the look in{element}.scss. Add both tostyles/index.scss, and the normalize layer tostyles/normalize.scss(a test checks both). - Direction-agnostic: logical properties and flex / grid
start/endonly; noleft/right,translateX,[dir]or:dir(). Mark a symmetric glyph that needs physical sides withrtl-safe:on the line above. - 0 specificity: token and component rules stay inside
:where(), so app overrides always win. - Keep customization hooks: don't remove or rename a public
--ms-*token without calling it out as a breaking change and offering a migration path. - Per-component color tokens (e.g.
--ms-button-bg) are deliberately absent. If one is needed, add it with a fallback so:root, subtree and element overrides all work:var(--ms-button-radius, var(--ms-control-radius)).