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

Allow conversion of rem font size to pixels in typography presets #1304

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion libs/@guardian/design-tokens/src/tokens.json
Original file line number Diff line number Diff line change
Expand Up @@ -1537,7 +1537,7 @@
"fontSize": "{typography.fontSize.14}",
"lineHeight": "{typography.lineHeight.regular}",
"fontWeight": "{typography.fontWeight.regular}",
"fontStyle": "Italic"
"fontStyle": "italic"
}
},
"textSansItalic15": {
Expand Down
2 changes: 1 addition & 1 deletion libs/@guardian/design-tokens/tokens.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1444,7 +1444,7 @@ export declare const tokens: {
readonly fontSize: '14px';
readonly lineHeight: 1.3;
readonly fontWeight: 400;
readonly fontStyle: 'Italic';
readonly fontStyle: 'italic';
};
readonly textSansItalic15: {
readonly fontFamily: readonly [
Expand Down
2 changes: 1 addition & 1 deletion libs/@guardian/design-tokens/tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -1440,7 +1440,7 @@ export const tokens = {
fontSize: '14px',
lineHeight: 1.3,
fontWeight: 400,
fontStyle: 'Italic',
fontStyle: 'italic',
},
textSansItalic15: {
fontFamily: [
Expand Down
2 changes: 1 addition & 1 deletion libs/@guardian/design-tokens/variables.css
Original file line number Diff line number Diff line change
Expand Up @@ -1410,7 +1410,7 @@
--source-typography-presets-textSansItalic14-font-size: var(
--source-typography-fontSize-14
);
--source-typography-presets-textSansItalic14-font-style: Italic;
--source-typography-presets-textSansItalic14-font-style: italic;
--source-typography-presets-textSansItalic14-font-weight: var(
--source-typography-fontWeight-regular
);
Expand Down
4 changes: 2 additions & 2 deletions libs/@guardian/source-foundations/src/tokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1870,7 +1870,7 @@ describe('Typography preset CSS output', () => {
font-size: 0.875rem;
line-height: 1.3;
font-weight: 400;
font-style: Italic;
font-style: italic;
--source-text-decoration-thickness: 2px;
`,
{ isFragment: true },
Expand Down Expand Up @@ -2681,7 +2681,7 @@ describe('Typography preset object output', () => {
fontSize: '0.875rem',
lineHeight: 1.3,
fontWeight: 400,
fontStyle: 'Italic',
fontStyle: 'italic',
});
expect(typePresetObject.textSansItalic15Object).toEqual({
fontFamily:
Expand Down
2 changes: 1 addition & 1 deletion libs/@guardian/source-foundations/src/typography/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ export const textSansItalic14 = `
font-size: 0.875rem;
line-height: 1.3;
font-weight: 400;
font-style: Italic;
font-style: italic;
--source-text-decoration-thickness: 2px;
`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ export const textSansItalic14Object = {
fontSize: '0.875rem',
lineHeight: 1.3,
fontWeight: 400,
fontStyle: 'Italic',
fontStyle: 'italic',
} as const;

export const textSansItalic15Object = {
Expand Down
9 changes: 9 additions & 0 deletions libs/@guardian/source-foundations/src/typography/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,12 @@ export type Fs = <
unit: ScaleUnit;
},
) => TypographyStyles;

export type TypographyPreset = `
font-family: ${string};
font-size: ${number}rem;
line-height: ${number};
font-weight: ${number};
font-style: ${'normal' | 'italic'};
--source-text-decoration-thickness: ${number}px;
`;
30 changes: 30 additions & 0 deletions libs/@guardian/source-foundations/src/utils/typography.test.ts
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 },
);
});
});
16 changes: 16 additions & 0 deletions libs/@guardian/source-foundations/src/utils/typography.ts
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/;
Copy link
Contributor

@mxdvl mxdvl Mar 28, 2024

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!

Suggested change
const REGEX_FONT_SIZE = /font-size:\s(\d+\.\d+)rem/;
const REGEX_FONT_SIZE = /font-size:\s(\d+\.?\d*)rem/;

Explore in Regex101

image


const matches = preset.match(REGEX_FONT_SIZE);
if (matches?.[1]) {
const pxVal = parseFloat(matches[1]) * 16;
Copy link
Contributor

Choose a reason for hiding this comment

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

This nombre magique is exposed as rootPixelFontSize in ./convert-value. Shall we pull it in here?

return preset.replace(REGEX_FONT_SIZE, `font-size: ${pxVal}px`);
}

return preset;
Comment on lines +9 to +15
Copy link
Contributor

@mxdvl mxdvl Mar 28, 2024

Choose a reason for hiding this comment

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

You can also use a replacer function as part of String.prototype.replace, which could make this simpler:

return preset.replace(REGEX_FONT_SIZE, (_, match) => {
  const pxValue = parseFloat(match) * 16;
  return `font-size: ${pxValue}px`;
})

};