Installation
Add the component
npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/tilt.jsonFirst component? Set up your project first
Frameworks
The live preview is React. Each tab below is the file that ships for that framework, with the same durations, springs and reduced-motion rule.
"use client";
import * as React from "react";
import {
motion,
useMotionValue,
useSpring,
type HTMLMotionProps,
type SpringOptions,
} from "motion/react";
import { cn } from "@/lib/utils";
import { useReduceMotion, useTempoTransition } from "../motion";
export type TiltProps = HTMLMotionProps<"div"> & {
maxTilt?: number;
perspective?: number;
transition?: SpringOptions;
};
const defaultSpring: SpringOptions = {
stiffness: 300,
damping: 25,
mass: 0.5,
};
export function Tilt({
maxTilt = 10,
perspective = 800,
transition,
className,
style,
onPointerMove,
onPointerLeave,
...props
}: TiltProps) {
const rawX = useMotionValue(0);
const rawY = useMotionValue(0);
const tempoSpring = useTempoTransition(transition ?? defaultSpring) as SpringOptions;
const rotateX = useSpring(rawX, tempoSpring);
const rotateY = useSpring(rawY, tempoSpring);
const reduce = useReduceMotion();
const handleMove = (event: React.PointerEvent<HTMLDivElement>) => {
onPointerMove?.(event);
if (reduce) return;
const rect = event.currentTarget.getBoundingClientRect();
const px = rect.width ? (event.clientX - rect.left) / rect.width : 0.5;
const py = rect.height ? (event.clientY - rect.top) / rect.height : 0.5;
rawY.set((px * 2 - 1) * maxTilt);
rawX.set(-(py * 2 - 1) * maxTilt);
};
const handleLeave = (event: React.PointerEvent<HTMLDivElement>) => {
onPointerLeave?.(event);
rawX.set(0);
rawY.set(0);
};
React.useEffect(() => {
if (!reduce) return;
rawX.jump(0);
rawY.jump(0);
rotateX.jump(0);
rotateY.jump(0);
}, [rawX, rawY, reduce, rotateX, rotateY]);
return (
<motion.div
data-slot="tilt"
className={cn("will-change-transform", className)}
style={{
rotateX,
rotateY,
transformPerspective: perspective,
transformStyle: "preserve-3d",
...style,
}}
onPointerMove={handleMove}
onPointerLeave={handleLeave}
{...props}
>
{props.children}
</motion.div>
);
}Install
npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/tilt.jsonDependencies
motion lucide-reactThe registry keeps the component source, its motion tokens and the reduced-motion branch together, so the install never pulls a different animation from the preview.
Usage
Import the component and compose it exactly like the primitive it wraps. Motion is enabled by default; the tempo prop scales every duration and softens every spring.
import { Tilt } from "@/components/prequelui/effects/tilt";
export default function Demo() {
return (
<Tilt maxTilt={10} perspective={800} className="size-56 rounded-2xl border p-5">
Look at me.
</Tilt>
);
}import { Tilt } from "@/components/prequelui/tilt";
export function Variant() {
return (
<Tilt tempo={1.2}>
…
</Tilt>
);
}Animation
The card maps the pointer position to a pair of rotations. The raw values are passed through the same spring, so the tilt eases in, retargets while the pointer moves and returns flat when it leaves. Perspective is set on the transform itself so the card keeps its own layout.
- Spring: stiffness 300, damping 25, mass 0.5.
- Maximum tilt is 10° by default and perspective is 800.
- The pointer is read from the element rect and normalised to −1…1.
- Reduced motion settles both rotations at zero.
API Reference
Each part accepts the usual HTML props in addition to the motion props below. No accessibility prop is hidden behind the animation layer.
Tilt
| Prop | Type | Default |
|---|---|---|
| maxTilt | number | 10 |
| perspective | number | 800 |
| transition | SpringOptions | 300 / 25 / 0.5 |
| className | string | — |