diff --git a/__tests__/common/transforms.test.js b/__tests__/common/transforms.test.js index 691e2ef2..012dfa3c 100644 --- a/__tests__/common/transforms.test.js +++ b/__tests__/common/transforms.test.js @@ -544,6 +544,27 @@ describe('common', () => { }); }); + describe('size/pxToRem', () => { + const pxToRemTransformer = transforms["size/pxToRem"].transformer; + + ['12', '12px', '12rem'].forEach((value) => { + it(`ignoring unit, scales "${value}" to rem`, () => { + expect(pxToRemTransformer({value})).toBe('0.75rem'); + }); + }); + it('converts pixel to rem using custom base font', () => { + expect(pxToRemTransformer({value: '14px'}, {basePxFontSize: 14})).toBe('1rem'); + }); + ['0', '0px', '0rem'].forEach((value) => { + it(`zero value "${value}" is returned without a unit`, () => { + expect(pxToRemTransformer({value})).toBe('0'); + }); + }); + it('should throw an error if prop value is Nan', () => { + expect( () => pxToRemTransformer({value: "a"})).toThrow(); + }); + }); + describe('size/rem', () => { it('should work', () => { var value = transforms["size/rem"].transformer({ diff --git a/lib/common/transforms.js b/lib/common/transforms.js index 5343309d..e971de4c 100644 --- a/lib/common/transforms.js +++ b/lib/common/transforms.js @@ -696,6 +696,35 @@ module.exports = { } }, + /** + * Scales non-zero numbers to rem, and adds 'rem' to the end. If you define a "basePxFontSize" on the platform in your config, it will be used to scale the value, otherwise 16 (default web font size) will be used. + * + * ```js + * // Matches: prop.attributes.category === 'size' + * // Returns: + * "0" + * "1rem" + * ``` + */ + 'size/pxToRem': { + type: 'value', + matcher: isSize, + transformer: (prop, options) => { + const baseFont = (options && options.basePxFontSize) || 16; + const floatVal = parseFloat(prop.value); + + if (isNaN(floatVal)) { + throwSizeError(prop.name, prop.value, 'rem'); + } + + if (floatVal === 0) { + return '0'; + } + + return `${floatVal / baseFont}rem`; + } + }, + /** * Takes a unicode point and transforms it into a form CSS can use. *