-
Notifications
You must be signed in to change notification settings - Fork 693
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
[css-values-5] What is the MVP for inline conditionals on custom properties? #10064
Comments
when i first discovered style queries, i admit i had intuitively tried to do something like this: @container style(--variant: large) {
& {
/* style the container itself */
}
} it seems a way to select the container that or maybe enabling style queries as part of the @scope style(--variant: large) {
:scope {}
} |
I had a chat with @andruud a few days ago, and he said that this proposed design would be fairly easy to implement. Edit: updated first post |
Yeah, at least if we choose the path forwards carefully, it should be easy. That carefully chosen path could be something like:
Of course we'll need to deal with cycles. e.g.:
But we should be well equipped to handle that. So if the CSSWG determines that this is worthwhile, it should be quite doable overall. I suppose it's worth thinking about custom functions here as well, since they also kind of provide "inline" conditionals. It might not be ergonomic to use for the use-cases you have in mind, though. |
It would seem a bit strange to me if |
I’m fine to make it invalid at parse time, but it seems like a lot of work for an edge case. Especially given there might be multiple conditionals for a single property value, so it's a combinatorial explosion to tell if any of them would result in a valid value. |
Yup. How hard would it be to support other queries beyond |
I don't see why we couldn't do |
Fair. So we’ll need a distinct token and not |
A few more workarounds authors can use right now:
I recommend going to the linked articles and codepens to witness the hacky CSS that is currently required to achieve those conditionals. Given how often authors reach out to things like space toggles or come up with more and more convoluted ways to achieve conditions for different use cases (Is a custom property defined? Is value A is smaller than B? Are those two conditions true? Etc.), we really need at least some way to have these kinds of conditions. Ideally, I'd want to have both inline conditions, but also an at-rule-level ones, but even if we will resolve only on inline conditions it will improve the lives of authors tremendously. |
Unfortunately not, you gotta just write out the full grammar. It's mostly copy-pasting tho. |
Ah, I was thinking of a |
This comment was marked as resolved.
This comment was marked as resolved.
The CSS Working Group just discussed RESOLVED: Add if() to css-values-5 The full IRC log of that discussion<astearns> zakim, open queue<Zakim> ok, astearns, the speaker queue is open <fantasai> leaverou: Motivating use cases <fantasai> leaverou: right now web components libraries introduce tons of presentational attributes <fantasai> leaverou: because custom properties include parts of values <fantasai> leaverou: you can transform numeric values using calc() but keywords not possible <fantasai> leaverou: Canonical example is changing background-color based on a `--variant` property <fantasai> leaverou: These are examples for why I started shoelace library now called WebAwesome <fantasai> leaverou: [lists many examples of utility classes] <fantasai> leaverou: Style queries get us a lot of the way there, but because they only work on descendants <fantasai> leaverou: that doesn't let us get all the way there <fantasai> leaverou: so these remain presentational attributes <fantasai> leaverou: but needing to branch on a condition is very common in CSS <fantasai> leaverou: several issues last few years about how can we do this <fantasai> leaverou: either as a block conditional <fantasai> leaverou: or inline <fantasai> leaverou: there are some extremely hacky workarounds that authors use <fantasai> leaverou: e.g. stting custom property to an empty token string so it gets a value or falls back. Very very hacky things <fantasai> leaverou: Despite huge demand there's no progress <fantasai> leaverou: I opened this issue to explore what is still useful, but still easy for implementers <fantasai> leaverou: Anders said if we re-use the conditional from container queries, that let's you compare values <fantasai> leaverou: and even media and supports queries <fantasai> leaverou: re-use that part and then declaration values chosen based on conditional <fantasai> leaverou: said it was pretty easy <fantasai> leaverou: For many of these use cases, instead of having one or two values, you could have a series of subsequent conditionals <fantasai> leaverou: that's nice to hvae <fantasai> leaverou: but main thing is single test, can always nest <fantasai> leaverou: ideally we want a block conditional that applies when matches <fantasai> leaverou: but that seems to be hard, so let's do inline first <fantasai> leaverou: because we need it anyway <fantasai> leaverou: if it can be implemented quickly, would be an easy win <TabAtkins> q+ <kizu> q+ <fantasai> TabAtkins: This sounds good. <astearns> ack TabAtkins <TabAtkins> https://github.com//issues/5009#issuecomment-626072319 <fantasai> TabAtkins: going back to previous thread, this comments lists out 3 variants of conditionals <fantasai> TabAtkins: This is a variant of item 3 <fantasai> TabAtkins: one important part is these shouldn't be a boolean (true/false), it should be multi-valued so you can provide more than one test <fantasai> TabAtkins: in simplest case that's true or false, just omit last test <leaverou2> q+ <leaverou2> q++ <fantasai> TabAtkins: but I want to make sure you can sequence tests <leaverou2> qq+ <leaverou2> q- <fantasai> leaverou: it's the second grammar <emilio> q- + <fantasai> TabAtkins: sgtm <astearns> ack fantasai <astearns> ack leaverou <TabAtkins> fantasai: I think that the IACVT behavior is not amazing <TabAtkins> fantasai: so i dont' want us to have authors rely on it all the time bc there's no alt <leaverou2> q+ <TabAtkins> fantasai: to the extent taht people want to switch on a custom prop, can't we do that with some sort of conditional... <TabAtkins> fantasai: if you just switch basics based on conditionals you can avoid cycles easily that way <fantasai> leaverou: Would be useful, but this is orthogonal <TabAtkins> leaverou: a block conditional *would* be useful, but it's orthogonal <TabAtkins> leaverou2: same as in JS <TabAtkins> fantasai: not orthogonal from ux perspective. whether someone uses block or inline conditional is somewhat based on availability <TabAtkins> fantasai: if you have both you'll use both, but if you have only one you'll shoehorn <TabAtkins> fantasai: i think we should do both if we can <fantasai> TabAtkins: isnt' the block version just style queries and media queries? <fantasai> leaverou: Style queries only work on descendants <TabAtkins> TabAtkins: getting a style query block *on the element* *requires* IACVT <andruud> q+ <fantasai> fantasai: Shouldn't be a problem if you have standard properties set based on custom properties <fantasai> TabAtkins: If you restrit it only to standard properties, that's substantially restrictive <fantasai> TabAtkins: a lot of cases will want to set more variables <miriam> q+ <fantasai> TabAtkins: Those will then need to use inline version, because that's a requirement of inline <fantasai> s/because/which uses IACVT because/ <fantasai> TabAtkins: IACVT triggers if you write something wrong <astearns> ack kizu <fantasai> kizu: Wanted to show some worse things <fantasai> kizu: In October 2016, I was using custom properties to switch between values using calculations <fantasai> kizu: today you can use comparisons with calculations, if length is bigger or smaller use different value -- only for lengths <fantasai> kizu: ?? made it possible to use values with custom properties <fantasai> kizu: using type griding <fantasai> kizu: a very long chain of fallbacks to registered custom property <fantasai> kizu: and that let's you get any value back <miriam> s/??/Jane Ori/ <kizu> https://github.com//issues/10064#issuecomment-2161742249 <fantasai> kizu: The code is very complicated <fantasai> kizu: authors can now do this <astearns> q? <fantasai> kizu: recent article where using layers and [missed] <fantasai> kizu: taking advantage of revert-layer <fantasai> kizu: The need for this from authors is very high <leaverou2> fantasai note that all the workarounds involve var(). So all workarounds ALREADY invoke IACVT. This is just about making the API and ergonomics infinitely nicer <fantasai> kizu: Also wanted to mention, how could we use registered custom properties to avoid some issues with IACVT <fantasai> kizu: For those we know what types they can accept, what they can return, might make it more simple <astearns> ack leaverou2 <astearns> ack leaverou <fantasai> leaverou2, Yeah. My point is I don't want authors to need to use this MORE because it'll be so much easier, simply because don't have a better alternative <fantasai> fantasai: I don't think we should be trying to encourage authors to use a bad behavior, rather than trying to make something that works better with the cascade <fantasai> fantasai: Like we should try to make it work the right way rather than settling for IACVT <astearns> ack andruud <fantasai> kizu: Using animations to do this is worse <fantasai> andruud: For block version, if your conditional contains a registered custom property and you try to style the font size with em units, you'll get a cycle <astearns> ack miriam <fantasai> andruud: so we'll still run into cycle problems with block version <fantasai> miriam: Do those cycles make th whole thing impossible, or do they just mean font-size is invalid and we do the condition <leaverou2> ;? <fantasai> andruud: for the block conditional? <leaverou2> q? <leaverou2> q+ <fantasai> andruud: I guess we could handle those cases 1 by 1 <fantasai> TabAtkins: dependencies will crop up over time, so hard to block ad-hoc <astearns> ack leaverou <fantasai> leaverou2: The block version will be inherently limited, because some values will only make sense on a single property <fantasai> leaverou2: e.g. em/auto/normal <fantasai> leaverou2: even if we have block version, still need inline version <fantasai> leaverou2: Also you can assign conditions to a variable and then use that variable in a function, including media test <fantasai> leaverou2: so handle breakpoints more easily <fantasai> astearns: Lots of acknowledgement that this is important to work on <fantasai> astearns: but also some concern about increasing the IACVT behavior on the web <fantasai> TabAtkins: I don't believe we can solve anything in this space without IACVT <fantasai> TabAtkins: so unless we can avoid cycles with some new idea, then we need to move forward with IACVT <TabAtkins> fantasai: i don't object <TabAtkins> fantasai: lea, can you outline the proposed syntax? <leaverou2> border-radius: if(style(--button-shape: pill), infinity); <ydaniv> Is this something that could later be speced to replace IACVT into a proper behavior? <leaverou2> Longer example: background-color: if( <leaverou2> style(--variant: success), var(--color-success-60), <leaverou2> style(--variant: warning), var(--color-warning-60), <leaverou2> style(--variant: danger), var(--color-danger-60), <leaverou2> style(--variant: primary), var(--color-primary) <leaverou2> ); <fantasai> fantasai: I think it would be better if the comma wasn't used both to separate conditional and value, as well as separate sets of conditionals <TabAtkins> use a colon! <florian> q+ <fantasai> leaverou2: seems reasonable. Could maybe use a slash <bkardell_> +1 TabAtkins <astearns> ack florian <fantasai> leaverou2: could even use a ? and be more like JS <lwarlow> q+ <TabAtkins> suggest we take that syntax question to the issue <fantasai> florian: suggest using switch() for multiple cases, like JS <fantasai> leaverou2: The conditionals here chain like else-if <fantasai> leaverou2: but this expands to ranges, etc. <TabAtkins> yeah, this is an if/elif/elif/else chain <astearns> ack lwarlow <fantasai> s/chain like else-if/chain like else-if, you have to provide the conditional each time/ <fantasai> lwarlow: agree with not using comma for both places <fantasai> lwarlow: slash would read better <leaverou2> Just noticed all examples above have no else value. This is another example: `border-radius: if(style(--button-shape: pill), infinity, .2em);` <fantasai> PROPOSED: Add this to css-values-5 <lwarlow> +1 <fantasai> astearns: any objections? <leaverou2> +1 <bkardell_> https://www.irccloud.com/pastebin/nrStwApc/ <fantasai> RESOVLED: Add if() to css-values-5 |
The syntax as described makes it slightly impossible to say "else be IACVT" in some cases. (For these examples, I'm gonna assume we use
(This ambiguity doesn't occur if there are multiple chained conditions. So I think we'll need an explicit syntax for saying "no default condition". You don't usually need it, but occasionally you will, and it might make some cases clear even when it's not strictly required. Like, |
@tabatkins Why not simply make the last argument mandatory and allow empty values (just like Compare: Option 1 (mandatory last argument that can be empty): border-radius: if(style(--button-shape: pill), infinity, ); Option 2 ( border-radius: if(style(--button-shape: pill), infinity / default: ); One advantage of having a special value (e.g. If that’s not an option, I'd rather introduce a different separator than a whole keyword. I’m generally all for prioritizing readability, but readability is a balance: too concise and it doesn't make sense, too wordy and you have to wade through the clutter to understand it. With something that I expect will be used all over the place, conciseness matters quite a lot. |
That's possible, I just find it less clear to read. I don't really like how
I'm not trying to solve any feature lack, just a syntax lack around the existing features.
We can't invent infinite separators. ^_^ But also, this is something that is rarely needed - like I said, the only case that requires it is when you want to supply a single test, with a positive value containing a comma, and no negative value. If you do want to use it for clarity in other cases, I think it's perfectly readable. For example: background-color: if(
style(--variant: success): var(--color-success-60),
style(--variant: warning): var(--color-warning-60),
style(--variant: danger): var(--color-danger-60),
style(--variant: primary): var(--color-primary),
no-value
); |
Actually, let me state this better: this isn't about the separator. The issue is just that the grammar doesn't allow an empty option (and I don't think it should), so there's one particular case you can't express. |
@tabatkins Actually, if we don’t use a custom character for separating the condition from the values like the original proposal, this becomes a non-problem. By the time you get to the first value, you know what your separator is, so there’s no ambiguity in @fantasai you raised the issue of using a distinct character for separating the condition from the value(s), and I agree this would be nice. However, given the ambiguity this introduces, are you ok with going with a comma/semicolon for both to avoid having to introduce weird new keywords like |
That doesn’t make sense, since we are now not using semicolons in there at all. |
??? The resolution was indeed to go with colon-and-semicolon. |
@tabatkins Oh right, my bad! I thought we had resolved to go with regular argument separators. Hmm. So we’ll have some functions where arguments are separated by commas, some where they are separated by semicolons, and some where they use the pattern we resolved to in #9539 ? That’s …pretty weird 😕 |
No, we have |
Edits are in, please open follow-up issues for anything that still needs adjustment! Thanks! |
@LeaVerou @fantasai et al. this is amazing! I'm a bit late to the party but I have two questions:
So what could we call it? I had a preprocessor based implementation called
But this has the issue you seemed to want to avoid where you need to nest multiple ternaries to evaluate multiple conditions (I didn't mind this because as a programmer I'm used to it and the use case was hidden in a library that rarely changed but I understand the motivation to avoid this):
So what could the keywords be? In a lot of ways your proposed syntax makes me think of
Maybe
That brings me to my second question which might illuminate a better keyword...
Thanks for this amazing feature and for your consideration regarding not using a ubiquitous programming keyword with such a well established meaning. |
@edbaafi for simple conditionals, maybe for pattern matching, which I think is what you're highlighting as "something better" here, but I don't think pattern matching can or should take the place of simple conditionals - they're for a different use case. (you probably don't want your code littered with "pattern matching" expressions with just a single condition and a default.) (it would be great to have both, but I suspect pattern matching is even more difficult to fit into existing syntax...) |
I don't think the the distinction of expressions vs statement matters much. In Rust you can use |
@Loirooriol I guess, yeah, Rust, Kotlin, Lua, Haskell all have in a web context though? I can't think of anything in a web context that has the SCSS for example has @if $light-theme {
background-color: $light-background;
color: $light-text;
} @else {
background-color: $dark-background;
color: $dark-text;
} |
@mindplay-dk Sorry the "something better" was just referring to a different set of keywords (while names are mostly subjective - "better" here refers to terms that lack the baggage of well known definitions for the target audience such as @Loirooriol thanks for pointing that out about rust! It's an expression driven language so that makes a lot of sense in that context. I still think we could pick a name that doesn't mean a specific, conflicting thing to so many developers (especially wrt the only language shipped with the browser). I was only pointing out that it felt wrong to me and trying to make sense of why. If we don't care about the expression vs statement question there is still the fact that we are adding multiple conditions to a single It seems this feature went through a lot of discussion! And digging through the multiple threads, it seems there was a point that Also still hoping for an answer to my second question which could help illuminate what exactly the proposed feature is doing which is important when discussing the naming/keywords. If evaluating to this "empty token stream" has no equivalent in any programming language conditional expression/statements, it makes the case even stronger to choose a new, "untainted" name to describe this new functionality:
|
|
@benface amazing! Thanks for the info. Hope it's not too late for a keyword change. To be clear I'm not suggesting anything other than swapping the if/else keywords for something else. Also, will look to canary to see if I can understand the meaning of "empty token stream" in this context. |
I hope it is! I really like |
haha.. I love |
Since I've been focused on Re-reading the spec makes me see how weird this is:
What other language's use of
This is why
All this can be avoided by picking a different name for |
Yeah, as Oriol said, that's a consequence of those languages being block-oriented, not something super attached to the keywords themselves. More functional languages that feature control-flow expressions usually name their conditional The "web languages" (aka JS and its compilation variants) all descend from a C-like syntax ancestry, so of course they don't use As someone who's learned a little bit of a lot of languages, I assure you that you'll get used to this sort of name reuse with diverging syntax. |
@tabatkins Thanks for your response, and sure, I think that addresses the first point, so we can disregard that. Again, I think this is a big win in terms of functionality. I think the fact that CSS will get many more "features that are programming-ish" is exactly why this needs a bit more thought. I think the rust example is a good one because that's a language that has both statements and expressions where a fully functional language like Elm does not. For languages with both (I've been using the analogy that declarations are like statements and everything after the I know this is the How would the syntax with the current issues I addressed as points 2-4 look with declaration blocks not just values? (I'm not asking us to focus on if this feature is likely but just as a thought experiment to understand the syntax that has been decided on).
So yes, functional languages without statements (and those like rust that seem to try to split the difference) have named their conditional expressions I think it's great that CSS will get many more "features that are programming-ish" and just think that we should nod to the norms there wherever we can. The point that CSS is different is also well taken, so if a given keyword evokes certain expectations across many languages (again just focusing on points 2-4), shouldn't we find a different keyword? |
Also, @tabatkins is the meaning of "empty token stream" defined anywhere in a spec? |
What? Plenty of conditional blocks in these specs:
There is no problem if a functional value and an at rule share the same name. |
Sorry, but I'm not writing on my own behalf, this is about meeting users where they are coming from and making the web platform as accessible as possible for the most people. How far do you have to go down your preferred list of languages by popularity (excluding non-programming languages) before you get to one with conditional expressions? But we've excluded that point, so what language anywhere on your favorite list does the other things I've highlighted as points 2-4 above? The point is I'm not living under a rock because my initial reaction was this felt wrong because of expressions vs. statements (I've used most of the top 10 languages on the stack overflow list below). But I've accepted that point, and I'm hoping my others are not overlooked. ![]() |
As @benface said, nothing prevents us from adding (Well, it turns out there is a reason to avoid The syntax would definitely be an at-rule, because that's what's allowed at the rule level in CSS syntax. No chance of confusion with value-level functions.
Not necessarily. Different languages express themselves differently. That's fine. "If" is by far the most common word for the basic conditional syntax across languages. This is the basic conditional syntax for CSS. It also doesn't look like any of the less-common conditional syntaxes in nearby languages, like "switch" or "cond" or "match", because those languages are also designing under their own assumptions and syntax precedents.
Tokens are discussed in CSS Syntax and, more relevantly for this topic, in CSS Variables. |
Loirooriol thanks for the references, I will go through them. I was trying to follow this discussion which is in so many threads and it was mentioned somewhere (including alluded to in this post's title) that the MVP would be inline only (value vs block). That's all I meant by "so far". Will look at the other proposals and see how far they are.
Thanks, I think I get the point about the block version being an
Sure, I would argue for avoiding using the same name for a value function as an But if this direction is set, than it's set. Again thanks all for pushing this regardless of my issues with the naming. |
@tabatkins re: "empty token stream", I think that might be a typo. I read CSS Syntax and as one would expect, tokens are described with respect to parsing:
Once the CSS is parsed and the CSSOM generated where does this concept of tokens come up again? What do tokens have to do with compute time values? I also read CSS Variables as you indicated this was the relevent spec. There is no reference to "empty token stream" in either of these documents but CSS Variables does talk about "empty strings" and "empty values" (the latter is what I suspect is meant in the new
Again, the new CSS Values section references "empty token stream" which AFAICT is not defined anywhere else in the spec and doesn't make sense if tokens are only a concern during the parsing step and not at custom property compute time. As you can see the question here is what happens when no condition in
|
Edit: Resolved to pursue this! 🎉
There are several issues across this repo proposing some kind of inline conditional function. Here is a sample:
Yet, this is another case where progress has stalled because we’re trying to flesh out a much more general and powerful feature, which involves a significant amount of design & implementation effort. Meanwhile, this major author pain point remains unsolved, and authors have to resort to HTML attributes instead (as explained in #5624).
The current workarounds are:
calc(var(--test) * var(--if-true) + (1 - var(--test)) * var(--if-false))
var(--test, var(--if-false))
However, there is no workaround for transforming arbitrary keywords to arbitrary values, even simple values. E.g. custom properties like these are impossible to implement (examples inspired from the presentational attributes found in Shoelace, one of the most popular web component libraries, but similar use cases can be found in almost any design system and/or WC library):
--variant: auto | primary | success | neutral | warning | danger
--effect: none | pulse
--button-style: fill | outline
--shape: rect | pill
--avatar-shape: square | rounded | circle
--size: small | medium | large
--suffix: none | caret
`--popup-placement: [top | right | bottom | left] [start | end]?
--popup-arrow-placement: center | start | end
What if we could come up with an MVP that could be implemented fast and extended later?
We could scope it down quite a lot and still have something that addresses the most pressing author pain points.
Some example restrictions we could start with:
style( <style-query> )
conditionalsEdit (June 10th, 2024):
I had a chat with @andruud a few days ago, and he said that this proposed design would be fairly easy to implement.
Grammar:
<container-query>
<declaration-value>
I did not discuss this but a nice DX improvement could be this grammar instead:
which would allow for multiple conditions more easily. Compare:
Grammar 1:
Grammar 2:
Behavior: Any invalid value would make the property IACVT.
Does it cover enough use cases? I think so. Ideally, you'd want a mechanism to fallback to whatever value the declaration would have normally if nothing matches, instead of IACVT, but that’s much harder to implement.
Implementors, would this make it tractable? If not, what would?
The text was updated successfully, but these errors were encountered: