Performance: Animate directive
MEDIUM IMPACT
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 |