@primer/primitives
Version:
Typography, spacing, and color primitives for Primer design system
40 lines (39 loc) • 1.84 kB
JavaScript
import { isNumber } from '../filters/index.js';
/**
* 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, unitless = false) => {
var _a, _b, _c, _d, _e, _f;
// short circut if value is not a number
if (typeof token.value !== 'number' ||
!((_b = (_a = token.$extensions) === null || _a === void 0 ? void 0 : _a['org.primer.data']) === null || _b === void 0 ? void 0 : _b.fontSize) ||
typeof ((_d = (_c = token.$extensions) === null || _c === void 0 ? void 0 : _c['org.primer.data']) === null || _d === void 0 ? void 0 : _d.fontSize) !== 'number') {
return token.value;
}
// convert value
const convertedValue = ((_f = (_e = token.$extensions) === null || _e === void 0 ? void 0 : _e['org.primer.data']) === null || _f === void 0 ? void 0 : _f.fontSize) * token.value;
// return converted value
return convertedValue === 0 ? 0 : unitless ? Math.round(convertedValue) : `${Math.round(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 = {
name: 'float/pixel',
type: 'value',
transitive: true,
filter: isNumber,
transform: (token) => convertFloatToPixel(token),
};
export const floatToPixelUnitless = {
name: 'float/pixelUnitless',
type: 'value',
transitive: true,
filter: isNumber,
transform: (token) => convertFloatToPixel(token, true),
};