Conditional render animation
Unanswered
Siberian posted this in #help-forum
SiberianOP
How can I have leave animation for conditionally rendered paragraph
This paragraph is in array of objects
so whenever I click a button it should switch isActive to true or false, animation works fine for entering, but for leaving it doesnt work
This paragraph is in array of objects
const [questions, setQuestion] = useState([
{
name: "Some name",
answer: `paragraph text`,
isActive: false,
}, {
name: "Some other name",
answer: `other paragraph text`,
isActive: false,
},]so whenever I click a button it should switch isActive to true or false, animation works fine for entering, but for leaving it doesnt work
{question.isActive && (
<p
className={`${styles.answer} ${
question.isActive ? styles.expand : styles.leave
}`}
>
{question.answer}
<br />
<br />
<b className={styles.b}>{note}</b>
</p>
)} here is my current code for paragraph19 Replies
SiberianOP
Anyone?
Oak apple gall wasp
Could you please provide screenshots instead?
Blue orchard bee
Are you using CSS animations? When you pop-out elements from the DOM tree it's done immediately, it won't play any closing or fading animations as it's removed instantly.
You could achieve this effect using framer-motion, which supports animations when an element is popped from the tree using AnimatePresence.
An alternative without libraries is to use a delay before popping out the element, which would require more logic.
Something like delay of 500ms with setTimeout before setting isActive to false
You'd need to be careful with state doing this as that can introduce side-effects.
@Blue orchard bee Are you using CSS animations? When you pop-out elements from the DOM tree it's done immediately, it won't play any closing or fading animations as it's removed instantly.
SiberianOP
I just started testing that React Transition Group, but im not sure how to achieve that animation when it should animate only one element of list items parent
Blue orchard bee
I haven't tried that lib
but I do know that framer-motion works out pretty well, just in case you're willing to try it out.
SiberianOP
Yeah, gonna test this soon, that lib could be good as well, but its
ul that is mapping through array of objects that is shown at li which has a button to show/hide pBlue orchard bee
Yea, tag me if you end up trying out framer-motion
and have some questions
@Blue orchard bee Yea, tag me if you end up trying out framer-motion
SiberianOP
Can i use my own animation with that?
SiberianOP
Well it works just fine, thank you so much. Would be awesome if I could add my own animation with single key, but its propably just me being not smart enough to achieve that 😄
Blue orchard bee
No problem! Yea it works good, you could port your animation to framer-motion, the library has sooo many features.
SiberianOP
any idea how to use animation from css?
Blue orchard bee
I don't think you can, you'd have to port it to framer-motion and use variants.