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:
1 parent
6c593b0201
commit
9aa81a22fe
6 files changed
+83
-4
No files matched your search
+4
-1
@@ -5,6 +5,7 @@ mod live;
|
||||
mod sheets;
|
||||
mod sync;
|
||||
mod tiles;
|
||||
mod transition;
|
||||
|
||||
use std::future::Future;
|
||||
|
||||
@@ -128,8 +129,10 @@ impl Ctx {
|
||||
self.sync.push(self, op);
|
||||
}
|
||||
|
||||
/// Moves the item between the list and the basket, with an animation.
|
||||
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
@@ -2,6 +2,7 @@ use leptos::ev;
|
||||
use leptos::prelude::*;
|
||||
|
||||
use super::icons::IconSvg;
|
||||
use super::transition;
|
||||
use super::{Ctx, ItemEditor};
|
||||
use crate::model::{Item, ItemInput, ItemOp, Suggestion};
|
||||
|
||||
@@ -44,8 +45,8 @@ pub fn ItemTile(item: Item) -> impl IntoView {
|
||||
open_editor();
|
||||
};
|
||||
|
||||
let (name, icon, category, badge) = item.with_value(|item| {
|
||||
(item.name.clone(), item.icon, item.category, item.quantity.map(|q| q.label()))
|
||||
let (id, name, icon, category, badge) = item.with_value(|item| {
|
||||
(item.id, item.name.clone(), item.icon, item.category, item.quantity.map(|q| q.label()))
|
||||
});
|
||||
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] {}",
|
||||
category.tile_class(),
|
||||
)
|
||||
style:view-transition-name=transition::item_name(id)
|
||||
aria-label=aria_label
|
||||
on:click=on_click
|
||||
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 Suggestion { name, icon, category } = suggestion;
|
||||
let label = format!("+ {name}");
|
||||
let transition_name = transition::suggestion_name(&name);
|
||||
|
||||
let add = move |_| {
|
||||
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! {
|
||||
<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"
|
||||
style:view-transition-name=transition_name
|
||||
on:click=add
|
||||
>
|
||||
<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]
|
||||
pub fn BasketTile(item: Item) -> impl IntoView {
|
||||
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 item = StoredValue::new(item);
|
||||
|
||||
view! {
|
||||
<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"
|
||||
style:view-transition-name=transition::item_name(id)
|
||||
aria-label=aria_label
|
||||
on:click=move |_| item.with_value(|item| ctx.toggle(item))
|
||||
>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user