DEV Community

Cover image for Animate Styled Components with Framer Motion!
Seth Corker
Seth Corker

Posted on

38 5

Animate Styled Components with Framer Motion!

I love Framer Motion because it's a library that's easy to use and powerful. I've created page transitions within Next.js and cool interactions like tap to expand. It's a very versatile library for animation in React and it's not just limited to building from scratch.

If you're using a CSS-in-JS library then it might not be immediately obvious how to integrate Framer Motion smoothly but once you know, it's as easy as πŸ₯§.

Watch this short tutorial on how you can add animations and gestures like drag, to an existing UI library built on top of styled-components.

The core concepts

Here's a quick reference to help you out!

Turning a styled-component into something animatable

Use the as prop and specify motion.div or another motion.<some html element> to start using Framer Motion for that component.

<MyComponent as={motion.div} animate={{ y: 100 }} />
Enter fullscreen mode Exit fullscreen mode

Animating it's as easy as adding the animate, transition, and other props you're used to adding directly to motion elements.

Making your own styled-components and integrating with Framer Motion

Define your styled component but instead of passing an html element as the first argument, give it a motion element instead.

const MyCustomComponent = styled(motion.div)`
  width: 2rem;
  background-color: tomato;
`;
Enter fullscreen mode Exit fullscreen mode

Need to add drag functionality? Simply pass in the prop when you use it!

<div>
  <MyCustomComponent drag />
  <MyCustomComponent animate={{ opacity: 1 }} />
</div>
Enter fullscreen mode Exit fullscreen mode

It's as easy as that. If you use styled-components to build your React apps then I hope you this helps you develop rich experiences with animation and interactivity without the hassle. Framer Motion is my favourite library for tinkering with animations and it's versatile enough to be used with a variety of different frontend technology.

References

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (2)

Collapse
 
dhiranegi profile image
DhiraNegi β€’

How about using it in an image instead of div.
Can you tell me how to do that?

Collapse
 
amaanahmad profile image
Amaan Ahmad β€’

use motion.custom

const MyCustomComponent = motion.custom(styled.img` 
// your styles ... 
`)
Enter fullscreen mode Exit fullscreen mode

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay