Hi. I need some basic help with gsap.timeline() in a react environment. See my code below:
import React, { useRef, useEffect } from 'react'
import gsap, { TweenMax, Linear } from 'gsap';
import { ReactComponent as Twitter } from '../assets/svg/social/twitter.svg'
const NavBar = () => {
let twitterIcon = useRef(null);
const tl = gsap.timeline()
// this is the hover effect, making it glow green (all attributes are correct)
const glowAnimation = (icon) => {
tl.to(icon, 0.3, {attr: {style: "fill: rgb(48,223,171)"}, ease: Linear})
}
// This just sets the initial colour of the black svg to white
useEffect(() => {
TweenMax.set(twitterIcon, {attr: {"style": "fill: rgb(255,255,255)"}})
})
return (
<div className="navbar">
<a
title="Twitter"
target="_blank"
rel="noopener noreferrer"
// onMouseEnter={ glowAnimation({twitterIcon}) }
// onMouseLeave={ something? reverse of tl }
href="redacted">
<Twitter // This is an SVG imported as ReactComponent
className="navbar__icon"
title="Twitter"
ref={el => {twitterIcon = el}}
/>
</a>
</div>
)
}
export default NavBar
The basic premise of this is that it is part of my header. It has only one twitter icon (svg) right now. I want to be able to add more icons in the future (e.g. facebook, github) hence why I want to be able to pass in some reference to an icon into a single gsap timeline to describe the hover animation (defined as glowAnimation function) when mouse over an icon, and reverse that timeline when mouse leaves that icon. So I created a function with that timeline. But I'm unsure how to pass the twitterIcon reference (made using useRef) into the onMouseEnter attribute.
What am I doing wrong? And also, what can I put in the onMouseLeave attribute to reverse the glowAnimation function? Sorry if this is stupid