Grid Motion
Grid of tiles on a rotated canvas whose rows slide in opposite directions as the pointer moves, with GSAP inertia.
pnpm dlx shadcn@latest add @leo/grid-motionProps
| Name | Type | Default | Description |
|---|---|---|---|
| className | string | undefined | - | |
| gradientColor | string | undefined | black | |
| items | ReactNode[] | undefined | [] |
Dependencies
- gsap@^3.13.0
Source
"use client";
import { gsap } from "gsap";
import { type FC, type ReactNode, useEffect, useRef } from "react";
import { cn } from "@/lib/utils";
export interface GridMotionProps {
items?: (string | ReactNode)[];
gradientColor?: string;
className?: string;
}
const GridMotion: FC<GridMotionProps> = ({ items = [], gradientColor = "black", className }) => {
const gridRef = useRef<HTMLDivElement>(null);
const rowRefs = useRef<(HTMLDivElement | null)[]>([]);
// `window` is not available while rendering on the server; centred in the effect below.
const mouseXRef = useRef<number>(0);
const totalItems = 28;
const defaultItems = Array.from({ length: totalItems }, (_, index) => `Item ${index + 1}`);
const combinedItems = items.length > 0 ? items.slice(0, totalItems) : defaultItems;
useEffect(() => {
gsap.ticker.lagSmoothing(0);
mouseXRef.current = window.innerWidth / 2;
const handleMouseMove = (e: MouseEvent): void => {
mouseXRef.current = e.clientX;
};
const updateMotion = (): void => {
const maxMoveAmount = 300;
const baseDuration = 0.8;
const inertiaFactors = [0.6, 0.4, 0.3, 0.2];
rowRefs.current.forEach((row, index) => {
if (row) {
const direction = index % 2 === 0 ? 1 : -1;
const moveAmount =
((mouseXRef.current / window.innerWidth) * maxMoveAmount - maxMoveAmount / 2) *
direction;
gsap.to(row, {
x: moveAmount,
duration: baseDuration + inertiaFactors[index % inertiaFactors.length],
ease: "power3.out",
overwrite: "auto",
});
}
});
};
const removeAnimationLoop = gsap.ticker.add(updateMotion);
window.addEventListener("mousemove", handleMouseMove);
return () => {
window.removeEventListener("mousemove", handleMouseMove);
removeAnimationLoop();
gsap.killTweensOf(rowRefs.current.filter(Boolean));
};
}, []);
return (
<div ref={gridRef} className={cn("h-full w-full overflow-hidden", className)}>
<section
className="w-full h-full overflow-hidden relative flex items-center justify-center"
style={{
background: `radial-gradient(circle, ${gradientColor} 0%, transparent 100%)`,
}}
>
<div className="absolute inset-0 pointer-events-none z-[4] bg-[length:250px]"></div>
<div className="gap-4 flex-none relative w-[150%] h-[150%] grid grid-rows-4 grid-cols-1 rotate-[-15deg] origin-center z-[2]">
{Array.from({ length: 4 }, (_, rowIndex) => (
<div
// biome-ignore lint/suspicious/noArrayIndexKey: fixed grid, never reordered
key={rowIndex}
className="grid gap-4 grid-cols-7"
style={{ willChange: "transform, filter" }}
ref={(el) => {
if (el) rowRefs.current[rowIndex] = el;
}}
>
{Array.from({ length: 7 }, (_, itemIndex) => {
const content = combinedItems[rowIndex * 7 + itemIndex];
return (
// biome-ignore lint/suspicious/noArrayIndexKey: fixed grid, never reordered
<div key={itemIndex} className="relative">
<div className="relative w-full h-full overflow-hidden rounded-[10px] bg-[#111] flex items-center justify-center text-white text-[1.5rem]">
{typeof content === "string" && content.startsWith("http") ? (
<div
className="w-full h-full bg-cover bg-center absolute top-0 left-0"
style={{ backgroundImage: `url(${content})` }}
></div>
) : (
<div className="p-4 text-center z-[1]">{content}</div>
)}
</div>
</div>
);
})}
</div>
))}
</div>
<div className="relative w-full h-full top-0 left-0 pointer-events-none"></div>
</section>
</div>
);
};
export { GridMotion };
export default GridMotion;