Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Animate your user interface in React with styled-components and "keyframes"

    Simon VrachliotisSimon Vrachliotis

    In this lesson, we learn how to handle CSS keyframe animations in styled-components, via the 'keyframes' helper.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Here is a react app that is a simple bowl component, which is just a DIV with some styles applied to it. Here are some CSS keyframes animations that animate the rotate and border-radius properties. To use keyframe animations in style components, we'll import the keyframes named exports. I'll create the const moff and right keyframes followed by bactics.

    In here, I'll paste the declaration of the moff animation. Let's do the same one more time, const spin equals keyframes, and we'll paste the CSS in there, too. In the bowl component here, which is a style component, I can now access these keyframes.

    Animation, dollar sign curly braces, moff -- which is the name of the constant here -- and we give it a duration of one second, a linear easing, and make it loop for ever. Let's save and check it out.

    Here we go. The bowl now looks like its breathing in and out. Let's take it for spin by chaining the other keyframes animation. Spin, one second as well. This time ease in, out, and infinite again. That's how you handle keyframe animation in style components.