-
Notifications
You must be signed in to change notification settings - Fork 782
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
CSS Variables (Custom Properties) 💅 #612
Conversation
FYI, you also can use <main style={{ cssText: '--foo:red' }}>
<h1 style={{ color: 'var(--foo)' }}>Hello</h1>
<button style={{ backgroundColor: 'var(--foo)' }}>+</button>
</main> |
test/dom.test.js
Outdated
tree: h("div", { style: { color: "blue", float: "left" } }), | ||
tree: h("div", { | ||
style: { color: "blue", float: "left", "--foo": "blue" } | ||
}), | ||
html: `<div style="color: blue; float: left;"></div>` |
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 like jsdom does not support custom properties yet =/
test('example', () => {
const div = document.createElement('div')
div.style.setProperty('--foo', 'red')
document.body.appendChild(div)
console.log(document.body.innerHTML) // => '<div></div>'
console.log(div.style.getPropertyValue('--foo')) // => undefined
expect(document.body.innerHTML).toBe('<div style="--foo:red"></div>') // => fail
expect(div.style.getPropertyValue('--foo')).toBe('red') // => fail
})
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.
Google Search crawler is based on Chrome 41, which does NOT support CSS Custom Properties: https://twitter.com/ebidel/status/968989651888295936
Codecov Report
@@ Coverage Diff @@
## master #612 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 1 1
Lines 152 155 +3
Branches 48 49 +1
=====================================
+ Hits 152 155 +3
Continue to review full report at Codecov.
|
@frenzzy What's the total byte increase? |
|
@frenzzy Sounds like a reasonable compromise. Is there anything else to add? Does this cover everything? What about
Probably a lot more code, but it would be cool haha. |
According to custom properties specification the only requirement is any custom property name must starts with two dashes. So, what notation to use ( Symbol |
You are right.
Right, but if we (just an idea) treated names starting with a { $cssVariable: "red" } ...instead of: { "--cssVariable": "red" } But yeah, I'd probably be crucified if we introduced this convenient oddball. |
For once, I am not totally against breaking the standard here as you point out |
How about this? const brandColor = '--brand-color'
const $brandColor = `var(${brandColor})`
const view = () => (
<main style={{ [brandColor]: 'red' }}>
<h1 style={{ color: $brandColor }}>Hello</h1>
<button style={{ backgroundColor: $brandColor }}>+</button>
</main>
) |
@frenzzy What about rewriting the code like this? for (var i in clone(oldValue, value)) {
if (i[0] === "-") {
element[name].setProperty(i, value)
} else {
element[name][i] = value == null || value[i] == null ? "" : value[i]
}
} Or what about using only for (var i in clone(oldValue, value)) {
element[name].setProperty(i, value == null || value[i] == null ? "" : value[i])
} |
f3c71b3
to
7cacaea
Compare
Test |
@frenzzy I think we can use
|
5a5d83b
to
85de826
Compare
But I suggest stick to the current convention of snale case in CSS and camel case in JS PS I only just found out you can set CSS vars easily in JS. no need to mess about manipulating stylesheets in JS. |
@SteveALee We went with the --no-surprise version of CSS variables! 😉 <div
style={{
"--color": "red",
color: "var(--color)"
}}
>
OK
</div> |
Totally Agree. Just making a note for users of HA :) |
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.
LGTM
Usage:
Refs: