Docs
Hover Animation
A spotlight effect that follows your mouse cursor and highlights borders on hover.
Installation
Install dependencies
npm install framer-motion lucide-react
Run the following command
It will create a new file hover-animation.tsx
inside the components/mage-ui/card
directory.
mkdir -p components/mage-ui/card && touch components/mage-ui/card/hover-animation.tsx
Paste the code
Open the newly created file and paste the following code:
"use client";
import { motion, useMotionTemplate, useMotionValue } from "framer-motion";
import React, { useCallback, useEffect, useRef } from "react";
import { cn } from "@/lib/utils";
import { Button } from "@/components/ui/button";
import {
Card,
CardContent,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { useTheme } from "next-themes";
interface MageCardProps {
children?: React.ReactNode;
className?: string;
gradientSize?: number;
gradientColor?: string;
gradientOpacity?: number;
gradientFrom?: string;
gradientTo?: string;
}
export function MageCard({
children,
className,
gradientSize = 200,
gradientColor = "#262626",
gradientOpacity = 0.8,
gradientFrom = "#9E7AFF",
gradientTo = "#FE8BBB",
}: MageCardProps) {
const cardRef = useRef<HTMLDivElement>(null);
const mouseX = useMotionValue(-gradientSize);
const mouseY = useMotionValue(-gradientSize);
const handleMouseMove = useCallback(
(e: MouseEvent) => {
if (cardRef.current) {
const { left, top } = cardRef.current.getBoundingClientRect();
mouseX.set(e.clientX - left);
mouseY.set(e.clientY - top);
}
},
[mouseX, mouseY]
);
const handleMouseOut = useCallback(
(e: MouseEvent) => {
if (!e.relatedTarget) {
document.removeEventListener("mousemove", handleMouseMove);
mouseX.set(-gradientSize);
mouseY.set(-gradientSize);
}
},
[handleMouseMove, mouseX, gradientSize, mouseY]
);
const handleMouseEnter = useCallback(() => {
document.addEventListener("mousemove", handleMouseMove);
mouseX.set(-gradientSize);
mouseY.set(-gradientSize);
}, [handleMouseMove, mouseX, gradientSize, mouseY]);
useEffect(() => {
document.addEventListener("mousemove", handleMouseMove);
document.addEventListener("mouseout", handleMouseOut);
document.addEventListener("mouseenter", handleMouseEnter);
return () => {
document.removeEventListener("mousemove", handleMouseMove);
document.removeEventListener("mouseout", handleMouseOut);
document.removeEventListener("mouseenter", handleMouseEnter);
};
}, [handleMouseEnter, handleMouseMove, handleMouseOut]);
useEffect(() => {
mouseX.set(-gradientSize);
mouseY.set(-gradientSize);
}, [gradientSize, mouseX, mouseY]);
return (
<div ref={cardRef} className={cn("group relative rounded-[inherit]", className)}>
<motion.div
className="pointer-events-none absolute inset-0 rounded-[inherit] bg-border duration-300 group-hover:opacity-100"
style={{
background: useMotionTemplate`
radial-gradient(${gradientSize}px circle at ${mouseX}px ${mouseY}px,
${gradientFrom},
${gradientTo},
hsl(var(--border)) 100%
)
`,
}}
/>
<div className="absolute inset-px rounded-[inherit] bg-background" />
<motion.div
className="pointer-events-none absolute inset-px rounded-[inherit] opacity-0 transition-opacity duration-300 group-hover:opacity-100"
style={{
background: useMotionTemplate`
radial-gradient(${gradientSize}px circle at ${mouseX}px ${mouseY}px, ${gradientColor}, transparent 100%)
`,
opacity: gradientOpacity,
}}
/>
<div className="relative">{children}</div>
</div>
);
}
export default function MageCardPage() {
const { theme } = useTheme();
return (
<div className="flex items-center justify-center min-h-screen bg-background">
<Card>
<MageCard gradientColor={theme === "dark" ? "#262626" : "#D9D9D955"}>
<CardHeader>
<CardTitle>Login</CardTitle>
<CardDescription>
Enter your credentials to access your account
</CardDescription>
</CardHeader>
<CardContent>
<form>
<div className="grid gap-4">
<div className="grid gap-2">
<Input id="email" type="email" placeholder="name@example.com" />
</div>
<div className="grid gap-2">
<Input id="password" type="password" />
</div>
</div>
</form>
</CardContent>
<CardFooter>
<Button className="w-full">Sign In</Button>
</CardFooter>
</MageCard>
</Card>
</div>
);
}
Props
Property | Type | Default | Description |
---|---|---|---|
children | React.ReactNode | - | The content that will be magnified by the lens |
zoomFactor | number | 1.3 | The magnification factor of the lens |
lensSize | number | 170 | The size of the lens in pixels (works as a diameter) |
position | Position | - | The current position of the lens |
defaultPosition | Position | - | The initial position of the lens |
isStatic | boolean | false | Determines if the lens will remain in a fixed position |
duration | number | 0.1 | Duration of the animation when the lens moves (in seconds) |
lensColor | string | - | The color of the lens (CSS color value) |
ariaLabel | string | - | Accessibility label for the lens component |