A tracing-beam component from Aceternity UI.
From Aceternity UI
1"use client";
2import React, { useEffect, useRef, useState } from "react";
3import {
4 motion,
5 useTransform,
6 useScroll,
7 useVelocity,
8 useSpring,
9} from "motion/react";
10import { cn } from "@/lib/utils";
11
12export const TracingBeam = ({
13 children,
14 className,
15}: {
16 children: React.ReactNode;
17 className?: string;
18}) => {
19 const ref = useRef<HTMLDivElement>(null);
20 const { scrollYProgress } = useScroll({
21 target: ref,
22 offset: ["start start", "end start"],
23 });
24
25 const contentRef = useRef<HTMLDivElement>(null);
26 const [svgHeight, setSvgHeight] = useState(0);
27
28 useEffect(() => {
29 if (contentRef.current) {
30 setSvgHeight(contentRef.current.offsetHeight);
31 }
32 }, []);
33
34 const y1 = useSpring(
35 useTransform(scrollYProgress, [0, 0.8], [50, svgHeight]),
36 {
37 stiffness: 500,
38 damping: 90,
39 },
40 );
41 const y2 = useSpring(
42 useTransform(scrollYProgress, [0, 1], [50, svgHeight - 200]),
43 {
44 stiffness: 500,
45 damping: 90,
46 },
47 );
48
49 return (
50 <motion.div
51 ref={ref}
52 className={cn("relative mx-auto h-full w-full max-w-4xl", className)}
53 >
54 <div className="absolute top-3 -left-4 md:-left-20">
55 <motion.div
56 transition={{
57 duration: 0.2,
58 delay: 0.5,
59 }}
60 animate={{
61 boxShadow:
62 scrollYProgress.get() > 0
63 ? "none"
64 : "rgba(0, 0, 0, 0.24) 0px 3px 8px",
65 }}
66 className="border-netural-200 ml-[27px] flex h-4 w-4 items-center justify-center rounded-full border shadow-sm"
67 >
68 <motion.div
69 transition={{
70 duration: 0.2,
71 delay: 0.5,
72 }}
73 animate={{
74 backgroundColor: scrollYProgress.get() > 0 ? "white" : "#10b981",
75 borderColor: scrollYProgress.get() > 0 ? "white" : "#059669",
76 }}
77 className="h-2 w-2 rounded-full border border-neutral-300 bg-white"
78 />
79 </motion.div>
80 <svg
81 viewBox={`0 0 20 ${svgHeight}`}
82 width="20"
83 height={svgHeight} // Set the SVG height
84 className="ml-4 block"
85 aria-hidden="true"
86 >
87 <motion.path
88 d={`M 1 0V -36 l 18 24 V ${svgHeight * 0.8} l -18 24V ${svgHeight}`}
89 fill="none"
90 stroke="#9091A0"
91 strokeOpacity="0.16"
92 transition={{
93 duration: 10,
94 }}
95 ></motion.path>
96 <motion.path
97 d={`M 1 0V -36 l 18 24 V ${svgHeight * 0.8} l -18 24V ${svgHeight}`}
98 fill="none"
99 stroke="url(#gradient)"
100 strokeWidth="1.25"
101 className="motion-reduce:hidden"
102 transition={{
103 duration: 10,
104 }}
105 ></motion.path>
106 <defs>
107 <motion.linearGradient
108 id="gradient"
109 gradientUnits="userSpaceOnUse"
110 x1="0"
111 x2="0"
112 y1={y1} // set y1 for gradient
113 y2={y2} // set y2 for gradient
114 >
115 <stop stopColor="#18CCFC" stopOpacity="0"></stop>
116 <stop stopColor="#18CCFC"></stop>
117 <stop offset="0.325" stopColor="#6344F5"></stop>
118 <stop offset="1" stopColor="#AE48FF" stopOpacity="0"></stop>
119 </motion.linearGradient>
120 </defs>
121 </svg>
122 </div>
123 <div ref={contentRef}>{children}</div>
124 </motion.div>
125 );
126};
127