Performance: Why transitions enhance user experience
Transitions affect the smoothness of visual changes and perceived responsiveness during user interactions.
import { fade } from 'svelte/transition'; let visible = false; function toggle() { visible = !visible; } {#if visible} <div transition:fade>Content fades in and out smoothly</div> {/if}
let visible = false; function toggle() { visible = !visible; } {#if visible} <div>Content appears instantly</div> {/if}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Instant show/hide without transition | Minimal | 1 reflow per toggle | High paint cost due to abrupt change | [X] Bad |
| Using Svelte fade transition | Minimal | Spread over frames, fewer abrupt reflows | Lower paint cost with smooth opacity animation | [OK] Good |
<script>
import { fade } from 'svelte/transition';
let visible = false;
</script>
<button on:click={() => visible = !visible}>Toggle</button>
{#if visible}
<p transition:fade>Hello World!</p>
{/if}<script>
import { fade } from 'svelte/transition';
let show = true;
</script>
{#if show}
<div transition=fade>Content</div>
{/if}
<button on:click={() => show = !show}>Toggle</button><script>
import { slide } from 'svelte/transition';
</script>
<div role="region" aria-live="polite" tabindex="0" transition:slide={{ duration: 500 }}>
Content here
</div> correctly imports 'slide' from 'svelte/transition' and uses transition:slide with duration option in object syntax.<script>
import { slide } from 'svelte/transition';
</script>
<div role="region" aria-live="polite" tabindex="0" transition:slide={{ duration: 500 }}>
Content here
</div> adds role="region", aria-live="polite", and tabindex="0" for screen readers and keyboard focus, improving accessibility.Pass the test and this pattern moves unknown