A text-hover-effect component from Aceternity UI.
From Aceternity UI
1"use client";
2import React, { useRef, useEffect, useState } from "react";
3import { motion } from "motion/react";
4
5export const TextHoverEffect = ({
6 text,
7 duration,
8}: {
9 text: string;
10 duration?: number;
11 automatic?: boolean;
12}) => {
13 const svgRef = useRef<SVGSVGElement>(null);
14 const [cursor, setCursor] = useState({ x: 0, y: 0 });
15 const [hovered, setHovered] = useState(false);
16 const [maskPosition, setMaskPosition] = useState({ cx: "50%", cy: "50%" });
17
18 useEffect(() => {
19 if (svgRef.current && cursor.x !== null && cursor.y !== null) {
20 const svgRect = svgRef.current.getBoundingClientRect();
21 const cxPercentage = ((cursor.x - svgRect.left) / svgRect.width) * 100;
22 const cyPercentage = ((cursor.y - svgRect.top) / svgRect.height) * 100;
23 setMaskPosition({
24 cx: `${cxPercentage}%`,
25 cy: `${cyPercentage}%`,
26 });
27 }
28 }, [cursor]);
29
30 return (
31 <svg
32 ref={svgRef}
33 width="100%"
34 height="100%"
35 viewBox="0 0 300 100"
36 xmlns="http://www.w3.org/2000/svg"
37 onMouseEnter={() => setHovered(true)}
38 onMouseLeave={() => setHovered(false)}
39 onMouseMove={(e) => setCursor({ x: e.clientX, y: e.clientY })}
40 className="select-none"
41 >
42 <defs>
43 <linearGradient
44 id="textGradient"
45 gradientUnits="userSpaceOnUse"
46 cx="50%"
47 cy="50%"
48 r="25%"
49 >
50 {hovered && (
51 <>
52 <stop offset="0%" stopColor="#eab308" />
53 <stop offset="25%" stopColor="#ef4444" />
54 <stop offset="50%" stopColor="#3b82f6" />
55 <stop offset="75%" stopColor="#06b6d4" />
56 <stop offset="100%" stopColor="#8b5cf6" />
57 </>
58 )}
59 </linearGradient>
60
61 <motion.radialGradient
62 id="revealMask"
63 gradientUnits="userSpaceOnUse"
64 r="20%"
65 initial={{ cx: "50%", cy: "50%" }}
66 animate={maskPosition}
67 transition={{ duration: duration ?? 0, ease: "easeOut" }}
68
69 // example for a smoother animation below
70
71 // transition={{
72 // type: "spring",
73 // stiffness: 300,
74 // damping: 50,
75 // }}
76 >
77 <stop offset="0%" stopColor="white" />
78 <stop offset="100%" stopColor="black" />
79 </motion.radialGradient>
80 <mask id="textMask">
81 <rect
82 x="0"
83 y="0"
84 width="100%"
85 height="100%"
86 fill="url(#revealMask)"
87 />
88 </mask>
89 </defs>
90 <text
91 x="50%"
92 y="50%"
93 textAnchor="middle"
94 dominantBaseline="middle"
95 strokeWidth="0.3"
96 className="fill-transparent stroke-neutral-200 font-[helvetica] text-7xl font-bold dark:stroke-neutral-800"
97 style={{ opacity: hovered ? 0.7 : 0 }}
98 >
99 {text}
100 </text>
101 <motion.text
102 x="50%"
103 y="50%"
104 textAnchor="middle"
105 dominantBaseline="middle"
106 strokeWidth="0.3"
107 className="fill-transparent stroke-neutral-200 font-[helvetica] text-7xl font-bold dark:stroke-neutral-800"
108 initial={{ strokeDashoffset: 1000, strokeDasharray: 1000 }}
109 animate={{
110 strokeDashoffset: 0,
111 strokeDasharray: 1000,
112 }}
113 transition={{
114 duration: 4,
115 ease: "easeInOut",
116 }}
117 >
118 {text}
119 </motion.text>
120 <text
121 x="50%"
122 y="50%"
123 textAnchor="middle"
124 dominantBaseline="middle"
125 stroke="url(#textGradient)"
126 strokeWidth="0.3"
127 mask="url(#textMask)"
128 className="fill-transparent font-[helvetica] text-7xl font-bold"
129 >
130 {text}
131 </text>
132 </svg>
133 );
134};
135