instead of the custom property and getComputedStyle(). That isn’t supported by Firefox though.
When I was experimenting for my original post, I briefly looked at the CSSOM, but did miss the .to('number') method! I suspected that it should provide something similar, and I am happy that it exists, and that Valtteri pointed it out to me!
For browsers that support CSSNumericValue, this is a much better solution, as it does not require getComputedStyle(), and thus should not cause a layout recalculation.
Here is the example from the last article, now using CSSNumericValue when it is available:
A Better Fallback
However, if we’re talking about fallbacks, my original method was still far from being perfect: after all, registerProperty has a worse support.
Can we have a better fallback for it? Yes, if instead of using a registered custom property, we will use a regular property that accepts a <number> syntax:
Just for fun (and because it is unlikely to clash with anything usually), I used the border-image-outset property. Did you know about its existence? It is there for as long as calc(), which we also need for things to work.
A Weird Solution: Calculated initial-value
Before I got to know about CSSNumericValue, I was playing with various ways to improve the performance of my initial method. I found a thing about which I did not know about, but which seems obvious in hindsight.
We can use calculations for initial-value of registered properties! Here is my original example which uses this method:
Ok, this one is very inefficient: we need to maintain a cache for the values (so we won’t attempt to register the same custom property twice, which is prohibited), and I don’t even want to know how the browser will behave if we will suddenly register hundreds of unused custom properties.
But hey, it works! Not sure what can be the practical purpose of this, though.
Room for Improvement
There are still many things that something like this will require for it to be a viable reusable solution.
But now I know about CSSNumericValue! I hope we will get it in all browsers, eventually. Mozilla is positive about CSS Typed OM, so, hopefully, someone will eventually implement it there.
It is fascinating how by implementing a powerful feature like this one in CSS, we unlock it not only there, but also for JS as well. Can it be viable to propose a native JS method for evaluating CSS math?
I’m also happy that I wrote the original post, even though the idea was very wild and unpolished. It might help someone (like Kilian), and now we know more about CSS and its API.
Please share your thoughts about this on Mastodon!