Skip to content

Commit e88e47d

Browse files
awearyflarnie
authored andcommitted
Use setProperty when setting style properties (facebook#9302)
* Use setProperty when setting style properties setProperty is faster in all/most modern browsers. It also lets us support CSS variables. * Only use setProperty when setting CSS variables * Add test to ensure setting CSS variables do not warn * Make this PR pretty again * Run fiber test script
1 parent 4ed44a0 commit e88e47d

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/renderers/dom/shared/CSSPropertyOperations.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ if (__DEV__) {
128128
* @param {ReactDOMComponent} component
129129
*/
130130
var warnValidStyle = function(name, value, component) {
131+
// Don't warn for CSS variables
132+
if (name.indexOf('--') === 0) {
133+
return;
134+
}
131135
var owner;
132136
if (component) {
133137
owner = component._currentElement._owner;
@@ -215,7 +219,9 @@ var CSSPropertyOperations = {
215219
if (styleName === 'float' || styleName === 'cssFloat') {
216220
styleName = styleFloatAccessor;
217221
}
218-
if (styleValue) {
222+
if (styleName.indexOf('--') === 0) {
223+
style.setProperty(styleName, styleValue);
224+
} else if (styleValue) {
219225
style[styleName] = styleValue;
220226
} else {
221227
var expansion =

src/renderers/dom/shared/__tests__/CSSPropertyOperations-test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,18 @@ describe('CSSPropertyOperations', () => {
253253
'Check the render method of `Comp`.',
254254
);
255255
});
256+
257+
it('should not warn when setting CSS variables', () => {
258+
class Comp extends React.Component {
259+
render() {
260+
return <div style={{'--foo-primary': 'red', backgroundColor: 'red'}} />;
261+
}
262+
}
263+
264+
spyOn(console, 'error');
265+
var root = document.createElement('div');
266+
ReactDOM.render(<Comp />, root);
267+
268+
expect(console.error.calls.count()).toBe(0);
269+
});
256270
});

0 commit comments

Comments
 (0)