A hover-border-gradient component from Aceternity UI.
From Aceternity UI
1"use client";
2import React, { useState, useEffect, useRef } from "react";
3
4import { motion } from "motion/react";
5import { cn } from "@/lib/utils";
6
7type Direction = "TOP" | "LEFT" | "BOTTOM" | "RIGHT";
8
9export function HoverBorderGradient({
10 children,
11 containerClassName,
12 className,
13 as: Tag = "button",
14 duration = 1,
15 clockwise = true,
16 ...props
17}: React.PropsWithChildren<
18 {
19 as?: React.ElementType;
20 containerClassName?: string;
21 className?: string;
22 duration?: number;
23 clockwise?: boolean;
24 } & React.HTMLAttributes<HTMLElement>
25>) {
26 const [hovered, setHovered] = useState<boolean>(false);
27 const [direction, setDirection] = useState<Direction>("TOP");
28
29 const rotateDirection = (currentDirection: Direction): Direction => {
30 const directions: Direction[] = ["TOP", "LEFT", "BOTTOM", "RIGHT"];
31 const currentIndex = directions.indexOf(currentDirection);
32 const nextIndex = clockwise
33 ? (currentIndex - 1 + directions.length) % directions.length
34 : (currentIndex + 1) % directions.length;
35 return directions[nextIndex];
36 };
37
38 const movingMap: Record<Direction, string> = {
39 TOP: "radial-gradient(20.7% 50% at 50% 0%, hsl(0, 0%, 100%) 0%, rgba(255, 255, 255, 0) 100%)",
40 LEFT: "radial-gradient(16.6% 43.1% at 0% 50%, hsl(0, 0%, 100%) 0%, rgba(255, 255, 255, 0) 100%)",
41 BOTTOM:
42 "radial-gradient(20.7% 50% at 50% 100%, hsl(0, 0%, 100%) 0%, rgba(255, 255, 255, 0) 100%)",
43 RIGHT:
44 "radial-gradient(16.2% 41.199999999999996% at 100% 50%, hsl(0, 0%, 100%) 0%, rgba(255, 255, 255, 0) 100%)",
45 };
46
47 const highlight =
48 "radial-gradient(75% 181.15942028985506% at 50% 50%, #3275F8 0%, rgba(255, 255, 255, 0) 100%)";
49
50 useEffect(() => {
51 if (!hovered) {
52 const interval = setInterval(() => {
53 setDirection((prevState) => rotateDirection(prevState));
54 }, duration * 1000);
55 return () => clearInterval(interval);
56 }
57 }, [hovered]);
58 return (
59 <Tag
60 onMouseEnter={(event: React.MouseEvent<HTMLDivElement>) => {
61 setHovered(true);
62 }}
63 onMouseLeave={() => setHovered(false)}
64 className={cn(
65 "relative flex rounded-full border content-center bg-black/20 hover:bg-black/10 transition duration-500 dark:bg-white/20 items-center flex-col flex-nowrap gap-10 h-min justify-center overflow-visible p-px decoration-clone w-fit",
66 containerClassName
67 )}
68 {...props}
69 >
70 <div
71 className={cn(
72 "w-auto text-white z-10 bg-black px-4 py-2 rounded-[inherit]",
73 className
74 )}
75 >
76 {children}
77 </div>
78 <motion.div
79 className={cn(
80 "flex-none inset-0 overflow-hidden absolute z-0 rounded-[inherit]"
81 )}
82 style={{
83 filter: "blur(2px)",
84 position: "absolute",
85 width: "100%",
86 height: "100%",
87 }}
88 initial={{ background: movingMap[direction] }}
89 animate={{
90 background: hovered
91 ? [movingMap[direction], highlight]
92 : movingMap[direction],
93 }}
94 transition={{ ease: "linear", duration: duration ?? 1 }}
95 />
96 <div className="bg-black absolute z-1 flex-none inset-[2px] rounded-[100px]" />
97 </Tag>
98 );
99}
100