Skip to content

Motion Containers

github-actions[bot] edited this page Nov 26, 2026 · 5 revisions

Motion Containers

The motion() element gives content declarative enter and exit animations. Use it for animated lists, page transitions, and conditional rendering. When the content appears, its enter animation starts. Your code starts an exit animation.

Basic Usage

use blinc::layout::motion::motion;

motion()
    .fade_in(300)     // Duration in milliseconds
    .child(my_content())

Animation Presets

Fade

motion()
    .fade_in(300)
    .fade_out(200)
    .child(content)

Scale

motion()
    .scale_in(300)    // Scale from 0 to 1
    .scale_out(200)   // Scale from 1 to 0
    .child(content)

Slide

use blinc::layout::motion::SlideDirection;

motion()
    .slide_in(SlideDirection::Left, 300)
    .slide_out(SlideDirection::Right, 200)
    .child(content)

// Available directions:
// SlideDirection::Top
// SlideDirection::Bottom
// SlideDirection::Left
// SlideDirection::Right

Bounce

motion()
    .bounce_in(400)   // Bouncy entrance
    .bounce_out(200)
    .child(content)

Pop

motion()
    .pop_in(300)      // Scale with overshoot
    .child(content)

There is no pop_out.

Exit

If the content goes away, its exit animation does not play. To start the animation, find the motion by its key and call exit():

use blinc::layout::selector::query_motion;

let panel = motion().fade_in(300).fade_out(200).child(content);
let key = panel.get_stable_key().to_string();

// Later, for example in a click handler:
query_motion(&key).exit();

Combining Animations

A motion has one enter animation and one exit animation. If you call two enter methods, the second one replaces the first. You can set one of each:

motion()
    .scale_in(300)
    .fade_out(200)
    .child(content)

Staggered Lists

You can animate list items with a delay between each item and the next:

use blinc::animation::AnimationPreset;
use blinc::layout::motion::{motion, StaggerConfig};

let items = vec!["Item 1", "Item 2", "Item 3", "Item 4"];

motion()
    .stagger(
        StaggerConfig::new(100, AnimationPreset::fade_in(300))
    )
    .children(
        items.iter().map(|item| {
            div()
                .p(3.0)
                .bg(Color::rgba(0.2, 0.2, 0.25, 1.0))
                .child(text(*item).color(Color::WHITE))
        })
    )

Stagger Configuration

let delay_ms = 100;
let preset = AnimationPreset::fade_in(300);

StaggerConfig::new(delay_ms, preset)
    .reverse()          // Animate last to first
    .from_center()      // Animate from center outward
    .limit(10)          // Only stagger first N items

Stagger Directions

let preset = || AnimationPreset::fade_in(300);

// Forward (default): 0, 1, 2, 3, 4...
StaggerConfig::new(100, preset());

// Reverse: 4, 3, 2, 1, 0...
StaggerConfig::new(100, preset()).reverse();

// From center: 2, 1/3, 0/4 (for 5 items)
StaggerConfig::new(100, preset()).from_center();

Binding to AnimatedValue

For continuous control of an animation, bind motion transforms to AnimatedValue:

use blinc::animation::SpringConfig;
use blinc::app::windowed::WindowedContext;

fn pull_to_refresh(ctx: &WindowedContext) -> impl ElementBuilder {
    let offset_y = ctx.use_animated_value(0.0, SpringConfig::wobbly());
    let icon_scale = ctx.use_animated_value(0.5, SpringConfig::snappy());
    let icon_opacity = ctx.use_animated_value(0.0, SpringConfig::snappy());

    stack()
        // Refresh icon (behind content)
        .child(
            motion()
                .scale(icon_scale.clone())
                .opacity(icon_opacity.clone())
                .child(refresh_icon())
        )
        // Content (translates down to reveal icon)
        .child(
            motion()
                .translate_y(offset_y.clone())
                .child(content_list())
        )
}

Example: Animated Card List

The button changes a signal. While the signal is true, show builds the cards. Each time the cards appear, they come in one after the other.

use blinc::animation::AnimationPreset;
use blinc::layout::motion::{motion, StaggerConfig};
use blinc::prelude::*;

fn animated_card_list(show_cards: Signal<bool>) -> impl ElementBuilder {
    let toggle = stateful::<ButtonState>()
        .deps([show_cards.id()])
        .on_state(move |ctx| {
            let label = if show_cards.get() { "Hide Cards" } else { "Show Cards" };
            let bg = match ctx.state() {
                ButtonState::Hovered => Color::rgba(0.4, 0.6, 1.0, 1.0),
                _ => Color::rgba(0.3, 0.5, 0.9, 1.0),
            };
            div().bg(bg).px(4.0).py(2.0).rounded(8.0)
                .child(text(label).color(Color::WHITE))
        })
        .on_click(move |_| show_cards.update(|shown| !shown));

    div()
        .flex_col()
        .gap(4.0)
        .child(toggle)
        .child(div().show(show_cards, card_list))
}

fn card_list() -> Motion {
    // Cards with staggered animation
    motion()
        .transient()
        .stagger(StaggerConfig::new(80, AnimationPreset::fade_in(300)))
        .children((0..5).map(|i| {
            div()
                .w(300.0)
                .p(4.0)
                .rounded(12.0)
                .bg(Color::rgba(0.15, 0.15, 0.2, 1.0))
                .child(text(format!("Card {}", i + 1)).color(Color::WHITE))
        }))
}

Example: Page Transition

For page navigation, use a signal. When the signal changes, build the page again. transient() makes the enter animation play each time.

use blinc::layout::motion::{motion, SlideDirection};
use blinc::prelude::*;

fn page_transition(current_page: Signal<u8>) -> impl ElementBuilder {
    stateful::<NoState>()
        .deps([current_page.id()])
        .on_state(move |_ctx| {
            // Render different content based on the current page signal
            let content = match current_page.get() {
                0 => div().child(text("Home Page").color(Color::WHITE)),
                1 => div().child(text("Settings Page").color(Color::WHITE)),
                _ => div().child(text("Profile Page").color(Color::WHITE)),
            };

            div().w_full().h_full().child(
                motion()
                    .transient()
                    .slide_in(SlideDirection::Right, 200)
                    .child(content),
            )
        })
}

// Navigate with the shared signal
fn nav_button(current_page: Signal<u8>, target: u8, label: &'static str) -> impl ElementBuilder {
    stateful::<ButtonState>()
        .on_state(move |ctx| {
            let bg = match ctx.state() {
                ButtonState::Hovered => Color::rgba(0.4, 0.6, 1.0, 1.0),
                _ => Color::rgba(0.3, 0.5, 0.9, 1.0),
            };
            div().bg(bg).px(4.0).py(2.0).rounded(8.0)
                .child(text(label).color(Color::WHITE))
        })
        .on_click(move |_| current_page.set(target))
}

Motion vs Manual Animation

Feature Motion AnimatedValue
Setup Declarative Imperative
Control Preset-based Full control
Enter/Exit Enter automatic, exit from your code Manual
Lists Stagger support Manual delays
Use case Transitions Interactive

Use motion for:

  • List item animations
  • Page transitions
  • Conditional content
  • Staggered reveals

Use AnimatedValue for:

  • Drag interactions
  • Hover effects
  • Custom physics
  • Continuous binding

Navigation

Getting Started

Migrating

Mobile Development

Example Gallery

Web Development

Core Concepts

Animation

Components

Component Library (blinc_cn)

Widgets

Canvas Kit

Advanced

Architecture

Contributing

Clone this wiki locally