-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Add support for CSS Custom Properties #666
Comments
Ah yeah I've been meaning to investigate this! Since this is Preact and every byte is slaved over, what do you think about always using - node.style[i] = /* snip */ value[i];
+ node.style.setProperty(i, /* snip */ value[i]); It seems like that would enable CSS Custom Properties and even improve perf at the same time, and the cost would be really small. Thoughts? (edit: presumably we'd need to switch |
I think it's a 💯 good idea - there's only one downside that the React team ran into - you would have to use the normal, hyphenated CSS property names instead of the // this won't work
node.style.setProperty('backgroundColor', 'green');
// this will work
node.style.setProperty('background-color', 'green'); This means that when passing in styles as objects, such as: <div style={{ backgroundColor: 'green' }} /> you'd have to do some mapping or normalization of the property name to get the hyphenated name. Or you can just always require the hyphenated name -- it would just be a breaking change, though. |
ahhh interesting... probably have to keep react compat there. node.style.cssText = '--foo: green; background-color: var(--foo, red);' |
It does on Chrome, FireFox, Safari (haven't tested Edge yet) |
Does work on Edge. |
Interesting - so it's possible serializing to |
Is there any way I can help on this @developit? If you tell me which strategy to take I'd be happy to open a PR. |
@tkh44 It might be safest to use the same implementation that Facebook used (at least for starters): facebook/react#9302 |
@developit any news on this feature? I saw the PR got out of date with master... If there's anything I can do to help would love to as I just found myself looking for this feature/wishing I could set variables inline this morning |
This feature has just landed in |
Cross-references with React:
React will soon be able to support CSS Custom Properties (CSS Variables) by using
.style.setProperty(<prop>, <value>)
instead of directly modifying.style.<prop> = <value>
for custom properties (detected by seeing if the property starts with two dashes, e.g.,--foo-bar
).It seems that, for objects passed into the
style
attribute of Preact components, this is currently not the case, as it is still directly setting the style properties: https://github.com/developit/preact/blob/9e1dd185bb1d6505c35477d8101c886c25dc51c8/src/dom/index.js#L57Furthermore, the use of
.style.setProperty(prop, value)
seems to be much faster in most environments (except for IE9-11, where it is slightly slower): https://jsperf.com/dom-style-vs-setproperty - shouldn't matter anyway if you choose to use.setProperty
for only custom properties.The end-goal is to have this work:
What do you think? 😄 It's a fairly minor change and I can make a PR if needed.
The text was updated successfully, but these errors were encountered: