User avatar

Jeroen Reumkens PRO

Motion Autoplay Bug

Unlock Playgrounds

Build, test and share frontend ideas right in your browser.

Choose a plan

Cancel anytime. Playgrounds are also included in PRO. Upgrade later and we credit 100% of your first 3 months and 50% of the rest toward it.

Already a member? Log in

Playground settings

Playground

Anyone with the link can view it. Private playgrounds are only visible to you.

Editor

Applied right away

Files in the same folder share one tab.

Packages

Importable from your code, loaded from esm.sh

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;

  • No matching files
↵ open⌘↵ open to the sideesc close