Skip to content

Commit 858c842

Browse files
bedakbgaearon
authored andcommitted
Don't hyphenate custom CSS properties for ReactDOMServer (#16167)
* Do not hyphenate custom CSS property * Move check into the processStyleName fn * Formatting * add test * Put isCustomProperty check after conditional return * add test to `ReactDOMServerIntegration` and supress warning * Don't indexOf twice * Simpler fix
1 parent d412eec commit 858c842

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

packages/react-dom/src/__tests__/CSSPropertyOperations-test.js

+9
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ describe('CSSPropertyOperations', () => {
5757
expect(html).toContain('"-ms-transition:none;-moz-transition:none"');
5858
});
5959

60+
it('should not hyphenate custom CSS property', () => {
61+
const styles = {
62+
'--someColor': '#000000',
63+
};
64+
const div = <div style={styles} />;
65+
const html = ReactDOMServer.renderToString(div);
66+
expect(html).toContain('"--someColor:#000000"');
67+
});
68+
6069
it('should set style attribute when styles exist', () => {
6170
const styles = {
6271
backgroundColor: '#000',

packages/react-dom/src/__tests__/ReactDOMServerIntegrationAttributes-test.js

+5
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,11 @@ describe('ReactDOMServerIntegration', () => {
401401
expect(e.style.Foo).toBe('5');
402402
});
403403

404+
itRenders('camel cased custom properties', async render => {
405+
const e = await render(<div style={{'--someColor': '#000000'}} />);
406+
expect(e.style.SomeColor).toBe('#000000');
407+
});
408+
404409
itRenders('no undefined styles', async render => {
405410
const e = await render(
406411
<div style={{color: undefined, width: '30px'}} />,

packages/react-dom/src/server/ReactPartialRenderer.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,10 @@ function createMarkupForStyles(styles): string | null {
231231
}
232232
}
233233
if (styleValue != null) {
234-
serialized += delimiter + processStyleName(styleName) + ':';
234+
serialized +=
235+
delimiter +
236+
(isCustomProperty ? styleName : processStyleName(styleName)) +
237+
':';
235238
serialized += dangerousStyleValue(
236239
styleName,
237240
styleValue,

packages/react-dom/src/shared/CSSPropertyOperations.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ export function createDangerousStringForStyles(styles) {
3535
const styleValue = styles[styleName];
3636
if (styleValue != null) {
3737
const isCustomProperty = styleName.indexOf('--') === 0;
38-
serialized += delimiter + hyphenateStyleName(styleName) + ':';
38+
serialized +=
39+
delimiter +
40+
(isCustomProperty ? styleName : hyphenateStyleName(styleName)) +
41+
':';
3942
serialized += dangerousStyleValue(
4043
styleName,
4144
styleValue,

0 commit comments

Comments
 (0)