A glowing-effect component from Aceternity UI.
From Aceternity UI
1"use client";
2
3import { memo, useCallback, useEffect, useRef } from "react";
4import { cn } from "@/lib/utils";
5import { animate } from "motion/react";
6
7interface GlowingEffectProps {
8 blur?: number;
9 inactiveZone?: number;
10 proximity?: number;
11 spread?: number;
12 variant?: "default" | "white";
13 glow?: boolean;
14 className?: string;
15 disabled?: boolean;
16 movementDuration?: number;
17 borderWidth?: number;
18}
19const GlowingEffect = memo(
20 ({
21 blur = 0,
22 inactiveZone = 0.7,
23 proximity = 0,
24 spread = 20,
25 variant = "default",
26 glow = false,
27 className,
28 movementDuration = 2,
29 borderWidth = 1,
30 disabled = true,
31 }: GlowingEffectProps) => {
32 const containerRef = useRef<HTMLDivElement>(null);
33 const lastPosition = useRef({ x: 0, y: 0 });
34 const animationFrameRef = useRef<number>(0);
35
36 const handleMove = useCallback(
37 (e?: MouseEvent | { x: number; y: number }) => {
38 if (!containerRef.current) return;
39
40 if (animationFrameRef.current) {
41 cancelAnimationFrame(animationFrameRef.current);
42 }
43
44 animationFrameRef.current = requestAnimationFrame(() => {
45 const element = containerRef.current;
46 if (!element) return;
47
48 const { left, top, width, height } = element.getBoundingClientRect();
49 const mouseX = e?.x ?? lastPosition.current.x;
50 const mouseY = e?.y ?? lastPosition.current.y;
51
52 if (e) {
53 lastPosition.current = { x: mouseX, y: mouseY };
54 }
55
56 const center = [left + width * 0.5, top + height * 0.5];
57 const distanceFromCenter = Math.hypot(
58 mouseX - center[0],
59 mouseY - center[1]
60 );
61 const inactiveRadius = 0.5 * Math.min(width, height) * inactiveZone;
62
63 if (distanceFromCenter < inactiveRadius) {
64 element.style.setProperty("--active", "0");
65 return;
66 }
67
68 const isActive =
69 mouseX > left - proximity &&
70 mouseX < left + width + proximity &&
71 mouseY > top - proximity &&
72 mouseY < top + height + proximity;
73
74 element.style.setProperty("--active", isActive ? "1" : "0");
75
76 if (!isActive) return;
77
78 const currentAngle =
79 parseFloat(element.style.getPropertyValue("--start")) || 0;
80 let targetAngle =
81 (180 * Math.atan2(mouseY - center[1], mouseX - center[0])) /
82 Math.PI +
83 90;
84
85 const angleDiff = ((targetAngle - currentAngle + 180) % 360) - 180;
86 const newAngle = currentAngle + angleDiff;
87
88 animate(currentAngle, newAngle, {
89 duration: movementDuration,
90 ease: [0.16, 1, 0.3, 1],
91 onUpdate: (value) => {
92 element.style.setProperty("--start", String(value));
93 },
94 });
95 });
96 },
97 [inactiveZone, proximity, movementDuration]
98 );
99
100 useEffect(() => {
101 if (disabled) return;
102
103 const handleScroll = () => handleMove();
104 const handlePointerMove = (e: PointerEvent) => handleMove(e);
105
106 window.addEventListener("scroll", handleScroll, { passive: true });
107 document.body.addEventListener("pointermove", handlePointerMove, {
108 passive: true,
109 });
110
111 return () => {
112 if (animationFrameRef.current) {
113 cancelAnimationFrame(animationFrameRef.current);
114 }
115 window.removeEventListener("scroll", handleScroll);
116 document.body.removeEventListener("pointermove", handlePointerMove);
117 };
118 }, [handleMove, disabled]);
119
120 return (
121 <>
122 <div
123 className={cn(
124 "pointer-events-none absolute -inset-px hidden rounded-[inherit] border opacity-0 transition-opacity",
125 glow && "opacity-100",
126 variant === "white" && "border-white",
127 disabled && "!block"
128 )}
129 />
130 <div
131 ref={containerRef}
132 style={
133 {
134 "--blur": `${blur}px`,
135 "--spread": spread,
136 "--start": "0",
137 "--active": "0",
138 "--glowingeffect-border-width": `${borderWidth}px`,
139 "--repeating-conic-gradient-times": "5",
140 "--gradient":
141 variant === "white"
142 ? `repeating-conic-gradient(
143 from 236.84deg at 50% 50%,
144 var(--black),
145 var(--black) calc(25% / var(--repeating-conic-gradient-times))
146 )`
147 : `radial-gradient(circle, #dd7bbb 10%, #dd7bbb00 20%),
148 radial-gradient(circle at 40% 40%, #d79f1e 5%, #d79f1e00 15%),
149 radial-gradient(circle at 60% 60%, #5a922c 10%, #5a922c00 20%),
150 radial-gradient(circle at 40% 60%, #4c7894 10%, #4c789400 20%),
151 repeating-conic-gradient(
152 from 236.84deg at 50% 50%,
153 #dd7bbb 0%,
154 #d79f1e calc(25% / var(--repeating-conic-gradient-times)),
155 #5a922c calc(50% / var(--repeating-conic-gradient-times)),
156 #4c7894 calc(75% / var(--repeating-conic-gradient-times)),
157 #dd7bbb calc(100% / var(--repeating-conic-gradient-times))
158 )`,
159 } as React.CSSProperties
160 }
161 className={cn(
162 "pointer-events-none absolute inset-0 rounded-[inherit] opacity-100 transition-opacity",
163 glow && "opacity-100",
164 blur > 0 && "blur-[var(--blur)] ",
165 className,
166 disabled && "!hidden"
167 )}
168 >
169 <div
170 className={cn(
171 "glow",
172 "rounded-[inherit]",
173 'after:content-[""] after:rounded-[inherit] after:absolute after:inset-[calc(-1*var(--glowingeffect-border-width))]',
174 "after:[border:var(--glowingeffect-border-width)_solid_transparent]",
175 "after:[background:var(--gradient)] after:[background-attachment:fixed]",
176 "after:opacity-[var(--active)] after:transition-opacity after:duration-300",
177 "after:[mask-clip:padding-box,border-box]",
178 "after:[mask-composite:intersect]",
179 "after:[mask-image:linear-gradient(#0000,#0000),conic-gradient(from_calc((var(--start)-var(--spread))*1deg),#00000000_0deg,#fff,#00000000_calc(var(--spread)*2deg))]"
180 )}
181 />
182 </div>
183 </>
184 );
185 }
186);
187
188GlowingEffect.displayName = "GlowingEffect";
189
190export { GlowingEffect };
191