124 lines
5.5 KiB
TypeScript
124 lines
5.5 KiB
TypeScript
import { COLOR_DEFAULTS, type ColorRole, type StaticToken } from './catalog.js'
|
|
import { FLUID_SCALE, type FluidRange, type FluidSize, type RemValue } from './fluid.js'
|
|
|
|
/** Colour values per role — any CSS colour (oklch recommended), or a `var(--ipal-…)` reference. */
|
|
export type ColorInput = Partial<Record<ColorRole, string>>
|
|
|
|
/**
|
|
* A project's brand, in `src/design/tokens.ts`. Everything is optional: what is left
|
|
* out keeps the neutral default from `@intecion/ipal-kit/tokens.css`.
|
|
*/
|
|
export type TokensInput = {
|
|
color?: {
|
|
/** Dark-theme values. A brand role (primary, secondary, accent, link, *-text) set only in `light` carries over. */
|
|
dark?: ColorInput
|
|
light?: ColorInput
|
|
}
|
|
container?: { default?: string; measure?: string; narrow?: string; wide?: string }
|
|
/** Which fluid file the site imports: `'wide'` = fluid-wide.css (grows to 1920px, the default for new sites). */
|
|
fluid?: FluidRange
|
|
font?: {
|
|
body?: string
|
|
fwBody?: number | string
|
|
fwHeading?: number | string
|
|
heading?: string
|
|
lhBody?: number | string
|
|
lhHeading?: number | string
|
|
mono?: string
|
|
trackingHeading?: string
|
|
}
|
|
/** Rewrite fluid sizes from min → max pairs (rem or px); the clamp() is computed for the `fluid` range. */
|
|
fs?: Partial<Record<FluidSizeName<'fs-'>, readonly [RemValue, RemValue]>>
|
|
motion?: { base?: string; easeInOut?: string; easeOut?: string; fast?: string; slow?: string }
|
|
radius?: { card?: string; control?: string; media?: string; sm?: string }
|
|
/** Any other `--ipal-*` value (name without `--ipal-`), e.g. `{ 'hero-min-h': 'min(100svh, 60rem)' }`. */
|
|
raw?: Record<string, string>
|
|
shadow?: { lg?: string; md?: string; sm?: string }
|
|
space?: Partial<Record<FluidSizeName<'space-'>, readonly [RemValue, RemValue]>>
|
|
/** `'both'` (default): light + dark. `'light'` / `'dark'`: a one-theme site; the other theme never applies. */
|
|
theme?: 'both' | 'dark' | 'light'
|
|
}
|
|
|
|
type FluidSizeName<P extends string> = FluidSize extends infer K ? (K extends `${P}${infer R}` ? R : never) : never
|
|
|
|
/** Roles whose light value carries over to dark when the brand sets no dark value. */
|
|
export const BRAND_ROLES: readonly ColorRole[] = [
|
|
'primary', 'primary-text', 'secondary', 'secondary-text', 'accent', 'accent-text',
|
|
]
|
|
|
|
function check(keys: Iterable<string>, known: Record<string, unknown>, where: string) {
|
|
for (const key of keys) {
|
|
if (!(key in known)) {throw new Error(`ipal-kit tokens: unknown ${where} "${key}"`)}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Declare the brand. Returns the input unchanged after checking every name, so a
|
|
* typo fails here (and in TypeScript) instead of silently missing from the CSS.
|
|
*/
|
|
export function defineTokens<T extends TokensInput>(input: T): T {
|
|
check(Object.keys(input.color?.light ?? {}), COLOR_DEFAULTS, 'colour role')
|
|
check(Object.keys(input.color?.dark ?? {}), COLOR_DEFAULTS, 'colour role')
|
|
check(Object.keys(input.fs ?? {}).map((k) => `fs-${k}`), FLUID_SCALE, 'fs size')
|
|
check(Object.keys(input.space ?? {}).map((k) => `space-${k}`), FLUID_SCALE, 'space size')
|
|
if (input.theme && !['both', 'dark', 'light'].includes(input.theme)) {
|
|
throw new Error(`ipal-kit tokens: theme must be 'both', 'light' or 'dark'`)
|
|
}
|
|
return input
|
|
}
|
|
|
|
export type ResolvedColors = Record<ColorRole, string>
|
|
|
|
const defaultsFor = (theme: 'dark' | 'light') =>
|
|
Object.fromEntries(Object.entries(COLOR_DEFAULTS).map(([role, v]) => [role, v[theme]])) as ResolvedColors
|
|
|
|
/**
|
|
* The colour values each theme ends up with (defaults + brand), as the CSS cascade
|
|
* would produce them. A one-theme site gets the same map for both.
|
|
*/
|
|
export function resolveColors(input: TokensInput): { dark: ResolvedColors; light: ResolvedColors } {
|
|
const light = { ...defaultsFor('light'), ...input.color?.light }
|
|
const carried = Object.fromEntries(
|
|
BRAND_ROLES.filter((role) => input.color?.light?.[role] && !input.color?.dark?.[role]).map((role) => [role, input.color!.light![role]!]),
|
|
)
|
|
const dark = { ...defaultsFor('dark'), ...carried, ...input.color?.dark }
|
|
if (input.theme === 'light') {return { dark: light, light }}
|
|
if (input.theme === 'dark') {return { dark, light: dark }}
|
|
return { dark, light }
|
|
}
|
|
|
|
/** Theme-independent values the brand sets, as token name → CSS value. */
|
|
export function staticOverrides(input: TokensInput): Partial<Record<StaticToken, string>> & Record<string, string> {
|
|
const out: Record<string, string> = {}
|
|
const put = (name: string, value: number | string | undefined) => {
|
|
if (value !== undefined) {out[name] = String(value)}
|
|
}
|
|
const { container = {}, font = {}, motion = {}, radius = {}, shadow = {} } = input
|
|
put('font-body', font.body)
|
|
put('font-heading', font.heading)
|
|
put('font-mono', font.mono)
|
|
put('fw-body', font.fwBody)
|
|
put('fw-heading', font.fwHeading)
|
|
put('lh-body', font.lhBody)
|
|
put('lh-heading', font.lhHeading)
|
|
put('tracking-heading', font.trackingHeading)
|
|
put('radius', radius.control)
|
|
put('radius-sm', radius.sm)
|
|
put('radius-card', radius.card)
|
|
put('radius-media', radius.media)
|
|
put('shadow-sm', shadow.sm)
|
|
put('shadow-md', shadow.md)
|
|
put('shadow-lg', shadow.lg)
|
|
put('container', container.default)
|
|
put('container-wide', container.wide)
|
|
put('container-narrow', container.narrow)
|
|
put('measure', container.measure)
|
|
put('duration-fast', motion.fast)
|
|
put('duration', motion.base)
|
|
put('duration-slow', motion.slow)
|
|
put('ease-out', motion.easeOut)
|
|
put('ease-in-out', motion.easeInOut)
|
|
for (const [name, value] of Object.entries(input.raw ?? {})) {put(name.replace(/^--ipal-/, ''), value)}
|
|
return out
|
|
}
|