1"use client"
2
3import { useEffect, useId, useState, type RefObject } from "react"
4import { motion } from "motion/react"
5
6import { cn } from "@/lib/utils"
7
8export interface AnimatedBeamProps {
9 className?: string
10 containerRef: RefObject<HTMLElement | null> // Container ref
11 fromRef: RefObject<HTMLElement | null>
12 toRef: RefObject<HTMLElement | null>
13 curvature?: number
14 reverse?: boolean
15 pathColor?: string
16 pathWidth?: number
17 pathOpacity?: number
18 gradientStartColor?: string
19 gradientStopColor?: string
20 delay?: number
21 duration?: number
22 repeat?: number
23 repeatDelay?: number
24 startXOffset?: number
25 startYOffset?: number
26 endXOffset?: number
27 endYOffset?: number
28}
29
30export const AnimatedBeam: React.FC<AnimatedBeamProps> = ({
31 className,
32 containerRef,
33 fromRef,
34 toRef,
35 curvature = 0,
36 reverse = false, // Include the reverse prop
37 duration = 5,
38 delay = 0,
39 pathColor = "gray",
40 pathWidth = 2,
41 pathOpacity = 0.2,
42 gradientStartColor = "#ffaa40",
43 gradientStopColor = "#9c40ff",
44 repeat = Infinity,
45 repeatDelay = 0,
46 startXOffset = 0,
47 startYOffset = 0,
48 endXOffset = 0,
49 endYOffset = 0,
50}) => {
51 const id = useId()
52 const [pathD, setPathD] = useState("")
53 const [svgDimensions, setSvgDimensions] = useState({ width: 0, height: 0 })
54
55 // Calculate the gradient coordinates based on the reverse prop
56 const gradientCoordinates = reverse
57 ? {
58 x1: ["90%", "-10%"],
59 x2: ["100%", "0%"],
60 y1: ["0%", "0%"],
61 y2: ["0%", "0%"],
62 }
63 : {
64 x1: ["10%", "110%"],
65 x2: ["0%", "100%"],
66 y1: ["0%", "0%"],
67 y2: ["0%", "0%"],
68 }
69
70 useEffect(() => {
71 const updatePath = () => {
72 if (containerRef.current && fromRef.current && toRef.current) {
73 const containerRect = containerRef.current.getBoundingClientRect()
74 const rectA = fromRef.current.getBoundingClientRect()
75 const rectB = toRef.current.getBoundingClientRect()
76
77 const svgWidth = containerRect.width
78 const svgHeight = containerRect.height
79 setSvgDimensions({ width: svgWidth, height: svgHeight })
80
81 const startX =
82 rectA.left - containerRect.left + rectA.width / 2 + startXOffset
83 const startY =
84 rectA.top - containerRect.top + rectA.height / 2 + startYOffset
85 const endX =
86 rectB.left - containerRect.left + rectB.width / 2 + endXOffset
87 const endY =
88 rectB.top - containerRect.top + rectB.height / 2 + endYOffset
89
90 const controlY = startY - curvature
91 const d = `M ${startX},${startY} Q ${
92 (startX + endX) / 2
93 },${controlY} ${endX},${endY}`
94 setPathD(d)
95 }
96 }
97
98 // Initialize ResizeObserver
99 const resizeObserver = new ResizeObserver(() => {
100 updatePath()
101 })
102
103 // Observe the container element
104 if (containerRef.current) {
105 resizeObserver.observe(containerRef.current)
106 }
107
108 // Call the updatePath initially to set the initial path
109 updatePath()
110
111 // Clean up the observer on component unmount
112 return () => {
113 resizeObserver.disconnect()
114 }
115 }, [
116 containerRef,
117 fromRef,
118 toRef,
119 curvature,
120 startXOffset,
121 startYOffset,
122 endXOffset,
123 endYOffset,
124 ])
125
126 return (
127 <svg
128 fill="none"
129 width={svgDimensions.width}
130 height={svgDimensions.height}
131 xmlns="http://www.w3.org/2000/svg"
132 className={cn(
133 "pointer-events-none absolute top-0 left-0 transform-gpu stroke-2",
134 className
135 )}
136 viewBox={`0 0 ${svgDimensions.width} ${svgDimensions.height}`}
137 >
138 <path
139 d={pathD}
140 stroke={pathColor}
141 strokeWidth={pathWidth}
142 strokeOpacity={pathOpacity}
143 strokeLinecap="round"
144 />
145 <path
146 d={pathD}
147 strokeWidth={pathWidth}
148 stroke={`url(#${id})`}
149 strokeOpacity="1"
150 strokeLinecap="round"
151 />
152 <defs>
153 <motion.linearGradient
154 className="transform-gpu"
155 id={id}
156 gradientUnits={"userSpaceOnUse"}
157 initial={{
158 x1: "0%",
159 x2: "0%",
160 y1: "0%",
161 y2: "0%",
162 }}
163 animate={{
164 x1: gradientCoordinates.x1,
165 x2: gradientCoordinates.x2,
166 y1: gradientCoordinates.y1,
167 y2: gradientCoordinates.y2,
168 }}
169 transition={{
170 delay,
171 duration,
172 ease: [0.16, 1, 0.3, 1], // https://easings.net/#easeOutExpo
173 repeat,
174 repeatDelay,
175 }}
176 >
177 <stop stopColor={gradientStartColor} stopOpacity="0"></stop>
178 <stop stopColor={gradientStartColor}></stop>
179 <stop offset="32.5%" stopColor={gradientStopColor}></stop>
180 <stop
181 offset="100%"
182 stopColor={gradientStopColor}
183 stopOpacity="0"
184 ></stop>
185 </motion.linearGradient>
186 </defs>
187 </svg>
188 )
189}
190