Interactive Grid Pattern
magicui/interactive-grid-patternFreeComponent
A interactive background grid pattern made with SVGs, fully customizable using Tailwind CSS.
Live preview
live · rendered by the registry
Rendered by magicui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://magicui.design/r/interactive-grid-pattern.jsonSource
1"use client"23import React, { useState } from "react"45import { cn } from "@/lib/utils"67/**8 * InteractiveGridPattern is a component that renders a grid pattern with interactive squares.9 *10 * @param width - The width of each square.11 * @param height - The height of each square.12 * @param squares - The number of squares in the grid. The first element is the number of horizontal squares, and the second element is the number of vertical squares.13 * @param className - The class name of the grid.14 * @param squaresClassName - The class name of the squares.15 */16interface InteractiveGridPatternProps extends React.SVGProps<SVGSVGElement> {17 width?: number18 height?: number19 squares?: [number, number] // [horizontal, vertical]20 className?: string21 squaresClassName?: string22}2324/**25 * The InteractiveGridPattern component.26 *27 * @see InteractiveGridPatternProps for the props interface.28 * @returns A React component.29 */30export function InteractiveGridPattern({31 width = 40,32 height = 40,33 squares = [24, 24],34 className,35 squaresClassName,36 ...props37}: InteractiveGridPatternProps) {38 const [horizontal, vertical] = squares39 const [hoveredSquare, setHoveredSquare] = useState<number | null>(null)4041 return (42 <svg43 width={width * horizontal}44 height={height * vertical}45 className={cn(46 "absolute inset-0 h-full w-full border border-gray-400/30",47 className48 )}49 {...props}50 >51 {Array.from({ length: horizontal * vertical }).map((_, index) => {52 const x = (index % horizontal) * width53 const y = Math.floor(index / horizontal) * height54 return (55 <rect56 key={index}57 x={x}58 y={y}59 width={width}60 height={height}61 className={cn(62 "stroke-gray-400/30 transition-all duration-100 ease-in-out not-[&:hover]:duration-1000",63 hoveredSquare === index ? "fill-gray-300/30" : "fill-transparent",64 squaresClassName65 )}66 onMouseEnter={() => setHoveredSquare(index)}67 onMouseLeave={() => setHoveredSquare(null)}68 />69 )70 })}71 </svg>72 )73}
Files it writes
More from magicui
All 247 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Androidandroid | magicui | Components | Free | no deps | |
| Animated Beamanimated-beam | magicui | Components | Free | 1 dep | |
| Animated Circular Progress Baranimated-circular-progress-bar | magicui | Components | Free | no deps | |
| Animated Gradient Textanimated-gradient-text | magicui | Components | Free | no deps | |
| Animated Grid Patternanimated-grid-pattern | magicui | Components | Free | 1 dep | |
| Animated Listanimated-list | magicui | Components | Free | 1 dep | |
| Animated Shiny Textanimated-shiny-text | magicui | Components | Free | no deps | |
| Theme Toggleranimated-theme-toggler | magicui | Components | Free | 1 dep |
