Crossfade helps items smoothly change places or fade in and out in a list. It makes the list look nice and easy to follow.
Crossfade for list items in Svelte
import { crossfade } from 'svelte/transition'; const [send, receive] = crossfade({ duration: 400, fallback(node, params) { return { duration: 400, css: t => `opacity: ${t}` }; } }); <ul> {#each items as item (item.id)} <li in:receive={{ key: item.id }} out:send={{ key: item.id }}>{item.text}</li> {/each} </ul>
You import crossfade from svelte/transition.
It returns two functions: send and receive. Use them as out:send and in:receive on list items.
import { crossfade } from 'svelte/transition'; const [send, receive] = crossfade(); <ul> {#each items as item (item.id)} <li in:receive={{ key: item.id }} out:send={{ key: item.id }}>{item.text}</li> {/each} </ul>
const [send, receive] = crossfade({ duration: 600 });
const [send, receive] = crossfade({ fallback(node) { return { duration: 300, css: t => `transform: scale(${t}); opacity: ${t}` }; } });
This Svelte component shows a list of fruits. You can add new fruits or remove existing ones. When items are added or removed, they smoothly crossfade in or out. The list items are keyboard accessible and the buttons have ARIA labels for screen readers.
<script> import { crossfade } from 'svelte/transition'; import { cubicOut } from 'svelte/easing'; let items = [ { id: 1, text: 'Apple' }, { id: 2, text: 'Banana' }, { id: 3, text: 'Cherry' } ]; const [send, receive] = crossfade({ duration: 500, easing: cubicOut, fallback(node) { return { duration: 500, css: t => `opacity: ${t}` }; } }); function removeItem(id) { items = items.filter(item => item.id !== id); } function addItem() { const newId = items.length ? Math.max(...items.map(i => i.id)) + 1 : 1; items = [...items, { id: newId, text: `Fruit ${newId}` }]; } </script> <button on:click={addItem} aria-label="Add item">Add Item</button> <ul> {#each items as item (item.id)} <li in:receive={{ key: item.id }} out:send={{ key: item.id }} tabindex="0"> {item.text} <button on:click={() => removeItem(item.id)} aria-label="Remove {item.text}">Remove</button> </li> {/each} </ul> <style> ul { list-style: none; padding: 0; max-width: 300px; margin-top: 1rem; } li { background: #def; margin: 0.5rem 0; padding: 0.5rem 1rem; border-radius: 0.5rem; display: flex; justify-content: space-between; align-items: center; } button { background: #48a; color: white; border: none; border-radius: 0.3rem; padding: 0.3rem 0.6rem; cursor: pointer; } button:hover, button:focus { background: #357; outline: none; } </style>
The crossfade animation runs in about 400-500 milliseconds by default.
Time complexity depends on the number of items but is usually fast enough for typical UI lists.
Common mistake: forgetting to add a unique key to each list item, which breaks the animation.
Use crossfade when you want smooth transitions between list states. For simple fade in/out, use fade transition instead.
Crossfade makes list items smoothly move or fade when added, removed, or reordered.
Use send and receive functions from svelte/transition on list items.
Always provide unique keys to list items for correct animation.
Practice
crossfade in Svelte list items?Solution
Step 1: Understand crossfade usage context
Crossfade is used to animate transitions between list states, such as adding or removing items.Step 2: Identify the animation effect
It creates smooth movement or fading effects for list items during changes.Final Answer:
To smoothly animate items when they are added, removed, or reordered in a list -> Option CQuick Check:
Crossfade = smooth list animations [OK]
- Confusing crossfade with data fetching
- Thinking crossfade sorts items
- Using crossfade for input validation
Solution
Step 1: Recall correct import source
Thecrossfadefunction comes fromsvelte/transition.Step 2: Match correct import syntax
import { crossfade } from 'svelte/transition';correctly importscrossfadefromsvelte/transition.Final Answer:
import { crossfade } from 'svelte/transition'; -> Option DQuick Check:
crossfade from svelte/transition [OK]
- Importing crossfade from 'svelte/animate'
- Trying to import send, receive directly
- Incorrect import paths or names
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?Solution
Step 1: Understand crossfade behavior on list changes
When items are removed or added, crossfade animates their disappearance and appearance smoothly.Step 2: Apply to given items change
Item 1 fades out smoothly, and new item 4 fades in with a smooth movement effect.Final Answer:
Item 1 fades out smoothly and item 4 fades in smoothly with movement -> Option AQuick Check:
Crossfade animates add/remove smoothly [OK]
- Expecting instant disappearance or appearance
- Thinking items blink without animation
- Assuming no visual update happens
const [send, receive] = crossfade({ duration: 400 });
let items = ["a", "b", "c"];
{#each items as item}
{{item}}
{/each}
What is the likely problem?Solution
Step 1: Check list rendering with crossfade
Crossfade requires unique keys on list items to track them for animation.Step 2: Identify missing key usage
The code does not provide akeyin the{#each}block, so animations fail.Final Answer:
You forgot to add a unique key to each list item -> Option AQuick Check:
Unique keys needed for crossfade animations [OK]
- Confusing send and receive usage
- Wrong import source for crossfade
- Setting duration as string instead of number
{#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 });
Solution
Step 1: Use unique keys for crossfade
Crossfade requires a unique key per item to track animations; here,item.idis unique.Step 2: Use correct transition directives
Usein:receive={receive}andout:send={send}on the element.Final Answer:
{#each items as item (item.id)} <div in:receive={receive} out:send={send}>{item.name}</div> {/each} -> Option BQuick Check:
Unique key + in:receive out:send = correct crossfade [OK]
- Omitting keys causing animation failure
- Using transition:fade instead of send/receive
- Using non-unique keys like item.name
Ready to prove it?
Pass the test and this pattern moves unknown
