Skip to content

Commit 757bd86

Browse files
Andaristemmatown
authored andcommitted
Fix issue with camelCased custom properties not being handled correctly in object styles (#1378)
1 parent 7dd888f commit 757bd86

File tree

5 files changed

+38
-5
lines changed

5 files changed

+38
-5
lines changed

.changeset/a9126a8f/changes.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"releases": [{ "name": "@emotion/serialize", "type": "patch" }],
3+
"dependents": []
4+
}

.changeset/a9126a8f/changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed issue with camelCased custom properties not being handled correctly in object styles

packages/core/__tests__/__snapshots__/css.js.snap

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ exports[`css call composition 1`] = `
3636
/>
3737
`;
3838

39+
exports[`handles camelCased custom properties in object styles properly 1`] = `
40+
.emotion-0 {
41+
--textColor: green;
42+
color: var(--textColor);
43+
}
44+
45+
<div
46+
className="emotion-0"
47+
/>
48+
`;
49+
3950
exports[`label in css call 1`] = `
4051
.emotion-0 {
4152
color: hotpink;

packages/core/__tests__/css.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,16 @@ test('child selector array', () => {
188188

189189
expect(tree.toJSON()).toMatchSnapshot()
190190
})
191+
192+
test('handles camelCased custom properties in object styles properly', () => {
193+
const tree = renderer.create(
194+
<div
195+
css={{
196+
'--textColor': 'green',
197+
color: 'var(--textColor)'
198+
}}
199+
/>
200+
)
201+
202+
expect(tree.toJSON()).toMatchSnapshot()
203+
})

packages/serialize/src/index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ import memoize from '@emotion/memoize'
1111
let hyphenateRegex = /[A-Z]|^ms/g
1212
let animationRegex = /_EMO_([^_]+?)_([^]*?)_EMO_/g
1313

14-
const processStyleName = memoize((styleName: string) =>
15-
styleName.replace(hyphenateRegex, '-$&').toLowerCase()
14+
const isCustomProperty = (property: string) => property.charCodeAt(1) === 45
15+
16+
const processStyleName = memoize(
17+
(styleName: string) =>
18+
isCustomProperty(styleName)
19+
? styleName
20+
: styleName.replace(hyphenateRegex, '-$&').toLowerCase()
1621
)
1722

1823
let processStyleValue = (
@@ -41,7 +46,7 @@ let processStyleValue = (
4146

4247
if (
4348
unitless[key] !== 1 &&
44-
key.charCodeAt(1) !== 45 && // custom properties
49+
!isCustomProperty(key) &&
4550
typeof value === 'number' &&
4651
value !== 0
4752
) {
@@ -88,11 +93,10 @@ if (process.env.NODE_ENV !== 'production') {
8893
}
8994

9095
const processed = oldProcessStyleValue(key, value)
91-
const isCssVariable = key.charCodeAt(1) === 45
9296

9397
if (
9498
processed !== '' &&
95-
!isCssVariable &&
99+
!isCustomProperty(key) &&
96100
key.indexOf('-') !== -1 &&
97101
hyphenatedCache[key] === undefined
98102
) {

0 commit comments

Comments
 (0)