Files
ipal-kit/src/modules/tokens/catalog.ts
T
Radosław SmolińskiandClaude Opus 5.5 d6675d2979 Design tokens: themes on any element, primary-inverse, form template
Follow-ups from the reference site's first visual test:
- data-theme="dark"/"light" switches the tokens on any element (LIGHT_SELECTOR,
  DARK_SELECTOR as selector lists); the base layer colours a themed panel's text
- inverted sections re-map primary / primary-hover / primary-text to the new
  primary-inverse / primary-inverse-text roles; the contrast report checks them
  (inherited roles resolved on the ancestor, as the cascade does)
- ipal-form formClasses.ts on token utilities + buttonClass(); theme.md: one-icon
  ThemeSwitcher pattern (the default two-option toggle stays)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-29 15:04:54 +01:00

146 lines
7.6 KiB
TypeScript

/**
* The design-token catalogue: every `--ipal-*` token, its neutral default and the
* Tailwind utility it maps to. `tokens.css` (the shipped defaults) is generated from
* this file — see test/designTokens.test.ts — so names and values live in one place.
*/
/** A colour role: a value per theme. Values may reference other tokens (`var(--ipal-…)`). */
export type ColorDefault = { dark: string; light: string }
/** Colour roles that switch with the theme. Key = token name without `--ipal-`. */
export const COLOR_DEFAULTS = {
bg: { dark: 'oklch(0.145 0 0)', light: 'oklch(1 0 0)' },
border: { dark: 'oklch(0.269 0 0)', light: 'oklch(0.922 0 0)' },
'border-strong': { dark: 'oklch(0.371 0 0)', light: 'oklch(0.87 0 0)' },
primary: { dark: 'oklch(0.546 0.245 262.881)', light: 'oklch(0.546 0.245 262.881)' },
surface: { dark: 'oklch(0.205 0 0)', light: 'oklch(1 0 0)' },
'surface-inverse': { dark: 'oklch(0.97 0 0)', light: 'oklch(0.205 0 0)' },
'surface-muted': { dark: 'oklch(0.18 0 0)', light: 'oklch(0.97 0 0)' },
'surface-raised': { dark: 'oklch(0.235 0 0)', light: 'oklch(1 0 0)' },
text: { dark: 'oklch(0.87 0 0)', light: 'oklch(0.371 0 0)' },
'text-inverse': { dark: 'oklch(0.205 0 0)', light: 'oklch(0.97 0 0)' },
'text-muted': { dark: 'oklch(0.708 0 0)', light: 'oklch(0.53 0 0)' },
'text-strong': { dark: 'oklch(0.97 0 0)', light: 'oklch(0.205 0 0)' },
// darker in both themes: the text on primary stays readable on hover
accent: { dark: 'var(--ipal-primary)', light: 'var(--ipal-primary)' },
'accent-text': { dark: 'var(--ipal-primary-text)', light: 'var(--ipal-primary-text)' },
danger: { dark: 'oklch(0.704 0.191 22.216)', light: 'oklch(0.505 0.213 27.518)' },
'danger-surface': { dark: 'oklch(0.258 0.092 26.042)', light: 'oklch(0.971 0.013 17.38)' },
focus: { dark: 'var(--ipal-link)', light: 'var(--ipal-link)' },
'focus-inverse': { dark: 'var(--ipal-text-inverse)', light: 'var(--ipal-text-inverse)' },
hover: { dark: 'oklch(0.269 0 0)', light: 'oklch(0.97 0 0)' },
info: { dark: 'oklch(0.707 0.165 254.624)', light: 'oklch(0.488 0.243 264.376)' },
'info-surface': { dark: 'oklch(0.282 0.091 267.935)', light: 'oklch(0.97 0.014 254.604)' },
link: { dark: 'color-mix(in oklch, var(--ipal-primary), white 40%)', light: 'var(--ipal-primary)' },
'link-hover': {
dark: 'color-mix(in oklch, var(--ipal-link), var(--ipal-text-strong) 30%)',
light: 'color-mix(in oklch, var(--ipal-link), var(--ipal-text-strong) 30%)',
},
'link-inverse': { dark: 'var(--ipal-text-inverse)', light: 'var(--ipal-text-inverse)' },
overlay: { dark: 'oklch(0 0 0 / 0.65)', light: 'oklch(0 0 0 / 0.5)' },
'primary-hover': {
dark: 'color-mix(in oklch, var(--ipal-primary), black 12%)',
light: 'color-mix(in oklch, var(--ipal-primary), black 12%)',
},
// primary inside an inverted section (a dark brand colour vanishes on a dark inverse surface)
'primary-inverse': { dark: 'var(--ipal-primary)', light: 'var(--ipal-primary)' },
'primary-inverse-text': { dark: 'var(--ipal-primary-text)', light: 'var(--ipal-primary-text)' },
'primary-text': { dark: 'oklch(1 0 0)', light: 'oklch(1 0 0)' },
secondary: { dark: 'var(--ipal-text-strong)', light: 'var(--ipal-text-strong)' },
'secondary-hover': {
dark: 'color-mix(in oklch, var(--ipal-secondary), black 12%)',
light: 'color-mix(in oklch, var(--ipal-secondary), white 18%)',
},
'secondary-text': { dark: 'var(--ipal-text-inverse)', light: 'var(--ipal-text-inverse)' },
success: { dark: 'oklch(0.792 0.209 151.711)', light: 'oklch(0.527 0.154 150.069)' },
'success-surface': { dark: 'oklch(0.266 0.065 152.934)', light: 'oklch(0.982 0.018 155.826)' },
warning: { dark: 'oklch(0.828 0.189 84.429)', light: 'oklch(0.555 0.163 48.998)' },
'warning-surface': { dark: 'oklch(0.279 0.077 45.635)', light: 'oklch(0.987 0.022 95.277)' },
} as const satisfies Record<string, ColorDefault>
export type ColorRole = keyof typeof COLOR_DEFAULTS
/**
* Roles the cookie banner / button shipped before the standard, each with a `-dark`
* twin. The twins stay as aliases (set in the dark theme) for project CSS that reads them.
*/
export const LEGACY_DARK_ROLES = [
'surface', 'border', 'text', 'text-strong', 'text-muted', 'primary', 'primary-hover', 'primary-text', 'hover',
] as const satisfies readonly ColorRole[]
/**
* What `[data-surface="inverse"]` re-maps, so content inside an inverted section
* (cards, rich text, links, focus rings) stays readable without inverse variants.
*/
const onInverse = (percent: number) => `color-mix(in oklch, var(--ipal-text-inverse) ${percent}%, var(--ipal-surface-inverse))`
export const INVERSE_SCOPE: Partial<Record<ColorRole, string>> = {
border: onInverse(18),
'border-strong': onInverse(30),
focus: 'var(--ipal-focus-inverse)',
hover: onInverse(12),
link: 'var(--ipal-link-inverse)',
primary: 'var(--ipal-primary-inverse)',
'primary-hover': 'color-mix(in oklch, var(--ipal-primary-inverse), black 12%)',
'primary-text': 'var(--ipal-primary-inverse-text)',
surface: onInverse(8),
text: 'var(--ipal-text-inverse)',
'text-muted': onInverse(72),
'text-strong': 'var(--ipal-text-inverse)',
}
/** Theme-independent tokens. Key = token name without `--ipal-`. */
export const STATIC_DEFAULTS = {
// next/font slots: variable '--font-family-body' / '--font-family-heading' on <html>. Not --font-body:
// that name is Tailwind's font-body utility key, which maps back to these tokens (a var() cycle).
'container-narrow': '48rem',
duration: '250ms',
'duration-fast': '150ms',
'duration-slow': '400ms',
'ease-in-out': 'cubic-bezier(0.65, 0, 0.35, 1)',
'ease-out': 'cubic-bezier(0.22, 1, 0.36, 1)',
'focus-offset': '2px',
'focus-width': '2px',
'font-body': 'var(--font-family-body, ui-sans-serif, system-ui, sans-serif)',
'font-heading': 'var(--font-family-heading, var(--ipal-font-body))',
'font-mono': 'var(--font-family-mono, ui-monospace, SFMono-Regular, Menlo, monospace)',
'fw-body': '400',
'fw-heading': '650',
'lh-body': '1.6',
'lh-heading': '1.15',
radius: '0.375rem',
'radius-card': '0.75rem',
'radius-full': '9999px',
'radius-media': '0.5rem',
'radius-sm': '0.25rem',
'shadow-lg': '0 16px 32px -8px oklch(0 0 0 / 0.14), 0 4px 8px -4px oklch(0 0 0 / 0.08)',
'shadow-md': '0 4px 8px -2px oklch(0 0 0 / 0.08), 0 2px 4px -2px oklch(0 0 0 / 0.06)',
'shadow-sm': '0 1px 2px 0 oklch(0 0 0 / 0.06), 0 1px 3px 0 oklch(0 0 0 / 0.08)',
'tracking-heading': '-0.01em',
'z-header': '40',
'z-overlay': '50',
'z-toast': '60',
} as const
/** Dark-theme shadows: deeper black plus a hairline ring, since a soft grey shadow vanishes on dark. */
export const DARK_SHADOWS = {
'shadow-lg': '0 16px 32px -8px oklch(0 0 0 / 0.6), 0 0 0 1px oklch(1 0 0 / 0.06)',
'shadow-md': '0 4px 8px -2px oklch(0 0 0 / 0.5), 0 0 0 1px oklch(1 0 0 / 0.05)',
'shadow-sm': '0 1px 2px 0 oklch(0 0 0 / 0.4), 0 0 0 1px oklch(1 0 0 / 0.04)',
} as const
export type StaticToken = keyof typeof STATIC_DEFAULTS
/** Tokens fluid.css / fluid-wide.css define (the type and space scale). */
export const FLUID_TOKENS = [
'fs-xs', 'fs-sm', 'fs-base', 'fs-lg', 'fs-h4', 'fs-h3', 'fs-h2', 'fs-h1', 'fs-display',
'space-stack', 'space-gap', 'space-gutter', 'space-section-sm', 'space-section',
'measure', 'container', 'container-wide', 'hero-min-h', 'fs-display-fit', 'fs-h1-fit', 'space-section-fit',
] as const
export type FluidToken = (typeof FLUID_TOKENS)[number]
export type IpalTokenName = `${(typeof LEGACY_DARK_ROLES)[number]}-dark` | ColorRole | FluidToken | StaticToken
/** Standard breakpoints in rem (Tailwind 4 defaults). CSS variables can't be used in `@media`. */
export const IPAL_BREAKPOINTS = { '2xl': 96, lg: 64, md: 48, sm: 40, xl: 80 } as const