magnet-lines
componentry/magnet-linesFreeComponent
A grid of lines that rotate to face the cursor, creating a magnetic field effect.
Live preview
live · rendered by the registry
Rendered by componentry on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://componentry.dev/r/magnet-lines.jsonSource
1"use client";23import React, { useRef, useState, useEffect } from "react";4import { motion } from "framer-motion";56interface MagnetLinesProps {7 rows?: number;8 columns?: number;9 containerSize?: string;10 lineColor?: string;11 lineWidth?: string;12 lineHeight?: string;13 baseAngle?: number;14 className?: string;15 style?: React.CSSProperties;16}1718export function MagnetLines({19 rows = 9,20 columns = 9,21 containerSize = "80vmin",22 lineColor = "#efefef",23 lineWidth = "1vmin",24 lineHeight = "6vmin",25 baseAngle = 0,26 className = "",27 style = {},28}: MagnetLinesProps) {29 const containerRef = useRef<HTMLDivElement>(null);3031 // Spans the grid32 const total = rows * columns;33 const spans = Array.from({ length: total }, (_, i) => i);3435 return (36 <div37 ref={containerRef}38 className={`relative grid place-items-center ${className}`}39 style={{40 gridTemplateColumns: `repeat(${columns}, 1fr)`,41 gridTemplateRows: `repeat(${rows}, 1fr)`,42 width: containerSize,43 height: containerSize,44 ...style,45 }}46 >47 {spans.map((i) => (48 <Line49 key={i}50 containerRef={containerRef}51 lineColor={lineColor}52 lineWidth={lineWidth}53 lineHeight={lineHeight}54 baseAngle={baseAngle}55 />56 ))}57 </div>58 );59}6061function Line({62 containerRef,63 lineColor,64 lineWidth,65 lineHeight,66 baseAngle,67}: {68 containerRef: React.RefObject<HTMLDivElement | null>;69 lineColor: string;70 lineWidth: string;71 lineHeight: string;72 baseAngle: number;73}) {74 const lineRef = useRef<HTMLDivElement>(null);75 const [rotate, setRotate] = useState(baseAngle);7677 useEffect(() => {78 const updateRotation = (e: MouseEvent) => {79 if (!lineRef.current || !containerRef.current) return;8081 const rect = lineRef.current.getBoundingClientRect();82 const centerX = rect.left + rect.width / 2;83 const centerY = rect.top + rect.height / 2;8485 const mouseX = e.clientX;86 const mouseY = e.clientY;8788 const distanceX = mouseX - centerX;89 const distanceY = mouseY - centerY;9091 const angle = (Math.atan2(distanceY, distanceX) * 180) / Math.PI;9293// … truncated
More from componentry
All 59 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Animated Gradientanimated-gradient | componentry | Components | Free | no deps | |
| ASCII Effectascii-effect | componentry | Components | Free | no deps | |
| Aurora Flowaurora-flow | componentry | Components | Free | no deps | |
| auth-modalauth-modal | componentry | Components | Free | no deps | |
| border-beamborder-beam | componentry | Components | Free | no deps | |
| circuit-boardcircuit-board | componentry | Components | Free | no deps | |
| closing-plasmaclosing-plasma | componentry | Components | Free | no deps | |
| collection-surfercollection-surfer | componentry | Components | Free | no deps |
