-
Notifications
You must be signed in to change notification settings - Fork 156
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
Proposal: Find alternative to appending units to CSS values #127
Comments
Need to think on this. This automatic adding of |
Yeah, I was studying it in the afternoon, I think that only jQuery and React append
And there is jQuery issue:
There is the React's list from its docs. |
I think your suggestion of the helper fn |
/// [MDN web docs](https://developer.mozilla.org/en-US/docs/Learn/CSS/Introduction_to_CSS/Values_and_units)
pub enum Unit {
Cm,
Em,
Ex,
In,
MM,
Pc,
Pt,
Px,
Q,
}
impl ToString for Unit {
fn to_string(&self) -> String {
match self {
Unit::Cm => "cm".into(),
Unit::Em => "em".into(),
Unit::Ex => "ex".into(),
Unit::In => "in".into(),
Unit::MM => "mm".into(),
Unit::Pc => "pc".into(),
Unit::Pt => "pt".into(),
Unit::Px => "px".into(),
Unit::Q => "q".into(),
}
}
}
/// Convert a number to a string with px appended. For use with inline styles.
pub fn unit(number: impl ToString + Copy, unit: Unit) -> String {
let mut value = number.to_string();
value.push_str(&unit.to_string());
value
}
/// Special case of `unit` for the common case of `px`.
pub fn px(number: impl ToString + Copy) -> String {
unit(number, Unit::Px)
} |
I'll try to implement it so we see how it looks in practice. |
Problem
Automatic appending
px
to int values are very error prone:i32
=>f64
) of the variable that represent some CSS value (e.g. "left"), Seed won't addpx
and CSS will fail silently in runtime.px
where it doesn't make sense - e.g.z-index
.px
and integer alone) - e.g.line-height
=> which version should be used?Solution
"width" => px(car.width);
(implemented in PR 126)."width" => unit!(car.width, "px");
(unit should be typed and we can call it also with enum:"width" => unit!(car.width, Unit::Pixel);
.style!
macro (if possible):"width" => car.width "px";
.I'll implement it if we agree on something.
The text was updated successfully, but these errors were encountered: