Skip to content

Commit

Permalink
fix: media queries computing (#1910)
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomura authored Jul 3, 2022
1 parent 3acf53b commit e94e50a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-pets-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@react-pdf/layout': patch
---

fix: media queries computing
6 changes: 4 additions & 2 deletions packages/layout/src/steps/resolveStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ const resolveNodeStyles = container => node => {
*/
const resolvePageStyles = page => {
const dpi = page.props?.dpi || 72;
const box = page.box || page.style;
const container = { ...box, dpi };
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 };

return resolveNodeStyles(container)(page);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,35 @@ Object {
}
`;

exports[`layout resolveStyles Should resolve nested node styles media queries with page style 1`] = `
Object {
"children": Array [
Object {
"children": Array [
Object {
"style": Object {
"backgroundColor": "green",
},
"type": "VIEW",
},
Object {
"style": Object {
"backgroundColor": "red",
},
"type": "VIEW",
},
],
"style": Object {
"height": 200,
"width": 100,
},
"type": "PAGE",
},
],
"type": "DOCUMENT",
}
`;

exports[`layout resolveStyles Should resolve page styles 1`] = `
Object {
"children": Array [
Expand Down
35 changes: 35 additions & 0 deletions packages/layout/tests/steps/resolveStyles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,41 @@ describe('layout resolveStyles', () => {
expect(result).toMatchSnapshot();
});

test('Should resolve nested node styles media queries with page style', () => {
const root = {
type: 'DOCUMENT',
children: [
{
type: 'PAGE',
style: { width: 100, height: 200 },
children: [
{
type: 'VIEW',
style: {
backgroundColor: 'red',
'@media max-width: 500': {
backgroundColor: 'green',
},
},
},
{
type: 'VIEW',
style: {
backgroundColor: 'red',
'@media min-width: 500': {
backgroundColor: 'green',
},
},
},
],
},
],
};
const result = resolveStyles(root);

expect(result).toMatchSnapshot();
});

test('Should resolve nested node styles array', () => {
const root = {
type: 'DOCUMENT',
Expand Down

0 comments on commit e94e50a

Please sign in to comment.