3 ms·
I think the way most people would want an if else to work in CSS is that you can say if this path exists then apply a list of rules that might not have anything
by bryanrasmussen 4y ago
I think the way most people would want an if else to work in CSS is that you can say if this path exists then apply a list of rules that might not have anything to do with that path else some other list.
Let's suppose we have a match function that could be used like this
:root {
--has-path: match(.modal .message .error);
}
and then if the following were allowed (or some variation were developed where this could be done)
.user:has(var(--has-path)) {color: red}
I think it might be nice. YMMV however.
on edit: thinking about it however while it might be nice if you could do queries like that, it does seem like it would be expensive (for browser performance) to implement.
- madeofpalk 4y agoThe only missing part from your example is supporting custom properties in selectors, which can't really work because custom properties are scoped and cascade, like regular properties. I'm not sure I totally understand the specifics of the example, but it's probably possible to get something close with the nested selectors spec being worked on now, and/or :is()
- bryanrasmussen 4y ago>The only missing part from your example is supporting custom properties in selectors I figured it was implied some rules would have to be made which might be properties used in selectors are scoped to ancestors of element. >I'm not sure I totally understand the specifics of the example example is probably too fanciful but assuming you have a match of selector .modal .message .error then element with class user which is on a totally different part of the page - like body header ....stuff .user /header article section .modal .warning .error then user has color red. Do I personally want this, not much, just considering the people who sometimes say they want if ( selector exists) then bunch of css. another more likely usage for something like this (a match function that takes a selector) might be the various CSS logical operations things people do sometimes https://css-tricks.com/logical-operations-with-css-variables/ https://css-tricks.com/logical-operations-with-css-variables... however, as I noted before I don't think it makes sense for css performance reasons, despite how interesting/amusing I might find the idea.
- Sesse__ 4y agoThe biggest problem is that it would completely kill all of the precomputation style engines do to make CSS matching fast. When you style an element, you don't actually go through the steps of matching it against every rule; but if the selector could change at any time, you basically would have to, and performance would tank completely. The other issue is that custom properties inherit, so now it's unclear what element you should fetch your var() from (you can't fetch it from the element you're styling, since you haven't computed its style yet). E.g. .user:has(var(--has-path)) { --has-path: match(.something .else); }
- bryanrasmussen 4y ago>The biggest problem is that it would completely kill all of the precomputation style engines do to make CSS matching fast which is why I specifically said it wouldn't work because of performance issues? This part here at the end >it does seem like it would be expensive (for browser performance) to implement. >The other issue is that custom properties inherit I'm not sure I see the problem with that actually - If I set a value on :root and then override it on .foo then I thought it only got overridden for the descendants of .foo? on edit: ah no of course you mean that if I set --base-color on .foo it is also available for .foo, but in that case I would think the rules would mean that use in selector was outside the scope of the element, that is to say scoped to the parent.
- alwillis 4y agoCSS already supports this, although in a slightly hacky way [1]. [1]: "The - -var: ; hack to toggle multiple values with one custom property"—https://lea.verou.me/2020/10/the-var-space-hack-to-toggle-multiple-values-with-one-custom-property https://lea.verou.me/2020/10/the-var-space-hack-to-toggle-mu...
- bryanrasmussen 4y agoyeah I'm familiar, but that wasn't at all what I was talking about. :)
- the_other 4y agoIf I understand this right, you're saying: if my `.user` or `.something-else` has a `.modal .message .error`, then colour all the text in the `.user` red. I think this is a mistake in system design. CSS doesn't know or care or work with what a user is, what a foo is, what a bar is or how any of those interact conceptually. CSS operates on the rendered output of decisions made at that level: it shouldn't make those decisions, nor encode knowledge of those decisions. That's what JS is for, and what it's better at. I think it makes much more sense architecturally to have JS respond to whatever condition triggered the error state by updating some metadata on everything involved (User{}, Booking{}, Etc{}). ``` // pseudo JS uiEvents.subscribe('error message', () => { appState.User.hasBadData = true; }) uiEvents.subscribe('error message', () => { appState.Booking.hasBadData = true; }) function validateForm(fields) { fields.forEach(field => { if(field.hasError) { uiEvents.publish({some: "error message"}); } } } // pseudo React/Vue/whatever in pseudo TypeScript render(({user, booking}: TAppState) => ( <> <div class={`user ${user.hasBadData? 'user--error' : ''}`}>/\* user icons and text */</div> <div class={`booking ${booking.hasBadData? 'booking--error' : ''}`}> /* booking stuff \* </div> <> }); ``` You could replace all the JS with server-side logic (which would probably take it closer to the "single source of truth" of users and booking anyway). Further, maybe you're making application with multiple communication modalities, and you want to play a sound or trigger speech when the user enters bad data, instead/as well as colouring everything red. CSS can't help you there. It's much better to keep the logic, state and behaviours out of the CSS and use it only to "dress up" the rendered markup, otherwise you make maintenance harder.