Documentation menu
Packages
React hooks
Motion values hold numbers and strings that change without re-rendering. The hooks create them, derive them, follow the scroll and start animations by hand.
Motion values
A motion value holds a number or string that changes without re-rendering the component. Pass it in style and the renderer writes it each frame.
import { animate, motion, useMotionValue, useTransform } from "@animaly/react";
import { useEffect } from "react";
export default function Slider() {
const x = useMotionValue(0);
const opacity = useTransform(x, [0, 200], [0.3, 1]);
useEffect(() => {
const controls = animate(x, 200, { type: "spring", stiffness: 120 });
return () => controls.stop();
}, [x]);
return (
<div className="rail">
<motion.div className="puck" style={{ x, opacity }} />
</div>
);
}import { animate, useMotionValue, useMotionValueEvent } from "@animaly/react";
import { useEffect, useState } from "react";
export default function Counter() {
const count = useMotionValue(0);
const [label, setLabel] = useState("0");
useMotionValueEvent(count, "change", (latest) => setLabel(String(Math.round(latest))));
useEffect(() => {
const controls = animate(count, 1280, { duration: 1.2, ease: "easeOut" });
return () => controls.stop();
}, [count]);
return (
<div className="card">
<span className="readout">{label}</span>
<p>downloads this week</p>
</div>
);
}import { motion, useMotionValue, useSpring } from "@animaly/react";
export default function Follower() {
const x = useMotionValue(0);
const smooth = useSpring(x, { stiffness: 200, damping: 20 });
return (
<div className="rail" onPointerMove={(event) => x.set(event.clientX - event.currentTarget.getBoundingClientRect().left - 26)}>
<motion.div className="puck pink" style={{ x: smooth }} />
</div>
);
}import { animate, motion, useMotionTemplate, useMotionValue } from "@animaly/react";
import { useEffect } from "react";
export default function Spoiler() {
const blur = useMotionValue(0);
const filter = useMotionTemplate`blur(${blur}px)`;
useEffect(() => {
const controls = animate(blur, 4, { duration: 0.6 });
return () => controls.stop();
}, [blur]);
return (
<motion.div className="card" style={{ filter }}>
<h3>Spoiler</h3>
<p>The butler did it.</p>
</motion.div>
);
}import { motion, useMotionValue, useTransform, useVelocity } from "@animaly/react";
export default function Skew() {
const x = useMotionValue(0);
const velocity = useVelocity(x);
const skewX = useTransform(velocity, [-1000, 0, 1000], [-15, 0, 15]);
return (
<div className="rail" onPointerMove={(event) => x.set(event.clientX - event.currentTarget.getBoundingClientRect().left - 26)}>
<motion.div className="puck" style={{ x, skewX }} />
</div>
);
}Scroll
useScroll returns scrollX, scrollY, scrollXProgress and scrollYProgress as motion values, with the same target, container and offset options as scroll.
import { motion, useScroll } from "@animaly/react";
export default function ReadingProgress() {
const { scrollYProgress } = useScroll();
return (
<motion.div
style={{ scaleX: scrollYProgress, transformOrigin: "left", position: "fixed", top: 0, left: 0, right: 0, height: 4, background: "#7c3aed" }}
/>
);
}import { motion, useScroll, useTransform } from "@animaly/react";
import { useRef } from "react";
export default function Reveal() {
const ref = useRef<HTMLDivElement>(null);
const { scrollYProgress } = useScroll({ target: ref, offset: ["start end", "end end"] });
const opacity = useTransform(scrollYProgress, [0, 1], [0, 1]);
const y = useTransform(scrollYProgress, [0, 1], [60, 0]);
return (
<div style={{ height: "200vh", paddingTop: "120vh" }}>
<motion.div ref={ref} className="card" style={{ opacity, y }}>
<h3>Revealed</h3>
<p>Tied to scroll, so it reverses on the way up.</p>
</motion.div>
</div>
);
}import { motion, useInView } from "@animaly/react";
import { useRef } from "react";
export default function Lazy() {
const ref = useRef<HTMLDivElement>(null);
const visible = useInView(ref, { once: true, amount: 0.6 });
return (
<motion.div
ref={ref}
className="card"
initial={{ opacity: 0.3, y: 24 }}
animate={visible ? { opacity: 1, y: 0 } : undefined}
transition={{ type: "spring", stiffness: 300, damping: 24 }}
>
<h3>{visible ? "Loaded" : "Waiting"}</h3>
<p>Switches once 60% of it is on screen.</p>
</motion.div>
);
}Imperative animations
useAnimate returns a scope ref and an animate whose selectors only match inside that scope. Its animations stop when the component unmounts.
import { useAnimate } from "@animaly/react";
export default function Shake() {
const [scope, animate] = useAnimate();
const shake = () => animate(scope.current, { x: [0, -8, 8, -6, 6, 0] }, { duration: 0.4 });
return (
<div ref={scope} className="card stack">
<input type="password" defaultValue="hunter2" aria-label="Password" style={{ width: "100%" }} />
<button type="button" className="btn" onClick={shake}>
Sign in
</button>
</div>
);
}import { useAnimate } from "@animaly/react";
import { useEffect } from "react";
export default function Intro() {
const [scope, animate] = useAnimate();
useEffect(() => {
animate([
["h3", { opacity: [0, 1], y: [16, 0] }],
["p", { opacity: [0, 1] }, { at: "-0.1" }],
]);
}, [animate]);
return (
<div ref={scope} className="card">
<h3 style={{ opacity: 0 }}>Hello</h3>
<p style={{ opacity: 0 }}>Selectors only match inside this component.</p>
</div>
);
}import { motion, useAnimationControls } from "@animaly/react";
export default function Bell() {
const controls = useAnimationControls();
return (
<div className="row">
<motion.span animate={controls} style={{ display: "inline-block", fontSize: 32, transformOrigin: "50% 0" }}>
🔔
</motion.span>
<button type="button" className="btn" onClick={() => controls.start({ rotate: [0, -15, 15, -10, 10, 0] })}>
Ring
</button>
</div>
);
}import { motion, useCycle } from "@animaly/react";
export default function Shape() {
const [shape, next] = useCycle({ borderRadius: 0 }, { borderRadius: 40 });
return <motion.div animate={shape} onTap={() => next()} style={{ width: 80, height: 80, background: "#7c3aed" }} />;
}