Magnet Lines
Grid of lines that rotate to point at the pointer.
pnpm dlx shadcn@latest add @leo/magnet-linesProps
| Name | Type | Default | Description |
|---|---|---|---|
| baseAngle | number | undefined | -10 | |
| className | string | undefined | - | |
| columns | number | undefined | 9 | |
| containerSize | string | undefined | 80vmin | |
| lineColor | string | undefined | #efefef | |
| lineHeight | string | undefined | 6vmin | |
| lineWidth | string | undefined | 1vmin | |
| rows | number | undefined | 9 | |
| style | CSSProperties | undefined | {} |
Source
"use client";
import React, { type CSSProperties, useEffect, useRef } from "react";
import { cn } from "@/lib/utils";
export interface MagnetLinesProps {
rows?: number;
columns?: number;
containerSize?: string;
lineColor?: string;
lineWidth?: string;
lineHeight?: string;
baseAngle?: number;
className?: string;
style?: CSSProperties;
}
const MagnetLines: React.FC<MagnetLinesProps> = ({
rows = 9,
columns = 9,
containerSize = "80vmin",
lineColor = "#efefef",
lineWidth = "1vmin",
lineHeight = "6vmin",
baseAngle = -10,
className,
style = {},
}) => {
const containerRef = useRef<HTMLDivElement | null>(null);
// biome-ignore lint/correctness/useExhaustiveDependencies: rows and columns re-create the spans the effect queries
useEffect(() => {
const container = containerRef.current;
if (!container) return;
const items = container.querySelectorAll<HTMLSpanElement>("span");
const onPointerMove = (pointer: { x: number; y: number }) => {
items.forEach((item) => {
const rect = item.getBoundingClientRect();
const centerX = rect.x + rect.width / 2;
const centerY = rect.y + rect.height / 2;
const b = pointer.x - centerX;
const a = pointer.y - centerY;
const c = Math.sqrt(a * a + b * b) || 1;
const r = ((Math.acos(b / c) * 180) / Math.PI) * (pointer.y > centerY ? 1 : -1);
item.style.setProperty("--rotate", `${r}deg`);
});
};
const handlePointerMove = (e: PointerEvent) => {
onPointerMove({ x: e.x, y: e.y });
};
window.addEventListener("pointermove", handlePointerMove);
if (items.length) {
const middleIndex = Math.floor(items.length / 2);
const rect = items[middleIndex].getBoundingClientRect();
onPointerMove({ x: rect.x, y: rect.y });
}
return () => {
window.removeEventListener("pointermove", handlePointerMove);
};
}, [rows, columns]);
const total = rows * columns;
const spans = Array.from({ length: total }, (_, i) => (
<span
// biome-ignore lint/suspicious/noArrayIndexKey: the grid is static and index-addressed
key={i}
className="block origin-center"
style={{
backgroundColor: lineColor,
width: lineWidth,
height: lineHeight,
//@ts-expect-error
"--rotate": `${baseAngle}deg`,
transform: "rotate(var(--rotate))",
willChange: "transform",
}}
/>
));
return (
<div
ref={containerRef}
className={cn("grid place-items-center", className)}
style={{
gridTemplateColumns: `repeat(${columns}, 1fr)`,
gridTemplateRows: `repeat(${rows}, 1fr)`,
width: containerSize,
height: containerSize,
...style,
}}
>
{spans}
</div>
);
};
export { MagnetLines };
export default MagnetLines;