-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type StyleDictionary from 'style-dictionary' | ||
import {isNumber} from '../filters' | ||
/** | ||
* takes a value and returns it if its a px string if it is a float and the token has a fontSize value in extensions['org.primer.data'] | ||
* @param value | ||
* @returns string | ||
*/ | ||
export const convertFloatToPixel = (token: StyleDictionary.TransformedToken) => { | ||
// 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 `${convertedValue}px` | ||
} | ||
/** | ||
* @description converts a float value to a pixel value based on the provided fontSize on the tokersn extension | ||
* @type value transformer — [StyleDictionary.ValueTransform](https://github.com/amzn/style-dictionary/blob/main/types/Transform.d.ts) | ||
* @matcher matches all tokens of $type `isNumber` | ||
* @transformer returns a pixel string | ||
*/ | ||
export const floatToPixel: StyleDictionary.Transform = { | ||
type: `value`, | ||
transitive: true, | ||
matcher: isNumber, | ||
transformer: (token: StyleDictionary.TransformedToken): string => convertFloatToPixel(token), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters