-
Notifications
You must be signed in to change notification settings - Fork 3
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
Allow conversion of rem font size to pixels in typography presets #1304
Changes from all commits
4565985
e9e1cdc
ef957b7
5ed321a
d4e7754
751bc9b
7a98a77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { headlineBold24, titlepiece42 } from '../typography/css'; | ||
import { presetToPx } from './typography'; | ||
|
||
describe('presetToPx', () => { | ||
it('should convert preset font size value from rem to px', () => { | ||
expect(presetToPx(headlineBold24)).toMatchCSS( | ||
` | ||
font-family: "GH Guardian Headline", "Guardian Egyptian Web", Georgia, serif; | ||
font-size: 24px; | ||
line-height: 1.15; | ||
font-weight: 700; | ||
font-style: normal; | ||
--source-text-decoration-thickness: 3px; | ||
`, | ||
{ isFragment: true }, | ||
); | ||
|
||
expect(presetToPx(titlepiece42)).toMatchCSS( | ||
` | ||
font-family: "GT Guardian Titlepiece", Georgia, serif; | ||
font-size: 42px; | ||
line-height: 1.15; | ||
font-weight: 700; | ||
font-style: normal; | ||
--source-text-decoration-thickness: 5px; | ||
`, | ||
{ isFragment: true }, | ||
); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { TypographyPreset } from '../typography/types'; | ||
|
||
/* | ||
* Convert `font-size` value in typography preset from rem to px | ||
*/ | ||
export const presetToPx = (preset: TypographyPreset) => { | ||
const REGEX_FONT_SIZE = /font-size:\s(\d+\.\d+)rem/; | ||
|
||
const matches = preset.match(REGEX_FONT_SIZE); | ||
if (matches?.[1]) { | ||
const pxVal = parseFloat(matches[1]) * 16; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This nombre magique is exposed as |
||
return preset.replace(REGEX_FONT_SIZE, `font-size: ${pxVal}px`); | ||
} | ||
|
||
return preset; | ||
Comment on lines
+9
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can also use a replacer function as part of return preset.replace(REGEX_FONT_SIZE, (_, match) => {
const pxValue = parseFloat(match) * 16;
return `font-size: ${pxValue}px`;
}) |
||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you might have an issue if the value does not contain a decimal part… Thankfully we currently do not use 16px, 32px or 48px as a font size, but we might in the future!
Explore in Regex101