Angles and times
deg and turn build an Angle, and ms and secs build a Time. A bare number, or a time where an angle belongs, does not compile.
Angle and Time
Angle and Time are numbers with a unit, built by four functions that each take impl Into<f64>:
| Function | Returns | CSS |
|---|---|---|
deg(90) | Angle | 90deg |
turn(0.25) | Angle | 0.25turn |
ms(150) | Time | 150ms |
secs(1.5) | Time | 1.5s |
use hypersig::style::value::{deg, ms, secs, turn};
assert_eq!(deg(90).to_string(), "90deg");
assert_eq!(turn(0.25).to_string(), "0.25turn");
assert_eq!(ms(150).to_string(), "150ms");
assert_eq!(secs(1.5).to_string(), "1.5s");
// Unlike a length, a zero angle or duration keeps its unit.
assert_eq!(deg(0).to_string(), "0deg");
assert_eq!(ms(0).to_string(), "0ms");A zero angle or duration keeps its unit, because CSS reads transition-duration: 0 as invalid, while a zero length is written 0. Angles in rad or grad have no function, and go through raw. Angles and durations take the math functions that lengths do, so ms(150) * 2.0 writes calc(150ms * 2).
Properties that take them
rotate, offset_rotate, and image_orientation take an angle. transition_duration, transition_delay, animation_duration, animation_delay, and the shorthands transition and animation take a time. A bare number is neither, so .transition_duration(150) is E0277:
use hypersig::style::Style;
// error[E0277]: the trait bound `hypersig::hypersig_style::value::TransitionDuration:
// From<{integer}>` is not satisfied
let fade = Style::new().transition_duration(150);The two types do not convert into each other, so a time where an angle belongs is E0277 too:
use hypersig::style::Style;
use hypersig::style::value::ms;
// error[E0277]: the trait bound `hypersig::hypersig_style::value::Rotate:
// From<hypersig::hypersig_style::value::Time>` is not satisfied
let tilt = Style::new().rotate(ms(90));A transform with a rotation, such as rotate(45deg) scale(1.1), has several parts and goes through raw. Transitions and Keyframes cover the properties that use times.
What is not checked
The sign is not checked. A negative delay is valid CSS and starts the transition partway through, while a negative duration is invalid, and the browser drops the declaration. Like the other unit functions, these four panic on NaN or infinity.
Example: a turning arrow
The arrow turns 90 degrees over 200 milliseconds while the pointer is over the button. Style::group marks the button, and the arrow's rotation applies under Condition::Group(PseudoClass::Hover), which Group and peer covers:
use hypersig::html::{El, button, span};
use hypersig::style::value::{Display, TransitionTimingFunction, deg, ms, raw};
use hypersig::style::{Condition, PseudoClass, Style};
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Faq {}
/// A button whose arrow turns a quarter turn while the pointer is over it.
fn more() -> El<Faq> {
button()
.css(Style::new().group())
.child(
span()
.css(
Style::new()
.display(Display::InlineBlock)
.transition_property(raw("rotate"))
.transition_duration(ms(200))
.transition_timing_function(TransitionTimingFunction::EaseOut)
.on(
Condition::Group(PseudoClass::Hover),
Style::new().rotate(deg(90)),
),
)
.text("›"),
)
.text(" More")
}
let html = Page::new("Faq", Faq {}).child(more()).render();Next, URLs and strings covers url and quoted.