Repository navigation
[css-values-5] What is the MVP for inline conditionals on custom properties? #10064
Description
Edit: Resolved to pursue this! 🎉
There are several issues across this repo proposing some kind of inline conditional function. Here is a sample:
- [css-variables?] Higher level custom properties that control multiple declarations #5624
- [css-values]: Express conditional values in a more terse way #5009
- [css-values] Emulate conditionals with discontinuous function(s) in calc() #6638
- [css-values] Iverson bracket functions: if(), media(), supports(), defined() #4731
- [css-values] if() function #3455
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:
- Using a 0/1 switch and linear interpolation:
calc(var(--test) * var(--if-true) + (1 - var(--test)) * var(--if-false)) - "Space toggle": Using presence/absence and
var(--test, var(--if-false)) - Style container queries: these are great, but only work on descendants.
- Cyclic Dependency Space Toggles
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:
- Only
style( <style-query> )conditionals - Only custom properties
- If it helps, the value space could be restricted too, even as much as single tokens
- If it helps, it could even be something that’s only allowed in custom property values
Edit (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:
<if()> = if( <container-query>, [<declaration-value>]{1, 2} )
- Links:
- Values can be nested to produce multiple branches
- If a third argument is not provided, it becomes equivalent to an empty token stream
I did not discuss this but a nice DX improvement could be this grammar instead:
<if()> = if(
[ <container-query>, [<declaration-value>]{2} ]#{0, },
<container-query>, [<declaration-value>]{1, 2}
)
which would allow for multiple conditions more easily. Compare:
Grammar 1:
background-color: if(
style(--variant: success), var(--color-success-60),
if(style(--variant: warning), var(--color-warning-60),
if(style(--variant: danger), var(--color-danger-60),
if(style(--variant: primary), var(--color-primary))
),
)
);Grammar 2:
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)
);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?
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsThursday morning
- StatusShow more project fieldsThursday morning
Activity
[-]What is the MVP for inline conditionals on custom properties?[/-][+][css-values?] What is the MVP for inline conditionals on custom properties?[/+]jjenzz commentedon Apr 13, 2024
when i first discovered style queries, i admit i had intuitively tried to do something like this:
it seems a way to select the container that
@container style()matches would be a nice stepping stone but i have nowhere near enough context/history to know if that is doable.or maybe enabling style queries as part of the
@scopeapi?LeaVerou commentedon Jun 10, 2024
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
andruud commentedon Jun 10, 2024
Yeah, at least if we choose the path forwards carefully, it should be easy. That carefully chosen path could be something like:
if()in a declaration's value, you get the "assumed valid at parse time" behavior we currently get when there's avar()orenv()in the value.if()then mostly just becomes an advancedvar()substitution, with some conditionals built in to it.if()function can evaluate exactly what style CQs can evaluate, and nothing else (for now).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.
Loirooriol commentedon Jun 10, 2024
It would seem a bit strange to me if
display: if(--x, nonsense, bullshit)is considered valid at parse time, since we know that both possibilities are invalid.LeaVerou commentedon Jun 10, 2024
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.
LeaVerou commentedon Jun 10, 2024
Yup. How hard would it be to support other queries beyond
style()? E.g. size queries,supports(),media()?(
style()does address the vast majority of use cases, but if any of these are easy, we may as well support them too.)andruud commentedon Jun 10, 2024
I don't see why we couldn't do
supports()andmedia(), but size queries would cause cycles with layout that are hard/impossible to even detect. (That's why we needed the restrictions we currently have for size CQs in the first place.)138 remaining items
benface commentedon Feb 24, 2025
if()function does not mean we can't also have an@ifat-rule.edbaafi commentedon Feb 24, 2025
@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.
benface commentedon Feb 24, 2025
I hope it is! I really like
if/else. 😂edbaafi commentedon Feb 24, 2025
haha.. I love
if/elsetoo just not for where this feature ended up goingedbaafi commentedon Feb 24, 2025
Since I've been focused on
iflet's take a look at how the current syntax changes the typical use ofelse:Re-reading the spec makes me see how weird this is:
What other language's use of
elseputs it within theif's()and has to describe it this way:This is why
defaultseems very appropriate for where this feature ended up (all of the reasons it's structured this way makes sense but now we're left with names that don't match the final structure). I'm going to add a third and fourth point to what we're changing from the typical (especially JS) use ofif/else.elif).()e.g.if(condition: value)vsif(condition1) value1elsea "special" condition of theif(within theif's()s e.g.if(condition: value1; else: value2)rather than a statement/expression at the same structural level as theif(e.g.if(condition: value1) else value2)All this can be avoided by picking a different name for
if. I thinkdefaultas a replacement forelsehere makes total sense. That way we can say: "Thedefaultkeyword represents a condition that is always true" in the spec and something like: "We can usedefault: <value>to provide a value to use when any of the previous conditions were not met" in the docs, instead of: "Theelsekeyword represents a condition that is always true" in the spec and who knows what in the docs.tabatkins commentedon Feb 24, 2025
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
iftoo. It's perfectly fine for CSS to do so, and I don't think there's a good reason to avoid usingif()as the most natural name here.The "web languages" (aka JS and its compilation variants) all descend from a C-like syntax ancestry, so of course they don't use
ifin the way we're using it here. CSS is completely divorced from that ancestry and differs in many, many ways from the syntax assumptions of those languages, and will continue to diverge as we add more features that are programming-ish (like, for example, custom functions, which use nested style rules and style application rather than imperative statement flow).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.
edbaafi commentedon Feb 24, 2025
@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
:in a declaration is like an expression), it is important thatifcan be used for both statements and expressions.I know this is the
MVP for inline conditionalsas the post title says, and that so far it's been decided not to provide conditionals for declaration blocks, but if this position changes in the future would you be able to make that use ofifmake sense or would you be forced to pick a different keyword?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
ifand sure they work on expressions, but which ones do the things I addressed as points 2-4 (singleifkeyword for multiple conditions, putting the condition and result in parens(), and nesting the if within the()at a different hierarchical level than theif)?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?
edbaafi commentedon Feb 24, 2025
Also, @tabatkins is the meaning of "empty token stream" defined anywhere in a spec?
Loirooriol commentedon Feb 24, 2025
What? Plenty of conditional blocks in these specs:
https://drafts.csswg.org/css-conditional-3/
https://drafts.csswg.org/css-conditional-4/
https://drafts.csswg.org/css-conditional-5/
There is no problem if a functional value and an at rule share the same name.
https://drafts.csswg.org/css-conditional-5/#when-rule uses
@whenbecause of a clash with SASS, not because of a conflict with inlineif(). Also see #6684,@whenmight still be renamed to@if.edbaafi commentedon Feb 24, 2025
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.
tabatkins commentedon Feb 24, 2025
As @benface said, nothing prevents us from adding
@ifin the future with whatever syntax we want. At-rules and functions are completely distinct.(Well, it turns out there is a reason to avoid
@if, but that's unrelated to this topic.)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.
edbaafi commentedon Feb 24, 2025
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
@rule e.g.@ifand being outside of the ruleset. Somewhere in this thread someone showed an@ifthat was inside of the ruleset's declaration block, and it created its own declaration block. I guess that stuck with me. With CSS nesting creating declaration blocks within declaration blocks, I guess it didn't read it as so off.Sure, I would argue for avoiding using the same name for a value function as an
@rule for all the reasons I'm arguing against usingifin a way that diverges from its use in many programming languages. Just because the language separates the@andfunction()namespaces doesn't mean it's a good idea to use the same name in both.But if this direction is set, than it's set. Again thanks all for pushing this regardless of my issues with the naming.
edbaafi commentedon Feb 25, 2025
@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
if()spec so as not to confuse with IACVT which serializes to the empty string):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
if()'s argument list is true, and this cannot be known at parse time, only compute time:tabatkins commentedon Feb 26, 2025
"Token" shows up many times in the Variables spec, and is referring to these concepts. "Empty token stream" should be pretty predictable from those concepts. Notably, the value of a custom property without a grammar associated with it (from
@propertyor theregisterProperty()API) is a sequence (or "stream") of tokens.edbaafi commentedon Feb 27, 2025
I think I can read between the lines or go look at Chromium to get your intent but my expectation (or at least hope) is that the spec would speak for itself and answer this question clearly and definitively and that you’d be more open to feedback if things could be read in a way that is potentially unclear. If you think the spec is clear on this I don’t know what else to say. But here are various things the spec says on this topic in case you are willing to give it a second look (letters a,b,c and emphasis added for illustration):
I think what is intended in (b) above is that what is ultimately resolved is a valid (empty) value as would occur if the author wrote a blank space as the value of a property (e.g.
—foo: ;) as was helpfully explained (in order to differentiate this from the guaranteed-invalid value) at another place in one of the specs:But since the actual term “empty token stream” was never explicitly defined (I didn’t say the single word "token" was never used) and the phrase "return nothing" was used (see (b)) to explain how to resolve an
if()function, how can you say this use of "return nothing" couldn’t be confused with being the same as "If no result was returned" (see (c)) leading the reader to believe that this could result in the guaranteed-invalid value as is explained by the actual steps (again see (c)) for resolving arbitrary substitution functions (which the spec previously stated an if() function is - see (a)).Or maybe the latter interpretation is in fact the intent and I got the intent wrong, but that's my point. I have read what you pointed me to and get more of the points now (thank you), but I'd appreciate a little clarity in this thread and in the spec. Thanks.