11import { StyleToken } from './ast' ;
22
3- export const CUSTOM_UNITS : Record < string , string | ( ( key : string ) => string ) > =
4- {
5- r : 'var(--radius)' ,
6- bw : 'var(--border-width)' ,
7- ow : 'var(--outline-width)' ,
8- x : 'var(--gap)' ,
9- fs : 'var(--font-size)' ,
10- lh : 'var(--line-height)' ,
11- rp : 'var(--rem-pixel)' ,
12- gp : 'var(--column-gap)' ,
13- } ;
14-
15- export function renderCustomUnit ( token : StyleToken ) {
16- const converter =
17- CUSTOM_UNITS [ token . unit as unknown as keyof typeof CUSTOM_UNITS ] ;
3+ export type CustomUnitMap = Record < string , string | ( ( key : string ) => string ) > ;
4+
5+ export function renderCustomUnit ( token : StyleToken , units ?: CustomUnitMap ) {
6+ units = units || { } ;
7+
8+ const converter = token . unit ? units [ token . unit ] : undefined ;
189
1910 if ( ! converter ) {
2011 return token . value ;
@@ -33,18 +24,24 @@ export function renderCustomUnit(token: StyleToken) {
3324
3425const INSTANT_VALUES = [ ')' , ',' ] ;
3526
36- export function renderStyleTokens ( tokens : StyleToken [ ] ) {
27+ /**
28+ * Render a style tokens to a string.
29+ */
30+ export function renderStyleTokens (
31+ tokens : StyleToken [ ] ,
32+ { units } : { units ?: CustomUnitMap } = { } ,
33+ ) {
3734 let result = '' ;
3835
3936 tokens . forEach ( ( token ) => {
4037 if ( INSTANT_VALUES . includes ( token . value ) ) {
4138 result += token . value ;
4239 } else if ( token . type === 'func' ) {
4340 result += token . children
44- ? `${ token . value } (${ renderStyleTokens ( token . children ) } )`
41+ ? `${ token . value } (${ renderStyleTokens ( token . children , units ) } )`
4542 : '' ;
4643 } else if ( token . type === 'value' ) {
47- result += renderCustomUnit ( token ) ;
44+ result += renderCustomUnit ( token , units ) ;
4845 } else if ( token . type === 'property' ) {
4946 result += `var(--${ token . value . slice ( 1 ) } )` ;
5047 } else {
0 commit comments