Hi. I have the following component in React, a hamburger icon, which works just fine as written. My issue is I'm trying to make the component more concise by using the reverse() method on my timeline, which I can't get to work as intended. I've read through a few posts to try and remedy the issue, but it's not coming to me.
const NavHamburger = props => {
const tl = new TimelineMax()
// use useRef to detect first render
const isFirstRender = useRef(true)
useEffect(() => {
// prevent hamburger from animating on first render
if (isFirstRender.current === true) {
isFirstRender.current = false
} else {
props.menuExpanded === true
? tl
.to([".top", ".bottom"], 0.2, { y: "0%" }, 0)
.to(".middle", 0, { autoAlpha: 0 }, 0.2)
.to(".top", 0.2, { transform: "rotate(45deg)" }, 0.2)
.to(".bottom", 0.2, { transform: "rotate(-45deg)" }, 0.2)
: tl
.to([".top", ".bottom"], 0.2, { transform: "rotate(0)" }, 0)
.to(".bottom", 0.2, { transform: "rotate(0)" }, 0)
.to(".middle", 0, { autoAlpha: 1 }, 0.2)
.to(".top", 0.2, { y: "-400%" }, 0.2)
.to(".bottom", 0.2, { y: "400%" }, 0.2)
}
}, [props.menuExpanded])
return (
<Container
className="nav-hamburger"
onClick={() => props.setMenuExpanded(!props.menuExpanded)}
>
<Inner>
{/* bars of hamburger */}
<Top className={"top" + props.menuState}></Top>
<Middle className={"middle" + props.menuState}></Middle>
<Bottom className={"bottom" + props.menuState}></Bottom>
</Inner>
</Container>
)
}
export default NavHamburger
I'm specifically trying to replace the following section of code with tl.reverse(). Any direction would be greatly appreciated. I have a feeling that it has to to with how I'm declaring my timeline, but can't figure out the correct way to go about it. Thanks.
tl
.to([".top", ".bottom"], 0.2, { transform: "rotate(0)" }, 0)
.to(".bottom", 0.2, { transform: "rotate(0)" }, 0)
.to(".middle", 0, { autoAlpha: 1 }, 0.2)
.to(".top", 0.2, { y: "-400%" }, 0.2)
.to(".bottom", 0.2, { y: "400%" }, 0.2)