motion: Does not seem to fully support styled-components?
I want to do this, passing the gatsby-image component `Img into a styled component, while animating it, like so:
const AnimatedImage = styled(motion(Img))`
width: 200px;
height: auto;
`
but this does not seem to work, giving me this error:
TypeError: Object(...) is not a function
Module.<anonymous>
src/pages/index.js:81
78 | }
79 | `
80 |
> 81 | const LaxImg = styled(motion(Img))`
82 | width: 200px;
83 | height: auto;
84 | `
it seems to work with regular html tags, just not react components, which is no bueno for me š¦
Am i doing something wrong? Is something like this not supported? It really needs to be supported, so hopefully I am doing something wrong or itās on the way.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 9
- Comments: 25
I think Iām missing something - I donāt see any instance in this example of either a component made with styled components or a component using motion.custom
just to be clear, this works fine in
framer-motion:ā¦without seemingly any issues, just not this:
ā¦which would be lovely if it did š
Hereās an example of Styled Components working with
motion.custom: https://codesandbox.io/s/framer-motion-with-styled-components-custom-components-i1wgkStumbled upon this⦠Doesnāt look like a clear cut answer was posted. But I was able to get this to work. Think this is what @InventingWithMonster meant.
styled(motion(Link))threw the same error @rchrdnsh was getting.@jacobgranberry @tpiersall This seems to show it a bit better: https://codesandbox.io/s/framer-motion-simple-animation-o32p0
Awesome library. š
For this particular issue, it would be great if this could work automatically (without the intermediate
forwardRefstep) sincestyled-components/emotionare so popular. š@samajammin Cool, I just got mine working too. I just wrapped the image in a div and animated that instead. Annoying but only way Iāve figured out so far.
I donāt believe this is documented as an oversight but you can do motion.custom(Img) and make sure you pass the ref prop from Img to the underlying motion.div or whatever.
On Sat, 13 Jul 2019 at 05:52, rchrdnsh notifications@github.com wrote:
Iām afraid
Linkisnāt a styled-component - itās an actual React component, just wrap it inside your own styled-component š@migsan For reference: A guide on how to use Framer Motion with Styled Components
https://inventingwithmonster.io/20200302-can-i-use-framer-motion-with-styled-components/
I still quite donāt understand how to Motion a Styled component. Does anyone have some examples? I remember with Posed it was quite easy and you could style and then add pose on top of it for transitions. How is that achieved now with Framer? Thanks in advance.
Okay I just figured this it now. You can use this instead.
Const styledImage = styled(motion.custom(props => <Img {ā¦props}/>))
then add your styles.
once you are then then you can do this.
<StyledImage fluid{fluid.childImageSharp.fluid} here you add your framer motion scripts />
thatās all. It works perfectly now.
I am baffled with this issue, while simple animations work, complex components do not, hereās my on componentā¦
If I remove the accordionPanel component my menu toggle has a background, if I add it back, the Toggle loses itās style. Iām completely baffled.
Usage:
@sbarry50 yup, it was a gatsby-image component. I found a way around my problem but I couldnāt find a way to accomplish this either. Iāll update here if I do!
@samajammin Following up⦠not sure if the
Imgyouāre using there is a gatsby-image component but I just ran into this very issue with a Gatsby image and forwarding refs as shown in the React docs is not working for me either.Seems like it could be related to this issue.
@samajammin itās pretty simple assuming
Imgis a component you can edit. Just follow the docs.If youāre trying to animate someone elseās component and canāt edit it like Gatsbyās image component you can try wrapping it in a custom component and forwarding refs to that. Not 100% sure that would work (havenāt tested it) but thats where I would start.
Ya I canāt see that either in the mentioned example