Re: [csswg-drafts] [css-values] Proposal: `round()` to a finite scale (#11067)

All good questions!

> Would infinities be automatically included?
> ```CSS
> round(down, 0, 2 3 5 7 11); /* -infinity or nan? */
> ```

Hmm, in this case I'd expect `2` — the closest available value even if it is higher than the target. The goal is usually to have a set of “design tokens” and choose only from them, taking the “closest”, so in the regular cases the `up`/`down` are for choosing one from two closest items. In this case, the `2` is the only available item, and is the closest.

> Would the list need to be in non-decreasing order, or leave it up to the browser to sort it?
> Would the list accept duplicates? I guess it would be a way to have a finite list with one item...

Hmm, an author in me wants the browser to do the work for me, and do both: remove the duplicates, and then sort the array. Is this a potential performance issue? I imagine 99% of real-world cases won't have too many items there, and if we really don't want people to sort thousands of items, we could have a certain limit in-place.

I remember the [`toggle()`](https://www.w3.org/TR/css-values-5/#toggle-notation) specified in a way where it should not contain duplicates, but it was never implemented by anyone, and I don't know of any other precedents that could help with this.

But the author experience would be for sure much better if the duplicates and non-strict order is allowed.

> But IMO it can be confusing that providing 2 values results in a 2-values finite list, while providing 1 value results in an infinite list.

I can see how this could be confusing, yes, but I wonder if this edge case is too unrealistic to try, and solve it. I'd go with it producing a finite list of 1 element, which is, uh, just that value, but better than falling back to infinite list, as it won't ever be something an author wants.

> It may be better to introduce a new syntax. It could also be useful to round to intervals, e.g.
> ```CSS
> round(var(--num), [0, 2 .. 3, 4, 5 .. 6])
> ```

Oh, so `2 .. 3` in this case will be something like “if a value falls between these, keep the value intact”, like a mini-`clamp()` inside this list?

This is an interesting idea, and I can see how introducing a new syntax can help to add things like this one. Although, I am not sure if I remember use cases for this specific case, but if there are some — we can consider it. But maybe this is something that could be potentially solved better with native functions and conditionals?

Many tools could be used for the same job, and I wonder if real-life cases that will need something like that will always want something even more complicated. Like, if the value is higher than 6, then do `round(var(--num), 2)` — this is easy to do with `if()` if we'd have number comparisons in it, and could be more expressive than introducing a new syntax to `round()`.

-- 
GitHub Notification of comment by kizu
Please view or discuss this issue at https://github.com/w3c/csswg-drafts/issues/11067#issuecomment-2430032785 using your GitHub account


-- 
Sent via github-notify-ml as configured in https://github.com/w3c/github-notify-ml-config

Received on Tuesday, 22 October 2024 19:03:30 UTC