by JaneOri on 7/17/20, 1:14 AM with 45 comments
by James0x57 on 7/17/20, 1:14 AM
I've created a couple JS Bins if you want to play around with it a bit, resize the "output" pane to see it update:
Minimal example: https://jsbin.com/giqedowale/edit?css,output
More involved example: https://jsbin.com/yicuqujehe/1/edit?html,css,output
If you're familiar with CSS, it may seem like it's impossible for this to work without scripts, but while working on another project I realized in the CSS Spec for custom properties that combining a few small details they've highlighted, in a specific order, it makes it possible to do all kinds of things that have never been possible before in CSS. This is the first project I've released using the idea.
Anyway, it's totally free and open source too so hack away if you'd like! https://github.com/propjockey/css-media-vars
I'll probably be refreshing this page for a while, so I'm happy to answer any questions or dive into the CSS Spec with you and talk about the tech if you're curious!
by Rotten194 on 7/17/20, 3:54 AM
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:
div {
--is-hovered: initial;
--is-special: initial;
}
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: div:hover {
--is-hovered: ;
}
div.special {
--is-special: ;
}
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): --hover-opacity: var(--is-hovered) 1.0;
If the div is hovered, `--is-hovered` is a single space, so the property value becomes: --hover-opacity: 1.0; // note two spaces
If the div isn't hovered, `--is-hovered` is `initial`, so the property value becomes: --hover-opacity: initial 1.0;
Which is invalid CSS!We can then interpret this in our final opacity property:
--regular-opacity: 0.5;
--opacity: var(--hover-opacity, var(--regular-opacity));
If the div is hovered, the first argument is evaluated to: --opacity: var( 1.0, var(--regular-opacity));
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: --opacity: var(initial 1.0, var(--regular-opacity));
The first argument is invalid! So the property falls back to the second argument, and we get `var(--regular-opacity)`.by cosmotic on 7/17/20, 2:08 AM
by LukeBMM on 7/17/20, 2:58 PM
by delgaudm on 7/17/20, 3:52 AM
by 9935c101ab17a66 on 7/17/20, 7:07 AM
Couple of nice-to-haves: - More examples (though I'm guessing this is really new, and they will come in time). - Guidance on best practices for variable naming (when an element has multiple responsive properties). I just ended up doing `--sm-property-name` for each variable.
Are you looking for contributors? I intend to use this approach for a project over the next few days, and I could help with the documentation.
by russdpale on 7/17/20, 8:13 PM
No build step should be the future of web development. The more steps that are added that require yet more tools to learn and more dependencies to maintain, the less inclusive and creative the web becomes. Hopefully we will one day be able to look back on this time period as an evil necessity.. I mean writing es6 and building to es5 is way better a lot of the time from a development perspective but on the face of it, its absurd and its why tooling gives people fatigue and turns away new promising developers for better programming languages.
I would definitely provide a way to download the css without npm first. Like big worm said, its about principalities.
by satvikpendem on 7/17/20, 6:03 AM
by tmpfs on 7/17/20, 4:55 AM
by sm4sh on 7/17/20, 4:44 AM
by ketzo on 7/17/20, 3:20 AM
by captn3m0 on 7/17/20, 9:52 AM
by PudgePacket on 7/17/20, 3:36 AM
by deltron3030 on 7/17/20, 5:01 AM