0
0
Svelteframework~20 mins

Animate directive in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Animate Directive Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when the animate directive is used with a keyed each block?

Consider a Svelte component that uses the animate:flip directive inside a keyed {#each} block. What is the visible effect when the list order changes?

Svelte
<script>
  import { flip } from 'svelte/animate';
  let items = [1, 2, 3];
  function shuffle() {
    items = [...items].sort(() => Math.random() - 0.5);
  }
</script>
<button on:click={shuffle}>Shuffle</button>
<ul>
  {#each items as item (item)}
    <li animate:flip>{item}</li>
  {/each}
</ul>
AThe list items instantly change order without any animation.
BThe list items smoothly move to their new positions with a flip animation when shuffled.
CThe list items fade out and fade back in when shuffled.
DThe list items disappear and do not reappear after shuffling.
Attempts:
2 left
💡 Hint

Think about what the flip animation does in Svelte when elements change position.

📝 Syntax
intermediate
2:00remaining
Which option correctly applies the animate directive with a custom function?

Given a custom animation function myAnimate, which syntax correctly applies it to an element in Svelte?

Svelte
function myAnimate(node, params) {
  // animation logic
  return {
    delay: 0,
    duration: 300
  };
}
A<div animate:myAnimate(params)>Content</div>
B<div animate={myAnimate(params)}>Content</div>
C<div use:animate={myAnimate(params)}>Content</div>
D<div animate:myAnimate={params}>Content</div>
Attempts:
2 left
💡 Hint

Remember the syntax for directives with parameters in Svelte.

🔧 Debug
advanced
2:00remaining
Why does the animate directive not trigger on element removal?

In this Svelte code, the animate:flip directive is used on list items. When an item is removed from the list, no animation plays. Why?

Svelte
<script>
  import { flip } from 'svelte/animate';
  let items = [1, 2, 3];
  function remove() {
    items = items.slice(0, -1);
  }
</script>
<button on:click={remove}>Remove last</button>
<ul>
  {#each items as item (item)}
    <li animate:flip>{item}</li>
  {/each}
</ul>
AThe animate directive only animates position changes, not element removal; use transitions for removal animations.
BThe flip animation requires a key on the each block to work on removals, which is missing here.
CThe animate directive must be combined with the transition directive to animate removals.
DThe flip animation only works on additions, not removals or moves.
Attempts:
2 left
💡 Hint

Think about what the animate directive is designed to do versus transitions.

state_output
advanced
2:00remaining
What is the final order of items after this animation sequence?

Given this Svelte component using animate:flip, what is the final order of items displayed after clicking the button twice?

Svelte
<script>
  import { flip } from 'svelte/animate';
  let items = [1, 2, 3, 4];
  function reorder() {
    items = [4, 3, 2, 1];
  }
</script>
<button on:click={reorder}>Reverse</button>
<ul>
  {#each items as item (item)}
    <li animate:flip>{item}</li>
  {/each}
</ul>
A[1, 4, 3, 2]
B[1, 2, 3, 4]
C[4, 3, 2, 1]
D[2, 3, 4, 1]
Attempts:
2 left
💡 Hint

Consider what the reorder function does to the items array.

🧠 Conceptual
expert
3:00remaining
Why is the animate directive preferred over manual CSS animations for list reordering?

In Svelte, why might you choose the animate:flip directive instead of writing manual CSS animations to handle list reordering?

ABecause <code>animate:flip</code> automatically calculates element position changes and animates smoothly without manual keyframes.
BBecause CSS animations cannot animate position changes at all.
CBecause <code>animate:flip</code> disables browser repainting, improving performance drastically.
DBecause manual CSS animations require JavaScript to trigger, while <code>animate:flip</code> does not.
Attempts:
2 left
💡 Hint

Think about what the flip animation does behind the scenes.