Jump to content
Search Community

How to uninterrupt the flip animation?

ashura test
Moderator Tag

Recommended Posts

Hi,

 

I don't see any Flip animation happening in your setup, because right now you're not targeting all the properties that are being updated. Also I spotted a few things that you should consider.

  1. Always use GSAP Context when working with React environments.
    https://greensock.com/docs/v3/GSAP/gsap.context()
  2. Always use React's events instead of adding event listeners. It's safer and React will clean those event handlers when/if the component is unmounted.
  3. Don't store variables in the global scope of your components, because a re-render will reset those. For a boolean either use state or a ref.

Flip.from returns a GSAP Timeline instance so you can store it in a ref. This seems to work the way you expect:

export default function App() {
  const boxRef = useRef();
  const flipAnimation = useRef();
  const ctx = useRef(gsap.context(() => {}));

  const clickHandler = () => {
    ctx.current.add(() => {
      const state = Flip.getState(boxRef.current, {
        props: "width,height, background",
      });
      boxRef.current.classList.toggle("enlarge");
      flipAnimation.current = Flip.from(state);
    });
  };

  useEffect(() => {
    return () => ctx.current.revert();
  }, []);

  return (
    <div className="App">
      <div className="composition-component">
        <div className="box" ref={boxRef} onClick={clickHandler}>
          Sky Balls Lopserum Madya
        </div>

        <div className="parentBox">
          <div className="parentBox1"></div>
          <div className="parentBox2"></div>
        </div>
      </div>
    </div>
  );
}

Then you can use flipAnimation.current to kill, pause, revert, etc. the timeline returned by the from() method:

https://greensock.com/docs/v3/GSAP/Timeline

 

Hopefully this helps.

Happy Tweening!

  • Like 2
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...