|
| 1 | +import React, { useState, useEffect, useRef } from 'react'; |
| 2 | + |
| 3 | +import { motion } from 'framer-motion'; |
| 4 | +import { useCursor } from '../../Cursor'; |
| 5 | + |
| 6 | +type Direction = 'TOP' | 'LEFT' | 'BOTTOM' | 'RIGHT'; |
| 7 | + |
| 8 | +const HoverBorderGradient = ({ children }: { children: React.ReactNode }) => { |
| 9 | + const ref = useRef<HTMLDivElement>(null); |
| 10 | + const { setCursorInsets } = useCursor(); |
| 11 | + |
| 12 | + const onMouseEnter = () => { |
| 13 | + const { top, left, width, height } = |
| 14 | + ref.current?.getBoundingClientRect() || { |
| 15 | + top: 0, |
| 16 | + left: 0, |
| 17 | + width: 0, |
| 18 | + height: 0, |
| 19 | + }; |
| 20 | + setCursorInsets(undefined); |
| 21 | + setTimeout(() => { |
| 22 | + setCursorInsets({ |
| 23 | + height: 0, |
| 24 | + width: 0, |
| 25 | + top: top + height / 2, |
| 26 | + left: left + width / 2, |
| 27 | + }); |
| 28 | + }, 0); |
| 29 | + }; |
| 30 | + |
| 31 | + const onMouseLeave = () => { |
| 32 | + setCursorInsets(undefined); |
| 33 | + }; |
| 34 | + return ( |
| 35 | + <div |
| 36 | + className="m-40 flex justify-center text-center" |
| 37 | + ref={ref} |
| 38 | + onMouseEnter={onMouseEnter} |
| 39 | + onMouseLeave={onMouseLeave} |
| 40 | + > |
| 41 | + <HoverBorderGradientBase |
| 42 | + containerClassName="rounded-full" |
| 43 | + as="button" |
| 44 | + className="dark:bg-black bg-white text-black dark:text-white flex items-center space-x-2" |
| 45 | + > |
| 46 | + {children} |
| 47 | + </HoverBorderGradientBase> |
| 48 | + </div> |
| 49 | + ); |
| 50 | +}; |
| 51 | + |
| 52 | +const HoverBorderGradientBase = ({ |
| 53 | + children, |
| 54 | + containerClassName, |
| 55 | + className, |
| 56 | + as: Tag = 'button', |
| 57 | + duration = 1, |
| 58 | + clockwise = true, |
| 59 | + ...props |
| 60 | +}: React.PropsWithChildren< |
| 61 | + { |
| 62 | + as?: React.ElementType; |
| 63 | + containerClassName?: string; |
| 64 | + className?: string; |
| 65 | + duration?: number; |
| 66 | + clockwise?: boolean; |
| 67 | + } & React.HTMLAttributes<HTMLElement> |
| 68 | +>) => { |
| 69 | + const [hovered, setHovered] = useState<boolean>(false); |
| 70 | + const [direction, setDirection] = useState<Direction>('TOP'); |
| 71 | + |
| 72 | + const rotateDirection = (currentDirection: Direction): Direction => { |
| 73 | + const directions: Direction[] = ['TOP', 'LEFT', 'BOTTOM', 'RIGHT']; |
| 74 | + const currentIndex = directions.indexOf(currentDirection); |
| 75 | + const nextIndex = clockwise |
| 76 | + ? (currentIndex - 1 + directions.length) % directions.length |
| 77 | + : (currentIndex + 1) % directions.length; |
| 78 | + return directions[nextIndex]; |
| 79 | + }; |
| 80 | + |
| 81 | + const movingMap: Record<Direction, string> = { |
| 82 | + TOP: 'radial-gradient(20.7% 50% at 50% 0%, hsl(0, 0%, 100%) 0%, rgba(255, 255, 255, 0) 100%)', |
| 83 | + LEFT: 'radial-gradient(16.6% 43.1% at 0% 50%, hsl(0, 0%, 100%) 0%, rgba(255, 255, 255, 0) 100%)', |
| 84 | + BOTTOM: |
| 85 | + 'radial-gradient(20.7% 50% at 50% 100%, hsl(0, 0%, 100%) 0%, rgba(255, 255, 255, 0) 100%)', |
| 86 | + RIGHT: |
| 87 | + 'radial-gradient(16.2% 41.199999999999996% at 100% 50%, hsl(0, 0%, 100%) 0%, rgba(255, 255, 255, 0) 100%)', |
| 88 | + }; |
| 89 | + |
| 90 | + const highlight = |
| 91 | + 'radial-gradient(75% 181.15942028985506% at 50% 50%, #3275F8 0%, rgba(255, 255, 255, 0) 100%)'; |
| 92 | + |
| 93 | + useEffect(() => { |
| 94 | + if (!hovered) { |
| 95 | + const interval = setInterval(() => { |
| 96 | + setDirection((prevState) => rotateDirection(prevState)); |
| 97 | + }, duration * 1000); |
| 98 | + return () => clearInterval(interval); |
| 99 | + } |
| 100 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 101 | + }, [hovered]); |
| 102 | + return ( |
| 103 | + <Tag |
| 104 | + onMouseEnter={(event: React.MouseEvent<HTMLDivElement>) => { |
| 105 | + setHovered(true); |
| 106 | + }} |
| 107 | + onMouseLeave={() => setHovered(false)} |
| 108 | + className={ |
| 109 | + 'relative flex rounded-full border border-gray-900 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' |
| 110 | + } |
| 111 | + {...props} |
| 112 | + > |
| 113 | + <div |
| 114 | + className={ |
| 115 | + 'w-auto text-white z-10 bg-black px-4 py-2 rounded-[inherit]' |
| 116 | + } |
| 117 | + > |
| 118 | + {children} |
| 119 | + </div> |
| 120 | + <motion.div |
| 121 | + className={ |
| 122 | + 'flex-none inset-0 overflow-hidden absolute z-0 rounded-[inherit]' |
| 123 | + } |
| 124 | + style={{ |
| 125 | + filter: 'blur(2px)', |
| 126 | + position: 'absolute', |
| 127 | + width: '100%', |
| 128 | + height: '100%', |
| 129 | + }} |
| 130 | + initial={{ background: movingMap[direction] }} |
| 131 | + animate={{ |
| 132 | + background: hovered |
| 133 | + ? [movingMap[direction], highlight] |
| 134 | + : movingMap[direction], |
| 135 | + }} |
| 136 | + transition={{ ease: 'linear', duration: duration ?? 1 }} |
| 137 | + /> |
| 138 | + <div className="bg-black absolute z-1 flex-none inset-[2px] rounded-[100px]" /> |
| 139 | + </Tag> |
| 140 | + ); |
| 141 | +}; |
| 142 | + |
| 143 | +export default HoverBorderGradient; |
0 commit comments