日本語

Transitions

A transition animates a property from its old value to its new one when a pseudo-class or a signal changes it.

The transition properties

MethodValuesExample and its CSS
transition_propertyTransitionProperty::All, TransitionProperty::None, rawraw("opacity, translate"): transition-property: opacity, translate
transition_durationA Timems(200): transition-duration: 200ms
transition_timing_functionEase, EaseIn, EaseOut, EaseInOut, Linear, StepStart, StepEndTransitionTimingFunction::EaseOut: transition-timing-function: ease-out
transition_delayA Timems(50): transition-delay: 50ms
transition_behaviorNormal, AllowDiscreteTransitionBehavior::AllowDiscrete: transition-behavior: allow-discrete

Each method has the shape fn transition_duration(self, value: impl Into<TransitionDuration>) -> Style, with the value type named after the property, and each also takes raw. The list of properties has no type of its own: TransitionProperty has only all and none, so a list of named properties is raw, and so is a timing function such as cubic-bezier(0.4, 0, 0.2, 1).

What starts a transition

The browser runs a transition when the value of a listed property changes on an element that stays in the document. On a hypersig page that is a condition of Style::on that starts or stops matching, such as PseudoClass::Hover, or a class of El::css_when or El::css_match that is added or removed as a signal changes. The declarations of the transition go on the element's own style, so they apply in both directions:

use hypersig::html::{button, div};
use hypersig::style::value::{
    Color, Opacity, TransitionTimingFunction, Translate, Visibility, ms, raw, rem,
};
use hypersig::style::{PseudoClass, Style};
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Menu {
    open: bool,
}

// The background fades over 150ms when the pointer enters or leaves.
let toggle = button::<Menu>()
    .css(
        Style::new()
            .background_color(Color::WHITE)
            .transition_property(raw("background-color"))
            .transition_duration(ms(150))
            .on(PseudoClass::Hover, Style::new().background_color(Color::GAINSBORO)),
    )
    .on_click(Menu::open().toggle())
    .text("Menu");

// Closed, the panel is transparent, 0.5rem higher, and hidden from clicks and
// focus. While `open` is true, the style of `css_when` overrides all three,
// and the browser animates the change.
let panel = div::<Menu>()
    .css(
        Style::new()
            .opacity(Opacity::Number(0.0))
            .translate(Translate::Length(rem(-0.5)))
            .visibility(Visibility::Hidden)
            .transition_property(raw("opacity, translate, visibility"))
            .transition_duration(ms(200))
            .transition_timing_function(TransitionTimingFunction::EaseOut),
    )
    .css_when(
        Style::new()
            .opacity(Opacity::Number(1.0))
            .translate(Translate::Length(rem(0)))
            .visibility(Visibility::Visible),
        Menu::open(),
    )
    .text("Settings");

let html = Page::new("Menu", Menu { open: false })
    .child(toggle)
    .child(panel)
    .render();
assert!(html.contains("{transition-property:opacity, translate, visibility}"));

The panel uses visibility with the other two: visibility: hidden keeps the closed panel from taking clicks and focus, and in a transition the browser keeps it visible until the fade-out ends.

The shorthand and the longhands

transition is the shorthand, and takes a Time or raw, as in transition(raw("opacity 150ms ease")). Its rule is written before the rules of the longhands, so on one element transition_duration(ms(300)) overrides the duration in the shorthand whichever method is called first.

What does not compile

A duration is a Time built with ms or secs, and a bare number has no unit:

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);

What the types leave unchecked

  • The names in a raw list. A misspelled name such as raw("opactiy") is valid CSS that matches no property, so the change happens at once.
  • TransitionProperty::All. It also animates every other property that changes on the element, including ones that a later change of the page sets for other reasons.
  • An element that enters the page. An element added by a patch, or shown by El::show, has no earlier value to start from, so it appears at its final value. The CSS rule @starting-style gives it a starting value, and Style has no method for it, so it goes in a Stylesheet.

Next, Page transitions animates the change from one page to the next with the browser's view transitions.