User avatar

Jeroen Reumkens PRO

Motion Autoplay Bug

Unlock Playground Access

Playgrounds let you build, test, and share frontend ideas instantly — right in your browser. Access is included in PRO. Just want Playgrounds? Pick a plan below.

Maker

€5,- / month

Unlimited Playgrounds access to build, experiment, and share without limits.

Join for €5 per month

Champion

€10,- / month

All the benefits of Maker, plus you help Frontend.fyi grow and get a Champion badge on your profile.

Join for €10 per month

Cancel anytime — no commitment.
Upgrade to PRO anytime and get a partial refund.We'll credit 100% of your first 3 months and 50% of the rest toward the PRO lifetime price.

Already a member? Login

Playground settings

Title

Description

Public

Editor settings


Packages

These packages can be imported in your JavaScript files.

Package name

Version

@

@

@

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
import { useAnimate, stagger } from "motion/react";
import { useEffect, useRef } from "react";

export const App = () => {
  const [scope, animate] = useAnimate();
  const animationRef = useRef(null);

  useEffect(() => {
    // Timeout is only here to show that even registering it later doesn't
    // make it suddenly work. The animation should not be started if autoplay is false.
    setTimeout(() => {
      animationRef.current = animate(".fade-it", {
        opacity: 0.5,
      }, { autoplay: false });
    }, 1000);
  }, []);

  const forwards = () => {
    if (!animationRef.current) return;
    animationRef.current.speed = 1;
    animationRef.current.play();
  };

  const backwards = () => {
    if (!animationRef.current) return;
    animationRef.current.speed = -1;
    animationRef.current.play();
  };

  return (
    <div className="grid min-h-dvh place-items-center gap-8">
      <div ref={scope}>
        <div className="fade-it grid aspect-square place-items-center rounded-full bg-white p-4 opacity-0">
          Have some fade in me.
        </div>

        <div className="mt-4 flex justify-center gap-2">
          <button className="rounded-lg bg-white px-3 py-2" onClick={forwards}>
            Run
          </button>
          <button className="rounded-lg bg-white px-3 py-2" onClick={backwards}>
            Reverse
          </button>
        </div>
      </div>
    </div>
  );
};

export default App;
One sec — editor's thinking…