User avatar

Jeroen Reumkens PRO

Motion for React – Pausing animations with delay

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, useState, useRef } from "react";

export const App = () => {
const [scope, animate] = useAnimate();
const [count, setCount] = useState(0);
const animation = useRef(null);

useEffect(() => {
animation.current = animate(
".thingy",
{
y: [0, "50%", "50%", 0, 0]
},
{ repeat: Infinity, duration: 2.5, delay: stagger(-1.25) },
);
}, []);

return (
<div className="grid min-h-dvh place-items-center" ref={scope}>
<div className="grid [&_*]:[grid-area:1/1] place-items-center">
<div className="thingy w-36 h-36 border-2 border-white rounded-full origin-center"/>
<div className="thingy w-36 h-36 bg-white rounded-full origin-center"/>
</div>
<div className="flex gap-8">
<button
className="rounded-md bg-white px-4 py-2 text-black"
onClick={() => animation.current?.play()}
>
Play
</button>
<button
className="rounded-md bg-white px-4 py-2 text-black"
onClick={() => animation.current?.pause()}
>
Stop
</button>
</div>
</div>
);
};

export default App;

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