I have simple code:
const CallButton = () => {
const [isActive, setIsActive] = useState(false);
return (
<div className={styles.container}>
{
isActive ?
<>
<div className={styles.callButton}/>
<div className={styles.callButton}/>
<div className={styles.callButton}/>
</> :
<div className={styles.main}
onClick={() => setIsActive(true)}>
<div className={styles.notActiveButton}>Связаться со мной</div>
</div>
}
</div>
);
};
export default CallButton;
Now I want to make animation for it. I want first div shifts to the right, and new div
appears on the left simultaneously. But I can’t because on ‘isActive’ state change first div disappears from DOM at once. How I can the easiest way to do it?
2
Answers
In this code, I’ve added the CSS class shiftRight to the first div when isActive is true. This class will be responsible for animating the shift to the right. The shiftRight class can be defined in your CSS file (CallButton.module.css) like this:
With these changes, the first div will transition smoothly to the right while a new div appears on the left simultaneously when isActive is true.
You need to always show the second div. So try below code (use ternary or
&&
). For animation you can add some CSS transitions