CSS Variables (Custom Properties) 💅#612
Conversation
| 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.
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.
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> |

Usage:
Refs: