@tdduydev/tokens
v0.1.0
Published
Design tokens for the xdev-ui kit: CSS variables, a Tailwind v4 theme block and a typed TypeScript object.
Maintainers
Readme
@tdduydev/tokens
Design tokens for the xdev-ui kit, built from one source
(tokens.json, synced from xDev Hive, plus overrides.json) into three outputs:
- CSS variables (
tokens.css) with a dark default and a light theme. - A Tailwind v4 theme block (
theme.css) that turns those variables into utility classes. - A typed TypeScript object (
tokens) with concrete values, for React Native and tooling.
Install
npm install @tdduydev/tokensRequires Node 20 or newer to build; the published package ships prebuilt files.
Use with CSS
/* app.css: Tailwind v4 */
@import "tailwindcss";
@import "@tdduydev/tokens/tokens.css";
@import "@tdduydev/tokens/theme.css";Without Tailwind, import @tdduydev/tokens/tokens.css alone and read the variables directly:
background: var(--bg-canvas).
Set the theme on any element (usually <html>). Dark is the default, so data-theme="dark" is
optional:
<html data-theme="light"></html>Warning: import
tokens.cssas plain, unlayered CSS. Do not write@import "@tdduydev/tokens/tokens.css" layer(...). Themed shadows (--shadow-1to--shadow-4,--shadow-sticky) have the same name intheme.cssand intokens.css. Tailwind emits them as--shadow-1: var(--shadow-1)inside@layer theme, and only the unlayeredtokens.cssvalue gives them a real value. Static values (radius,shadow-0) are written totheme.cssas literals, so they do not depend on this.
tokens.css also contains a prefers-reduced-motion: reduce block that sets transition-duration
and animation-duration to 0s.
Token groups
| Group | In tokens.css | In theme.css | In tokens (TS) |
| ------------ | --------------------------------------- | ------------------- | ----------------------------- |
| color | yes, themed tokens in both theme blocks | yes | dark, light |
| shadow | yes, themed tokens in both theme blocks | yes | shadow.dark, shadow.light |
| radius | yes | yes | radius |
| size | yes | yes (--spacing-*) | size |
| layout | yes | yes (--spacing-*) | layout |
| spacing | yes | no | spacing |
| border | yes | no | border |
| zIndex | yes | no | zIndex |
| tracking | yes | no | tracking |
| breakpoint | no | no | breakpoint |
| type | no | no | type |
Why some groups stop early:
spacing: Tailwind's default 4px scale already covers the 4/8 grid, so there is no class to add.breakpoint: CSS variables cannot be used inside@media, and the values equal Tailwind's defaults.type: a text style combines size, line height, weight and tracking. Use the TypeScript object.tracking: no@themeentry is needed.tracking-wide,tracking-tightandtracking-normalread--tracking-wide,--tracking-tightand--tracking-normal, whichtokens.csssets.
A token is themed when it has different light and dark values, or when it is an alias of a
themed token. Everything else is declared once in :root.
Tailwind class naming rule
Every token has a CSS variable named after it (--<token name>). theme.css maps each variable to
a Tailwind theme key using these rules. Components use these classes, never primitive palette
colors or hex values.
| Token | Theme key | Class examples |
| ------------------- | ------------------ | ------------------------------------------------------------------------------------ |
| color bg-X | --color-X | bg-canvas, bg-surface, bg-inverse |
| color text-X | --color-fg-X | text-fg-primary, text-fg-secondary, text-fg-link |
| color border-X | --color-line-X | border-line-subtle, border-line-default |
| any other color | --color-<name> | bg-surface-1, bg-action-primary-bg, text-status-success-fg, bg-accent-violet |
| radius-X | --radius-X | rounded-card, rounded-pill, rounded-control, rounded-chip |
| shadow-X | --shadow-X | shadow-3, shadow-sticky |
| any other shadow | --shadow-<name> | shadow-ring-glass, shadow-button-glass-shadow, shadow-focus-ring |
| size and layout | --spacing-<name> | h-button-h-md, h-topbar-h, w-sidebar-w, max-w-content-max |
Notes:
radius-md,radius-lgand the other radius names replace Tailwind'srounded-mdandrounded-lgvalues, because the variable name is the same.- Color primitives (
blue-*,neutral-*,navy-*,green-*,amber-*,red-*,brand-*) also get theme keys. Only the stops that Hive defines are replaced. Prefer semantic tokens. - The build stops with an error if two tokens produce the same theme key.
Edit tokens through overrides.json
tokens.json is a synced copy of the Hive tokens. Do not edit it. Put changes in
overrides.json, grouped by the same keys as tokens.json (color, shadow, layout, and so on):
{
"color": [
{ "name": "text-link-hover", "value": { "dark": "#b8a8ff", "light": "#4b30b8" } },
{ "name": "status-danger-bg", "value": { "dark": "#3a2324" } },
{ "name": "text-off", "add": true, "value": { "dark": "#5c5c60", "light": "#96929c" } }
],
"layout": [{ "name": "topbar-h", "value": "56px" }]
}- An entry replaces the value of an existing token of that group. A
{ "dark": ... }value withoutlightkeeps the Hive light value (the reverse works too). "add": trueadds a new token. It must give bothlightanddarkwhen it is themed.- A value can reference another token with
{token-name}. It becomesvar(--token-name)in CSS and is resolved to a concrete value in the TypeScript object. - The build fails with a clear message when an entry names a token that does not exist (and has no
add), whenaddnames a token that already exists, when an alias points to a missing token, or when aliases form a cycle.
Build and test:
npm run build -w @tdduydev/tokens # writes dist/
npm test -w @tdduydev/tokens@tdduydev/tokens/tokens.json is the upstream Hive source without overrides. Read dist outputs
or the tokens object for final values.
Use the TypeScript object in React Native
React Native has no CSS variables, so the tokens object holds concrete values: aliases are
resolved, and each theme has its own map. Keys are token names (the CSS variable name without --).
import { useColorScheme } from 'react-native'
import { tokens } from '@tdduydev/tokens'
export function useTokens() {
const scheme = useColorScheme() === 'light' ? 'light' : 'dark'
return {
color: tokens[scheme],
shadow: tokens.shadow[scheme],
radius: tokens.radius,
size: tokens.size,
}
}
const t = useTokens()
const styles = {
card: {
backgroundColor: t.color['bg-surface'],
borderRadius: parseFloat(t.radius['radius-card']), // "24px" -> 24
minHeight: parseFloat(t.size['button-h-md']),
},
}All values are strings, as written in CSS. Convert lengths with parseFloat. shadow values are
CSS box-shadow strings and need translating to React Native's shadow props (or a library that
parses them). type.styles holds composed text styles (family, fontSize, lineHeight,
fontWeight, optional letterSpacing) and type.families maps the family keys to font stacks.
License
MIT
