Performance: Animate directive
The animate directive affects the smoothness and responsiveness of UI changes by controlling element transitions and layout animations.
<script> let items = [1, 2, 3]; function shuffle() { items = items.sort(() => Math.random() - 0.5); } </script> <ul animate:flip> {#each items as item (item)} <li>{item}</li> {/each} </ul> <button on:click={shuffle}>Shuffle</button>
<script> let items = [1, 2, 3]; function shuffle() { items = items.sort(() => Math.random() - 0.5); } </script> <ul> {#each items as item (item)} <li>{item}</li> {/each} </ul> <button on:click={shuffle}>Shuffle</button>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No animate directive on list reorder | Multiple node moves | Multiple reflows triggered | High paint cost due to layout shifts | [X] Bad |
| Using animate:flip directive | Same node moves batched | Single reflow batch | Lower paint cost with transform animations | [OK] Good |
animate:flip directive in Svelte?animate:flip doesanimate:flip directive animates elements smoothly when their position changes, especially in keyed {#each} blocks.animate:flip.animate:flip to apply the flip animation as a directive on elements.animate:flip correctly. Options A, B, and D use incorrect syntax such as use:animate, hyphenated directive, or object notation which are invalid here.{`
animate:flip on each li. When the array order changes, the directive animates the position changes smoothly.animate:flip directive animates the movement of items to their new positions.{`
animate:flip to animate position changes in a list, the {#each} block must be keyed to track items uniquely.animate:flip directive to ensure smooth animations when tasks move or disappear?{`
showDone = !showDone}>Toggle Done
task.id as key, which is correct for tracking items uniquely.animate:flip on the li elements animates position changes smoothly when tasks reorder or disappear due to filtering.Pass the test and this pattern moves unknown