motion
Automatic motion for lists and shows, every preset, morphs between a feed and a post, the edge-swipe back gesture, and on /islands: a boundary with derived and remembered skeletons, boundaries with fallbacks and retry, and islands loaded when idle, pressed or visible.
Run it
cargo run -p duolith-cli -- dev examples/motion # live, reloads on save
cargo run -p duolith-cli -- build examples/motion # static output in examples/motion/dist
The folder on the web: examples/motion.
Features it shows
| Feature | Where | Screenshot |
|---|---|---|
morph on native: a FLIP copy flies from the feed's thumbnail to the post's picture and back, nothing with reduced motion | motion pages/index.duo to pages/post/[id].duo | packages/engine/test/native-morph-in-browser.test.ts |
Islands on native: idle, interaction (a "Load" skeleton) and visible (Lynx's exposure events), with a boundary skeleton until then | motion pages/islands.duo | packages/engine/test/native-morph-in-browser.test.ts |
| Remembered skeleton shapes kept per app through the storage effect, drawn at that height on the next launch | motion pages/islands.duo | packages/engine/test/native-morph-in-browser.test.ts |
| Automatic motion: rows and show sides play in and out, moved rows glide (FLIP), reduced motion respected | motion pages/index.duo (Add, Shuffle, Remove) | motion-feed.png |
Motion presets: motion="fade|scale|slide-up|slide-down|slide-left|slide-right|spring|none" | motion pages/index.duo (Presets) | motion-presets.png |
morph: an element carried between screens (View Transitions, or a FLIP copy) | motion pages/index.duo to pages/post/[id].duo | motion-post.png |
Edge-swipe back on touch (from the right edge under dir="rtl"), the screens following the finger | motion pages/post/[id].duo | motion-post.png |
Islands: load="visible|idle|interaction" on a placed component, its code loaded only then | motion pages/islands.duo (Islands) | motion-islands.png |
<boundary> with <fallback as="failure"> and failure.retry() | motion pages/islands.duo | motion-islands.png |
Skeletons: derived from the layout with loading={...}, remembered with remember="name" | motion pages/islands.duo | motion-skeleton.png |
Source
components/Chart.duo
---
import { props } from "@duolith/client";
let { broken = false } = props<{ broken?: boolean }>();
// A chart whose data cannot be read throws while it mounts: its boundary shows the fallback.
if (broken) throw new Error("The chart's data could not be read.");
const bars = [40, 72, 55, 90, 64];
---
<view class="row items-end gap-2 p-4 rounded-lg bg-surface border chart">
<view class="bar rounded-sm bg-primary" class:b0={true} />
<view class="bar rounded-sm bg-primary" class:b1={true} />
<view class="bar rounded-sm bg-primary" class:b2={true} />
<view class="bar rounded-sm bg-primary" class:b3={true} />
<view class="bar rounded-sm bg-primary" class:b4={true} />
<text class="text-caption text-muted">{bars.length} weeks</text>
</view>
<style>
.chart { height: 140px; }
.bar { width: 28px; }
.b0 { height: 40%; }
.b1 { height: 72%; }
.b2 { height: 55%; }
.b3 { height: 90%; }
.b4 { height: 64%; }
</style>components/Comments.duo
---
import { props } from "@duolith/client";
let { title } = props<{ title: string }>();
const comments = [
{ id: "c1", author: "Ada", text: "The light on the water is perfect." },
{ id: "c2", author: "Grace", text: "Worth the early start." },
{ id: "c3", author: "Alan", text: "Which pier is this?" },
];
---
<view class="gap-2 p-4 rounded-lg bg-surface border">
<text class="text-label text-ink">{title}</text>
<list each={comments} as="comment" key={comment.id} class="gap-2">
<view class="gap-1">
<text class="text-caption text-muted">{comment.author}</text>
<text class="text-body text-ink">{comment.text}</text>
</view>
</list>
</view>pages/index.duo
---
import { state } from "@duolith/client";
type Post = { id: number; title: string; tone: number };
const titles = ["Harbour at dawn", "Desk, again", "The moth", "River walk", "Splashdown", "Fountain pen", "Night ferry"];
let posts = state<Post[]>(
[1, 2, 3, 4].map((id) => ({ id, title: titles[id - 1] ?? "", tone: (id - 1) % 6 })),
);
let nextId = 5;
let showPresets = state(false);
// A new post comes in at the top: it plays in, and the rows below glide down.
function add() {
const id = nextId++;
posts = [{ id, title: titles[(id - 1) % titles.length] ?? "", tone: (id - 1) % 6 }, ...posts];
}
// The last post moves to the top: every row glides to its new place.
function shuffle() {
const last = posts[posts.length - 1];
if (last === undefined) return;
posts = [last, ...posts.slice(0, -1)];
}
function remove(post: Post) {
posts = posts.filter((each) => each.id !== post.id);
}
---
<screen title="Motion" class="p-4 gap-4 bg-canvas">
<view class="gap-1">
<text class="text-title text-ink">Motion</text>
<text class="text-caption text-muted">
Rows that come, go and move animate by themselves. Open a post: its picture travels to the
next screen. On a phone, swipe from the leading edge to go back: the left one, or the right
one in a right-to-left language.
</text>
</view>
<view class="row gap-2">
<button class="px-4 py-2 rounded-md bg-primary text-on-primary text-label" on:press={add}>
Add
</button>
<button class="px-4 py-2 rounded-md bg-surface border text-label text-ink" on:press={shuffle}>
Shuffle
</button>
<button
class="px-4 py-2 rounded-md bg-surface border text-label text-ink"
on:press={() => (showPresets = !showPresets)}
>
{showPresets ? "Hide presets" : "Presets"}
</button>
</view>
<view class="row wrap gap-2">
<show when={showPresets}>
<view motion="fade" class="px-3 py-2 rounded-md bg-surface border">
<text class="text-label text-ink">fade</text>
</view>
<view motion="scale" class="px-3 py-2 rounded-md bg-surface border">
<text class="text-label text-ink">scale</text>
</view>
<view motion="slide-up" class="px-3 py-2 rounded-md bg-surface border">
<text class="text-label text-ink">slide-up</text>
</view>
<view motion="slide-left" class="px-3 py-2 rounded-md bg-surface border">
<text class="text-label text-ink">slide-left</text>
</view>
<view motion="spring" class="px-3 py-2 rounded-md bg-surface border">
<text class="text-label text-ink">spring</text>
</view>
</show>
</view>
<list each={posts} as="post" key={post.id} class="gap-3">
<view class="row gap-3 items-center p-3 rounded-lg bg-surface border">
<view href={`/post/${post.id}`} class="row gap-3 items-center grow pressed:opacity-75">
<view
morph={`post-${post.id}`}
class="thumb rounded-md"
class:tone-0={post.tone === 0}
class:tone-1={post.tone === 1}
class:tone-2={post.tone === 2}
class:tone-3={post.tone === 3}
class:tone-4={post.tone === 4}
class:tone-5={post.tone === 5}
/>
<view class="gap-1">
<text class="text-label text-ink">{post.title}</text>
<text class="text-caption text-muted">Post {post.id}</text>
</view>
</view>
<button
class="px-3 py-2 rounded-md text-label text-danger"
label={`Remove ${post.title}`}
on:press={() => remove(post)}
>
Remove
</button>
</view>
</list>
<view href="/islands" class="p-3 rounded-md bg-surface border items-center">
<text class="text-label text-primary">Islands, boundaries and skeletons ›</text>
</view>
</screen>
<style>
.thumb {
width: 56px;
height: 56px;
}
.tone-0 { background: #d9793b; }
.tone-1 { background: #3b7fd9; }
.tone-2 { background: #6a9e4f; }
.tone-3 { background: #9a5fc4; }
.tone-4 { background: #c9a227; }
.tone-5 { background: #d14d6a; }
</style>pages/islands.duo
---
import { state } from "@duolith/client";
import { Skeleton } from "@duolith/kit";
import Chart from "../components/Chart.duo";
import Comments from "../components/Comments.duo";
type Post = { id: number; author: string; caption: string };
let loading = state(true);
let posts = state<Post[]>([]);
let chartBroken = state(true);
let taps = state(0);
// Stands in for a request: the skeleton shows until the posts arrive.
function load() {
loading = true;
setTimeout(() => {
posts = [
{ id: 1, author: "Ada", caption: "First light over the harbour." },
{ id: 2, author: "Linus", caption: "New desk, same bugs." },
{ id: 3, author: "Grace", caption: "Found the moth." },
];
loading = false;
}, 1200);
}
load();
// Past three taps the count cannot be shown: the update throws, and only its boundary falls back.
function shown(count: number): string {
if (count > 3) throw new Error("The counter overflowed.");
return `${count} of 3 taps`;
}
---
<screen title="Islands" class="p-4 gap-4 bg-canvas">
<view class="row items-center justify-between">
<text class="text-label text-primary" href="/">‹ Motion</text>
<button class="px-3 py-2 rounded-md bg-surface border text-label text-ink" on:press={load}>
Reload
</button>
</view>
<text class="text-title text-ink">Islands</text>
<text class="text-label text-muted">A boundary with a skeleton</text>
<!-- While loading, the skeleton has the shape of the rows below; after the first load, the shape they last had. -->
<boundary loading={loading} remember="islands-posts">
<list each={posts} as="post" key={post.id} class="gap-3">
<view class="row gap-3 items-center p-3 rounded-lg bg-surface border">
<view class="avatar rounded-full bg-raised" />
<view class="gap-1 grow">
<text class="text-label text-ink">{post.author}</text>
<text class="text-body text-muted">{post.caption}</text>
</view>
</view>
</list>
</boundary>
<text class="text-label text-muted">A boundary with a fallback</text>
<boundary>
<Chart broken={chartBroken} />
<fallback as="failure">
<view role="alert" class="gap-2 p-4 rounded-lg bg-surface border items-center">
<text class="text-label text-danger">{failure.message}</text>
<button
class="px-4 py-2 rounded-md bg-primary text-on-primary text-label"
on:press={() => {
chartBroken = false;
failure.retry();
}}
>
Fix and try again
</button>
</view>
</fallback>
</boundary>
<boundary>
<view class="row items-center justify-between p-3 rounded-lg bg-surface border">
<text class="text-body text-ink">{shown(taps)}</text>
<button
class="px-3 py-2 rounded-md bg-primary text-on-primary text-label"
on:press={() => (taps = taps + 1)}
>
Tap
</button>
</view>
</boundary>
<text class="text-caption text-muted">
Tapped {taps} times: the rest of the screen carries on.
</text>
<text class="text-label text-muted">Islands</text>
<Comments title="Loaded when idle" load="idle" />
<Comments title="Loaded when pressed or focused" load="interaction" />
<view class="tall items-center justify-center gap-2">
<Skeleton lines={2} width="1/2" label="Scroll on for the last island" />
</view>
<Comments title="Loaded when visible" load="visible" />
</screen>
<style>
.avatar {
width: 40px;
height: 40px;
}
.tall {
min-height: 900px;
}
</style>pages/post/[id].duo
---
import { back, canGoBack, route } from "@duolith/client";
let { id } = route();
const number = Number(id);
const tone = (number - 1) % 6;
---
<screen title={`Post ${id}`} class="bg-canvas">
<!-- The same morph name as the feed's thumbnail: the picture travels from there to here. -->
<view
morph={`post-${id}`}
class="picture"
class:tone-0={tone === 0}
class:tone-1={tone === 1}
class:tone-2={tone === 2}
class:tone-3={tone === 3}
class:tone-4={tone === 4}
class:tone-5={tone === 5}
/>
<view class="p-4 gap-3">
<text class="text-title text-ink">Post {id}</text>
<text class="text-body text-muted">
The picture came from the feed's thumbnail. Go back, or swipe from the leading edge on a
phone, and it returns to its place in the list.
</text>
<show when={canGoBack()}>
<button class="px-4 py-2 rounded-md bg-primary text-on-primary text-label" on:press={back}>
Back
</button>
</show>
<otherwise>
<view href="/" class="px-4 py-2 rounded-md bg-surface border items-center">
<text class="text-label text-primary">Feed</text>
</view>
</otherwise>
</view>
</screen>
<style>
.picture {
width: 100%;
height: 280px;
}
.tone-0 { background: #d9793b; }
.tone-1 { background: #3b7fd9; }
.tone-2 { background: #6a9e4f; }
.tone-3 { background: #9a5fc4; }
.tone-4 { background: #c9a227; }
.tone-5 { background: #d14d6a; }
</style>