4

I'm trying to achieve callback-based route transitions using Next.js's framework and Greensock animation library (if applicable). For example when I start on the homepage and then navigate to /about, I want to be able to do something like:

HomepageComponent.transitionOut(() => router.push('/about'))

ideally by listening to the router like a sort of middleware or something before pushing state

Router.events.on('push', (newUrl) => { currentPage.transitionOut().then(() => router.push(newUrl)) });

Main Problem

The main problem is that I also have a WebGL app running in the background, decoupled from the React ecosystem (since it uses requestAnimationFrame). So the reason I want callback-based transitions is because I need to run them after the WebGL transitions are done.

Current Implementation

I've looked into using React Transition Group and I've seen the docs for the Router object but neither seems to be callback-based. In other words, when I transition to a new page, the WebGL and the page transitions run at the same time. And I don't want to do a hacky solution like adding a delay for the page transitions so they happen after the WebGL ones.

This is what I have right now:

app.js

<TransitionGroup>
  <Transition
    timeout={{ enter: 2000, exit: 2000 }}
    // unmountOnExit={true}
    onEnter={(node) => {
      gsap.fromTo(node, { opacity: 0 }, { opacity: 1, duration: 1 });
    }}
    onExit={(node) => {
      gsap.to(node, { opacity: 0, duration: 1 });
    }}
    key={router.route}
  >
    <Component {...pageProps}></Component>
  </Transition>
</TransitionGroup>

webgl portion

Router.events.on('routeChangeStart', (url) => {
  // transition webGL elements

  // ideally would transition webGL elements and then allow callback to transition out html elements
});

I've also tried using the eventemitter3 library to do something like:

// a tag element click handler
onClick(e, href) {
  e.preventDefault();
  this.transitionOut().then(() => { Emitter.emit('push', href); });
  // then we listen to Emitter 'push' event and that's when we Router.push(href)
}

However this method ran into huge issues when using the back / forward buttons for navigating

cheng
  • 1,264
  • 2
  • 18
  • 41

2 Answers2

2

Bit late on this but I was looking into this myself today. It's really easy to use Framer Motion for this but I also wanted to use GSAP / React Transition Group.

For Framer Motion I just wrapped the Next < Component > with a motion component:

  <motion.div
    key={router.asPath}
    initial={{ opacity: 0 }}
    animate={{ opacity: 1 }}
    exit={{ opacity: 0 }}
  >
    <Component {...pageProps} />
  </motion.div>

For GSAP / React Transition Group, not sure if this is the right way but it's working as intended for me (see comments):

  const [state, setstate] = useState(router.asPath) // I set the current asPath as the state

  useEffect(() => {
  const handleStart = () => {
    setstate(router.asPath) // then on a router change, I'm setting the state again
    // other handleStart logic goes here 
  }
  const handleStop = () => {
    ... // handleStop logic goes here
  }

  router.events.on("routeChangeStart", handleStart)
  router.events.on("routeChangeComplete", handleStop)
  router.events.on("routeChangeError", handleStop)

  return () => {
    router.events.off("routeChangeStart", handleStart)
    router.events.off("routeChangeComplete", handleStop)
    router.events.off("routeChangeError", handleStop)
  }
}, [router])

  <Transition
    in={router.asPath !== state} // here I'm just checking if the state has changed, then triggering the animations
    onEnter={enter => gsap.set(enter, { opacity: 0 })}
    onEntered={entered => gsap.to(entered, { opacity: 1, duration: 0.3 })}
    onExit={exit => gsap.to(exit, { opacity: 0, duration: 0.3 })}
    timeout={300}
    appear
  >
    <Component {...pageProps} />
  </Transition>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Tom Barber
  • 21
  • 3
2

First I recommend reading Greensock’s React documentation.

Intro Animations in Next.JS

For intro animations, if you use useLayoutEffect with SSR your console will fill up with warnings. To avoid this apply useIsomorphicLayoutEffect instead. Go to useIsomorphicLayoutEffect.

To prevent the flash of unstyled content (FOUC) with SSR, you need to set the initial styling state of the component. For example, if we are fading in, the initial style of that component should be an opacity of zero.

Outro Animations in Next.JS

For outro animations, intercept the page transition, and do the exit animations, then onComplete route to the next page.

To pull this off, we can use TransitionLayout higher order component as a wrapper to delay the routing change until after any animations have completed, and a TransitionProvider component that will take advantage of React’s useContext hook to share an outro timeline across multiple components, regardless of where they are nested.

Transition Context

In order to make a page transition effect, we need to prevent rendering the new page before our outro animation is done.

We may have many components with different animation effects nested in our pages. To keep track of all the different outro transitions, we will use a combination of React’s Context API and a top-level GSAP timeline.

In TransitionContext we will create our TransitionProvider which will make our GSAP timeline for outro animations available to any components who would like to transition out during a page change.

import React, { useState, createContext, useCallback } from "react"
import gsap from "gsap"

const TransitionContext = createContext({})

const TransitionProvider = ({ children }) => {
  const [timeline, setTimeline] = useState(() =>
    gsap.timeline({ paused: true })
  )

  return (
    <TransitionContext.Provider
      value={{
        timeline,
        setTimeline,
      }}
    >
      {children}
    </TransitionContext.Provider>
  )
}

export { TransitionContext, TransitionProvider }

Next, we have TransitionLayout which will be our controller that will initiate the outro animations and update the page when they are all complete.

import { gsap } from "gsap"
import { TransitionContext } from "../context/TransitionContext"
import { useState, useContext, useRef } from "react"
import useIsomorphicLayoutEffect from "../animation/useIsomorphicLayoutEffect"

export default function TransitionLayout({ children }) {
  const [displayChildren, setDisplayChildren] = useState(children)
  const { timeline, background } = useContext(TransitionContext)
  const el = useRef()

  useIsomorphicLayoutEffect(() => {
    if (children !== displayChildren) {
      if (timeline.duration() === 0) {
        // there are no outro animations, so immediately transition
        setDisplayChildren(children)
      } else {
        timeline.play().then(() => {
          // outro complete so reset to an empty paused timeline
          timeline.seek(0).pause().clear()
          setDisplayChildren(children)
        })
      }
    }
  }, [children])

  return <div ref={el}>{displayChildren}</div>
}

In a custom App component, we can have TransitionProvider and TransitionLayout wrap the other elements so they can access the TransitionContext properties. Header and Footer exist outside of Component so that they will be static after the initial page load.

import { TransitionProvider } from "../src/context/TransitionContext"
import TransitionLayout from "../src/animation/TransitionLayout"
import { Box } from "theme-ui"
import Header from "../src/ui/Header"
import Footer from "../src/ui/Footer"

export default function MyApp({ Component, pageProps }) {
  return (
    <TransitionProvider>
      <TransitionLayout>
        <Box
          sx={{
            display: "flex",
            minHeight: "100vh",
            flexDirection: "column",
          }}
        >
          <Header />
          <Component {...pageProps} />
          <Footer />
        </Box>
      </TransitionLayout>
    </TransitionProvider>
  )
}

Component-Level Animation

Here is an example of a basic animation we can do at the component level. We can add as many of these as we want to a page and they will all do the same thing, wrap all its children in a transparent div and fade it in on page load, then fade out when navigating to a different page.

import { useRef, useContext } from "react"
import { gsap } from "gsap"
import { Box } from "theme-ui"
import useIsomorphicLayoutEffect from "./useIsomorphicLayoutEffect"
import { TransitionContext } from "../context/TransitionContext"

const FadeInOut = ({ children }) => (
  const { timeline } = useContext(TransitionContext)
  const el = useRef()

  // useIsomorphicLayoutEffect to avoid console warnings
  useIsomorphicLayoutEffect(() => {
    // intro animation will play immediately
    gsap.to(el.current, {
      opacity: 1,
      duration: 1,
    })

    // add outro animation to top-level outro animation timeline
    timeline.add(
      gsap.to(el.current, {
        opacity: 1,
        duration: .5,
      }),
      0
    )
  }, [])

  // set initial opacity to 0 to avoid FOUC for SSR
  <Box ref={el} sx={{opacity: 0}}>
    {children}
  </Box>
)

export default FadeInOut

We can take this pattern and extract it into an extendable AnimateInOut helper component for reusable intro/outro animation patterns in our app.

import React, { useRef, useContext } from "react"
import { gsap } from "gsap"
import { Box } from "theme-ui"
import useIsomorphicLayoutEffect from "./useIsomorphicLayoutEffect"
import { TransitionContext } from "../context/TransitionContext"

const AnimateInOut = ({
  children,
  as,
  from,
  to,
  durationIn,
  durationOut,
  delay,
  delayOut,
  set,
  skipOutro,
}) => {
  const { timeline } = useContext(TransitionContext)
  const el = useRef()

  useIsomorphicLayoutEffect(() => {
    // intro animation
    if (set) {
      gsap.set(el.current, { ...set })
    }
    gsap.to(el.current, {
      ...to,
      delay: delay || 0,
      duration: durationIn,
    })

    // outro animation
    if (!skipOutro) {
      timeline.add(
        gsap.to(el.current, {
          ...from,
          delay: delayOut || 0,
          duration: durationOut,
        }),
        0
      )
    }
  }, [])

  return (
    <Box as={as} sx={from} ref={el}>
      {children}
    </Box>
  )
}

export default AnimateInOut

The AnimateInOut component has built in flexibility for different scenarios:

  • Setting different animations, durations and delays for intros and outros
  • Skipping the outro
  • Setting the element tag for the wrapper, e.g. use a <span> instead of a <div>
  • Use GSAP’s set option to define initial values for the intro

Using this we can create all sorts of reusable intro/outro animations, such as <FlyInOut>, <ScaleInOut>, <RotateInOut3D> and so forth.

I have a demo project where you can see the above in practice: TweenPages

johnpolacek
  • 2,599
  • 2
  • 23
  • 16