Skip to content

Commit 6375faf

Browse files
committed
feat: make nested text inherit background colors (#2504)
* feat: make nested text inherit background colors * chore: add changeset
1 parent c423f9f commit 6375faf

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

.changeset/cold-tips-work.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@react-pdf/layout': patch
3+
---
4+
5+
feat: nested text inherit background color

packages/layout/src/steps/resolveInheritance.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as P from '@react-pdf/primitives';
22
import { pick, compose } from '@react-pdf/fns';
33

4-
const INHERITED_PROPERTIES = [
4+
const BASE_INHERITABLE_PROPERTIES = [
55
'color',
66
'fontFamily',
77
'fontSize',
@@ -17,8 +17,15 @@ const INHERITED_PROPERTIES = [
1717
'wordSpacing',
1818
];
1919

20+
const TEXT_INHERITABLE_PROPERTIES = [
21+
...BASE_INHERITABLE_PROPERTIES,
22+
'backgroundColor',
23+
];
24+
2025
const isSvg = node => node.type === P.Svg;
2126

27+
const isText = node => node.type === P.Text;
28+
2229
// Merge style values
2330
const mergeValues = (styleName, value, inheritedValue) => {
2431
switch (styleName) {
@@ -67,9 +74,14 @@ const mergeStyles = inheritedStyles => node => {
6774
*/
6875
const resolveInheritance = node => {
6976
if (isSvg(node)) return node;
77+
7078
if (!node.children) return node;
7179

72-
const inheritStyles = pick(INHERITED_PROPERTIES, node.style || {});
80+
const inheritableProperties = isText(node)
81+
? TEXT_INHERITABLE_PROPERTIES
82+
: BASE_INHERITABLE_PROPERTIES;
83+
84+
const inheritStyles = pick(inheritableProperties, node.style || {});
7385

7486
const resolveChild = compose(resolveInheritance, mergeStyles(inheritStyles));
7587

packages/layout/tests/steps/resolveInhritance.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,32 @@ describe('layout resolveInheritance', () => {
111111
);
112112
});
113113

114+
test('Should inherit background color for text childs', () => {
115+
const root = {
116+
type: 'DOCUMENT',
117+
children: [
118+
{
119+
type: 'PAGE',
120+
style: {},
121+
children: [
122+
{
123+
type: 'TEXT',
124+
style: { backgroundColor: 'red' },
125+
children: [{ type: 'TEXT' }],
126+
},
127+
],
128+
},
129+
],
130+
};
131+
132+
const result = resolveInheritance(root);
133+
const text1 = result.children[0].children[0];
134+
const text2 = text1.children[0];
135+
136+
expect(text1.style).toHaveProperty('backgroundColor', 'red');
137+
expect(text2.style).toHaveProperty('backgroundColor', 'red');
138+
});
139+
114140
test('Should inherit color value', shouldInherit('color'));
115141
test('Should inherit fontFamily value', shouldInherit('fontFamily'));
116142
test('Should inherit fontSize value', shouldInherit('fontSize'));

0 commit comments

Comments
 (0)