61
loading...
This website collects cookies to deliver better user experience
<!-- CLOSED STATE -->
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<line y1="9.5" x2="24" y2="9.5" stroke="#FFFFFF"/>
<line y1="14.5" x2="15" y2="14.5" stroke="#FFFFFF"/>
</svg>
<!-- MIDDLE STATE -->
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<line y1="9.5" x2="24" y2="9.5" stroke="#FFFFFF"/>
<line y1="14.5" x2="24" y2="14.5" stroke="#FFFFFF"/>
</svg>
<!-- OPEN STATE -->
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M3.06061 2.99999L21.0606 21" stroke="#FFFFFF"/>
<path d="M3.00006 21.0607L21 3.06064" stroke="#FFFFFF"/>
</svg>
d="Mx1 y1Lx2 y2"
<!-- CLOSED STATE -->
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M0 9.5L24 9.5" stroke="#FFFFFF"/>
<path d="M0 14.5L15 14.5" stroke="#FFFFFF"/>
</svg>
<!-- MIDDLE STATE -->
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M0 9.5L24 9.5" stroke="#FFFFFF"/>
<path d="M0 14.5L24 14.5" stroke="#FFFFFF"/>
</svg>
const path01Variants = {
open: { d: 'M3.06061 2.99999L21.0606 21' },
closed: { d: 'M0 9.5L24 9.5' },
}
const path02Variants = {
open: { d: 'M3.00006 21.0607L21 3.06064' },
moving: { d: 'M0 14.5L24 14.5' },
closed: { d: 'M0 14.5L15 14.5' },
}
const [animation, setAnimation] = useState('closed');
const onClick = () => {
setAnimation('moving');
setTimeout(() => {
setAnimation(status === 'closed' ? 'open' : 'closed');
}, 200);
};
<button onClick={onClick}>
<svg width='24' height='24' viewBox='0 0 24 24'>
<motion.path
stroke='#FFFFFF'
animate={animation}
variants={path01Variants}
/>
<motion.path
stroke='#FFFFFF'
animate={animation}
variants={path02Variants}
/>
</svg>
</button>
animate={animation}
changes its state with the value inside my React state and then the proper variant inside 'pathXXVariants' is used.const [isOpen, setOpen] = useState(false);
const path01Controls = useAnimation();
const path02Controls = useAnimation();
const onClick = async () => {
// change the internal state
setOpen(!isOpen);
// start animation
if (!isOpen) {
await path02Controls.start(path02Variants.moving);
path01Controls.start(path01Variants.open);
path02Controls.start(path02Variants.open);
} else {
path01Controls.start(path01Variants.closed);
await path02Controls.start(path02Variants.moving);
path02Controls.start(path02Variants.closed);
}
};
<button onClick={onClick}>
<svg width='24' height='24' viewBox='0 0 24 24'>
<motion.path
{...path01Variants.closed}
animate={path01Controls}
transition={{ duration: 0.2 }}
stroke='#FFFFFF'
/>
<motion.path
{...path02Variants.closed}
animate={path02Controls}
transition={{ duration: 0.2 }}
stroke='#FFFFFF'
/>
</svg>
</button>