top | item 23865900

Show HN: A completely different way to write responsive, vanilla, CSS

248 points| JaneOri | 5 years ago |propjockey.github.io

45 comments

order
[+] James0x57|5 years ago|reply
I know the docs need a little love but I think this has potential to be a game changer for writing responsive CSS. It has named breakpoints, lets you write DRY selectors, doesn't rely on JS or any sort of build step, and if you're already used to seeing --css-variables it's easier to read/maintain too.

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!

[+] progx|5 years ago|reply
For vanilla css-writer it could be a game changer, but who writes vanilla css in 2020? SASS make it so easy to write complex css simple.

css-media-vars would be a game changer for me, if it will be possible to use one var and not define many varnames and put them together to one var.

Like your Example:

  --xs-width: var(--media-xs) 100%;
  --sm-width: var(--media-sm) 49%;
  --md-width: var(--media-md) 32%;
  --lg-width: var(--media-gte-lg) 24%;
  width: var(--xs-width, var(--sm-width, var(--md-width, var(--lg-width))));
If it would work something like this, it would be really awesome:

  --width: var(--media-xs) 100%;
  --width: ifundefvar(--media-sm) 49%;
  --width: ifundefvar(--media-md) 32%;
  --width: ifundefvar(--media-gte-lg) 24%;
  width: var(--width);
Or

  --width: var(--media-xs) 100%
    else var(--media-sm) 49%
    else var(--media-md) 32%
    else var(--media-gte-lg) 24%
    else 5%
  width: var(--width);
[+] agloeregrets|5 years ago|reply
The upsell on browser support is a little misleading. Sure, it’s 94% worldwide supported..but what isn’t mentioned is that the 6% is entirely IE11 and down and that the browser support numbers are inaccurate as they don’t include places like China or sorts where IE spin-offs are extremely common.

A much better way to say it is “supports almost all browsers except IE” it’s accurate (as this does support almost all browsers in the world) while details what may be a major hangup for a business user.

[+] JoshTriplett|5 years ago|reply
Very impressive approach!

Do you have plans to build a full framework based on this? (Other than augmented-ui?)

[+] swyx|5 years ago|reply
congrats, i think this is a very good idea.

the AND and OR logic syntax a little clunky to me. i'd love something more first class from the CSSWG in accommodating this.

[+] Rotten194|5 years ago|reply
So I think I understand how this works? Correct me if I'm wrong...

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)`.
[+] James0x57|5 years ago|reply
YEP! Absolutely nailed it. That's what's up! I've been calling it Space Toggle. It does all kinds of stuff beyond toggle though - the whole world of conditional logic is possible. You can combine them in any way, &&, ||, !, it's all possible.

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

[+] rkagerer|5 years ago|reply
dear god, how did we get here

(Neat trick I'm just weary of web languages that in practice feel like they're working against clarity)

[+] cosmotic|5 years ago|reply
When I clicked to see vanilla CSS, an NPM package was not something I expected to see.
[+] PudgePacket|5 years ago|reply
Seems like it's just being used as a distribution method here.
[+] LukeBMM|5 years ago|reply
This a profoundly creative and clever idea which I hope to never see in any actual use, ever. I really appreciate the ingenuity, but I can't help but feel its legibility and maintainability are nightmarish.
[+] 9935c101ab17a66|5 years ago|reply
This is really cool. As I was writing some media queries today, I was yearning for a simpler solution.

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.

[+] russdpale|5 years ago|reply
This is awesome, build steps in CSS are absurd if you really sit there and think about what the purpose of the browser is compared to what it used to be..

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.

[+] satvikpendem|5 years ago|reply
Making CSS Turing complete, nice. Now we can run entire websites with functionality even if we turn JS off. Maybe we'll even see CSS only programming frameworks.
[+] onion2k|5 years ago|reply
I wrote a game in CSS to demonstrate what you can do with it - https://codepen.io/onion2k/pen/qBbKYee (no mobile because I was too lazy to make it responsive). CSS is very powerful, but you need to do some weird stuff to get interactivity.
[+] tmpfs|5 years ago|reply
This is really smart, good job. I look forward to taking this for a spin, the point of not adding a build step is critical for some of my current work. I think the time for using CSS preprocessors is over, they add very little value now we can use variables and combined with tricks like these we can speed up our builds!
[+] sm4sh|5 years ago|reply
Am I not getting this or is this just a slightly different way of using breakpoints like bootstrap some years ago? I think we've come full circle.
[+] esperent|5 years ago|reply
What makes this one different is that, for example, if you want to set the width, you just do it once. Other approaches (including bootstrap) you have to type "width" once for every break point where it's different.

The resulting code from this approach looks a little cleaner and nicer compared to bootstrap or normal CSS, at first glance. I'd have to test it before commenting further though.

[+] ketzo|5 years ago|reply
I quite like this. It’s a small idea that tooootally changes the way responsive stuff gets written. I’m gonna try this out on my next project.
[+] captn3m0|5 years ago|reply
Suggestion: provide a plain text version of the combined media queries to help the reader understand.
[+] deltron3030|5 years ago|reply
This breakpointless approach is interesting:

https://utopia.fyi/

[+] esperent|5 years ago|reply
I use something similar on my own website and it's great. However, it's very limited - I've never seen it applied to anything except for text. It's likely that for most use cases you will need to combine this with at least some traditional breakpoints.