-
Notifications
You must be signed in to change notification settings - Fork 690
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] Introduce nth-item() #11103
Comments
Would negative indices count from the end of the list? What would index I’ve expressed a preference for a unified select by index function before. I’m still in favor of it, but overall it’s just syntax details. select( random | <integer> , [ <declaration-value>? ]# ) If this also supported select by key, we’d basically have a switch construct without fallthrough behavior. select( random | <integer> | <string> , [ [<string> :]? <declaration-value>? ]# ) |
Since other indexed things in CSS (grid lines, notably) allow counting from the end with negative values, presumably we'd do the same. 0 would just be invalid (or clamped to 1, if we add the clamp option). Selecting by random requires the fuller handling of random caching that Selecting by key should be handleable by |
I think I would prefer decoupling the list specification from the list operations, so we can have a dedicated There are many, many use cases around lists and scales, e.g.:
All of the above have several use cases around design tokens/design systems. Tackling 1 first is ok, but we should have a plan for the others down the line. It may also be worth thinking how lists of dimensions fit in to lists of colors (see #10034) or other types of values. |
@LeaVerou I don't think I agree, at least not without a lot more support for list handling. None of the things you described appear to be doable in CSS even with a list type; they all require looping, variable assignment, etc. We'd have to introduce more functions to represent these operations. And if we did introduce these additional functions, they don't, as far as I can tell, require any special "list" type; they can all be handled just fine by the existing infrastructure of "lists" being a convention for comma-separated items. Custom functions can take lists as an argument without any additional input from us (the caller would just wrap the list in |
Back in #5009 (comment) I summarized the various "conditional" things we might want to add. We've now added the
if()
function, and the big one left from that summary list isnth-item()
(callednth-value()
in that comment, but we should be consistent withrandom-item()
's naming).This is a fairly trivial addition functionality-wise, and it has two good arguments for including it:
random-item()
to select a random item from a list, surely you should be able to donth-item()
to select a non-random item from a list, that's just something authors will expect. ^_^Syntax would just be:
And it'll select the nth argument according to the first argument, yielding the guaranteed-invalid value if the index goes past the end of the list.
Maybe the first arg can have an optional
clamp
keyword, which would make the index clamp to the largest valid index instead of being invalid, so authors can use the final argument as the "default" value.The text was updated successfully, but these errors were encountered: