Skip to content
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-typed-om] Lengths and Angles (and other unit-group objects) should have consistent structure #356

Closed
tabatkins opened this issue Feb 3, 2017 · 4 comments

Comments

@tabatkins
Copy link
Member

tabatkins commented Feb 3, 2017

Right now CSSSimpleLength and CSSAngleValue are dramatically different internally - {value, unit} vs {deg, rad, grad, turn}.

The reason for this is that all the existing angle values are trivially interconvertible, so you can set a value in deg and read it in rad and get a reasonable result. However, only some of the length values are interconvertible - you should be able to set a value in px and read it in in, but not in em.

However, this makes the interfaces annoying to work with - lengths and angles seem similar, but you need fairly different code to interact with them - x.value (assuming it's already in the unit you want) vs x.deg (regardless of what unit it was originally in).

A further complication is the CSSCalcLength interface: it superficially resembles the CSSAngleValue shape, but it's actually completely different. All of the members are independent, rather than separate views of a shared core value - setting a value to x.px and then reading x.in will probably just return null, even tho the two values are interconvertible.

An ergonomics point is that people usually work in a single unit at a time while working with lengths (typically, px or em), so converting between units isn't a huge deal, but when dealing with angles people convert between degrees and radians commonly (because degrees are easier to read/write, but radians are needed for trig).

I'm not sure what the right answer is for this yet, but the ergonomics are currently bad, and need to be fixed.

/cc @dbaron

@tabatkins
Copy link
Member Author

tabatkins commented Feb 4, 2017

I'm thinking we should have all the "simple" (single-unit) types should be {value, unit}, and we should add a .to(unit) function that converts between units when possible (and throws otherwise).

So CSS.px(5).to("in") works, as does CSS.deg(5).to("rad"), but CSS.px(5).to("em") and CSS.px(5).to("deg") both throw.

This gives us a consistent API between all the single-unit values, makes switching units fairly easy, and makes the API for single-unit and calc look substantially different.

@tabatkins
Copy link
Member Author

Okay, I ended up writing up my proposal more formally: https://tabatkins.github.io/specs/unit-problems/

@shans
Copy link
Contributor

shans commented Feb 7, 2017

This is a compelling enough and small enough change that we should add it to the specification now, and raise an issue for discussion with the TF in April.

@tabatkins
Copy link
Member Author

Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants