Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transforms): adds 'px to rem' transform #491

Merged
merged 4 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions __tests__/common/transforms.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,28 @@ describe('common', () => {
});
});

describe('size/pxToRem', () => {
jbarreiros marked this conversation as resolved.
Show resolved Hide resolved
const pxToRemTransformer = transforms["size/pxToRem"].transformer;

it('converts pixel to rem', () => {
expect(pxToRemTransformer({value: '12px'})).toBe('0.75rem');
});
it('converts pixel to rem using custom base font', () => {
expect(pxToRemTransformer({value: '14px'}, {basePxFontSize: 14})).toBe('1rem');
});
it('nonzero rem value not changed', () => {
expect(pxToRemTransformer({value: '2rem'})).toBe('2rem');
});
['0px', '0', '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({
Expand Down
14 changes: 14 additions & 0 deletions docs/transforms.md
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,20 @@ Scales the number by 16 (default web font size) and adds 'px' to the end.
```


* * *

### size/pxToRem
jbarreiros marked this conversation as resolved.
Show resolved Hide resolved

Scales the pixel value to rem, and adds 'rem' to the end of non-zero values. If you define a "basePxFontSize" on the platform in your config, it will be used to scale the pixel value, otherwise 16 (default web font size) will be used.

```js
// Matches: prop.attributes.category === 'size'
// Returns:
"0"
"1rem"
```


* * *

### content/icon
Expand Down
34 changes: 34 additions & 0 deletions lib/common/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,40 @@ module.exports = {
}
},

/**
* Scales the pixel value to rem, and adds 'rem' to the end of non-zero values. If you define a "basePxFontSize" on the platform in your config, it will be used to scale the pixel value, otherwise 16 (default web font size) will be used.
*
* ```js
* // Matches: prop.attributes.category === 'size'
* // Returns:
* "0"
* "1rem"
* ```
*/
'size/pxToRem': {
type: 'value',
matcher: (prop) => prop.attributes.category === 'size',
jbarreiros marked this conversation as resolved.
Show resolved Hide resolved
transformer: (prop, options) => {
const baseFont = (options && options.basePxFontSize) || 16;
jbarreiros marked this conversation as resolved.
Show resolved Hide resolved
const floatVal = parseFloat(prop.value);

if (isNaN(floatVal)) {
throwSizeError(prop.name, prop.value, 'rem');
}

if (floatVal === 0) {
return '0';
}

if (String(prop.value).indexOf('px') === -1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the spirit of this check, but no other transforms right now check for a unit, making this an outlier. What are your thoughts on removing this check in this transform, and then creating some more generic size transforms that look for units and apply the proper transform? I think it would be good to have these more intelligent transforms for people that want to specify borders in px but everything else in rem.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I can drop the check. Using parseFloat allows safely ignoring the unit if it is present.

I'm not entirely sure what you mean by "[create] some more generic size transforms that look for units and apply the proper transform"? Do you mean something like font/remToPx, where matcher: isFontSize? That's the only example that I can think of.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking of a transform that would transform web-based values (in either px or rem) and covert them to dp/sp on Android and pts (CGFloats) on iOS. And it would do a check like above to tell whether the number should be directly translated (px are roughly equivalent to dp/sp/pt) or multiplied by the basePxFontSize for rem units. That way someone could define sizes in pixels and rems and not need 2 separate transforms. Does that make sense? Maybe I just need to write a PR with some example code...

Copy link
Contributor Author

@jbarreiros jbarreiros Dec 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this?

name matcher notes
size/webToSp isFontSize() if "px", replace "px" with "sp"
if "rem", scale and append "sp"
size/webToDp isNotFontSize() if "px", replace "px" with "dp"
if "rem", scale and append "dp"
size/webToPt isSize() if "px", replace "px" with "pt"
if "rem", scale and append "pt"

The one thing I can't wrap my head around is from your original comment...

I think it would be good to have these more intelligent transforms for people that want to specify borders in px but everything else in rem.

Looking at existing transforms:

  • *ToSp always uses isFontSize, so it is already specific to fonts.
  • *ToDp always uses isNotFontSize, so, I think, it only applies to borders, spacing, etc.
  • *ToPt aways uses isSize, so it applies to everything.

Android appears to already handle making a distinction between fonts and borders. Not entirely sure how the same can be done to ios, or the web for that matter?

Apologies, I have zero experience with andoid and ios development, which I think is causing me all kinds of confusion.

Sidenote, I dropped the "px" check.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your table is exactly what I was imagining. Right now the existing transforms assume all sizes are either defined in rem or px, but could never be a combination of them. For example I could not define my borders in px and my font size and paddings in rem because the transforms assume all sizes are one or the other (using size/remToSp and size/remToDp or size/pxToSp and size/pxToDp). With the current transforms I could have fonts in rems, and paddings and borders in px, but I couldn't have font and padding in rem and borders in pixels. Hopefully that makes sense 🥴

Copy link
Contributor Author

@jbarreiros jbarreiros Dec 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think I'm getting confused because I'm trying to apply the size/webTo* transforms to my scenario, when I think they might not actually apply.

In my scenario, the design system has everything in pixels. Our only target for now is the web (no mobile) and all values are to be converted to rems. Therefore, I want to use something like size/pxToRem to avoid manually performing the px-to-rem conversions.

If I'm understanding your comments, another scenario is a design system that defines mixed units. In that case, the expectation would be that any rem values would have been manually converted prior to being dropped into a property.json file? And, therefore, size/pxToRem would not be used at the same time as size/webTo*? Is that correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what I'm ultimately trying to get at... is adding the size/webTo* transforms appropriate for this PR, or should they be the star of their own PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should address it in a separate PR, I think this one is good enough as it is!

// ignore anything without a "px" unit
return prop.value;
}

return `${floatVal / baseFont}rem`;
}
},

/**
* Takes a unicode point and transforms it into a form CSS can use.
*
Expand Down