Skip to content

Commit

Permalink
Use setProperty when setting style properties (facebook#9302)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
aweary authored and flarnie committed Jun 7, 2017
1 parent 4ed44a0 commit e88e47d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/renderers/dom/shared/CSSPropertyOperations.js
Expand Up @@ -128,6 +128,10 @@ if (__DEV__) {
* @param {ReactDOMComponent} component
*/
var warnValidStyle = function(name, value, component) {
// Don't warn for CSS variables
if (name.indexOf('--') === 0) {
return;
}
var owner;
if (component) {
owner = component._currentElement._owner;
Expand Down Expand Up @@ -215,7 +219,9 @@ var CSSPropertyOperations = {
if (styleName === 'float' || styleName === 'cssFloat') {
styleName = styleFloatAccessor;
}
if (styleValue) {
if (styleName.indexOf('--') === 0) {
style.setProperty(styleName, styleValue);
} else if (styleValue) {
style[styleName] = styleValue;
} else {
var expansion =
Expand Down
14 changes: 14 additions & 0 deletions src/renderers/dom/shared/__tests__/CSSPropertyOperations-test.js
Expand Up @@ -253,4 +253,18 @@ describe('CSSPropertyOperations', () => {
'Check the render method of `Comp`.',
);
});

it('should not warn when setting CSS variables', () => {
class Comp extends React.Component {
render() {
return <div style={{'--foo-primary': 'red', backgroundColor: 'red'}} />;
}
}

spyOn(console, 'error');
var root = document.createElement('div');
ReactDOM.render(<Comp />, root);

expect(console.error.calls.count()).toBe(0);
});
});

0 comments on commit e88e47d

Please sign in to comment.