<div id="root" class="panel center text-center"></div>
.app {
  padding: 10px;
}

.box {
  margin: 10px auto;
  will-change: transform;
}
const { useEffect, useLayoutEffect, useRef, forwardRef, useState } = React;

gsap.registerPlugin(CustomEase, CustomWiggle);
gsap.config({ trialWarn: false });

CustomWiggle.create("myWiggle", {
  wiggles: 8,
  type: "uniform"
});

gsap.registerEffect({
  name: "pulse",
  effect(targets) {
    return gsap.fromTo(targets, {
      scale: 1
    }, {
      scale: 1.5,
      repeat: 1,
      ease: "bounce",
      yoyoEase: "power3"
    });
  }
});

gsap.registerEffect({
  name: "spin",
  effect(targets) {
    return gsap.to(targets, {
      rotation: (i, el) => gsap.utils.snap(360, gsap.getProperty(el, "rotation") + 360)
    });
  }
});

gsap.registerEffect({
  name: "shake",
  effect(targets) {
    return gsap.fromTo(targets, {
      x: 0
    }, {
      x: 10, 
      ease: "myWiggle"      
    });
  }
});

function useGsapEffect(target, effect, vars) {
  const [animation, setAnimation] = useState();
  
  useLayoutEffect(() => {
    if (gsap.effects[effect]) {
      setAnimation(gsap.effects[effect](target.current, vars));
    }
  }, [effect, target, vars]);
  
  return animation;
}

const Box = forwardRef(({ children }, ref) => {
  return <div className="box gradient-blue" ref={ref}>{children}</div>;
});

const wrap = gsap.utils.wrap(["pulse", "spin", "shake"]);

function App() {    
  const boxRef = useRef();
  const count = useRef(0);  
  const [effect, setEffect] = useState("");   
  const animation = useGsapEffect(boxRef, effect);
  
  const toggle = () => {
    setEffect(wrap(count.current++));
  };
    
  return (
    <div className="app">
      <div>
        <button onClick={toggle}>Toggle</button>
      </div>
      <p>Effect: {effect}</p>
      <Box ref={boxRef}>Box</Box>
    </div>
  );
}

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);
View Compiled

External CSS

  1. https://codepen.io/GreenSock/pen/xxmzBrw.css

External JavaScript

  1. https://assets.codepen.io/16327/CustomEase3.min.js
  2. https://assets.codepen.io/16327/CustomWiggle3.min.js
  3. https://unpkg.co/gsap@3/dist/gsap.min.js
  4. https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js
  5. https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js
  6. https://codepen.io/GreenSock/pen/NWoLXRG.js