Skip to content

Commit

Permalink
converting line height to pixel value
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasoppermann committed Apr 19, 2024
1 parent 108d613 commit c920fd9
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-seas-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/primitives': patch
---

Convert lineHeight from float to pixel value for figma
6 changes: 6 additions & 0 deletions src/PrimerStyleDictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
shadowToCss,
typographyToCss,
dimensionToRemPxArray,
convertWithFontSizeToPixel,
} from './transformers'
import {
javascriptCommonJs,
Expand Down Expand Up @@ -132,6 +133,11 @@ StyleDictionary.registerTransform({
...colorToHex,
})

StyleDictionary.registerTransform({
name: 'convertWithFontSize/pixel',
...convertWithFontSizeToPixel,
})

StyleDictionary.registerTransform({
name: 'dimension/rem',
...dimensionToRem,
Expand Down
1 change: 1 addition & 0 deletions src/platforms/figma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const figma: PlatformInitializer = (outputFile, prefix, buildPath, option
// 'name/pathToSlashNotation',
'figma/attributes',
'fontFamily/figma',
'convertWithFontSize/pixel',
'dimension/pixelUnitless',
// 'border/figma',
// 'typography/figma',
Expand Down
24 changes: 21 additions & 3 deletions src/tokens/functional/typography/typography.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,34 @@
"text": {
"display": {
"lineBoxHeight": {
"$value": "56px",
"$type": "dimension"
"$value": 1.4,
"$type": "number",
"$extensions": {
"org.primer.data": {
"fontSize": 40
},
"org.primer.figma": {
"collection": "typography",
"scopes": ["all"]
}
}
},
"size": {
"$value": "40px",
"$type": "dimension"
},
"lineHeight": {
"$value": 1.4,
"$type": "number"
"$type": "number",
"$extensions": {
"org.primer.data": {
"fontSize": 40
},
"org.primer.figma": {
"collection": "typography",
"scopes": ["all"]
}
}
},
"weight": {
"$value": "{base.text.weight.medium}",
Expand Down
34 changes: 34 additions & 0 deletions src/transformers/convertWithFontSizeToPixel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type StyleDictionary from 'style-dictionary'
import {isNumber} 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 convertNumberWithFontSize = (token: StyleDictionary.TransformedToken, unitless?: boolean) => {
// short circut if value is not a number
if (
typeof token.value !== 'number' ||
!token.$extensions?.['org.primer.data']?.fontSize ||
typeof token.$extensions?.['org.primer.data']?.fontSize !== 'number'
) {
return token.value
}
// convert value
const convertedValue = token.$extensions?.['org.primer.data']?.fontSize * token.value
// return converted value
return unitless ? convertedValue : `${convertedValue}px`
}
/**
* @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 convertWithFontSizeToPixel: StyleDictionary.Transform = {
type: `value`,
transitive: true,
matcher: isNumber,
transformer: (token: StyleDictionary.TransformedToken, platform: StyleDictionary.Platform): string =>
convertNumberWithFontSize(token, platform.options?.unitless ?? false),
}
1 change: 1 addition & 0 deletions src/transformers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export {colorToHexAlpha} from './colorToHexAlpha'
export {colorToHexMix} from './colorToHexMix'
export {colorToRgbAlpha} from './colorToRgbAlpha'
export {colorToRgbaFloat} from './colorToRgbaFloat'
export {convertWithFontSizeToPixel} from './convertWithFontSizeToPixel'
export {dimensionToRem} from './dimensionToRem'
export {dimensionToRemPxArray} from './dimensionToRemPxArray'
export {dimensionToPixelUnitless} from './dimensionToPixelUnitless'
Expand Down

0 comments on commit c920fd9

Please sign in to comment.