-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request for conditional values #1894
Comments
To add an example of why I'm suggesting this:
|
For the last example see
It's not that you never need per-property conditions (sometimes you do) but if you need them too often then it's definitely something wrong with the approach you use. After all these are just style sheets not a behavioural script so I doubt this proposal will get too much support. Ternary operator looks too alien for the CSS. Maybe it could be some kind of function like |
also note: your current example is probably a bad use-case for using e.g. this is shorter (though I do admit it duplicates the selector in the less)
|
p.s. but I agree @seven-phases-max approach is even better |
I can see a small use-case for this, but its quite a lot of sugar for a little benefit. It has come up before but I can't find the previous requests. |
Well, my example was contrived, but the point was multiple logical decisions within a single selector without having to call out to yet another mixin. Even if there was support for this, I'm afraid it would be still be limited. For example, what if there was no 'else', or the value was set as null. Doesn't seem there would be a way to avoid outputting the property, so you would wind up with "property: ;", or "property: null;" FWIW, I wrote the following mixin, which gives all sorts of control on a per property basis.
|
Of course, it's much easier when you have groups of rules that are all dependant on a single value to just use CSS Guards.
Sometimes things are more complicated than that however, and adding in 3 or more rules that each has their own logic can turn an otherwise simple declaration block into spaghetti. |
It is spaghetti by definition with or without ternary operator (e.g. "spaghetti code" is not counted by number of written lines but by the number of conditions used). |
Curious as to why you say that?
This gives you one declaration block without having to repeat any rules. What you suggest would have me make a separate declaration for the static rules, or repeat them in both blocks. Is there a reason those options would be preferable? Am I missing something? |
Btw. if it comes to hacks and workarounds it is possible to simplify the |
Example mixin? Not sure I see how to use pattern matching here. Update: Specifically, how would you evaluate "@x lte @y" in your example? |
Something like this (I did not bother with too deep testing so it may have some bugs). |
Wow, thanks! I'll have to study that more in depth. I'm not experienced with extracting values like that; looks like a great tool to have available. Really appreciate the feedback guys. |
Didn't realize you could pattern match any of the arguments. Thought it was only the first argument for some reason. Mind blown. Also, didn't know you could extract items that weren't separated by commas. Thanks again! |
So people may not be for my original request, what about simply allowing to assign a query as a value to a var?
Is there a reason something like the above hasn't been implemented? |
Well, it is not that easy as you might think. Notice that most of comparison operators are the same symbols as CSS selector combinators so such implementation needs very detailed specification of where they mean either of two (e.g. notice that |
I needed slightly more functionality, but your example helped a lot. Probably superfluous in most cases, but those who come here wanting to use conditionals might find it useful. |
Updated link: https://gist.github.com/skyshab/9217113 |
Closing in favor of: #3019 |
I often find myself needing to apply one style if an argument is x, and another style if y .
The addition of CSS Guards in 1.5.0 made it easy to wrap a group of styles like so:
However, the syntax seem excessive when it's just one or two attributes in a style block. In PHP and JS, I am accustomed to the ternary operator syntax, and it seemed like it might easily fit into the Less language. Some examples:
Any thoughts?
The text was updated successfully, but these errors were encountered: