-
Notifications
You must be signed in to change notification settings - Fork 689
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-conditional] prefer()
function
#7881
Comments
This is somewhat related to @jimmyfrasche's issue #4731 and would likely fall into the same category as those functions
It may be worth linking these issues if there's a mechanism for that, beyond this mere mention |
I chatted with @joshvickerson about this idea on Twitter, and he proposed the function name |
fallback()
functionprefer()
function
This is basically #5055, but with especial behavior for variables. Some notes:
|
Great points across the board. Closing this ticket out in favor of @tabatkins' nearly identical one 👏🏼 |
Duplicates #5055 |
The problem
I just recently became more increasingly aware of how CSS doesn't provide a built-in method for providing fallback values, outside of what's currently supported within
var()
and what's coming toattr()
.With this in mind, if I wanted to provide multiple fallback values for an expression based on the first that works, the general approaches are these—
Declare the property multiple times, in priority order (ascending):
Custom properties
If, however, you want to bring custom properties into the mix, it suddenly becomes a bit more complicated, as
var()
always validates as truthy, so you instead need to rely onvar
's built-in fallback value (<declaration-value>
).As is clear in the second example above, nesting in this way can get pretty messy, not to mention that this method doesn't support multiple fallback values taking into current account browser support.
With the same type of nesting and incorporating the
if()
function currently being discussed, it might look something more like this:This same pattern could be used to implement colors in color spaces many browsers don't support, and that's still just one example using color for the sake of this example.
And still… this example is awfully messy.
Description of the proposal
I propose adding a
prefer()
(or something similar; I'm not sold on the name), which takes a list of values and returns the first value that doesn't error out (or that doesn't return an undefined value).Syntax
Usage
It's important to note, as my previous examples might be found misleading, that this would not change the existing functionality of
var()
. Take for example the same example from above, but wrapping each custom property name withvar()
:Because
var()
always validates as truthy, the above example would always evaluate tovar(--property-1)
even if its value is undefined. This is important so as, not to clash with the existing functionality ofvar()
. This is still helpful, as variables can still be used with their own fallback values if the variable value is undefined.As such, these two would be equivalent ✅
These two are not ❌
So generally, speaking, this:
…would be preferred over this:
…though I can certainly foresee IRL examples where the latter would be what is evaluated (but not actually used) simply because one variable value used inside
prefer()
has its own fallback value already.For example:
This would be perfectly acceptable and would be the equivalent of this:
The text was updated successfully, but these errors were encountered: