Jump to content
Search Community

Recommended Posts

Posted (edited)

Hey there,

 

Trying to animated mapped components in my Next.js app.

I have succeeded, but something is off..

The animation is not 'fluid'. See clip. I noticed it does not happen with 'a hardcoded array of items'.
I also tried to grab all the buttons using gsap.utils.toArray(#button), but the same happens.

How can I adjust the code for it to be fluent? 
 

 
Here is my code:
const buttonRefs = useRef<HTMLAnchorElement[]>([]);
buttonRefs.current = [];

tl.fromTo(
  buttonRefs.current,
  { autoAlpha: 0, yPercent: 100 },
  { autoAlpha: 1, yPercent: 0, ease: "linear", stagger: 0.1 }
  );
}, []);

const addToRefs = (el: HTMLAnchorElement | null) => {
    if (el && !buttonRefs.current.includes(el)) {
      buttonRefs.current.push(el);
    }
  };

<div className="flex flex-row gap-4 w-fit">
  {slice.primary.cta_buttons.map((item) => (
  <Button
          ref={addToRefs}
          key={item.link.text}
          className="text-body-base font-medium"
          buttonStyle={item.style ?? "primary"}
          field={item.link}
          >
    {item.link.text}
  </Button>
  ))}
  </div>
 


Much appreciated!

Edited by jorik.tsx
Remove unnecessary code
Posted

Without a minimal demo, it's very difficult to troubleshoot; the issue could be caused by CSS, markup, a third party library, a 3rd party script, etc. Would you please provide a very simple CodePen or Stackblitz that illustrates 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 dependencies as possible. Start minimal and then incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

See the Pen aYYOdN by GreenSock (@GreenSock) on 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 GSAP as shown in the Install Helper in our Learning Center : 

 

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. 

Posted

Have you tried using the useGSAP hook? It's built especially for React-based solutions, which Next.js would be. I would start with that and work from there with regards to your animation fluidity.

Posted

Perhaps the problem is that React 18+ runs in "strict" mode locally by default which causes your useEffect() to get called TWICE! Very annoying. It has caused a lot of headaches for a lot of people outside the GSAP community too.

 

.from() tweens use the CURRENT value as the destination and it renders immediately the value you set in the tween, so when it's called the first time it'd work great but if you call it twice, it ends up animating from the from value (no animation). It's not a GSAP bug - it's a logic thing.

 

For example, let's say el.x is 0 and you do this: 

useEffect(() => {
  // what happens if this gets called twice?
  gsap.from(el, {x: 100})
}, []);

 

The first time makes el.x jump immediately to 100 and start animating backwards toward the current value which is 0 (so 100 --> 0). But the second time, it would jump to 100 (same) and animate back to the current value which is now 100 (100 --> 100)!  See the issue?

 

Since GSAP 3.12, we have the useGSAP() hook (the NPM package is here) that simplifies creating and cleaning up animations in React (including Next, Remix, etc). It's a drop-in replacement for useEffect()/useLayoutEffect(). All the GSAP-related objects (animations, ScrollTriggers, etc.) created while the function executes get collected and then reverted when the hook gets torn down.

 

Here is how it works:

const container = useRef(); // the root level element of your component (for scoping selector text which is optional)

useGSAP(() => {
  // gsap code here...
}, { dependencies: [endX], scope: container }); // config object offers maximum flexibility

Or if you prefer, you can use the same method signature as useEffect():

useGSAP(() => {
  // gsap code here...
}, [endX]); // simple dependency Array setup like useEffect()

This pattern follows React's best practices.

 

We strongly recommend reading the React guide we've put together at https://gsap.com/resources/React/

 

If you still need help, here's a React starter template that you can fork to create a minimal demo illustrating whatever issue you're running into. Post a link to your fork back here and we'd be happy to take a peek and answer any GSAP-related questions you have. Just use simple colored <div> elements in your demo; no need to recreate your whole project with client artwork, etc. The simpler the better. 

Posted

Apologies, I am already using the useGSAP hook, see below:

useGSAP(() => {
    const tl = gsap.timeline();

    tl.fromTo(
        buttonRefs.current,
        { autoAlpha: 0, yPercent: 100 },
        { autoAlpha: 1, yPercent: 0, ease: "linear", stagger: 0.1 }
      );
  }, []);

  const addToRefs = (el: HTMLAnchorElement | null) => {
    if (el && !buttonRefs.current.includes(el)) {
      buttonRefs.current.push(el);
    }
  };

 

Posted

Hi,

 

As mentioned before without a minimal demo is hard to know what could be the issue here. Perhaps your buttons have some kind of CSS filter or a CSS Transition applied to them that is causing this sort of jump even with no easing function applied to the Tween.

 

Check that and let us know

Happy Tweening!

Posted

Well. I'll be darned.
This is my Button component, and after removing the CSS transitions/filter, it animates fluently :D.
So, that messed it up. Now I need to find a way to make the CSS animations work on hover as well.
Maybe animate them with GSAP? Haha.

Thanks for mentioning the CSS transitioning! 💪
Cheers!

const Button = ({
  buttonStyle = "primary",
  className = "",
  type = "button",
  ariaLabel = "",
  ...restProps
}: ButtonProps) => {
  const styleClasses: Record<ButtonStyle, string> = {
    primary:
      "bg-white hover:bg-cyan-900/85 text-navy-800 transition duration-200 ease-in-out",
    secondary:
      "bg-white/20 text-white hover:bg-white/40 backdrop-blur-md transition duration-200 ease-in-out border border-white/20",
    tertiary:
      "bg-white/70 backdrop-blur-sm text-navy-900 transition transition-all duration-200 ease-in-out border border-black/20",
  };

  return (
    <PrismicNextLink
      className={` ${styleClasses[buttonStyle]} text-body-base font-normal flex w-fit justify-center rounded-full px-4 py-2 ${className}`}
      {...restProps}
      type={type}
      aria-label={ariaLabel}
    />
  );
};

 

  • Like 1
Posted
3 hours ago, jorik.tsx said:

Now I need to find a way to make the CSS animations work on hover as well.

What you can do is be very specific about CSS Transitions and properties being animated with GSAP. Definitely don't animate the same property on the same element with GSAP and CSS Transitions, that's basically the problem you were having.

3 hours ago, jorik.tsx said:

Maybe animate them with GSAP? Haha.

Yeah, that's the solution we love around here 😉 💚

 

Happy Tweening!

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...