11import * as R from 'ramda' ;
2- import type { Format , TransformedToken } from 'style-dictionary/types' ;
2+ import type { Dictionary , Format , TransformedToken } from 'style-dictionary/types' ;
33import { createPropertyFormatter } from 'style-dictionary/utils' ;
4- import { basePxFontSize } from '../../configs/shared.js' ;
54import { buildOptions } from '../../platform.js' ;
65import { sizingTemplate } from './size.js' ;
76
@@ -10,29 +9,42 @@ const isTypographyFontFamilyToken = R.allPass([
109 R . pathSatisfies ( R . includes ( 'typography' ) , [ 'path' ] ) ,
1110 R . pathSatisfies ( R . includes ( 'fontFamily' ) , [ 'path' ] ) ,
1211] ) ;
12+ // Predicate to filter font-scale tokens
13+ const isFontScaleToken = R . pathSatisfies ( R . includes ( 'font-scale' ) , [ 'path' ] ) ;
1314
1415type TokensWithCalcAndRoundFormatting = { tokens : TransformedToken [ ] ; calc : string [ ] ; round : string [ ] } ;
1516
1617const formatTypographySizeToken = (
18+ dictionary : Dictionary ,
1719 format : ( t : TransformedToken ) => string ,
1820 token : TransformedToken ,
1921) : { name : string ; calc : string ; round : string } => {
2022 const [ name , value ] = format ( token ) . replace ( / ; $ / , '' ) . split ( ': ' ) ;
23+
2124 let calc : string ;
2225 let round : string | undefined ;
2326 if ( R . startsWith ( [ 'font-size' ] , token . path ) ) {
24- calc = `calc(${ value } * var(--_ds-font-size-factor))` ;
27+ const originalWithCssReference = ( token . original . $value as string ) . replaceAll ( / \{ f o n t - s c a l e \. [ ^ } ] + \} / g, ( match ) => {
28+ const t = dictionary . unfilteredTokenMap ?. get ( match ) ;
29+ return `var(--${ t ?. name as string } )` ;
30+ } ) ;
31+ const cssCalcValue = originalWithCssReference . replace ( / ^ r o u n d T o \( ( .* ) , 0 \) $ / , '$1' ) ;
32+ calc = `calc(1rem * ${ cssCalcValue } )` ;
2533 round = `round(${ calc } , 1px)` ;
2634 } else {
2735 calc = value ;
2836 }
2937 return { name, calc, round : round ?? calc } ;
3038} ;
3139
32- const formatTypographySizeTokens = ( format : ( t : TransformedToken ) => string , tokens : TransformedToken [ ] ) =>
40+ const formatTypographySizeTokens = (
41+ dictionary : Dictionary ,
42+ format : ( t : TransformedToken ) => string ,
43+ tokens : TransformedToken [ ] ,
44+ ) =>
3345 R . reduce < TransformedToken , TokensWithCalcAndRoundFormatting > (
3446 ( acc , token ) => {
35- const { name, calc, round } = formatTypographySizeToken ( format , token ) ;
47+ const { name, calc, round } = formatTypographySizeToken ( dictionary , format , token ) ;
3648 acc . tokens . push ( token ) ;
3749 acc . calc . push ( `${ name } : ${ calc } ;` ) ;
3850 acc . round . push ( `${ name } : ${ round } ;` ) ;
@@ -56,8 +68,8 @@ export const typeScale: Format = {
5668 usesDtcg,
5769 } ) ;
5870
59- const filteredTokens = R . reject ( R . anyPass ( [ isTypographyFontFamilyToken ] ) , dictionary . allTokens ) ;
60- const formattedTokens = formatTypographySizeTokens ( format , filteredTokens ) ;
71+ const filteredTokens = R . reject ( R . anyPass ( [ isTypographyFontFamilyToken , isFontScaleToken ] ) , dictionary . allTokens ) ;
72+ const formattedTokens = formatTypographySizeTokens ( dictionary , format , filteredTokens ) ;
6173
6274 const formattedMap = formattedTokens . round . map ( ( t , i ) => ( {
6375 token : formattedTokens . tokens [ i ] ,
@@ -66,8 +78,7 @@ export const typeScale: Format = {
6678
6779 buildOptions . buildTokenFormats [ destination ] = formattedMap ;
6880
69- const sizeFactor = ` --_ds-font-size-factor: calc(var(--ds-size-mode-font-size) / (var(--ds-size-base) / ${ basePxFontSize } ));` ;
70- const content = `${ selector } {\n${ sizeFactor } ${ sizingTemplate ( formattedTokens ) } \n}` ;
81+ const content = `${ selector } {${ sizingTemplate ( formattedTokens ) } \n}` ;
7182 const body = R . isNotNil ( layer ) ? `@layer ${ layer } {\n${ content } \n}` : content ;
7283
7384 return body ;
0 commit comments