Skip to content

Transforms

Transforms mutate token values, names, or both. They run after filters, just before rendering. Use them to convert colors to hex, dimensions to rem, or names to kebab-case for CSS variables.

type Transform = {
matcher?: (token: ResolvedToken) => boolean
transform: (token: ResolvedToken) => ResolvedToken
}
  • matcher (optional) — If provided, the transform applies only when matcher(token) returns true. If omitted, the transform applies to all tokens.
  • transform — Receives a ResolvedToken and returns a new one. Should be immutable (return a new object, don’t mutate in place).

Import transforms from dispersa/transforms:

import { colorToHex, nameKebabCase, dimensionToRem } from 'dispersa/transforms'

Color transforms convert the color object (colorSpace + components) into platform-specific strings.

TransformInputOutput
colorToHex(){ colorSpace: 'srgb', components: [0, 0.4, 0.8] }#0066cc
colorToRgb()samergb(0, 102, 204)
colorToHsl()samehsl(210, 100%, 40%)
colorToOklch()sameoklch(...)
colorToOklab()sameoklab(...)
colorToLch()samelch(...)
colorToLab()samelab(...)
colorToHwb()samehwb(...)
colorToColorFunction()samecolor function string
TransformInputOutput
dimensionToPx(){ value: 16, unit: 'px' }"16px"
dimensionToRem(){ value: 16, unit: 'px' }"1rem"
dimensionToUnitless(){ value: 16, unit: 'px' }16
TransformInput (name)Output
nameKebabCase()color.brand.primarycolor-brand-primary
nameCamelCase()color.brand.primarycolorBrandPrimary
namePascalCase()color.brand.primaryColorBrandPrimary
nameSnakeCase()color.brand.primarycolor_brand_primary
nameConstantCase()color.brand.primaryCOLOR_BRAND_PRIMARY
nameCssVar()color.brand.primary--color-brand-primary
namePrefix(‘ds-’)color-brand-primaryds-color-brand-primary
nameSuffix(‘-token’)color-brand-primarycolor-brand-primary-token
TransformInputOutput
fontWeightToNumber()"bold"700
durationToMs(){ value: 0.2, unit: 's' }{ value: 200, unit: 'ms' }
durationToSeconds(){ value: 200, unit: 'ms' }{ value: 0.2, unit: 's' }
  • Global — Transforms in BuildConfig apply to all outputs first.
  • Per-output — Transforms in OutputConfig apply after global transforms, only for that output.

Execution order example:

  • Global: nameKebabCase() (applies to all outputs)
  • Per-output (CSS): colorToHex() (applies only to the CSS output)
await dispersa.build({
transforms: [nameKebabCase()],
outputs: [
css({
name: 'css',
file: 'tokens.css',
transforms: [colorToHex()],
}),
],
})