Skip to content

UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.

feat: add Interactive Grid Pattern component (#480)

Isaac "H3rmel" Reginatod861967c89b2
diff --git a/__registry__/index.tsx b/__registry__/index.tsx
index 0f4ecde1..8afc39c4 100644
--- a/__registry__/index.tsx
+++ b/__registry__/index.tsx
@@ -109,6 +109,19 @@ export const Index: Record<string, any> = {
subcategory: "undefined",
chunks: [],
},
+ "interactive-grid-pattern": {
+ name: "interactive-grid-pattern",
+ type: "registry:ui",
+ registryDependencies: undefined,
+ files: ["registry/default/magicui/interactive-grid-pattern.tsx"],
+ component: React.lazy(
+ () => import("@/registry/default/magicui/interactive-grid-pattern.tsx"),
+ ),
+ source: "",
+ category: "undefined",
+ subcategory: "undefined",
+ chunks: [],
+ },
"dot-pattern": {
name: "dot-pattern",
type: "registry:ui",
@@ -1274,6 +1287,38 @@ export const Index: Record<string, any> = {
subcategory: "undefined",
chunks: [],
},
+ "interactive-grid-pattern-demo": {
+ name: "interactive-grid-pattern-demo",
+ type: "registry:example",
+ registryDependencies: ["interactive-grid-pattern"],
+ files: ["registry/default/example/interactive-grid-pattern-demo.tsx"],
+ component: React.lazy(
+ () =>
+ import(
+ "@/registry/default/example/interactive-grid-pattern-demo.tsx"
+ ),
+ ),
+ source: "",
+ category: "undefined",
+ subcategory: "undefined",
+ chunks: [],
+ },
+ "interactive-grid-pattern-demo-2": {
+ name: "interactive-grid-pattern-demo-2",
+ type: "registry:example",
+ registryDependencies: ["interactive-grid-pattern"],
+ files: ["registry/default/example/interactive-grid-pattern-demo-2.tsx"],
+ component: React.lazy(
+ () =>
+ import(
+ "@/registry/default/example/interactive-grid-pattern-demo-2.tsx"
+ ),
+ ),
+ source: "",
+ category: "undefined",
+ subcategory: "undefined",
+ chunks: [],
+ },
"border-beam-demo": {
name: "border-beam-demo",
type: "registry:example",
diff --git a/config/docs.ts b/config/docs.ts
index 98ed7a93..368341f1 100644
--- a/config/docs.ts
+++ b/config/docs.ts
@@ -478,6 +478,12 @@ export const docsConfig: DocsConfig = {
href: `/docs/components/grid-pattern`,
items: [],
},
+ {
+ title: "Interactive Grid Pattern",
+ href: `/docs/components/interactive-grid-pattern`,
+ items: [],
+ label: "New",
+ },
],
},
],
diff --git a/content/docs/components/interactive-grid-pattern.mdx b/content/docs/components/interactive-grid-pattern.mdx
new file mode 100644
index 00000000..6b0ca04e
--- /dev/null
+++ b/content/docs/components/interactive-grid-pattern.mdx
@@ -0,0 +1,57 @@
+---
+title: Interactive Grid Pattern
+date: 2024-12-31
+description: A interactive background grid pattern made with SVGs, fully customizable using Tailwind CSS.
+author: h3rmel
+published: true
+---
+
+<ComponentPreview name="interactive-grid-pattern-demo" />
+
+## Installation
+
+<Tabs defaultValue="cli">
+
+<TabsList>
+ <TabsTrigger value="cli">CLI</TabsTrigger>
+ <TabsTrigger value="manual">Manual</TabsTrigger>
+</TabsList>
+<TabsContent value="cli">
+
+```bash
+npx shadcn@latest add "https://magicui.design/r/interactive-grid-pattern"
+```
+
+</TabsContent>
+
+<TabsContent value="manual">
+
+<Steps>
+
+<Step>Copy and paste the following code into your project.</Step>
+
+<ComponentSource name="interactive-grid-pattern" />
+
+<Step>Update the import paths to match your project setup.</Step>
+
+</Steps>
+
+</TabsContent>
+
+</Tabs>
+
+## Examples
+
+### Colorful
+
+<ComponentPreview name="interactive-grid-pattern-demo-2" />
+
+## Props
+
+| Prop | Type | Description | Default |
+| ---------------- | ---------------- | --------------------------------------------------------------------------------------------- | ------- |
+| width | number | Width of each square in the grid | 40 |
+| height | number | Height of each square in the grid | 40 |
+| squares | [number, number] | Number of squares in the grid. First number is horizontal squares, second is vertical squares | [24,24] |
+| className | string | Class name applied to the grid container | - |
+| squaresClassName | string | Class name applied to individual squares in the grid | - |
diff --git a/public/r/index.json b/public/r/index.json
index 63b415bf..e15900a8 100644
--- a/public/r/index.json
+++ b/public/r/index.json
@@ -138,6 +138,16 @@
}
]
},
+ {
+ "name": "interactive-grid-pattern",
+ "type": "registry:ui",
+ "files": [
+ {
+ "path": "magicui/interactive-grid-pattern.tsx",
+ "type": "registry:ui"
+ }
+ ]
+ },
{
"name": "dot-pattern",
"type": "registry:ui",
diff --git a/public/r/styles/default/interactive-grid-pattern.json b/public/r/styles/default/interactive-grid-pattern.json
new file mode 100644
index 00000000..9dcfcb8f
--- /dev/null
+++ b/public/r/styles/default/interactive-grid-pattern.json
@@ -0,0 +1,12 @@
+{
+ "name": "interactive-grid-pattern",
+ "type": "registry:ui",
+ "files": [
+ {
+ "path": "magicui/interactive-grid-pattern.tsx",
+ "content": "import { cn } from \"@/lib/utils\";\nimport React, { useState } from \"react\";\n\n/**\n * InteractiveGridPattern is a component that renders a grid pattern with interactive squares.\n *\n * @param width - The width of each square.\n * @param height - The height of each square.\n * @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.\n * @param className - The class name of the grid.\n * @param squaresClassName - The class name of the squares.\n */\ninterface InteractiveGridPatternProps extends React.SVGProps<SVGSVGElement> {\n width?: number;\n height?: number;\n squares?: [number, number]; // [horizontal, vertical]\n className?: string;\n squaresClassName?: string;\n}\n\n/**\n * The InteractiveGridPattern component.\n *\n * @see InteractiveGridPatternProps for the props interface.\n * @returns A React component.\n */\nexport function InteractiveGridPattern({\n width = 40,\n height = 40,\n squares = [24, 24],\n className,\n squaresClassName,\n ...props\n}: InteractiveGridPatternProps) {\n const [horizontal, vertical] = squares;\n const [hoveredSquare, setHoveredSquare] = useState<number | null>(null);\n\n return (\n <svg\n width={width * horizontal}\n height={height * vertical}\n className={cn(\n \"absolute inset-0 h-full w-full border border-gray-400/30\",\n className,\n )}\n {...props}\n >\n {Array.from({ length: horizontal * vertical }).map((_, index) => {\n const x = (index % horizontal) * width;\n const y = Math.floor(index / horizontal) * height;\n return (\n <rect\n key={index}\n x={x}\n y={y}\n width={width}\n height={height}\n className={cn(\n \"stroke-gray-400/30 transition-all duration-100 ease-in-out [&:not(:hover)]:duration-1000\",\n hoveredSquare === index ? \"fill-gray-300/30\" : \"fill-transparent\",\n squaresClassName,\n )}\n onMouseEnter={() => setHoveredSquare(index)}\n onMouseLeave={() => setHoveredSquare(null)}\n />\n );\n })}\n </svg>\n );\n}\n",
+ "type": "registry:ui",
+ "target": ""
+ }
+ ]
+}
\ No newline at end of file
diff --git a/registry/default/example/interactive-grid-pattern-demo-2.tsx b/registry/default/example/interactive-grid-pattern-demo-2.tsx
new file mode 100644
index 00000000..a72aafa5
--- /dev/null
+++ b/registry/default/example/interactive-grid-pattern-demo-2.tsx
@@ -0,0 +1,23 @@
+"use client";
+
+import { cn } from "@/lib/utils";
+import { InteractiveGridPattern } from "@/registry/default/magicui/interactive-grid-pattern";
+
+export default function InteractiveGridPatternDemo() {
+ return (
+ <div className="relative flex h-[500px] w-full flex-col items-center justify-center overflow-hidden rounded-lg border bg-background md:shadow-xl">
+ <p className="z-10 whitespace-pre-wrap text-center text-5xl font-medium tracking-tighter text-black dark:text-white">
+ Interactive Grid Pattern
+ </p>
+ <InteractiveGridPattern
+ className={cn(
+ "[mask-image:radial-gradient(400px_circle_at_center,white,transparent)]",
+ )}
+ width={20}
+ height={20}
+ squares={[80, 80]}
+ squaresClassName="hover:fill-blue-500"
+ />
+ </div>
+ );
+}
diff --git a/registry/default/example/interactive-grid-pattern-demo.tsx b/registry/default/example/interactive-grid-pattern-demo.tsx
new file mode 100644
index 00000000..7f98b84c
--- /dev/null
+++ b/registry/default/example/interactive-grid-pattern-demo.tsx
@@ -0,0 +1,20 @@
+"use client";
+
+import { cn } from "@/lib/utils";
+import { InteractiveGridPattern } from "@/registry/default/magicui/interactive-grid-pattern";
+
+export default function InteractiveGridPatternDemo() {
+ return (
+ <div className="relative flex h-[500px] w-full flex-col items-center justify-center overflow-hidden rounded-lg border bg-background md:shadow-xl">
+ <p className="z-10 whitespace-pre-wrap text-center text-5xl font-medium tracking-tighter text-black dark:text-white">
+ Interactive Grid Pattern
+ </p>
+ <InteractiveGridPattern
+ className={cn(
+ "[mask-image:radial-gradient(400px_circle_at_center,white,transparent)]",
+ "inset-x-0 inset-y-[-30%] h-[200%] skew-y-12",
+ )}
+ />
+ </div>
+ );
+}
diff --git a/registry/default/magicui/interactive-grid-pattern.tsx b/registry/default/magicui/interactive-grid-pattern.tsx
new file mode 100644
index 00000000..ba8ccd28
--- /dev/null
+++ b/registry/default/magicui/interactive-grid-pattern.tsx
@@ -0,0 +1,70 @@
+import { cn } from "@/lib/utils";
+import React, { useState } from "react";
+
+/**
+ * InteractiveGridPattern is a component that renders a grid pattern with interactive squares.
+ *
+ * @param width - The width of each square.
+ * @param height - The height of each square.
+ * @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.
+ * @param className - The class name of the grid.
+ * @param squaresClassName - The class name of the squares.
+ */
+interface InteractiveGridPatternProps extends React.SVGProps<SVGSVGElement> {
+ width?: number;
+ height?: number;
+ squares?: [number, number]; // [horizontal, vertical]
+ className?: string;
+ squaresClassName?: string;
+}
+
+/**
+ * The InteractiveGridPattern component.
+ *
+ * @see InteractiveGridPatternProps for the props interface.
+ * @returns A React component.
+ */
+export function InteractiveGridPattern({
+ width = 40,
+ height = 40,
+ squares = [24, 24],
+ className,
+ squaresClassName,
+ ...props
+}: InteractiveGridPatternProps) {
+ const [horizontal, vertical] = squares;
+ const [hoveredSquare, setHoveredSquare] = useState<number | null>(null);
+
+ return (
+ <svg
+ width={width * horizontal}
+ height={height * vertical}
+ className={cn(
+ "absolute inset-0 h-full w-full border border-gray-400/30",
+ className,
+ )}
+ {...props}
+ >
+ {Array.from({ length: horizontal * vertical }).map((_, index) => {
+ const x = (index % horizontal) * width;
+ const y = Math.floor(index / horizontal) * height;
+ return (
+ <rect
+ key={index}
+ x={x}
+ y={y}
+ width={width}
+ height={height}
+ className={cn(
+ "stroke-gray-400/30 transition-all duration-100 ease-in-out [&:not(:hover)]:duration-1000",
+ hoveredSquare === index ? "fill-gray-300/30" : "fill-transparent",
+ squaresClassName,
+ )}
+ onMouseEnter={() => setHoveredSquare(index)}
+ onMouseLeave={() => setHoveredSquare(null)}
+ />
+ );
+ })}
+ </svg>
+ );
+}
diff --git a/registry/registry-examples.ts b/registry/registry-examples.ts
index ad0b20f6..5bd464b6 100644
--- a/registry/registry-examples.ts
+++ b/registry/registry-examples.ts
@@ -238,6 +238,18 @@ export const examples: Registry = [
registryDependencies: ["animated-grid-pattern"],
files: ["example/animated-grid-pattern-demo.tsx"],
},
+ {
+ name: "interactive-grid-pattern-demo",
+ type: "registry:example",
+ registryDependencies: ["interactive-grid-pattern"],
+ files: ["example/interactive-grid-pattern-demo.tsx"],
+ },
+ {
+ name: "interactive-grid-pattern-demo-2",
+ type: "registry:example",
+ registryDependencies: ["interactive-grid-pattern"],
+ files: ["example/interactive-grid-pattern-demo-2.tsx"],
+ },
{
name: "border-beam-demo",
type: "registry:example",
diff --git a/registry/registry-ui.ts b/registry/registry-ui.ts
index de3c321d..7c5ca4ca 100644
--- a/registry/registry-ui.ts
+++ b/registry/registry-ui.ts
@@ -87,6 +87,11 @@ export const ui: Registry = [
type: "registry:ui",
files: ["magicui/grid-pattern.tsx"],
},
+ {
+ name: "interactive-grid-pattern",
+ type: "registry:ui",
+ files: ["magicui/interactive-grid-pattern.tsx"],
+ },
{
name: "dot-pattern",
type: "registry:ui",