<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mark Volkmann</title>
    <description>The latest articles on DEV Community by Mark Volkmann (@mvolkmann).</description>
    <link>https://dev.to/mvolkmann</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F493119%2F8899a48a-fc49-45c2-b18b-c48b09e86482.jpeg</url>
      <title>DEV Community: Mark Volkmann</title>
      <link>https://dev.to/mvolkmann</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mvolkmann"/>
    <language>en</language>
    <item>
      <title>Implementing Svelte Custom Transitions</title>
      <dc:creator>Mark Volkmann</dc:creator>
      <pubDate>Sun, 16 May 2021 14:04:06 +0000</pubDate>
      <link>https://dev.to/mvolkmann/implementing-svelte-custom-transitions-123h</link>
      <guid>https://dev.to/mvolkmann/implementing-svelte-custom-transitions-123h</guid>
      <description>&lt;p&gt;#  Implementing Custom Transitions&lt;br&gt;
From &lt;a href="https://www.manning.com/books/svelte-and-sapper-in-action?utm_source=blog&amp;amp;utm_medium=organic&amp;amp;utm_campaign=book_volkmann_svelte_8_19_20&amp;amp;utm_content=devto" rel="noopener noreferrer"&gt;&lt;em&gt;Svelte and Sapper in Action&lt;/em&gt;&lt;/a&gt; by Mark Volkmann&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgzu2oppmo0z9onkmv2an.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgzu2oppmo0z9onkmv2an.jpg" alt="Alt Text" width="164" height="206"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This article walks through implementing custom transitions with Svelte and Sapper.&lt;/p&gt;

&lt;p&gt;Take 40% off &lt;a href="https://www.manning.com/books/svelte-and-sapper-in-action?utm_source=blog&amp;amp;utm_medium=organic&amp;amp;utm_campaign=book_volkmann_svelte_8_19_20&amp;amp;utm_content=devto" rel="noopener noreferrer"&gt;&lt;em&gt;Svelte and Sapper in Action&lt;/em&gt;&lt;/a&gt; by entering &lt;strong&gt;fccvolkmann&lt;/strong&gt; into the discount code box at checkout at &lt;a href="https://www.manning.com/?utm_source=blog&amp;amp;utm_medium=organic&amp;amp;utm_campaign=book_volkmann_svelte_8_19_20&amp;amp;utm_content=devto" rel="noopener noreferrer"&gt;manning.com&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Implementing custom transitions is easy. The only thing required is to write a function that follows a few basic rules. The function should take two arguments, the DOM node to be transitioned and an options object. Examples of options include:&lt;/p&gt;

&lt;p&gt;• &lt;code&gt;delay&lt;/code&gt; - This is the number of milliseconds to wait before the transition begins.&lt;br&gt;
• &lt;code&gt;duration&lt;/code&gt; - This is the number of milliseconds over which the transition should occur.&lt;br&gt;
• &lt;code&gt;easing&lt;/code&gt; - This is an easing function that takes a value between zero and one, and returns a value in that same range.&lt;/p&gt;

&lt;p&gt;Options which are specific to a given transition can also be provided. For example, the &lt;code&gt;fly&lt;/code&gt; transition accepts &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; options.&lt;/p&gt;

&lt;p&gt;The function must return an object whose properties include the transition options and a &lt;code&gt;css&lt;/code&gt; method. The &lt;code&gt;css&lt;/code&gt; method must return the appropriate CSS string for the number between zero and one which is returned by calling the &lt;code&gt;easing&lt;/code&gt; function. Svelte takes care of honoring the &lt;code&gt;delay&lt;/code&gt; and &lt;code&gt;duration&lt;/code&gt; options.&lt;/p&gt;

&lt;p&gt;The transition options returned can be given default values which are used when they aren’t passed to the custom function. For example, default values for &lt;code&gt;duration&lt;/code&gt; and &lt;code&gt;easing&lt;/code&gt; can be provided.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;css&lt;/code&gt; method has passed a time value between zero and one. It must return a string containing CSS properties to be applied to the DOM node for that time value. Examples of CSS properties which might vary over time include opacity, size, font size, position, rotation, and color.&lt;/p&gt;

&lt;p&gt;Here’s an example of a custom transition. It animates the scale and rotation of an element to make it appear to spiral down a drain when removed from the DOM. We apply this to a div element containing the text "Take me for a spin!" sized to wrap to two lines. Press the "Toggle" button to toggle between hiding and showing the &lt;code&gt;div&lt;/code&gt; element. Check the "Springy" checkbox to use the &lt;code&gt;backInOut&lt;/code&gt; easing function instead of the &lt;code&gt;linear&lt;/code&gt; easing function.&lt;/p&gt;

&lt;p&gt;Figure 1. custom transition&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fubo2op3hs0r0el7lug1o.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fubo2op3hs0r0el7lug1o.jpg" alt="Alt Text" width="800" height="463"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Copy this code to the REPL to try it.&lt;br&gt;
Listing 1. src/App.svelte&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;script&amp;gt;
  import {backInOut, linear} from 'svelte/easing';

  let springy = false;
  $: duration = springy ? 2000 : 1000;
  $: easing = springy ? backInOut : linear;
  $: options = {duration, easing, times: 2};

  let show = true;
  const toggle = () =&amp;gt; show = !show;

  function spin(node, options) {
    const {easing, times = 1} = options;
    return {
      ...options,
      css(t) {                              1 
        const eased = easing(t);            2
        const degrees = 360 * times;        3
        return `transform: scale(${eased}) rotate(${eased * degrees}deg);`;
      }
    };
  }
&amp;lt;/script&amp;gt;

&amp;lt;label&amp;gt;
  &amp;lt;input type="checkbox" bind:checked={springy} /&amp;gt; Springy
&amp;lt;/label&amp;gt;
&amp;lt;div&amp;gt;duration = {duration}&amp;lt;/div&amp;gt;
&amp;lt;button on:click={toggle}&amp;gt;Toggle&amp;lt;/button&amp;gt;

{#if show}
  &amp;lt;div class="center" in:spin={options} out:spin={options}&amp;gt;        4
    &amp;lt;div class="content"&amp;gt;Take me for a spin!&amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
{/if}

&amp;lt;style&amp;gt;
  .center {                             5
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }

  .content {
                                        6
    position: absolute;
    transform: translate(-50%, -50%);

    font-size: 64px;
    text-align: center;
    width: 300px;
  }
&amp;lt;/style&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;❶ The value of t passed to the css method varies between zero and one during an "in" transition and between one and zero during an "out" transition.&lt;br&gt;
❷ Recall that easing functions return a value between zero and one inclusive.&lt;br&gt;
❸ This is the degrees through which to spin.&lt;br&gt;
❹ The reasons for using in and out instead of transition is explained in the book.&lt;br&gt;
❺ This has a width and height of zero and it’s only used to center the content on the page.&lt;br&gt;
❻ These CSS properties give rotation about the center.&lt;/p&gt;

&lt;p&gt;If you want to learn more about the book, check it out on Manning’s liveBook platform &lt;a href="https://livebook.manning.com/book/svelte-and-sapper-in-action?origin=product-look-inside&amp;amp;utm_source=blog&amp;amp;utm_medium=organic&amp;amp;utm_campaign=book_volkmann_svelte_8_19_20&amp;amp;utm_content=devto" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>svelte</category>
      <category>animation</category>
    </item>
  </channel>
</rss>
