-
Notifications
You must be signed in to change notification settings - Fork 47k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use setProperty when setting style properties #9302
Conversation
It seems like going down this route would inevitably involve a change from camelCase to hyphenation by convention. Probably including React Native then. |
@sebmarkbage I'm a fan of dropping the camelCase conventions if we can going forward (we can transform them for initial releases with warnings) for RN too. Keeping them closer to true "CSS" helps reduce friction. |
It’s more awkward to type though unless you provide some compile-time helper. Also more awkward to transform in JS. |
@gaearon We could do something nice at compile-time to help people here. We could also simply avoid passing an object literal too and let people pass in a block of |
Personally, I prefer the camelCase convention as its consistent with the DOM prop APIs like |
@aweary In my opinion, this has always been one of the harder things for newcomers to understand, especially |
Why not do the name conversion at the JSX compiler level?
|
Because you can't know something is a style if it's passed around. |
Can we consider accepting this without updating the public API for now? Assuming the cost of transforming the names is minimal (they're memoized so it should be in most cases). It would close #6411 |
We need to benchmark this. I don't think we'll want to take a significant hit. The memoized forms still needs to be looked up in a map which is not free. If we want to support #6411 a safer bet might be to just check if it starts with |
@sebmarkbage I'll benchmark before we move any further. |
I think for now it makes sense to call .setProperty for names starting with -- and leave everything else the same. |
@spicyj @sebmarkbage I've updated it so that only properties starting with |
setProperty is faster in all/most modern browsers. It also lets us support CSS variables.
Have you verified this works in a browser? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me but please verify it works in the browser.
@gaearon I did, I tested in in Chrome, Firefox and Safari on macOS and both styles with CSS variables and styles without were set as expected. I also tested in IE9-11 and Android 4 and verified the styles that didn't use CSS variables were being set as expected. |
great, feel free to merge :) |
@aweary You could do the following to also support both camelCase and dash-cased styles. var style = props.style;
for (...) {
if (name in DOMStyleObject) {
DOMStyleObject[name] = style[name];
} else {
DOMStyleObject.setProperty(name, style[name]);
}
} Edit: though ^^ might not be the best for performance. |
* 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
Work in progress. Resolves #6411
Using
setProperty
does incur the cost of parsing the style name assetProperty
requires the hyphenated property name (background-color
notbackgroundColor
). I'm also not sure if we want to special case IE given the performance benefits mentioned #6411 (comment)cc @trueadm @sebmarkbage @spicyj