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 hover button component with dark and light mode support (#407)

Aayush Bharti38c0aeb9b4a7
diff --git a/__registry__/index.tsx b/__registry__/index.tsx
index 81112cae..a408e40a 100644
--- a/__registry__/index.tsx
+++ b/__registry__/index.tsx
@@ -737,6 +737,19 @@ export const Index: Record<string, any> = {
subcategory: "undefined",
chunks: [],
},
+ "interactive-hover-button": {
+ name: "interactive-hover-button",
+ type: "registry:ui",
+ registryDependencies: undefined,
+ files: ["registry/default/magicui/interactive-hover-button.tsx"],
+ component: React.lazy(
+ () => import("@/registry/default/magicui/interactive-hover-button.tsx"),
+ ),
+ source: "",
+ category: "undefined",
+ subcategory: "undefined",
+ chunks: [],
+ },
"magic-card-demo": {
name: "magic-card-demo",
type: "registry:example",
@@ -1878,6 +1891,22 @@ export const Index: Record<string, any> = {
subcategory: "undefined",
chunks: [],
},
+ "interactive-hover-button-demo": {
+ name: "interactive-hover-button-demo",
+ type: "registry:example",
+ registryDependencies: undefined,
+ files: ["registry/default/example/interactive-hover-button-demo.tsx"],
+ component: React.lazy(
+ () =>
+ import(
+ "@/registry/default/example/interactive-hover-button-demo.tsx"
+ ),
+ ),
+ source: "",
+ category: "undefined",
+ subcategory: "undefined",
+ chunks: [],
+ },
utils: {
name: "utils",
type: "registry:lib",
diff --git a/config/docs.ts b/config/docs.ts
index f6992f29..90b738d9 100644
--- a/config/docs.ts
+++ b/config/docs.ts
@@ -417,6 +417,12 @@ export const docsConfig: DocsConfig = {
items: [],
label: "",
},
+ {
+ title: "Interactive Hover Button",
+ href: `/docs/components/interactive-hover-button`,
+ items: [],
+ label: "New",
+ },
{
title: "Animated Subscribe Button",
href: `/docs/components/animated-subscribe-button`,
diff --git a/content/docs/components/interactive-hover-button.mdx b/content/docs/components/interactive-hover-button.mdx
new file mode 100644
index 00000000..497cbe0f
--- /dev/null
+++ b/content/docs/components/interactive-hover-button.mdx
@@ -0,0 +1,52 @@
+---
+title: Interactive Hover Button
+date: 2024-12-17
+description: A visually engaging button component that responds to hover with dynamic transitions, adapting smoothly between light and dark modes for enhanced user interactivity.
+author: aayushbharti
+published: true
+---
+
+<ComponentPreview name="interactive-hover-button-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-hover-button"
+```
+
+</TabsContent>
+
+<TabsContent value="manual">
+
+<Steps>
+
+<Step>Copy and paste the following code into your project.</Step>
+
+<ComponentSource name="interactive-hover-button" />
+
+<Step>Update the import paths to match your project setup.</Step>
+
+</Steps>
+
+</TabsContent>
+
+</Tabs>
+
+## Props
+
+| Prop | Type | Description | Default |
+| --------- | ------ | --------------------------------------------- | --------- |
+| text | string | The text to be displayed inside the button | Button |
+| className | string | Additional class names to style the component | Undefined |
+
+## Credits
+
+- Credit to [@AayushBharti](https://github.com/AayushBharti)
diff --git a/public/r/index.json b/public/r/index.json
index 7c501ed7..f15c95cc 100644
--- a/public/r/index.json
+++ b/public/r/index.json
@@ -999,5 +999,18 @@
"--color-5": "90 100% 63%"
}
}
+ },
+ {
+ "name": "interactive-hover-button",
+ "type": "registry:ui",
+ "dependencies": [
+ "lucide-react"
+ ],
+ "files": [
+ {
+ "path": "magicui/interactive-hover-button.tsx",
+ "type": "registry:ui"
+ }
+ ]
}
]
\ No newline at end of file
diff --git a/public/r/styles/default/animated-beam.json b/public/r/styles/default/animated-beam.json
index 0df98c2e..b3229f52 100644
--- a/public/r/styles/default/animated-beam.json
+++ b/public/r/styles/default/animated-beam.json
@@ -7,7 +7,7 @@
"files": [
{
"path": "magicui/animated-beam.tsx",
- "content": "\"use client\";\n\nimport { RefObject, useEffect, useId, useState } from \"react\";\nimport { motion } from \"framer-motion\";\n\nimport { cn } from \"@/lib/utils\";\n\nexport interface AnimatedBeamProps {\n className?: string;\n containerRef: RefObject<HTMLElement>; // Container ref\n fromRef: RefObject<HTMLElement>;\n toRef: RefObject<HTMLElement>;\n curvature?: number;\n reverse?: boolean;\n pathColor?: string;\n pathWidth?: number;\n pathOpacity?: number;\n gradientStartColor?: string;\n gradientStopColor?: string;\n delay?: number;\n duration?: number;\n startXOffset?: number;\n startYOffset?: number;\n endXOffset?: number;\n endYOffset?: number;\n}\n\nexport const AnimatedBeam: React.FC<AnimatedBeamProps> = ({\n className,\n containerRef,\n fromRef,\n toRef,\n curvature = 0,\n reverse = false, // Include the reverse prop\n duration = Math.random() * 3 + 4,\n delay = 0,\n pathColor = \"gray\",\n pathWidth = 2,\n pathOpacity = 0.2,\n gradientStartColor = \"#ffaa40\",\n gradientStopColor = \"#9c40ff\",\n startXOffset = 0,\n startYOffset = 0,\n endXOffset = 0,\n endYOffset = 0,\n}) => {\n const id = useId();\n const [pathD, setPathD] = useState(\"\");\n const [svgDimensions, setSvgDimensions] = useState({ width: 0, height: 0 });\n\n // Calculate the gradient coordinates based on the reverse prop\n const gradientCoordinates = reverse\n ? {\n x1: [\"90%\", \"-10%\"],\n x2: [\"100%\", \"0%\"],\n y1: [\"0%\", \"0%\"],\n y2: [\"0%\", \"0%\"],\n }\n : {\n x1: [\"10%\", \"110%\"],\n x2: [\"0%\", \"100%\"],\n y1: [\"0%\", \"0%\"],\n y2: [\"0%\", \"0%\"],\n };\n\n useEffect(() => {\n const updatePath = () => {\n if (containerRef.current && fromRef.current && toRef.current) {\n const containerRect = containerRef.current.getBoundingClientRect();\n const rectA = fromRef.current.getBoundingClientRect();\n const rectB = toRef.current.getBoundingClientRect();\n\n const svgWidth = containerRect.width;\n const svgHeight = containerRect.height;\n setSvgDimensions({ width: svgWidth, height: svgHeight });\n\n const startX =\n rectA.left - containerRect.left + rectA.width / 2 + startXOffset;\n const startY =\n rectA.top - containerRect.top + rectA.height / 2 + startYOffset;\n const endX =\n rectB.left - containerRect.left + rectB.width / 2 + endXOffset;\n const endY =\n rectB.top - containerRect.top + rectB.height / 2 + endYOffset;\n\n const controlY = startY - curvature;\n const d = `M ${startX},${startY} Q ${\n (startX + endX) / 2\n },${controlY} ${endX},${endY}`;\n setPathD(d);\n }\n };\n\n // Initialize ResizeObserver\n const resizeObserver = new ResizeObserver((entries) => {\n // For all entries, recalculate the path\n for (let entry of entries) {\n updatePath();\n }\n });\n\n // Observe the container element\n if (containerRef.current) {\n resizeObserver.observe(containerRef.current);\n }\n\n // Call the updatePath initially to set the initial path\n updatePath();\n\n // Clean up the observer on component unmount\n return () => {\n resizeObserver.disconnect();\n };\n }, [\n containerRef,\n fromRef,\n toRef,\n curvature,\n startXOffset,\n startYOffset,\n endXOffset,\n endYOffset,\n ]);\n\n return (\n <svg\n fill=\"none\"\n width={svgDimensions.width}\n height={svgDimensions.height}\n xmlns=\"http://www.w3.org/2000/svg\"\n className={cn(\n \"pointer-events-none absolute left-0 top-0 transform-gpu stroke-2\",\n className,\n )}\n viewBox={`0 0 ${svgDimensions.width} ${svgDimensions.height}`}\n >\n <path\n d={pathD}\n stroke={pathColor}\n strokeWidth={pathWidth}\n strokeOpacity={pathOpacity}\n strokeLinecap=\"round\"\n />\n <path\n d={pathD}\n strokeWidth={pathWidth}\n stroke={`url(#${id})`}\n strokeOpacity=\"1\"\n strokeLinecap=\"round\"\n />\n <defs>\n <motion.linearGradient\n className=\"transform-gpu\"\n id={id}\n gradientUnits={\"userSpaceOnUse\"}\n initial={{\n x1: \"0%\",\n x2: \"0%\",\n y1: \"0%\",\n y2: \"0%\",\n }}\n animate={{\n x1: gradientCoordinates.x1,\n x2: gradientCoordinates.x2,\n y1: gradientCoordinates.y1,\n y2: gradientCoordinates.y2,\n }}\n transition={{\n delay,\n duration,\n ease: [0.16, 1, 0.3, 1], // https://easings.net/#easeOutExpo\n repeat: Infinity,\n repeatDelay: 0,\n }}\n >\n <stop stopColor={gradientStartColor} stopOpacity=\"0\"></stop>\n <stop stopColor={gradientStartColor}></stop>\n <stop offset=\"32.5%\" stopColor={gradientStopColor}></stop>\n <stop\n offset=\"100%\"\n stopColor={gradientStopColor}\n stopOpacity=\"0\"\n ></stop>\n </motion.linearGradient>\n </defs>\n </svg>\n );\n};\n",
+ "content": "\"use client\";\n\nimport { RefObject, useEffect, useId, useState } from \"react\";\nimport { motion } from \"framer-motion\";\n\nimport { cn } from \"@/lib/utils\";\n\nexport interface AnimatedBeamProps {\n className?: string;\n containerRef: RefObject<HTMLElement | null>; // Container ref\n fromRef: RefObject<HTMLElement | null>;\n toRef: RefObject<HTMLElement | null>;\n curvature?: number;\n reverse?: boolean;\n pathColor?: string;\n pathWidth?: number;\n pathOpacity?: number;\n gradientStartColor?: string;\n gradientStopColor?: string;\n delay?: number;\n duration?: number;\n startXOffset?: number;\n startYOffset?: number;\n endXOffset?: number;\n endYOffset?: number;\n}\n\nexport const AnimatedBeam: React.FC<AnimatedBeamProps> = ({\n className,\n containerRef,\n fromRef,\n toRef,\n curvature = 0,\n reverse = false, // Include the reverse prop\n duration = Math.random() * 3 + 4,\n delay = 0,\n pathColor = \"gray\",\n pathWidth = 2,\n pathOpacity = 0.2,\n gradientStartColor = \"#ffaa40\",\n gradientStopColor = \"#9c40ff\",\n startXOffset = 0,\n startYOffset = 0,\n endXOffset = 0,\n endYOffset = 0,\n}) => {\n const id = useId();\n const [pathD, setPathD] = useState(\"\");\n const [svgDimensions, setSvgDimensions] = useState({ width: 0, height: 0 });\n\n // Calculate the gradient coordinates based on the reverse prop\n const gradientCoordinates = reverse\n ? {\n x1: [\"90%\", \"-10%\"],\n x2: [\"100%\", \"0%\"],\n y1: [\"0%\", \"0%\"],\n y2: [\"0%\", \"0%\"],\n }\n : {\n x1: [\"10%\", \"110%\"],\n x2: [\"0%\", \"100%\"],\n y1: [\"0%\", \"0%\"],\n y2: [\"0%\", \"0%\"],\n };\n\n useEffect(() => {\n const updatePath = () => {\n if (containerRef.current && fromRef.current && toRef.current) {\n const containerRect = containerRef.current.getBoundingClientRect();\n const rectA = fromRef.current.getBoundingClientRect();\n const rectB = toRef.current.getBoundingClientRect();\n\n const svgWidth = containerRect.width;\n const svgHeight = containerRect.height;\n setSvgDimensions({ width: svgWidth, height: svgHeight });\n\n const startX =\n rectA.left - containerRect.left + rectA.width / 2 + startXOffset;\n const startY =\n rectA.top - containerRect.top + rectA.height / 2 + startYOffset;\n const endX =\n rectB.left - containerRect.left + rectB.width / 2 + endXOffset;\n const endY =\n rectB.top - containerRect.top + rectB.height / 2 + endYOffset;\n\n const controlY = startY - curvature;\n const d = `M ${startX},${startY} Q ${\n (startX + endX) / 2\n },${controlY} ${endX},${endY}`;\n setPathD(d);\n }\n };\n\n // Initialize ResizeObserver\n const resizeObserver = new ResizeObserver((entries) => {\n // For all entries, recalculate the path\n for (let entry of entries) {\n updatePath();\n }\n });\n\n // Observe the container element\n if (containerRef.current) {\n resizeObserver.observe(containerRef.current);\n }\n\n // Call the updatePath initially to set the initial path\n updatePath();\n\n // Clean up the observer on component unmount\n return () => {\n resizeObserver.disconnect();\n };\n }, [\n containerRef,\n fromRef,\n toRef,\n curvature,\n startXOffset,\n startYOffset,\n endXOffset,\n endYOffset,\n ]);\n\n return (\n <svg\n fill=\"none\"\n width={svgDimensions.width}\n height={svgDimensions.height}\n xmlns=\"http://www.w3.org/2000/svg\"\n className={cn(\n \"pointer-events-none absolute left-0 top-0 transform-gpu stroke-2\",\n className,\n )}\n viewBox={`0 0 ${svgDimensions.width} ${svgDimensions.height}`}\n >\n <path\n d={pathD}\n stroke={pathColor}\n strokeWidth={pathWidth}\n strokeOpacity={pathOpacity}\n strokeLinecap=\"round\"\n />\n <path\n d={pathD}\n strokeWidth={pathWidth}\n stroke={`url(#${id})`}\n strokeOpacity=\"1\"\n strokeLinecap=\"round\"\n />\n <defs>\n <motion.linearGradient\n className=\"transform-gpu\"\n id={id}\n gradientUnits={\"userSpaceOnUse\"}\n initial={{\n x1: \"0%\",\n x2: \"0%\",\n y1: \"0%\",\n y2: \"0%\",\n }}\n animate={{\n x1: gradientCoordinates.x1,\n x2: gradientCoordinates.x2,\n y1: gradientCoordinates.y1,\n y2: gradientCoordinates.y2,\n }}\n transition={{\n delay,\n duration,\n ease: [0.16, 1, 0.3, 1], // https://easings.net/#easeOutExpo\n repeat: Infinity,\n repeatDelay: 0,\n }}\n >\n <stop stopColor={gradientStartColor} stopOpacity=\"0\"></stop>\n <stop stopColor={gradientStartColor}></stop>\n <stop offset=\"32.5%\" stopColor={gradientStopColor}></stop>\n <stop\n offset=\"100%\"\n stopColor={gradientStopColor}\n stopOpacity=\"0\"\n ></stop>\n </motion.linearGradient>\n </defs>\n </svg>\n );\n};\n",
"type": "registry:ui",
"target": ""
}
diff --git a/public/r/styles/default/interactive-hover-button.json b/public/r/styles/default/interactive-hover-button.json
new file mode 100644
index 00000000..f3495fc6
--- /dev/null
+++ b/public/r/styles/default/interactive-hover-button.json
@@ -0,0 +1,15 @@
+{
+ "name": "interactive-hover-button",
+ "type": "registry:ui",
+ "dependencies": [
+ "lucide-react"
+ ],
+ "files": [
+ {
+ "path": "magicui/interactive-hover-button.tsx",
+ "content": "import React from \"react\";\nimport { ArrowRight } from \"lucide-react\";\n\ninterface InteractiveHoverButtonProps {\n text?: string;\n className?: string;\n}\n\nexport default function InteractiveHoverButton({\n text = \"Button\",\n className,\n}: InteractiveHoverButtonProps = {}) {\n return (\n <div\n className={`group relative w-32 cursor-pointer overflow-hidden rounded-full border bg-white p-2 text-center font-semibold text-black ${className}`}\n >\n <span className=\"inline-block translate-x-1 transition-all duration-300 group-hover:translate-x-12 group-hover:opacity-0\">\n {text}\n </span>\n <div className=\"absolute top-0 z-10 flex h-full w-full translate-x-12 items-center justify-center gap-2 text-white opacity-0 transition-all duration-300 group-hover:-translate-x-1 group-hover:opacity-100\">\n <span>{text}</span>\n <ArrowRight />\n </div>\n <div className=\"absolute left-[20%] top-[40%] h-2 w-2 scale-[1] rounded-lg bg-black transition-all duration-300 group-hover:left-[0%] group-hover:top-[0%] group-hover:h-full group-hover:w-full group-hover:scale-[1.8] group-hover:bg-[#263381] dark:group-hover:bg-[#7e85ee]\"></div>\n </div>\n );\n}\n",
+ "type": "registry:ui",
+ "target": ""
+ }
+ ]
+}
\ No newline at end of file
diff --git a/registry/default/example/interactive-hover-button-demo.tsx b/registry/default/example/interactive-hover-button-demo.tsx
new file mode 100644
index 00000000..32b9e6ef
--- /dev/null
+++ b/registry/default/example/interactive-hover-button-demo.tsx
@@ -0,0 +1,9 @@
+import InteractiveHoverButton from "@/registry/default/magicui/interactive-hover-button";
+
+export default function InteractiveHoverButtonDemo() {
+ return (
+ <div className="relative justify-center">
+ <InteractiveHoverButton />
+ </div>
+ );
+}
diff --git a/registry/default/magicui/interactive-hover-button.tsx b/registry/default/magicui/interactive-hover-button.tsx
new file mode 100644
index 00000000..9fb09aa0
--- /dev/null
+++ b/registry/default/magicui/interactive-hover-button.tsx
@@ -0,0 +1,37 @@
+import React from "react";
+import { ArrowRight } from "lucide-react";
+import { cn } from "@/lib/utils";
+
+interface InteractiveHoverButtonProps
+ extends React.ButtonHTMLAttributes<HTMLButtonElement> {
+ text?: string;
+}
+
+const InteractiveHoverButton = React.forwardRef<
+ HTMLButtonElement,
+ InteractiveHoverButtonProps
+>(({ text = "Button", className, ...props }, ref) => {
+ return (
+ <button
+ ref={ref}
+ className={cn(
+ "group relative w-32 cursor-pointer overflow-hidden rounded-full border bg-background p-2 text-center font-semibold",
+ className,
+ )}
+ {...props}
+ >
+ <span className="inline-block translate-x-1 transition-all duration-300 group-hover:translate-x-12 group-hover:opacity-0">
+ {text}
+ </span>
+ <div className="absolute top-0 z-10 flex h-full w-full translate-x-12 items-center justify-center gap-2 text-primary-foreground opacity-0 transition-all duration-300 group-hover:-translate-x-1 group-hover:opacity-100">
+ <span>{text}</span>
+ <ArrowRight />
+ </div>
+ <div className="absolute left-[20%] top-[40%] h-2 w-2 scale-[1] rounded-lg bg-primary transition-all duration-300 group-hover:left-[0%] group-hover:top-[0%] group-hover:h-full group-hover:w-full group-hover:scale-[1.8] group-hover:bg-primary"></div>
+ </button>
+ );
+});
+
+InteractiveHoverButton.displayName = "InteractiveHoverButton";
+
+export default InteractiveHoverButton;
diff --git a/registry/registry-examples.ts b/registry/registry-examples.ts
index 8b175d1c..3847f425 100644
--- a/registry/registry-examples.ts
+++ b/registry/registry-examples.ts
@@ -488,4 +488,9 @@ export const examples: Registry = [
type: "registry:example",
files: ["example/rainbow-button-demo.tsx"],
},
+ {
+ name: "interactive-hover-button-demo",
+ type: "registry:example",
+ files: ["example/interactive-hover-button-demo.tsx"],
+ },
];
diff --git a/registry/registry-ui.ts b/registry/registry-ui.ts
index 96e48178..5eacbb5e 100644
--- a/registry/registry-ui.ts
+++ b/registry/registry-ui.ts
@@ -636,4 +636,10 @@ export const ui: Registry = [
},
},
},
+ {
+ name: "interactive-hover-button",
+ type: "registry:ui",
+ dependencies: ["lucide-react"],
+ files: ["magicui/interactive-hover-button.tsx"],
+ },
];