Anime le passage d'un article entre la liste et le panier

Le basculement passe par une View Transition : chaque tuile porte un
view-transition-name, la tuile glisse vers sa nouvelle place et les
autres se décalent. Sans support navigateur, rien ne change.

Closes #1

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Romulus21andClaude Opus 5.5 committed 2026-11-01 15:54:23 +01:00
1 parent 6c593b0201
commit 9aa81a22fe
6 files changed
+83 -4

No files matched your search

Generated
+1
View File
@@ -1397,6 +1397,7 @@ dependencies = [
"console_error_panic_hook", "console_error_panic_hook",
"dotenvy", "dotenvy",
"hex", "hex",
"js-sys",
"leptos", "leptos",
"leptos_axum", "leptos_axum",
"leptos_meta", "leptos_meta",
+2
View File
@@ -21,6 +21,7 @@ hex = { version = "0.4", optional = true }
dotenvy = { version = "0.15", optional = true } dotenvy = { version = "0.15", optional = true }
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
wasm-bindgen = { version = "=0.2.128", optional = true } wasm-bindgen = { version = "=0.2.128", optional = true }
js-sys = { version = "0.3", optional = true }
web-sys = { version = "0.3", features = ["CloseEvent", "Document", "Location", "MessageEvent", "VisibilityState", "WebSocket", "Window"], optional = true } web-sys = { version = "0.3", features = ["CloseEvent", "Document", "Location", "MessageEvent", "VisibilityState", "WebSocket", "Window"], optional = true }
send_wrapper = { version = "0.6", optional = true } send_wrapper = { version = "0.6", optional = true }
console_error_panic_hook = { version = "0.1", optional = true } console_error_panic_hook = { version = "0.1", optional = true }
@@ -30,6 +31,7 @@ hydrate = [
"leptos/hydrate", "leptos/hydrate",
"dep:console_error_panic_hook", "dep:console_error_panic_hook",
"dep:wasm-bindgen", "dep:wasm-bindgen",
"dep:js-sys",
"dep:web-sys", "dep:web-sys",
"dep:send_wrapper", "dep:send_wrapper",
] ]
+4 -1
View File
@@ -5,6 +5,7 @@ mod live;
mod sheets; mod sheets;
mod sync; mod sync;
mod tiles; mod tiles;
mod transition;
use std::future::Future; use std::future::Future;
@@ -128,8 +129,10 @@ impl Ctx {
self.sync.push(self, op); self.sync.push(self, op);
} }
/// Moves the item between the list and the basket, with an animation.
fn toggle(self, item: &Item) { fn toggle(self, item: &Item) {
self.change(ItemOp::SetChecked { id: item.id, checked: !item.checked }); let op = ItemOp::SetChecked { id: item.id, checked: !item.checked };
transition::animate(move || self.change(op));
} }
} }
+8 -3
View File
@@ -2,6 +2,7 @@ use leptos::ev;
use leptos::prelude::*; use leptos::prelude::*;
use super::icons::IconSvg; use super::icons::IconSvg;
use super::transition;
use super::{Ctx, ItemEditor}; use super::{Ctx, ItemEditor};
use crate::model::{Item, ItemInput, ItemOp, Suggestion}; use crate::model::{Item, ItemInput, ItemOp, Suggestion};
@@ -44,8 +45,8 @@ pub fn ItemTile(item: Item) -> impl IntoView {
open_editor(); open_editor();
}; };
let (name, icon, category, badge) = item.with_value(|item| { let (id, name, icon, category, badge) = item.with_value(|item| {
(item.name.clone(), item.icon, item.category, item.quantity.map(|q| q.label())) (item.id, item.name.clone(), item.icon, item.category, item.quantity.map(|q| q.label()))
}); });
let aria_label = format!("{name} : mettre dans le panier"); let aria_label = format!("{name} : mettre dans le panier");
@@ -55,6 +56,7 @@ pub fn ItemTile(item: Item) -> impl IntoView {
"tile-shadow relative flex aspect-square touch-manipulation flex-col items-center gap-1 rounded-2xl border-2 border-ink p-2 pb-2.5 text-center select-none active:translate-y-1 active:shadow-none [-webkit-touch-callout:none] {}", "tile-shadow relative flex aspect-square touch-manipulation flex-col items-center gap-1 rounded-2xl border-2 border-ink p-2 pb-2.5 text-center select-none active:translate-y-1 active:shadow-none [-webkit-touch-callout:none] {}",
category.tile_class(), category.tile_class(),
) )
style:view-transition-name=transition::item_name(id)
aria-label=aria_label aria-label=aria_label
on:click=on_click on:click=on_click
on:contextmenu=on_context_menu on:contextmenu=on_context_menu
@@ -85,6 +87,7 @@ pub fn SuggestionTile(suggestion: Suggestion, list_id: i64) -> impl IntoView {
let ctx = expect_context::<Ctx>(); let ctx = expect_context::<Ctx>();
let Suggestion { name, icon, category } = suggestion; let Suggestion { name, icon, category } = suggestion;
let label = format!("+ {name}"); let label = format!("+ {name}");
let transition_name = transition::suggestion_name(&name);
let add = move |_| { let add = move |_| {
let input = ItemInput { name: name.clone(), quantity: None, icon, category }; let input = ItemInput { name: name.clone(), quantity: None, icon, category };
@@ -94,6 +97,7 @@ pub fn SuggestionTile(suggestion: Suggestion, list_id: i64) -> impl IntoView {
view! { view! {
<button <button
class="relative flex aspect-square flex-col items-center gap-1 rounded-2xl border-2 border-dashed border-ink/35 p-2 pb-2.5 text-center text-ink/55 active:bg-ink/5" class="relative flex aspect-square flex-col items-center gap-1 rounded-2xl border-2 border-dashed border-ink/35 p-2 pb-2.5 text-center text-ink/55 active:bg-ink/5"
style:view-transition-name=transition_name
on:click=add on:click=add
> >
<IconSvg icon class="min-h-0 w-full flex-1 opacity-60" /> <IconSvg icon class="min-h-0 w-full flex-1 opacity-60" />
@@ -106,13 +110,14 @@ pub fn SuggestionTile(suggestion: Suggestion, list_id: i64) -> impl IntoView {
#[component] #[component]
pub fn BasketTile(item: Item) -> impl IntoView { pub fn BasketTile(item: Item) -> impl IntoView {
let ctx = expect_context::<Ctx>(); let ctx = expect_context::<Ctx>();
let (name, icon) = (item.name.clone(), item.icon); let (id, name, icon) = (item.id, item.name.clone(), item.icon);
let aria_label = format!("{name} : remettre dans la liste"); let aria_label = format!("{name} : remettre dans la liste");
let item = StoredValue::new(item); let item = StoredValue::new(item);
view! { view! {
<button <button
class="flex aspect-square flex-col items-center gap-0.5 rounded-2xl bg-mist p-1.5 text-center text-ink/45 active:bg-ink/10" class="flex aspect-square flex-col items-center gap-0.5 rounded-2xl bg-mist p-1.5 text-center text-ink/45 active:bg-ink/10"
style:view-transition-name=transition::item_name(id)
aria-label=aria_label aria-label=aria_label
on:click=move |_| item.with_value(|item| ctx.toggle(item)) on:click=move |_| item.with_value(|item| ctx.toggle(item))
> >
+50
View File
@@ -0,0 +1,50 @@
//! Animated DOM updates: tiles carrying a `view-transition-name` glide from
//! their old place to their new one, even across lists.
/// Runs `update` inside a view transition, or right away when the browser
/// does not support them.
pub fn animate(update: impl FnOnce() + 'static) {
#[cfg(feature = "hydrate")]
browser::animate(update);
#[cfg(not(feature = "hydrate"))]
update();
}
/// `view-transition-name` of an item tile, the same in both lists.
pub fn item_name(id: i64) -> String {
format!("item-{id}")
}
/// `view-transition-name` of a suggestion tile. Product names may contain
/// spaces or accents, which are not allowed in an identifier: hex-encode them.
pub fn suggestion_name(name: &str) -> String {
let hex: String = name.bytes().map(|b| format!("{b:02x}")).collect();
format!("suggestion-{hex}")
}
#[cfg(feature = "hydrate")]
mod browser {
use js_sys::{Function, Promise, Reflect};
use leptos::prelude::*;
use wasm_bindgen::JsCast;
use wasm_bindgen::closure::Closure;
pub fn animate(update: impl FnOnce() + 'static) {
let document = document();
let start = Reflect::get(&document, &"startViewTransition".into())
.ok()
.and_then(|start| start.dyn_into::<Function>().ok());
let Some(start) = start else {
return update();
};
let callback = Closure::once_into_js(move || {
update();
// Leptos patches the DOM in a microtask: the new state is
// ready by the next timeout.
Promise::new(&mut |resolve, _| {
let _ = window().set_timeout_with_callback(&resolve);
})
});
let _ = start.call1(&document, &callback);
}
}
+18
View File
@@ -55,3 +55,21 @@
html { html {
-webkit-tap-highlight-color: transparent; -webkit-tap-highlight-color: transparent;
} }
/* Tiles moving between the list and the basket (see src/app/transition.rs). */
::view-transition-group(*) {
animation-duration: 320ms;
animation-timing-function: cubic-bezier(0.2, 0.8, 0.2, 1);
}
::view-transition-old(*),
::view-transition-new(*) {
animation-duration: 200ms;
}
@media (prefers-reduced-motion: reduce) {
::view-transition-group(*),
::view-transition-old(*),
::view-transition-new(*) {
animation: none;
}
}