Transitions
You can use Svelte transitions just like you would any other component.
Let’s add a fade to our backdrop by adding transition:fade
<script>
import { Modals, closeModal } from 'svelte-modals'
import { fade } from 'svelte/transition'
</script>
<Modals>
<div
slot="backdrop"
class="backdrop"
transition:fade
on:click={closeModal}
/>
</Modals>
and let’s do the same for the modal
<script>
import { closeModal } from 'svelte-modals'
import { fade } from 'svelte/transition'
export let isOpen
export let title
export let message
</script>
{#if isOpen}
<div role="dialog" class="modal" transition:fade>
<div class="contents">
<h2>{title}</h2>
<p>{message}</p>
<div class="actions">
<button on:click={closeModal}>OK</button>
</div>
</div>
</div>
{/if}
Transitions between Modals
If you are opening one modal after another, the intro and outro transitions of both modals will overlap. Depending on your animation, this might be ok, but often it’s cleaner to transition one at a time.
You can do this by forwarding the on:introstart
and on:outroend
events in your modal components.
<script>
import { fade } from 'svelte/transition'
export let isOpen
</script>
{#if isOpen}
<div role="dialog" class="modal" transition:fade on:introstart on:outroend>
<!-- ... -->
</div>
{/if}
Let’s see how the transitions compare: