Reduced motion
Media::ReducedMotion applies a style for a visitor who asks the operating system for less motion, so an animation can stop, slow down, or become instant for them.
The condition
Media::ReducedMotion is the media query (prefers-reduced-motion: reduce). The browser matches it when the visitor turns on the reduce-motion setting of the operating system, and Chrome's DevTools can emulate it in the Rendering panel. Style::on(Media::ReducedMotion, style) writes the declarations of style inside @media (prefers-reduced-motion: reduce) { ... }, and those rules come after the element's base rules, so they override them for that visitor.
Media has no variant for no-preference. Write the motion in the element's base style, and change it under Media::ReducedMotion. A variant spelled after the CSS name does not exist:
use hypersig::style::value::{AnimationName, Opacity};
use hypersig::style::{Media, Style};
// error[E0599]: no variant, associated function, or constant named
// `PrefersReducedMotion` found for enum `hypersig::hypersig_style::Media`
let fade = Style::new()
.opacity(Opacity::Number(1.0))
.on(Media::PrefersReducedMotion, Style::new().animation_name(AnimationName::None));Ways to reduce motion
Under Media::ReducedMotion | The CSS | What the visitor sees |
|---|---|---|
animation_iteration_count(AnimationIterationCount::Number(0.0)) | animation-iteration-count: 0 | The animation does not run, and the element shows its own style |
animation_name(AnimationName::None) | animation-name: none | No animation is applied, and the element appears in place |
transition_duration(ms(0)) | transition-duration: 0ms | The new value applies at once |
animation_duration(ms(4000)) | animation-duration: 4000ms | A spinner that still turns, five times slower than at 800ms |
The first three remove the motion. The last one keeps a spinner turning, because it tells the visitor that the page is still working, and slows it down. The sample below uses the first three:
use hypersig::html::div;
use hypersig::style::value::{AnimationIterationCount, AnimationName, Opacity, Translate, ms, raw, rem};
use hypersig::style::{Keyframes, Media, Style};
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Feed {}
let pulse = Keyframes::new("pulse").at(50, Style::new().opacity(Opacity::Number(0.5)));
let slide = Keyframes::new("slide-in").at(0, Style::new().translate(Translate::Length(rem(1))));
// A loading placeholder: it stops pulsing for a visitor who asks for reduced
// motion, and stays at full opacity.
let skeleton = div::<Feed>().css(
Style::new()
.keyframes(&pulse)
.animation_duration(ms(2000))
.animation_iteration_count(AnimationIterationCount::Infinite)
.on(
Media::ReducedMotion,
Style::new().animation_iteration_count(AnimationIterationCount::Number(0.0)),
),
);
// A card that slides in from the right, and appears in place under reduced motion.
let card = div::<Feed>().css(
Style::new()
.keyframes(&slide)
.animation_duration(ms(300))
.on(Media::ReducedMotion, Style::new().animation_name(AnimationName::None)),
);
// A color change that is instant under reduced motion.
let link = div::<Feed>().css(
Style::new()
.transition_property(raw("color"))
.transition_duration(ms(150))
.on(Media::ReducedMotion, Style::new().transition_duration(ms(0))),
);
let html = Page::new("Feed", Feed {})
.child(skeleton)
.child(card)
.child(link)
.render();
assert!(html.contains("{animation-name:none}"));The components of hypersig_ui do the same: their color transitions take 0ms, the skeleton stops pulsing, and the spinner turns four times slower.
View transitions
The browser plays view transitions under reduced motion too. Their rules are in a Stylesheet, so the condition is written in CSS there: put the animations under @media (prefers-reduced-motion: no-preference), and set animation: none on ::view-transition-group(*), ::view-transition-old(*), and ::view-transition-new(*) under @media (prefers-reduced-motion: reduce). Without the second rule, a visitor who asks for reduced motion still sees the browser's default cross-fade. The stylesheet on Page transitions does both, and so does this site.
What the types leave unchecked
- Whether an animation has a reduced form. Nothing checks that a style with
keyframesor a transition also has a style underMedia::ReducedMotion. Without one, the visitor sees the full motion. - The motion in a
Stylesheetor inraw. A@mediawritten in the CSS text of aStylesheetis not checked, and a misspelled feature such asprefers-reduce-motionnever matches.
Next, Unchecked values lists every place where a string reaches the CSS without a check.