Skip to content

Commit 351c1e0

Browse files
felthyHenri
authored andcommitted
Nested function values not working in React (#1144)
* Add failing tests to demonstrate nested dynamic rule issue * Fix nested dynamic styles
1 parent 808acb4 commit 351c1e0

3 files changed

Lines changed: 56 additions & 17 deletions

File tree

packages/react-jss/.size-snapshot.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
{
22
"dist/react-jss.js": {
3-
"bundled": 168517,
4-
"minified": 58191,
5-
"gzipped": 19016
3+
"bundled": 168992,
4+
"minified": 58283,
5+
"gzipped": 19042
66
},
77
"dist/react-jss.min.js": {
8-
"bundled": 111849,
9-
"minified": 41586,
10-
"gzipped": 14098
8+
"bundled": 112324,
9+
"minified": 41678,
10+
"gzipped": 14127
1111
},
1212
"dist/react-jss.cjs.js": {
13-
"bundled": 26550,
14-
"minified": 11551,
15-
"gzipped": 3816
13+
"bundled": 27019,
14+
"minified": 11643,
15+
"gzipped": 3854
1616
},
1717
"dist/react-jss.esm.js": {
18-
"bundled": 25588,
19-
"minified": 10716,
20-
"gzipped": 3693,
18+
"bundled": 26057,
19+
"minified": 10808,
20+
"gzipped": 3733,
2121
"treeshaked": {
2222
"rollup": {
2323
"code": 1841,

packages/react-jss/src/utils/sheets.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,20 @@ export const addDynamicRules = (sheet: StyleSheet, data: any): ?DynamicRules =>
113113
// Loop over each dynamic rule and add it to the stylesheet
114114
for (const key in meta.dynamicStyles) {
115115
const name = `${key}-${meta.dynamicRuleCounter++}`
116-
const rule = sheet.addRule(name, meta.dynamicStyles[key])
116+
const initialRuleCount = sheet.rules.index.length
117117

118-
sheet.update(name, data)
118+
const originalRule = sheet.addRule(name, meta.dynamicStyles[key])
119119

120-
if (rule) {
121-
rules[key] = rule
120+
// Loop through all created rules, fixes updating dynamic rules
121+
for (let i = initialRuleCount; i < sheet.rules.index.length; i++) {
122+
const rule = sheet.rules.index[i]
123+
124+
// $FlowFixMe: Not sure why flow has an issue here
125+
sheet.update(rule.key, data)
126+
127+
// If it's the original rule, we need to add it by the correct key so the hook and hoc
128+
// can correctly concat the dynamic class with the static one
129+
rules[originalRule === rule ? key : rule.key] = rule
122130
}
123131
}
124132

packages/react-jss/test-utils/createDynamicStylesTests.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ export default ({createStyledComponent}) => {
2828
{
2929
button: {
3030
color: 'rgb(255, 255, 255)',
31-
height: ({height = 1}) => `${height}px`
31+
height: ({height = 1}) => `${height}px`,
32+
'&::before': {
33+
content: '""',
34+
height: ({height = 1}) => `${height}px`
35+
}
3236
}
3337
},
3438
{name: 'NoRenderer'}
@@ -100,12 +104,21 @@ export default ({createStyledComponent}) => {
100104
.button-0 {
101105
color: rgb(255, 255, 255);
102106
}
107+
.button-0::before {
108+
content: "";
109+
}
103110
.button-0-1 {
104111
height: 10px;
105112
}
113+
.button-0-1::before {
114+
height: 10px;
115+
}
106116
.button-1-2 {
107117
height: 20px;
108118
}
119+
.button-1-2::before {
120+
height: 20px;
121+
}
109122
`)
110123
})
111124

@@ -124,12 +137,21 @@ export default ({createStyledComponent}) => {
124137
.button-0 {
125138
color: rgb(255, 255, 255);
126139
}
140+
.button-0::before {
141+
content: "";
142+
}
127143
.button-0-1 {
128144
height: 10px;
129145
}
146+
.button-0-1::before {
147+
height: 10px;
148+
}
130149
.button-1-2 {
131150
height: 20px;
132151
}
152+
.button-1-2::before {
153+
height: 20px;
154+
}
133155
`)
134156

135157
renderer.update(<Container height={20} />)
@@ -138,12 +160,21 @@ export default ({createStyledComponent}) => {
138160
.button-0 {
139161
color: rgb(255, 255, 255);
140162
}
163+
.button-0::before {
164+
content: "";
165+
}
141166
.button-0-1 {
142167
height: 20px;
143168
}
169+
.button-0-1::before {
170+
height: 20px;
171+
}
144172
.button-1-2 {
145173
height: 40px;
146174
}
175+
.button-1-2::before {
176+
height: 40px;
177+
}
147178
`)
148179
})
149180

0 commit comments

Comments
 (0)