Skip to content

Commit

Permalink
figma font family transformer (#899)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasoppermann authored Apr 18, 2024
1 parent e032ac6 commit 26cae47
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/PrimerStyleDictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
durationToCss,
figmaAttributes,
fontFamilyToCss,
fontFamilyToFigma,
fontWeightToNumber,
jsonDeprecated,
namePathToDotNotation,
Expand Down Expand Up @@ -216,6 +217,11 @@ StyleDictionary.registerTransform({
...fontFamilyToCss,
})

StyleDictionary.registerTransform({
name: 'fontFamily/figma',
...fontFamilyToFigma,
})

/**
* @name {@link PrimerStyleDictionary}
* @description Returns style dictionary object with primer preset that includes parsers, formats and transformers
Expand Down
6 changes: 6 additions & 0 deletions src/platforms/figma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@ export const figma: PlatformInitializer = (outputFile, prefix, buildPath, option
'name/pathToFigma',
// 'name/pathToSlashNotation',
'figma/attributes',
'fontFamily/figma',
'dimension/pixelUnitless',
// 'border/figma',
// 'typography/figma',
'fontWeight/number',
],
options: {
basePxFontSize: 16,
fontFamilies: {
'fontStack/system': 'SF Pro Text',
'fontStack/sansSerif': 'SF Pro Text',
'fontStack/monospace': 'SF Mono',
},
...options,
},
files: [
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/fontFamilyToCss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const parseFontFamily = (value: unknown): string => {
* @description converts fontFamily tokens value to string
* @type value transformer — [StyleDictionary.ValueTransform](https://github.com/amzn/style-dictionary/blob/main/types/Transform.d.ts)
* @matcher matches all tokens of $type `fontFamily`
* @transformer returns a number
* @transformer returns a string
*/
export const fontFamilyToCss: StyleDictionary.Transform = {
type: `value`,
Expand Down
35 changes: 35 additions & 0 deletions src/transformers/fontFamilyToFigma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type StyleDictionary from 'style-dictionary'
import {isFontFamily} from '../filters'
/**
* takes a value and returns it if its a string or concats strings in an array quoting strings with spaces
* @param value
* @returns string
*/
export const parseFontFamily = (
token: StyleDictionary.TransformedToken,
fontFamilies: Record<string, string> = {},
): string => {
// return value from fontFamilies
if (token.name in fontFamilies) {
return fontFamilies[token.name]
}
// return string
if (typeof token.value === 'string') {
return token.value
}
// invalid value
throw new Error(`Invalid value ${token.value}, should be a string or array of strings`)
}
/**
* @description converts fontFamily tokens value to string
* @type value transformer — [StyleDictionary.ValueTransform](https://github.com/amzn/style-dictionary/blob/main/types/Transform.d.ts)
* @matcher matches all tokens of $type `fontFamily`
* @transformer returns a string
*/
export const fontFamilyToFigma: StyleDictionary.Transform = {
type: `value`,
transitive: true,
matcher: isFontFamily,
transformer: (token: StyleDictionary.TransformedToken, platform: StyleDictionary.Platform): string =>
parseFontFamily(token, platform.options?.fontFamilies),
}
1 change: 1 addition & 0 deletions src/transformers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export {dimensionToPixelUnitless} from './dimensionToPixelUnitless'
export {durationToCss} from './durationToCss'
export {figmaAttributes} from './figmaAttributes'
export {fontFamilyToCss} from './fontFamilyToCss'
export {fontFamilyToFigma} from './fontFamilyToFigma'
export {fontWeightToNumber} from './fontWeightToNumber'
export {jsonDeprecated} from './jsonDeprecated'
export {namePathToCamelCase} from './namePathToCamelCase'
Expand Down

0 comments on commit 26cae47

Please sign in to comment.