i would like to create a framer motion animation in React that removes and element on click and visualizes another element.
Here is an example
example
This is my code:
{!tileOpen ? (
<motion.div
whileHover={{ scale: 1.2 }}
onClick={handleTrigger}
className={styles.tile}
>
</motion.div>
) : (
<motion.div
animate={{ opacity: 1, scale: 1 }}
transition={{ delay: 0.9, duration: 0.5 }}
onClick={handleTrigger}
className={styles.tileOpen}
>
<Image
src={`/test/test.svg`}
alt="testimage"
width={116}
height={35}
/>
</motion.div>
)}
The problem is that in my animation the element does not appear the same way. I am not able to delay the animation.
2
Answers
The issue you are facing is the sum of states and framer-motion.
You can’t swap a component and animate it in the same time.
You could try to play the handleTrigger after the animation is done.
Then your logic should look like this:
this matches the exit duration of your animation (500ms)
And now you can make sure the title isn’t swapped until the animation is done:
I hope this works for you. I use the same technique and for me this is the one which works.
Is this what you want?
Demo