From a074293e005d7a31ea8468e5f221dc4ee78f74fb Mon Sep 17 00:00:00 2001 From: Diego Muracciole Date: Sun, 17 Nov 2024 19:06:30 +0100 Subject: [PATCH 1/3] feat: support rem units --- packages/layout/src/steps/resolveStyles.js | 3 ++- packages/stylesheet/src/transform/units.js | 4 +++- packages/stylesheet/tests/resolve.test.js | 12 ++++++++++++ packages/stylesheet/tests/transform.test.js | 2 +- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/layout/src/steps/resolveStyles.js b/packages/layout/src/steps/resolveStyles.js index b94637432..a3dbc26e8 100644 --- a/packages/layout/src/steps/resolveStyles.js +++ b/packages/layout/src/steps/resolveStyles.js @@ -60,7 +60,8 @@ export const resolvePageStyles = (page) => { const width = page.box?.width || page.style.width; const height = page.box?.height || page.style.height; const orientation = page.props?.orientation || 'portrait'; - const container = { width, height, orientation, dpi }; + const remBase = page.style?.fontSize || 18; + const container = { width, height, orientation, dpi, remBase }; return resolveNodeStyles(container)(page); }; diff --git a/packages/stylesheet/src/transform/units.js b/packages/stylesheet/src/transform/units.js index 01ec54b2e..2e675ac4e 100644 --- a/packages/stylesheet/src/transform/units.js +++ b/packages/stylesheet/src/transform/units.js @@ -5,7 +5,7 @@ * @returns {Object} parsed value */ const parseValue = (value) => { - const match = /^(-?\d*\.?\d+)(in|mm|cm|pt|vh|vw|px)?$/g.exec(value); + const match = /^(-?\d*\.?\d+)(in|mm|cm|pt|vh|vw|px|rem)?$/g.exec(value); return match ? { value: parseFloat(match[1]), unit: match[2] || 'pt' } @@ -27,6 +27,8 @@ const transformUnit = (container, value) => { const cmFactor = (1 / 2.54) * dpi; switch (scalar.unit) { + case 'rem': + return scalar.value * (container.remBase || 18); case 'in': return scalar.value * dpi; case 'mm': diff --git a/packages/stylesheet/tests/resolve.test.js b/packages/stylesheet/tests/resolve.test.js index ee978975c..c75e8fbea 100644 --- a/packages/stylesheet/tests/resolve.test.js +++ b/packages/stylesheet/tests/resolve.test.js @@ -581,4 +581,16 @@ describe('stylesheet resolve', () => { expect(styles).toEqual({ fontSize: 10 }); }); + + test('should resolve rem units correctly', () => { + const styles = resolve({ remBase: 10 }, { fontSize: '2rem' }); + + expect(styles).toEqual({ fontSize: 20 }); + }); + + test('should resolve rem units when base not specificed', () => { + const styles = resolve({}, { fontSize: '2rem' }); + + expect(styles).toEqual({ fontSize: 36 }); + }); }); diff --git a/packages/stylesheet/tests/transform.test.js b/packages/stylesheet/tests/transform.test.js index 30a61c073..09182c3db 100644 --- a/packages/stylesheet/tests/transform.test.js +++ b/packages/stylesheet/tests/transform.test.js @@ -2,7 +2,7 @@ import { describe, expect, test } from 'vitest'; import _transformStyles from '../src/transform'; -const CONTAINER = { width: 200, height: 400 }; +const CONTAINER = { width: 200, height: 400, remBase: 10 }; const transformStyles = _transformStyles(CONTAINER); From 8ec351547d440d0188a25450d36b5d19e3f2b4a1 Mon Sep 17 00:00:00 2001 From: Diego Muracciole Date: Sun, 17 Nov 2024 19:07:40 +0100 Subject: [PATCH 2/3] chore: add changeset --- .changeset/famous-kings-dress.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/famous-kings-dress.md diff --git a/.changeset/famous-kings-dress.md b/.changeset/famous-kings-dress.md new file mode 100644 index 000000000..33b26da08 --- /dev/null +++ b/.changeset/famous-kings-dress.md @@ -0,0 +1,6 @@ +--- +"@react-pdf/layout": patch +"@react-pdf/stylesheet": patch +--- + +feat: support rem units From 37763821cb18fe2653151334a91ef93158f19e66 Mon Sep 17 00:00:00 2001 From: Diego Muracciole Date: Sun, 17 Nov 2024 19:08:17 +0100 Subject: [PATCH 3/3] chore: make stylesheet minor bump --- .changeset/famous-kings-dress.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/famous-kings-dress.md b/.changeset/famous-kings-dress.md index 33b26da08..a028fef30 100644 --- a/.changeset/famous-kings-dress.md +++ b/.changeset/famous-kings-dress.md @@ -1,6 +1,6 @@ --- "@react-pdf/layout": patch -"@react-pdf/stylesheet": patch +"@react-pdf/stylesheet": minor --- feat: support rem units