Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use Framer Motion + Styled Components together?

I’m wanting to use Styled Components and Framer Motion Together to style and animate.... Can I use a variable from SC or FM to style and animate it?

What is a example?

like image 876
Justcode123 Avatar asked Oct 19 '25 09:10

Justcode123


2 Answers

You should be able to use styled function as a HOC for any motion component

import styled from "styled-components";
import { motion } from "framer-motion";

const AnimatedDiv = styled(motion.div)`
  background-color: rebeccapurple;
  width: 200px;
  height: 200px;
`;

This allows you to use the Component as such:

<AnimatedDiv animate={{ scale: 3 }} />
like image 197
jubalm Avatar answered Oct 22 '25 04:10

jubalm


The best method is to use the as prop:

<StyledComponent as={motion.div}/>

This maintains the framer-motion intent of "variability" as you put it, in being able to easily replace DOM elements with their motion counterparts.

This was unusually hard to find online so I want to post it.

like image 23
Yuji 'Tomita' Tomita Avatar answered Oct 22 '25 06:10

Yuji 'Tomita' Tomita



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!