The animate directive

In the previous chapter, we used deferred transitions to create the illusion of motion as elements move from one todo list to the other.

To complete the illusion, we also need to apply motion to the elements that aren’t transitioning. For this, we use the animate directive.

First, import the flip function — flip stands for ‘First, Last, Invert, Play’ — from svelte/animate:

  1. import { flip } from 'svelte/animate';

Then add it to the <label> elements:

  1. <label
  2. in:receive="{{key: todo.id}}"
  3. out:send="{{key: todo.id}}"
  4. animate:flip
  5. >

The movement is a little slow in this case, so we can add a duration parameter:

  1. <label
  2. in:receive="{{key: todo.id}}"
  3. out:send="{{key: todo.id}}"
  4. animate:flip="{{duration: 200}}"
  5. >

duration can also be a d => milliseconds function, where d is the number of pixels the element has to travel

Note that all the transitions and animations are being applied with CSS, rather than JavaScript, meaning they won’t block (or be blocked by) the main thread.