diff --git a/libs/@guardian/design-tokens/src/tokens.json b/libs/@guardian/design-tokens/src/tokens.json index 704d2bdb0..e636d7a3f 100644 --- a/libs/@guardian/design-tokens/src/tokens.json +++ b/libs/@guardian/design-tokens/src/tokens.json @@ -1537,7 +1537,7 @@ "fontSize": "{typography.fontSize.14}", "lineHeight": "{typography.lineHeight.regular}", "fontWeight": "{typography.fontWeight.regular}", - "fontStyle": "Italic" + "fontStyle": "italic" } }, "textSansItalic15": { diff --git a/libs/@guardian/design-tokens/tokens.d.ts b/libs/@guardian/design-tokens/tokens.d.ts index 6cc89de9d..2cf19f477 100644 --- a/libs/@guardian/design-tokens/tokens.d.ts +++ b/libs/@guardian/design-tokens/tokens.d.ts @@ -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 [ diff --git a/libs/@guardian/design-tokens/tokens.js b/libs/@guardian/design-tokens/tokens.js index 91ae76d49..24b562efe 100644 --- a/libs/@guardian/design-tokens/tokens.js +++ b/libs/@guardian/design-tokens/tokens.js @@ -1440,7 +1440,7 @@ export const tokens = { fontSize: '14px', lineHeight: 1.3, fontWeight: 400, - fontStyle: 'Italic', + fontStyle: 'italic', }, textSansItalic15: { fontFamily: [ diff --git a/libs/@guardian/design-tokens/variables.css b/libs/@guardian/design-tokens/variables.css index 57492a7c1..ac42bf5e7 100644 --- a/libs/@guardian/design-tokens/variables.css +++ b/libs/@guardian/design-tokens/variables.css @@ -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 ); diff --git a/libs/@guardian/source-foundations/src/tokens.test.ts b/libs/@guardian/source-foundations/src/tokens.test.ts index 1949430ca..3136b215f 100644 --- a/libs/@guardian/source-foundations/src/tokens.test.ts +++ b/libs/@guardian/source-foundations/src/tokens.test.ts @@ -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 }, @@ -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: diff --git a/libs/@guardian/source-foundations/src/typography/css.ts b/libs/@guardian/source-foundations/src/typography/css.ts index 1a9532a45..0eb43fcb6 100644 --- a/libs/@guardian/source-foundations/src/typography/css.ts +++ b/libs/@guardian/source-foundations/src/typography/css.ts @@ -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; `; diff --git a/libs/@guardian/source-foundations/src/typography/objects.ts b/libs/@guardian/source-foundations/src/typography/objects.ts index 8327bd06a..4fc4ce739 100644 --- a/libs/@guardian/source-foundations/src/typography/objects.ts +++ b/libs/@guardian/source-foundations/src/typography/objects.ts @@ -741,7 +741,7 @@ export const textSansItalic14Object = { fontSize: '0.875rem', lineHeight: 1.3, fontWeight: 400, - fontStyle: 'Italic', + fontStyle: 'italic', } as const; export const textSansItalic15Object = { diff --git a/libs/@guardian/source-foundations/src/typography/types.ts b/libs/@guardian/source-foundations/src/typography/types.ts index 70865d6e8..0a830f934 100644 --- a/libs/@guardian/source-foundations/src/typography/types.ts +++ b/libs/@guardian/source-foundations/src/typography/types.ts @@ -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; +`; diff --git a/libs/@guardian/source-foundations/src/utils/typography.test.ts b/libs/@guardian/source-foundations/src/utils/typography.test.ts new file mode 100644 index 000000000..4e233e2aa --- /dev/null +++ b/libs/@guardian/source-foundations/src/utils/typography.test.ts @@ -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 }, + ); + }); +}); diff --git a/libs/@guardian/source-foundations/src/utils/typography.ts b/libs/@guardian/source-foundations/src/utils/typography.ts new file mode 100644 index 000000000..080bd49b8 --- /dev/null +++ b/libs/@guardian/source-foundations/src/utils/typography.ts @@ -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; + return preset.replace(REGEX_FONT_SIZE, `font-size: ${pxVal}px`); + } + + return preset; +};