AI-native design editor. Open-source Figma alternative.
import type { TokenUnit, Variable, VariableScope } from '@open-pencil/scene-graph'
import { tokenNumberInUnit } from '@open-pencil/scene-graph/variables'
import { variableNamespace } from './names'
const UNITLESS_SCOPES = new Set<VariableScope>(['OPACITY', 'FONT_STYLE', 'FONT_VARIATIONS'])
/**
* A property that takes values in each token unit, where a browser checks or computes a CSS
* expression written for the token: `scale` takes any plain number, `width` any length.
*/
export const TOKEN_UNIT_PROPERTY: Record<TokenUnit, string> = {
none: 'scale',
px: 'width',
rem: 'width',
'%': 'width',
ms: 'transition-duration',
s: 'transition-duration',
deg: 'rotate'
}
/**
* The unit a FLOAT token is written in. An explicit unit wins; opacity and font weights are
* unitless; every other number is a pixel length, which is what the canvas draws.
*/
export function variableUnit(variable: Variable): TokenUnit {
if (variable.type !== 'FLOAT') return 'none'
if (variable.unit) return variable.unit
const scopes = variable.scopes ?? []
if (scopes.length > 0 && scopes.every((scope) => UNITLESS_SCOPES.has(scope))) return 'none'
return variableNamespace(variable) === 'font-weight' ? 'none' : 'px'
}
/** Six decimals keep `rem` exact for every pixel step down to 1/1024px (0.5px is 0.03125rem). */
function trimNumber(value: number): string {
return String(Number(value.toFixed(6)))
}
/** A stored number written in its unit: 24 as `rem` is `1.5rem`, 150 as `ms` is `150ms`. */
export function tokenNumberToCSS(value: number, unit: TokenUnit): string {
const number = trimNumber(tokenNumberInUnit(value, unit))
if (unit === 'none' || number === '0') return number
return `${number}${unit}`
}