日本語

Page transitions

The browser's view transitions animate the move from one page to the next, and Stmt::view_transition animates a change within a page.

From one page to the next

A link from one page to another loads the new page, so the browser's cross-document view transitions animate the change. A page opts in with the CSS rule @view-transition { navigation: auto; }, and both the page left and the page reached need it. The browser captures a snapshot of the old page, shows the new one, and cross-fades between the two unless ::view-transition-old(root) and ::view-transition-new(root) set other animations. Style has no at-rule or pseudo-element for these, so they go in a Stylesheet, which Page::style adds to <head>:

use hypersig::html::{Stylesheet, header};
use hypersig::style::Style;
use hypersig::style::value::raw;
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Article {}

// Every page that takes part adds the same stylesheet.
const TRANSITIONS: Stylesheet = Stylesheet::new(
    "
@view-transition { navigation: auto; }
@media (prefers-reduced-motion: no-preference) {
  ::view-transition-old(root) { animation: 250ms ease-in both fade-out; }
  ::view-transition-new(root) { animation: 400ms ease-out both fade-in; }
}
@media (prefers-reduced-motion: reduce) {
  ::view-transition-group(*), ::view-transition-old(*), ::view-transition-new(*) { animation: none; }
}
@keyframes fade-out { to { opacity: 0; } }
@keyframes fade-in { from { opacity: 0; } }
",
);

let page = Page::new("Article", Article {})
    .style(TRANSITIONS)
    // A name of its own keeps the header in place while the rest moves.
    .child(header().css(Style::new().view_transition_name(raw("site-header"))));
assert!(page.render().contains("@view-transition"));

Chrome and Edge play cross-document view transitions from version 126, and Safari from 18.2. A browser without them changes the page as usual.

Elements that move on their own

An element with a view-transition-name moves on its own, from where it was on the old page to where it is on the new one. Style::view_transition_name takes a ViewTransitionName: None, MatchElement, or a name through raw. In the sample above, view_transition_name(raw("site-header")) writes view-transition-name: site-header, and the header, which keeps its place on both pages, stays still while the rest of the page changes.

ViewTransitionName has no conversion from a string, so a name written by hand is E0277:

use hypersig::style::Style;

// error[E0277]: the trait bound `hypersig::hypersig_style::value::ViewTransitionName:
//     From<&str>` is not satisfied
let header = Style::new().view_transition_name("site-header");

Within a page

A statement changes the page without loading another, and a patch from the server changes it in place, so neither plays a view transition on its own. Stmt::view_transition(body: impl IntoStmt<S>) -> Stmt<S> runs body inside document.startViewTransition: the browser captures a snapshot of the page, body runs and its writes render, and the two snapshots cross-fade unless the page's CSS sets other animations. The theme button in this site's header changes its colors this way.

use hypersig::html::button;
use hypersig::{Page, Signals, Stmt};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Gallery {
    grid: bool,
}

// The browser captures the page, the toggle renders, and the two cross-fade.
let switch = Stmt::view_transition(Gallery::grid().toggle());
let page = Page::new("Gallery", Gallery { grid: false })
    .child(button().on_click(switch).text("Switch layout"));
assert!(page.render().contains("transition"));
  • To animate the move between pages and the change within a page differently, give the navigation a type with @view-transition { navigation: auto; types: page; } and put its animations under html:active-view-transition-type(page). A transition from Stmt::view_transition has no type, so html:not(:active-view-transition-type(page)) selects it. This site writes its two animations that way.
  • Chrome and Edge run document.startViewTransition from version 111, Safari from 18, and Firefox from 144, according to MDN's compatibility data. A browser without it runs the statement with no animation. :active-view-transition-type() needs Chrome 125, Safari 18.2, or Firefox 147.
  • An error thrown while body runs is written to the console with the prefix hypersig:, and the transition still finishes.

What the types leave unchecked

  • The CSS of the Stylesheet. Stylesheet::new refuses only </style, and the browser drops a rule it cannot parse. A page that lacks @view-transition changes without an animation.
  • Unique names. The name in raw is not checked, and two elements of one page with the same view-transition-name make the browser skip the transition.
  • Reduced motion. The browser plays view transitions for a visitor who asks for reduced motion too, and Reduced motion shows the rules that turn them off.

Next, Reduced motion changes these animations for a visitor who asks for less motion.