Performance: Crossfade for list items
This affects the smoothness of visual transitions and interaction responsiveness when list items change or reorder.
Use Svelte's crossfade function to coordinate animations between old and new list items with shared transitions.Each list item uses separate fade and slide animations triggered on mount and unmount without coordination.
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Independent fade/slide per item | Multiple mounts/unmounts | Multiple reflows per item | Multiple paints per item | [X] Bad |
| Svelte crossfade coordinated animation | Minimal DOM changes, reuses elements | Single reflow per animation cycle | Single paint and composite per cycle | [OK] Good |
crossfade in Svelte list items?crossfade function comes from svelte/transition.import { crossfade } from 'svelte/transition'; correctly imports crossfade from svelte/transition.const [send, receive] = crossfade({ duration: 300 });
let items = [1, 2, 3];
// Later items = [2, 3, 4];
What will happen visually when item 1 is removed and item 4 is added?const [send, receive] = crossfade({ duration: 400 });
let items = ["a", "b", "c"];
{#each items as item}
{{item}}
{/each}
What is the likely problem?key in the {#each} block, so animations fail.{#each} block to ensure proper crossfade animations?
let items = [
{ id: 1, name: 'Apple' },
{ id: 2, name: 'Banana' },
{ id: 3, name: 'Cherry' }
];
const [send, receive] = crossfade({ duration: 500 });
item.id is unique.in:receive={receive} and out:send={send} on the element.Pass the test and this pattern moves unknown