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

[hiccup-css] Use of unsigned shift operator prevents negative values #383

Open
acarabott opened this issue Feb 9, 2023 · 4 comments
Open

Comments

@acarabott
Copy link
Contributor

import { px } from "@thi.ng/hiccup-css";

console.log(px(-10.666)); 

// Result   "4294967286px" 
// Expected "-10px"
acarabott added a commit to acarabott/umbrella that referenced this issue Feb 9, 2023
Swap use of unsigned shift for OR `0`

Fixes issue thi-ng#383
@acarabott
Copy link
Contributor Author

It could also be argued that decimal places should be allowed for px, as browsers support them and the rendered results are different between (e.g.) margin-left: 10px and margin-left: 10.5px

@postspectacular
Copy link
Member

Hi @acarabott - sorry just saw this now (after merging your PR) - you're right & I even think we should amend all spatial units to using .toFixed() with 1-3 fractional digits... ?!

export const px = (x: number) => `${x.toFixed(1)}px`;

export const percent = (x: number) => `${x.toFixed(3)}%`;

@acarabott
Copy link
Contributor Author

Hmm, it doesn't seem like there's anything in the spec about limits. I'd be particularly wary about reducing the precision of % as with very large elements the results could be off by a number of pixels...

I use these helpers to prevent typos and as sugar to avoid `${value + otherValue}px`, so I think I would avoid any adjustments and let that be up to the caller:

export const px = (x: number) => `${x}px`;

postspectacular added a commit that referenced this issue Feb 17, 2023
- remove pixel rounding
- use signed flooring for ms()
@postspectacular
Copy link
Member

Hey @acarabott - agreed & was thinking about that too, but then again if I really would want pixel precision, I wouldn't use percentages... 😉 In any way, I agree it's best to leave the rounding of these values to the user. Milliseconds are the only unit for which this is done now. New release is already out since last Friday...

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

No branches or pull requests

2 participants