-
-
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
#666 support css variables #827
Conversation
src/dom/index.js
Outdated
@@ -52,10 +52,21 @@ export function setAccessor(node, name, old, value, isSvg) { | |||
} | |||
if (value && typeof value==='object') { | |||
if (typeof old!=='string') { | |||
for (let i in old) if (!(i in value)) node.style[i] = ''; | |||
for (let i in old) if (!(i in value)) { | |||
if (i.indexOf('--') === 0) { |
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.
I think we can always use setProperty()
/ removeProperty()
here, which saves a bunch of bytes. AFAIK style.setProperty('color', 'red')
is equivalent to style.color='red'
.
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.
just re-read the original thread, i was wrong since setProperty() uses CSS case rather than camelCase. poop! one option would be if (~i.indexOf('-'))
, which might be neat since it also enables support for css-case
in general.
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.
Damn GitHub collapsing comments. Missed this
Awesome. Regarding the testing, it's probably a good time to switch the default launcher to be Headless Chrome, I don't really use Phantom for Preact anymore since the performance numbers are unreliable. |
the bundle size thing didn't kick in, but it looks like this goes from One size thing I thought of - do you think |
According to MDN this should work |
What about using the ternary operator ?
|
@JeremyVSR pretty sure it gets minified like that anyways. But I think removing the |
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.
Untested -- assumes everything can be set via setProperty
, which looks to be the case at quick glance.
@@ -58,10 +58,21 @@ export function setAccessor(node, name, old, value, isSvg) { | |||
} | |||
if (value && typeof value==='object') { | |||
if (typeof old!=='string') { | |||
for (let i in old) if (!(i in value)) node.style[i] = ''; | |||
for (let i in old) if (!(i in value)) { |
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.
Challenge: https://twitter.com/preactjs/status/998981752130588673
if (value && typeof value==='object') {
let i; // placeholder
if (typeof old!=='string') {
for (i in old) {
if (!(i in value)) node.style.setProperty(i, '');
}
}
for (i in value) {
node.style.setProperty(i, typeof value[i]==='number' && IS_NON_DIMENSIONAL.test(i)===false ? (value[i]+'px') : value[i]);
}
}
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.
This would be backwards incompatible. Doesn't work for properties that are defined using camel-casing.
node.style.backgroundColor = value; // works
node.style['background-color'] = value; // works
node.style.setProperty('background-color', value); // works
node.style.setProperty('backgroundColor', value); // doesn't work
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.
@PepsRyuu aye, saw this after 🙈 /delete
@tkh44 Do we need the if (value && typeof value==='object') {
if (typeof old!=='string') {
for (let i in old) if (!(i in value)) {
node.style.setProperty(i, '');
node.style[i] = '';
}
}
for (let i in value) {
node.style.setProperty(i, value[i]);
node.style[i] = typeof value[i]==='number' && IS_NON_DIMENSIONAL.test(i)===false ? (value[i]+'px') : value[i];
}
} |
@andrewiggins that should work. There was a bug in the older code that I just realized.
needs to be in both the setProperty and the assignment. abstracting it would look like
Not sure where this puts us in terms of bytes. |
@@ -83,10 +83,21 @@ export function setAccessor(node, name, old, value, isSvg) { | |||
} | |||
if (value && typeof value==='object') { | |||
if (typeof old!=='string') { | |||
for (let i in old) if (!(i in value)) node.style[i] = ''; | |||
for (let i in old) if (!(i in value)) { | |||
if (~i.indexOf('-')) { |
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.
We should check what the size is when doing if (i[0]=='-')
- we only care about using setProperty for custom properties, so it'll always be a leading --
.
Closing, we finally have support for css variables in Preact X 🎉💯 |
This is most likely unacceptable as far as size goes, but I wanted to get the process started.
Note that PhantomJS does not support cssText with custom properties in it. No idea what to do about this other than to drop css vars from that test.
fixes #666