You can see some more of my examples here: https://twitter.com/James0x57/status/1283912525248069632 https://twitter.com/James0x57/status/1283596399196680192 https://twitter.com/James0x57/status/1282303255826046977
The second version of augmented-ui is packed to the brim with it, I've been working on it for months :D
[0] https://twitter.com/James0x57/status/1283909866915074048
[1] https://www.w3.org/TR/css-variables-1/#invalid-variables
When I tried other strings (my name, "null", other CSS "words" like opacity) it broke.
I wonder if this is because the initial variable definition needs to be valid css in order to be replaced?
I had already been using spaces as a fallback value to do dynamic composition of standard properties ( https://twitter.com/James0x57/status/1233246818118533120 ) so it wasn't too far off to combine it with the "initial" behavior that causes the entire var dependency tree to evaluate to initial, allowing fallback behavior from any point in the tree. :)
I filed bugs to get it fixed though: https://bugzilla.mozilla.org/show_bug.cgi?id=1630488 (not fixed yet)
https://bugs.chromium.org/p/chromium/issues/detail?id=107129... (fixed!)
And minifiers hate it. lol
Should be fixed in FF sooner or later though, I reported it in April
So I made this example: https://jsbin.com/meqibawotu/1/edit?html,css,output
We have two "toggle vars", `--is-hovered` and `--is-special`, which are triggered by a hover selector and a class, respectively, though they could be triggered by anything (like a media query or JS).
The "false" value for the toggle is "initial", so at the top I set:
The first trick is that assigning a variable value as a single space token is valid according to spec (https://www.w3.org/TR/css-variables-1/#syntax), making the var(...) usage substitute a single space. So if our variable should be "true", we use whatever method to set it to a single space: The second trick is that an invalid property value will fall back to the second argument of the `var()` function. So we first create a property that only has a valid value if the flag is true (a single space): If the div is hovered, `--is-hovered` is a single space, so the property value becomes: If the div isn't hovered, `--is-hovered` is `initial`, so the property value becomes: Which is invalid CSS!We can then interpret this in our final opacity property:
If the div is hovered, the first argument is evaluated to: And since that's valid syntax, the second argument is short-circuited (ignored). However, if the div isn't hovered, then the opacity property is computed as: The first argument is invalid! So the property falls back to the second argument, and we get `var(--regular-opacity)`.