UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.
feat: add grid-beams component (#749)
Uday Raj Singha3166328f28e
diff --git a/__registry__/index.tsx b/__registry__/index.tsxindex e937c16e..1574e49a 100644--- a/__registry__/index.tsx+++ b/__registry__/index.tsx@@ -1720,6 +1720,30 @@ export const Index: Record<string, any> = {}),meta: undefined,},+ "grid-beams": {+ name: "grid-beams",+ description:+ "A dynamic grid background with animated light beams rays and grid patterns.",+ type: "registry:ui",+ registryDependencies: undefined,+ files: [+ {+ path: "registry/magicui/grid-beams.tsx",+ type: "registry:ui",+ target: "components/magicui/grid-beams.tsx",+ },+ ],+ component: React.lazy(async () => {+ const mod = await import("@/registry/magicui/grid-beams.tsx");+ const exportName =+ Object.keys(mod).find(+ (key) =>+ typeof mod[key] === "function" || typeof mod[key] === "object",+ ) || item.name;+ return { default: mod.default || mod[exportName] };+ }),+ meta: undefined,+ },"magic-card-demo": {name: "magic-card-demo",description:@@ -4789,6 +4813,30 @@ export const Index: Record<string, any> = {}),meta: undefined,},+ "grid-beams-demo": {+ name: "grid-beams-demo",+ description:+ "Demo of the grid-beams component showcasing animated light rays over a grid",+ type: "registry:example",+ registryDependencies: ["grid-beams"],+ files: [+ {+ path: "registry/example/grid-beams-demo.tsx",+ type: "registry:example",+ target: "",+ },+ ],+ component: React.lazy(async () => {+ const mod = await import("@/registry/example/grid-beams-demo.tsx");+ const exportName =+ Object.keys(mod).find(+ (key) =>+ typeof mod[key] === "function" || typeof mod[key] === "object",+ ) || item.name;+ return { default: mod.default || mod[exportName] };+ }),+ meta: undefined,+ },utils: {name: "utils",description: "",diff --git a/config/docs.ts b/config/docs.tsindex 59e091a4..2a6fe4d8 100644--- a/config/docs.ts+++ b/config/docs.ts@@ -599,6 +599,12 @@ export const docsConfig: DocsConfig = {items: [],label: "",},+ {+ title: "Grid Beams",+ href: `/docs/components/grid-beams`,+ items: [],+ label: "New",+ },],},],diff --git a/content/docs/components/grid-beams.mdx b/content/docs/components/grid-beams.mdxnew file mode 100644index 00000000..6d31501f--- /dev/null+++ b/content/docs/components/grid-beams.mdx@@ -0,0 +1,71 @@+---+title: Grid Beams+date: 2024-08-22+description: A dynamic grid background with animated light beams that create stunning visual effects with customizable rays and grid patterns.+author: Uday+published: true+---++<ComponentPreview name="grid-beams-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/grid-beams"+```++</TabsContent>++<TabsContent value="manual">++<Steps>++<Step>Copy and paste the following code into your project.</Step>++<ComponentSource name="grid-beams" />++<Step>Update the import paths to match your project setup.</Step>++</Steps>++</TabsContent>++</Tabs>++## Usage++```tsx+import { GridBeams } from "@/components/magicui/grid-beams";+```++```tsx+<GridBeams>+ <div className="flex h-96 items-center justify-center">+ <h2 className="text-2xl font-bold text-white">Grid Beams Effect</h2>+ </div>+</GridBeams>+```++## Props++| Prop | Type | Default | Description |+| ----------------- | ----------------- | ---------------------------- | --------------------------------------------- |+| `children` | `React.ReactNode` | - | Content to display over the grid beams effect |+| `gridSize` | `number` | `40` | Size of each grid cell in pixels |+| `gridColor` | `string` | `"rgba(200, 220, 255, 0.2)"` | Color of the grid lines (CSS color value) |+| `rayCount` | `number` | `15` | Number of animated light rays |+| `rayOpacity` | `number` | `0.35` | Opacity of the light rays (0-1) |+| `raySpeed` | `number` | `1` | Speed multiplier for ray animations |+| `rayLength` | `string` | `"45vh"` | Length of the light rays (CSS length value) |+| `gridFadeStart` | `number` | `30` | Start percentage for grid fade effect |+| `gridFadeEnd` | `number` | `90` | End percentage for grid fade effect |+| `backgroundColor` | `string` | `"#020412"` | Background color of the component |+| `className` | `string` | - | Additional CSS classes |diff --git a/public/r/grid-beams-demo.json b/public/r/grid-beams-demo.jsonnew file mode 100644index 00000000..871e4c2b--- /dev/null+++ b/public/r/grid-beams-demo.json@@ -0,0 +1,16 @@+{+ "$schema": "https://ui.shadcn.com/schema/registry-item.json",+ "name": "grid-beams-demo",+ "type": "registry:example",+ "description": "Demo of the grid-beams component showcasing animated light rays over a grid",+ "registryDependencies": [+ "grid-beams"+ ],+ "files": [+ {+ "path": "registry/example/grid-beams-demo.tsx",+ "content": "import { GridBeams } from \"@/registry/magicui/grid-beams\";\n\nexport default function GridBeamsDemo() {\n return (\n <div className=\"relative h-[500px] w-full overflow-hidden rounded-lg border\">\n <GridBeams\n gridSize={0}\n gridColor=\"rgba(255, 255, 255, 0.2)\"\n rayCount={20}\n rayOpacity={0.55}\n raySpeed={1.5}\n rayLength=\"40vh\"\n gridFadeStart={5}\n gridFadeEnd={90}\n className=\"h-full w-full\"\n >\n <div className=\"flex h-full items-center justify-center\">\n <div className=\"text-center space-y-6 px-4 mt-50\">\n <h1 className=\"text-5xl md:text-7xl font-bold text-white leading-tight\">\n Grid <span className=\"text-blue-400\">BEAMS</span>\n </h1>\n </div>\n </div>\n </GridBeams>\n </div>\n );\n}\n",+ "type": "registry:example"+ }+ ]+}\ No newline at end of filediff --git a/public/r/grid-beams.json b/public/r/grid-beams.jsonnew file mode 100644index 00000000..75bca2c5--- /dev/null+++ b/public/r/grid-beams.json@@ -0,0 +1,18 @@+{+ "$schema": "https://ui.shadcn.com/schema/registry-item.json",+ "name": "grid-beams",+ "type": "registry:ui",+ "title": "Grid Beams",+ "description": "A dynamic grid background with animated light beams rays and grid patterns.",+ "dependencies": [+ "motion"+ ],+ "files": [+ {+ "path": "registry/magicui/grid-beams.tsx",+ "content": "\"use client\";\n\nimport { motion } from \"motion/react\";\nimport React, { HTMLAttributes, useMemo } from \"react\";\nimport { cn } from \"@/lib/utils\";\n\nconst createGridMask = (start: number, end: number): string => {\n const mid = (start + end) / 2;\n return `linear-gradient(to bottom, transparent 0%, rgba(0,0,0,0.05) ${start}%, rgba(0,0,0,0.2) ${mid}%, rgba(0,0,0,0.6) ${end - 20}%, rgba(0,0,0,1) ${end}%)`;\n};\n\nconst generateRayConfig = (index: number, total: number) => {\n const progress = index / Math.max(total - 1, 1);\n const leftPercent = 2 + progress * 96;\n const rotation = 28 - progress * 56;\n const variation = (index * 0.618) % 1;\n\n return {\n left: `${leftPercent}%`,\n rotation,\n width: 40 + variation * 25,\n duration: 6 + variation * 5,\n delay: -variation * 10,\n swayDuration: 12 + variation * 9,\n swayDelay: -variation * 10,\n blur: 24 + variation * 9,\n strongSway: index % 2 === 0,\n };\n};\n\ninterface GridBeamsProps extends HTMLAttributes<HTMLDivElement> {\n children: React.ReactNode;\n gridSize?: number;\n gridColor?: string;\n rayCount?: number;\n rayOpacity?: number;\n raySpeed?: number;\n rayLength?: string;\n gridFadeStart?: number;\n gridFadeEnd?: number;\n backgroundColor?: string;\n}\n\ninterface LightRayProps {\n left: string;\n rotation: number;\n width: number;\n delay: number;\n duration: number;\n swayDuration: number;\n swayDelay: number;\n blurAmount: number;\n isStrongerSway: boolean;\n opacity: number;\n speed: number;\n length: string;\n}\n\nconst LightRay = React.memo<LightRayProps>(\n ({\n left,\n rotation,\n width,\n delay,\n duration,\n swayDuration,\n swayDelay,\n blurAmount,\n isStrongerSway,\n opacity,\n speed,\n length,\n }) => {\n return (\n <motion.div\n className=\"absolute pointer-events-none -top-[5%] left-[var(--ray-left)] w-[var(--ray-width)] h-[var(--ray-height)] origin-top mix-blend-screen bg-[linear-gradient(to_bottom,rgba(200,220,255,var(--ray-opacity)),rgba(200,220,255,0))] blur-[var(--ray-blur)] translate-x-[-50%] rotate-[var(--ray-rotation)]\"\n style={\n {\n \"--ray-left\": left,\n \"--ray-width\": `${width}px`,\n \"--ray-height\": length,\n \"--ray-opacity\": opacity,\n \"--ray-blur\": `${blurAmount}px`,\n \"--ray-rotation\": `${rotation}deg`,\n } as React.CSSProperties\n }\n animate={{\n opacity: [0.3, 0.7, 0.3],\n transform: [\n `translateX(-50%) rotate(${rotation}deg)`,\n `translateX(-50%) rotate(${rotation + (isStrongerSway ? 1 : 0.5)}deg)`,\n `translateX(-50%) rotate(${rotation}deg)`,\n ],\n }}\n transition={{\n opacity: {\n duration: duration / speed,\n delay: delay / speed,\n repeat: Infinity,\n ease: \"easeInOut\",\n },\n transform: {\n duration: swayDuration / speed,\n delay: swayDelay / speed,\n repeat: Infinity,\n ease: \"easeInOut\",\n },\n }}\n />\n );\n },\n);\n\nexport const GridBeams: React.FC<GridBeamsProps> = ({\n children,\n className,\n gridSize = 40,\n gridColor = \"rgba(200, 220, 255, 0.2)\",\n rayCount = 15,\n rayOpacity = 0.35,\n raySpeed = 1,\n rayLength = \"45vh\",\n gridFadeStart = 30,\n gridFadeEnd = 90,\n backgroundColor = \"#020412\",\n ...props\n}) => {\n const rayConfigs = useMemo(() => {\n return Array.from({ length: rayCount }, (_, i) =>\n generateRayConfig(i, rayCount),\n );\n }, [rayCount]);\n\n const gridMask = useMemo(\n () => createGridMask(gridFadeStart, gridFadeEnd),\n [gridFadeStart, gridFadeEnd],\n );\n\n return (\n <div\n className={cn(\n \"relative overflow-hidden bg-[var(--bg-color)] bg-[radial-gradient(ellipse_at_50%_-20%,#1a2c5a,transparent_70%)]\",\n className,\n )}\n style={\n {\n \"--bg-color\": backgroundColor,\n } as React.CSSProperties\n }\n {...props}\n >\n <div\n className=\"absolute inset-0 pointer-events-none bg-[linear-gradient(var(--grid-color)_1px,transparent_1px),linear-gradient(90deg,var(--grid-color)_1px,transparent_1px)] bg-size-[var(--grid-size)_var(--grid-size)] [mask-image:var(--grid-mask)] [webkit-mask-image:var(--grid-mask)]\"\n style={\n {\n \"--grid-color\": gridColor,\n \"--grid-size\": `${gridSize}px`,\n \"--grid-mask\": gridMask,\n } as React.CSSProperties\n }\n />\n <div className=\"absolute inset-0 overflow-hidden pointer-events-none\">\n {rayConfigs.map((config, index) => (\n <LightRay\n key={index}\n left={config.left}\n rotation={config.rotation}\n width={config.width}\n delay={config.delay}\n duration={config.duration}\n swayDuration={config.swayDuration}\n swayDelay={config.swayDelay}\n blurAmount={config.blur}\n isStrongerSway={config.strongSway}\n opacity={rayOpacity}\n speed={raySpeed}\n length={rayLength}\n />\n ))}\n </div>\n\n <div className=\"relative z-10\">{children}</div>\n </div>\n );\n};\n",+ "type": "registry:ui",+ "target": "components/magicui/grid-beams.tsx"+ }+ ]+}\ No newline at end of filediff --git a/public/registry.json b/public/registry.jsonindex 7d760204..0a524cc1 100644--- a/public/registry.json+++ b/public/registry.json@@ -1363,6 +1363,22 @@}}},+ {+ "name": "grid-beams",+ "type": "registry:ui",+ "title": "Grid Beams",+ "description": "A dynamic grid background with animated light beams rays and grid patterns.",+ "dependencies": [+ "motion"+ ],+ "files": [+ {+ "path": "registry/magicui/grid-beams.tsx",+ "type": "registry:ui",+ "target": "components/magicui/grid-beams.tsx"+ }+ ]+ },{"name": "magic-card-demo","type": "registry:example",@@ -3487,6 +3503,20 @@}]},+ {+ "name": "grid-beams-demo",+ "type": "registry:example",+ "description": "Demo of the grid-beams component showcasing animated light rays over a grid",+ "registryDependencies": [+ "grid-beams"+ ],+ "files": [+ {+ "path": "registry/example/grid-beams-demo.tsx",+ "type": "registry:example"+ }+ ]+ },{"name": "utils","type": "registry:lib",diff --git a/registry.json b/registry.jsonindex 7d760204..0a524cc1 100644--- a/registry.json+++ b/registry.json@@ -1363,6 +1363,22 @@}}},+ {+ "name": "grid-beams",+ "type": "registry:ui",+ "title": "Grid Beams",+ "description": "A dynamic grid background with animated light beams rays and grid patterns.",+ "dependencies": [+ "motion"+ ],+ "files": [+ {+ "path": "registry/magicui/grid-beams.tsx",+ "type": "registry:ui",+ "target": "components/magicui/grid-beams.tsx"+ }+ ]+ },{"name": "magic-card-demo","type": "registry:example",@@ -3487,6 +3503,20 @@}]},+ {+ "name": "grid-beams-demo",+ "type": "registry:example",+ "description": "Demo of the grid-beams component showcasing animated light rays over a grid",+ "registryDependencies": [+ "grid-beams"+ ],+ "files": [+ {+ "path": "registry/example/grid-beams-demo.tsx",+ "type": "registry:example"+ }+ ]+ },{"name": "utils","type": "registry:lib",diff --git a/registry/example/grid-beams-demo.tsx b/registry/example/grid-beams-demo.tsxnew file mode 100644index 00000000..244dea7e--- /dev/null+++ b/registry/example/grid-beams-demo.tsx@@ -0,0 +1,27 @@+import { GridBeams } from "@/registry/magicui/grid-beams";++export default function GridBeamsDemo() {+ return (+ <div className="relative h-[500px] w-full overflow-hidden rounded-lg border">+ <GridBeams+ gridSize={0}+ gridColor="rgba(255, 255, 255, 0.2)"+ rayCount={20}+ rayOpacity={0.55}+ raySpeed={1.5}+ rayLength="40vh"+ gridFadeStart={5}+ gridFadeEnd={90}+ className="h-full w-full"+ >+ <div className="flex h-full items-center justify-center">+ <div className="text-center space-y-6 px-4 mt-50">+ <h1 className="text-5xl md:text-7xl font-bold text-white leading-tight">+ Grid <span className="text-blue-400">BEAMS</span>+ </h1>+ </div>+ </div>+ </GridBeams>+ </div>+ );+}diff --git a/registry/magicui/grid-beams.tsx b/registry/magicui/grid-beams.tsxnew file mode 100644index 00000000..cac99f27--- /dev/null+++ b/registry/magicui/grid-beams.tsx@@ -0,0 +1,185 @@+"use client";++import { motion } from "motion/react";+import React, { HTMLAttributes, useMemo } from "react";+import { cn } from "@/lib/utils";++const createGridMask = (start: number, end: number): string => {+ const mid = (start + end) / 2;+ return `linear-gradient(to bottom, transparent 0%, rgba(0,0,0,0.05) ${start}%, rgba(0,0,0,0.2) ${mid}%, rgba(0,0,0,0.6) ${end - 20}%, rgba(0,0,0,1) ${end}%)`;+};++const generateRayConfig = (index: number, total: number) => {+ const progress = index / Math.max(total - 1, 1);+ const leftPercent = 2 + progress * 96;+ const rotation = 28 - progress * 56;+ const variation = (index * 0.618) % 1;++ return {+ left: `${leftPercent}%`,+ rotation,+ width: 40 + variation * 25,+ duration: 6 + variation * 5,+ delay: -variation * 10,+ swayDuration: 12 + variation * 9,+ swayDelay: -variation * 10,+ blur: 24 + variation * 9,+ strongSway: index % 2 === 0,+ };+};++interface GridBeamsProps extends HTMLAttributes<HTMLDivElement> {+ children: React.ReactNode;+ gridSize?: number;+ gridColor?: string;+ rayCount?: number;+ rayOpacity?: number;+ raySpeed?: number;+ rayLength?: string;+ gridFadeStart?: number;+ gridFadeEnd?: number;+ backgroundColor?: string;+}++interface LightRayProps {+ left: string;+ rotation: number;+ width: number;+ delay: number;+ duration: number;+ swayDuration: number;+ swayDelay: number;+ blurAmount: number;+ isStrongerSway: boolean;+ opacity: number;+ speed: number;+ length: string;+}++const LightRay = React.memo<LightRayProps>(+ ({+ left,+ rotation,+ width,+ delay,+ duration,+ swayDuration,+ swayDelay,+ blurAmount,+ isStrongerSway,+ opacity,+ speed,+ length,+ }) => {+ return (+ <motion.div+ className="absolute pointer-events-none -top-[5%] left-[var(--ray-left)] w-[var(--ray-width)] h-[var(--ray-height)] origin-top mix-blend-screen bg-[linear-gradient(to_bottom,rgba(200,220,255,var(--ray-opacity)),rgba(200,220,255,0))] blur-[var(--ray-blur)] translate-x-[-50%] rotate-[var(--ray-rotation)]"+ style={+ {+ "--ray-left": left,+ "--ray-width": `${width}px`,+ "--ray-height": length,+ "--ray-opacity": opacity,+ "--ray-blur": `${blurAmount}px`,+ "--ray-rotation": `${rotation}deg`,+ } as React.CSSProperties+ }+ animate={{+ opacity: [0.3, 0.7, 0.3],+ transform: [+ `translateX(-50%) rotate(${rotation}deg)`,+ `translateX(-50%) rotate(${rotation + (isStrongerSway ? 1 : 0.5)}deg)`,+ `translateX(-50%) rotate(${rotation}deg)`,+ ],+ }}+ transition={{+ opacity: {+ duration: duration / speed,+ delay: delay / speed,+ repeat: Infinity,+ ease: "easeInOut",+ },+ transform: {+ duration: swayDuration / speed,+ delay: swayDelay / speed,+ repeat: Infinity,+ ease: "easeInOut",+ },+ }}+ />+ );+ },+);++export const GridBeams: React.FC<GridBeamsProps> = ({+ children,+ className,+ gridSize = 40,+ gridColor = "rgba(200, 220, 255, 0.2)",+ rayCount = 15,+ rayOpacity = 0.35,+ raySpeed = 1,+ rayLength = "45vh",+ gridFadeStart = 30,+ gridFadeEnd = 90,+ backgroundColor = "#020412",+ ...props+}) => {+ const rayConfigs = useMemo(() => {+ return Array.from({ length: rayCount }, (_, i) =>+ generateRayConfig(i, rayCount),+ );+ }, [rayCount]);++ const gridMask = useMemo(+ () => createGridMask(gridFadeStart, gridFadeEnd),+ [gridFadeStart, gridFadeEnd],+ );++ return (+ <div+ className={cn(+ "relative overflow-hidden bg-[var(--bg-color)] bg-[radial-gradient(ellipse_at_50%_-20%,#1a2c5a,transparent_70%)]",+ className,+ )}+ style={+ {+ "--bg-color": backgroundColor,+ } as React.CSSProperties+ }+ {...props}+ >+ <div+ className="absolute inset-0 pointer-events-none bg-[linear-gradient(var(--grid-color)_1px,transparent_1px),linear-gradient(90deg,var(--grid-color)_1px,transparent_1px)] bg-size-[var(--grid-size)_var(--grid-size)] [mask-image:var(--grid-mask)] [webkit-mask-image:var(--grid-mask)]"+ style={+ {+ "--grid-color": gridColor,+ "--grid-size": `${gridSize}px`,+ "--grid-mask": gridMask,+ } as React.CSSProperties+ }+ />+ <div className="absolute inset-0 overflow-hidden pointer-events-none">+ {rayConfigs.map((config, index) => (+ <LightRay+ key={index}+ left={config.left}+ rotation={config.rotation}+ width={config.width}+ delay={config.delay}+ duration={config.duration}+ swayDuration={config.swayDuration}+ swayDelay={config.swayDelay}+ blurAmount={config.blur}+ isStrongerSway={config.strongSway}+ opacity={rayOpacity}+ speed={raySpeed}+ length={rayLength}+ />+ ))}+ </div>++ <div className="relative z-10">{children}</div>+ </div>+ );+};diff --git a/registry/registry-examples.ts b/registry/registry-examples.tsindex d6e7178a..b7d03f09 100644--- a/registry/registry-examples.ts+++ b/registry/registry-examples.ts@@ -1865,4 +1865,17 @@ export const examples: Registry["items"] = [},],},+ {+ name: "grid-beams-demo",+ description:+ "Demo of the grid-beams component showcasing animated light rays over a grid",+ type: "registry:example",+ registryDependencies: ["grid-beams"],+ files: [+ {+ path: "registry/example/grid-beams-demo.tsx",+ type: "registry:example",+ },+ ],+ },];diff --git a/registry/registry-ui.ts b/registry/registry-ui.tsindex dbec2c94..d0672c8a 100644--- a/registry/registry-ui.ts+++ b/registry/registry-ui.ts@@ -1293,4 +1293,19 @@ export const ui: Registry["items"] = [},},},+ {+ name: "grid-beams",+ type: "registry:ui",+ title: "Grid Beams",+ description:+ "A dynamic grid background with animated light beams rays and grid patterns.",+ dependencies: ["motion"],+ files: [+ {+ path: "registry/magicui/grid-beams.tsx",+ type: "registry:ui",+ target: "components/magicui/grid-beams.tsx",+ },+ ],+ },];