-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pixel-to-rem): adding pxToRem helper (#190)
Co-authored-by: Michael Amaral <[email protected]>
- Loading branch information
Showing
7 changed files
with
404 additions
and
202 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,22 @@ | ||
# `pxToRem` | ||
|
||
Convert `px` metric to `rem`, using an optional baseline as argument. | ||
|
||
## Arguments | ||
|
||
- `pixel: number`: value to be converted | ||
- `base?: number`: optional baseline, normally 16 | ||
|
||
## Returns | ||
|
||
- `rem: string`: The result between the pixel and base calculation | ||
|
||
## Usage | ||
|
||
```jsx | ||
import { pxToRem } from '@platformbuilders/helpers'; | ||
|
||
pxToRem(20); // return '1.25rem' | ||
|
||
pxToRem(20, 20); // return '1rem' | ||
``` |
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,24 @@ | ||
import { pxToRem } from '../pixelToRem'; | ||
|
||
describe('pixelToRem helpers tests', () => { | ||
it('should return a formatted rem', () => { | ||
// given | ||
const pixelValue = 20; | ||
const expected = '1.25rem'; | ||
// when | ||
const formatted = pxToRem(pixelValue); | ||
// then | ||
expect(formatted).toBe(expected); | ||
}); | ||
|
||
it('should return a formatted rem using baseValue', () => { | ||
// given | ||
const pixelValue = 20; | ||
const baseValue = 20; | ||
const expected = '1rem'; | ||
// when | ||
const formatted = pxToRem(pixelValue, baseValue); | ||
// then | ||
expect(formatted).toBe(expected); | ||
}); | ||
}); |
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,3 @@ | ||
export const pxToRem = (pixels: number, baseline = 16): string => { | ||
return `${pixels / baseline}rem`; | ||
}; |
Oops, something went wrong.