Jump to content
Search Community

text shining animation on hover

amine.daimallah90 test
Moderator Tag

Recommended Posts

I am building a link component using react js and gsap for animation, what I want to do is when I hover I want the animation to start, and while hovering it should keep repeating but after I the mouse leave I want it to resume the animation first and then kill it so it shouldn't kill animation immediately.

 

const Link = forwardRef<HTMLAnchorElement, LinkProps>(
  ({ children, variant, ...props }, ref) => {
    const textRef = useRef<HTMLAnchorElement>(null);
 
    const tl = useRef<GSAPTimeline>();
 
    useEffect(() => {
      if (textRef.current) {
        const ourText = new SplitType(textRef.current, {
          tagName: "span",
          types: "words, chars",
          wordClass: "",
          charClass: "",
        });
 
        const chars = ourText.chars;
 
        tl.current = gsap.timeline({
          paused: true,
        });
 
        tl.current
          .to(chars, {
            opacity: 0.2,
            ease: "power2.inOut",
            stagger: {
              amount: 0.4,
              ease: "none",
            },
          })
          .to(
            chars,
            {
              opacity: 1,
              ease: "power2.inOut",
              stagger: {
                amount: 0.4,
                ease: "none",
              },
            },
            "<+=0.3"
          );
      }
    }, []);
 
    const handleMouseEnter = () => {
      tl.current?.restart().repeat(-1);
    };
 
    const handleMouseLeave = () => {
      if (tl.current) {
        tl.current.resume().kill();
      }
    };
 
    return (
      <a
        ref={textRef}
        onMouseEnter={handleMouseEnter}
        onMouseLeave={handleMouseLeave}
        className={cn(linkStyle({ variant }))}
        {...props}
      >
        {children}
      </a>
    );
  }
);
Link to comment
Share on other sites

Hi there! I see you're using React -

Proper animation cleanup is very important with frameworks, but especially with React. React 18 runs in strict mode locally by default which causes your useEffect() and useLayoutEffect() to get called TWICE.

In GSAP 3.11, we introduced a new gsap.context() feature that helps make animation cleanup a breeze. All you need to do is wrap your code in a context call. All GSAP animations and ScrollTriggers created within the function get collected up in that context so that you can easily revert() ALL of them at once.

Here's the structure:

// typically it's best to useLayoutEffect() instead of useEffect() to have React render the initial state properly from the very start.
useLayoutEffect(() => {
  let ctx = gsap.context(() => {
    // all your GSAP animation code here
  });
  return () => ctx.revert(); // <- cleanup!
}, []);

This pattern follows React's best practices, and one of the React team members chimed in here if you'd like more background.

We strongly recommend reading the React information we've put together at https://greensock.com/react

Happy tweening!

  • Like 1
Link to comment
Share on other sites

thank you so much I appreciate it, I will have a look at the doc, but this still doesn't fix my problem and what i want to component to do

27 minutes ago, GSAP Helper said:

Hi there! I see you're using React -

Proper animation cleanup is very important with frameworks, but especially with React. React 18 runs in strict mode locally by default which causes your useEffect() and useLayoutEffect() to get called TWICE.

In GSAP 3.11, we introduced a new gsap.context() feature that helps make animation cleanup a breeze. All you need to do is wrap your code in a context call. All GSAP animations and ScrollTriggers created within the function get collected up in that context so that you can easily revert() ALL of them at once.

Here's the structure:

// typically it's best to useLayoutEffect() instead of useEffect() to have React render the initial state properly from the very start.
useLayoutEffect(() => {
  let ctx = gsap.context(() => {
    // all your GSAP animation code here
  });
  return () => ctx.revert(); // <- cleanup!
}, []);

This pattern follows React's best practices, and one of the React team members chimed in here if you'd like more background.

We strongly recommend reading the React information we've put together at https://greensock.com/react

Happy tweening!

 

Link to comment
Share on other sites

It's pretty tough to troubleshoot without a minimal demo - the issue could be caused by CSS, markup, a third party library, your browser, an external script that's totally unrelated to GSAP, etc. Would you please provide a very simple CodePen or Stackblitz that demonstrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependancies as possible. If not, incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, then at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

Here's a starter CodePen that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import the gsap-trial NPM package for using any of the bonus plugins: 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Link to comment
Share on other sites

3 hours ago, GSAP Helper said:

It's pretty tough to troubleshoot without a minimal demo - the issue could be caused by CSS, markup, a third party library, your browser, an external script that's totally unrelated to GSAP, etc. Would you please provide a very simple CodePen or Stackblitz that demonstrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependancies as possible. If not, incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, then at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

Here's a starter CodePen that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

 

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import the gsap-trial NPM package for using any of the bonus plugins: 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

sorry for not providing a demo, I just made one quickly I hope it will be clear to you what I am trying to do, also I already read the article thank you so much, this is the first time i use GSAP, so i am sure my code won't be perfect, anyway here is the demo

https://codesandbox.io/embed/serene-violet-fsm8mm?fontsize=14&hidenavigation=1&theme=dark

Link to comment
Share on other sites

Hi, I had no idea in which file your GSAP code was located, I had to search. Luckily there weren't to much files. 

 

I've used the .tweenTo() function https://greensock.com/docs/v3/GSAP/Timeline/tweenTo to animate to the end and then onComplete .kill() the timeline. It is probably not necessary to .kill() the timeline, but if it is needed I would do it that way. Hope it helps and happy tweening! 

 

https://codesandbox.io/s/sad-jerry-2zrmcn?file=/src/components/Link.js

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

13 minutes ago, mvaneijgen said:

Hi, I had no idea in which file your GSAP code was located, I had to search. Luckily there weren't to much files. 

 

I've used the .tweenTo() function https://greensock.com/docs/v3/GSAP/Timeline/tweenTo to animate to the end and then onComplete .kill() the timeline. It is probably not necessary to .kill() the timeline, but if it is needed I would do it that way. Hope it helps and happy tweening! 

 

https://codesandbox.io/s/sad-jerry-2zrmcn?file=/src/components/Link.js

thank you so much, this is exactly what I need, I appreciate your time

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...