A GSAP timeline lets you write an intro sequence as one object: every tween is placed relative to the others, and the whole thing can be played, reversed or thrown away in a single call. In React, the hard parts are when it gets created, which elements its selectors reach, and who cleans it up. useGSAP from @gsap/react answers all three. This guide builds one small component, and the demo below runs exactly the code shown further down.

The demo

Press Play intro to run the sequence, or Reverse to run it backward. Nothing plays on its own, every tween moves only transform and opacity, and with reduced motion turned on both buttons jump straight to the end state.

Guide

One timeline, five tweens

Each piece starts relative to the one before it, so the sequence retimes itself.

useGSAP · scoped · reverted on unmount

Install and register the hook

Install both packages with npm install gsap @gsap/react, then register the hook once with gsap.registerPlugin(useGSAP). useGSAP is a drop-in for useEffect or useLayoutEffect that runs your GSAP code inside a gsap.context(), so everything it creates can be reverted together (GSAP's React guide). It touches the DOM, so the file needs "use client" in the Next.js App Router.

One timeline, sequenced with the position parameter

gsap.timeline() returns a container you chain tweens onto. By default each tween starts when the previous one ends. The optional last argument, the position parameter, moves it:

Position parameter values used in the intro timeline
PositionWhere the tween startsIn the demo
(none)At the end of the timeline so farThe card rises in first
"-=0.2"0.2 s before the end of the timeline, so it overlapsThe badge pops in while the card settles
addLabel("copy") then "copy"At a named point you can reuseThe title starts at the label
"<"At the start of the previous tweenThe body moves with the title
"copy+=0.15"0.15 s after the labelThe meta line fades in last

Because the positions are relative, changing one duration retimes everything after it. That's much easier to tune than a column of hand-added delays, and it keeps the sequence short and quiet, in line with the motion rules behind every piece.

defaults, so each tween says only what's different

gsap.timeline({ defaults: { duration: 0.5, ease: "power3.out" } }) hands those values to every child tween. A tween can still override them: the meta line passes duration: 0.3. Without defaults, GSAP uses 0.5 s and power1.out. For why an ease-out family suits elements arriving on screen, see easing that feels right.

Scope selectors to the container

Passing { scope: container } scopes every selector string inside the hook to descendants of that ref. ".intro-card" then means this component's card, not every .intro-card on the page. Without a scope, two copies of the component would each animate both cards.

contextSafe() for animations triggered by events

The hook's function runs once after mount. Anything you create later, in a click handler, isn't part of its context, so it wouldn't be scoped or cleaned up. contextSafe() wraps a handler so the animations it creates are added to the context and its selector text uses the same scope. useGSAP returns it, and also passes it to your function as the second argument. The component takes the second route: it builds the handlers inside the hook, where they can close over the timeline, attaches them to the buttons, and returns a cleanup function that removes the listeners. Here the handlers only play or reverse a timeline the hook already built, so they would work unwrapped. Wrapping them still pays off: a tween you add later, such as a gsap.to(".intro-badge", …) pulse on click, is scoped and cleaned up with no extra work.

Cleanup, and why Strict Mode makes it visible

When the component unmounts, useGSAP reverts its context: every tween and timeline is killed and the inline styles GSAP wrote are removed. In development, React Strict Mode runs effects an extra setup and cleanup cycle to surface missing cleanup. Without a revert, the second run would build a second timeline over the first, and a .from() tween would read the hidden values the first one had just set as its end state, so elements can end up stuck invisible. With useGSAP, the first run is reverted before the second, and the second starts from clean markup.

A reduced-motion path with gsap.matchMedia()

gsap.matchMedia() runs a function when a media query matches. Given an object of conditions, it runs when any of them match and passes context.conditions with a true or false value for each. When the visitor's setting changes, everything created inside is reverted and the function runs again with the new values. Created inside useGSAP, the matchMedia instance belongs to the hook's context and uses its scope, so it's reverted on unmount as well.

With (prefers-reduced-motion: reduce), the component builds the same timeline with no movement (y offsets of 0 and no scale), and the buttons call progress(1) and progress(0) instead of play() and reverse(). The state still changes, but in a single frame with no tweening. That's the same approach every Motionbits component takes; see how each piece handles reduced motion.

"use client";
 
import { useRef } from "react";
import gsap from "gsap";
import { useGSAP } from "@gsap/react";
 
gsap.registerPlugin(useGSAP);
 
export function IntroCard() {
  const container = useRef<HTMLDivElement>(null);
  const playButton = useRef<HTMLButtonElement>(null);
  const reverseButton = useRef<HTMLButtonElement>(null);
 
  useGSAP(
    (context, contextSafe) => {
      let intro: gsap.core.Timeline | undefined;
      let instant = false;
 
      const mm = gsap.matchMedia();
      mm.add(
        {
          motion: "(prefers-reduced-motion: no-preference)",
          reduce: "(prefers-reduced-motion: reduce)",
        },
        (media) => {
          const reduce = Boolean(media.conditions?.reduce);
          instant = reduce;
          const lift = reduce ? 0 : 1;
 
          intro = gsap
            .timeline({ paused: true, defaults: { duration: 0.5, ease: "power3.out" } })
            .from(".intro-card", { y: 24 * lift, opacity: 0 })
            .from(".intro-badge", { scale: reduce ? 1 : 0.6, opacity: 0 }, "-=0.2")
            .addLabel("copy")
            .from(".intro-title", { y: 12 * lift, opacity: 0 }, "copy")
            .from(".intro-body", { y: 12 * lift, opacity: 0 }, "<")
            .from(".intro-meta", { opacity: 0, duration: 0.3 }, "copy+=0.15")
            .progress(1);
        },
      );
 
      const play = contextSafe!(() => {
        if (instant) intro?.progress(1);
        else intro?.play(0);
      });
      const reverse = contextSafe!(() => {
        if (instant) intro?.progress(0);
        else intro?.reverse();
      });
 
      const playEl = playButton.current!;
      const reverseEl = reverseButton.current!;
      playEl.addEventListener("click", play);
      reverseEl.addEventListener("click", reverse);
      return () => {
        playEl.removeEventListener("click", play);
        reverseEl.removeEventListener("click", reverse);
      };
    },
    { scope: container },
  );
 
  return (
    <div ref={container} className="intro">
      <div className="intro-card">
        <span className="intro-badge">Guide</span>
        <p className="intro-title">One timeline, five tweens</p>
        <p className="intro-body">Each piece starts relative to the one before it, so the sequence retimes itself.</p>
        <p className="intro-meta">useGSAP · scoped · reverted on unmount</p>
      </div>
      <div className="intro-controls">
        <button ref={playButton} type="button">
          Play intro
        </button>
        <button ref={reverseButton} type="button">
          Reverse
        </button>
      </div>
    </div>
  );
}

The timeline ends with .progress(1), so the server-rendered card is already in its finished state and doesn't flash hidden before hydration. Play intro restarts from the beginning with play(0).

Common mistakes

  • Creating a timeline in the component body. That runs on every render and never cleans up. Build it inside useGSAP, and create the handlers that use it there too.
  • Forgetting scope. Class selectors then match anywhere on the page, including other copies of the same component.
  • Animating layout properties like top, left, width or height. They make the browser redo layout as they change; x, y, scale and opacity map to transform and opacity, which usually don't.
  • Not killing timelines made outside the hook. A timeline built in a plain useEffect with no cleanup, or in an unwrapped handler, keeps its targets and callbacks after unmount. Use useGSAP or contextSafe(), or call kill() yourself.
  • Letting an intro autoplay or loop with no way to stop it. If motion starts on its own and lasts more than five seconds, it needs a pause control; see pausable heroes and marquees.

Where to go next

Theme Tip and Tumble Tabs are free Motionbits components with live demos, both built to the same rules: short, transform and opacity only, and a reduced-motion path. A free account gets you the source of the free components. New releases are part of Pro, which isn't open yet.

Sources

  1. GSAP, React and useGSAP
  2. GSAP, Timeline
  3. GSAP, Understanding the position parameter
  4. GSAP, gsap.context()
  5. GSAP, gsap.matchMedia()
  6. React, <StrictMode>