<?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: Ellen Styrélius</title>
    <description>The latest articles on DEV Community by Ellen Styrélius (@styrelius).</description>
    <link>https://dev.to/styrelius</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%2F788432%2F84b0f397-e4dc-41ea-b157-7afccb321fb9.jpeg</url>
      <title>DEV Community: Ellen Styrélius</title>
      <link>https://dev.to/styrelius</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/styrelius"/>
    <language>en</language>
    <item>
      <title>styled-components keyframes animations - a very short guide</title>
      <dc:creator>Ellen Styrélius</dc:creator>
      <pubDate>Tue, 15 Feb 2022 20:50:33 +0000</pubDate>
      <link>https://dev.to/styrelius/styled-components-keyframes-animations-a-very-short-guide-44e3</link>
      <guid>https://dev.to/styrelius/styled-components-keyframes-animations-a-very-short-guide-44e3</guid>
      <description>&lt;p&gt;To create a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/@keyframes"&gt;@keyframes animation&lt;/a&gt; and use it with styled-components the first thing you need to do is to import the &lt;code&gt;keyframes&lt;/code&gt; helper.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;keyframes&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;styled-components&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just as the &lt;code&gt;div&lt;/code&gt;, or any other “element” property that exists on the &lt;code&gt;styled&lt;/code&gt; object imported from &lt;code&gt;styled-components&lt;/code&gt;, &lt;code&gt;keyframes&lt;/code&gt; is a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#:~:text=call%20a%20function%20(-,the%20tag%20function,-)%20with%20an%20array"&gt;function that you use as a tag&lt;/a&gt; for your template literal style declarations. &lt;/p&gt;

&lt;p&gt;And just as with the &lt;code&gt;div&lt;/code&gt;s and the &lt;code&gt;button&lt;/code&gt;s the result from the tag function is assigned to a variable. This variable is then used to reference the animation in your &lt;code&gt;styled&lt;/code&gt; components (and since your keyframes animation is not a React component the variable should be named in camelCase and not PascalCase).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rotate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;keyframes&lt;/span&gt;&lt;span class="s2"&gt;`
  to {
    transform: rotate(360deg);
  }
`&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;InfiniteRotate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="s2"&gt;`
  animation: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;rotate&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; 4s linear infinite;
`&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Spinner&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;InfiniteRotate&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;🥴&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;InfiniteRotate&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It’s not really more complicated than that! And this syntax allows you to export the animation and use it in several places in your app with different durations, iteration counts etc.&lt;/p&gt;




&lt;p&gt;P.S. Might be worth noting that keyframes can NOT be used with react-native, but there is a library for that called &lt;a href="https://reactnative.dev/docs/animated"&gt;Animated&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>styledcomponents</category>
      <category>react</category>
      <category>animations</category>
    </item>
  </channel>
</rss>
