A text-reveal-card component from Aceternity UI.
From Aceternity UI
1"use client";
2import React, { useEffect, useRef, useState, memo } from "react";
3import { motion } from "motion/react";
4import { twMerge } from "tailwind-merge";
5import { cn } from "@/lib/utils";
6
7export const TextRevealCard = ({
8 text,
9 revealText,
10 children,
11 className,
12}: {
13 text: string;
14 revealText: string;
15 children?: React.ReactNode;
16 className?: string;
17}) => {
18 const [widthPercentage, setWidthPercentage] = useState(0);
19 const cardRef = useRef<HTMLDivElement | any>(null);
20 const [left, setLeft] = useState(0);
21 const [localWidth, setLocalWidth] = useState(0);
22 const [isMouseOver, setIsMouseOver] = useState(false);
23
24 useEffect(() => {
25 if (cardRef.current) {
26 const { left, width: localWidth } =
27 cardRef.current.getBoundingClientRect();
28 setLeft(left);
29 setLocalWidth(localWidth);
30 }
31 }, []);
32
33 function mouseMoveHandler(event: any) {
34 event.preventDefault();
35
36 const { clientX } = event;
37 if (cardRef.current) {
38 const relativeX = clientX - left;
39 setWidthPercentage((relativeX / localWidth) * 100);
40 }
41 }
42
43 function mouseLeaveHandler() {
44 setIsMouseOver(false);
45 setWidthPercentage(0);
46 }
47 function mouseEnterHandler() {
48 setIsMouseOver(true);
49 }
50 function touchMoveHandler(event: React.TouchEvent<HTMLDivElement>) {
51 event.preventDefault();
52 const clientX = event.touches[0]!.clientX;
53 if (cardRef.current) {
54 const relativeX = clientX - left;
55 setWidthPercentage((relativeX / localWidth) * 100);
56 }
57 }
58
59 const rotateDeg = (widthPercentage - 50) * 0.1;
60 return (
61 <div
62 onMouseEnter={mouseEnterHandler}
63 onMouseLeave={mouseLeaveHandler}
64 onMouseMove={mouseMoveHandler}
65 onTouchStart={mouseEnterHandler}
66 onTouchEnd={mouseLeaveHandler}
67 onTouchMove={touchMoveHandler}
68 ref={cardRef}
69 className={cn(
70 "bg-[#1d1c20] border border-white/[0.08] w-[40rem] rounded-lg p-8 relative overflow-hidden",
71 className
72 )}
73 >
74 {children}
75
76 <div className="h-40 relative flex items-center overflow-hidden">
77 <motion.div
78 style={{
79 width: "100%",
80 }}
81 animate={
82 isMouseOver
83 ? {
84 opacity: widthPercentage > 0 ? 1 : 0,
85 clipPath: `inset(0 ${100 - widthPercentage}% 0 0)`,
86 }
87 : {
88 clipPath: `inset(0 ${100 - widthPercentage}% 0 0)`,
89 }
90 }
91 transition={isMouseOver ? { duration: 0 } : { duration: 0.4 }}
92 className="absolute bg-[#1d1c20] z-20 will-change-transform"
93 >
94 <p
95 style={{
96 textShadow: "4px 4px 15px rgba(0,0,0,0.5)",
97 }}
98 className="text-base sm:text-[3rem] py-10 font-bold text-white bg-clip-text text-transparent bg-gradient-to-b from-white to-neutral-300"
99 >
100 {revealText}
101 </p>
102 </motion.div>
103 <motion.div
104 animate={{
105 left: `${widthPercentage}%`,
106 rotate: `${rotateDeg}deg`,
107 opacity: widthPercentage > 0 ? 1 : 0,
108 }}
109 transition={isMouseOver ? { duration: 0 } : { duration: 0.4 }}
110 className="h-40 w-[8px] bg-gradient-to-b from-transparent via-neutral-800 to-transparent absolute z-50 will-change-transform"
111 ></motion.div>
112
113 <div className=" overflow-hidden [mask-image:linear-gradient(to_bottom,transparent,white,transparent)]">
114 <p className="text-base sm:text-[3rem] py-10 font-bold bg-clip-text text-transparent bg-[#323238]">
115 {text}
116 </p>
117 <MemoizedStars />
118 </div>
119 </div>
120 </div>
121 );
122};
123
124export const TextRevealCardTitle = ({
125 children,
126 className,
127}: {
128 children: React.ReactNode;
129 className?: string;
130}) => {
131 return (
132 <h2 className={twMerge("text-white text-lg mb-2", className)}>
133 {children}
134 </h2>
135 );
136};
137
138export const TextRevealCardDescription = ({
139 children,
140 className,
141}: {
142 children: React.ReactNode;
143 className?: string;
144}) => {
145 return (
146 <p className={twMerge("text-[#a9a9a9] text-sm", className)}>{children}</p>
147 );
148};
149
150const Stars = () => {
151 const randomMove = () => Math.random() * 4 - 2;
152 const randomOpacity = () => Math.random();
153 const random = () => Math.random();
154 return (
155 <div className="absolute inset-0">
156 {[...Array(80)].map((_, i) => (
157 <motion.span
158 key={`star-${i}`}
159 animate={{
160 top: `calc(${random() * 100}% + ${randomMove()}px)`,
161 left: `calc(${random() * 100}% + ${randomMove()}px)`,
162 opacity: randomOpacity(),
163 scale: [1, 1.2, 0],
164 }}
165 transition={{
166 duration: random() * 10 + 20,
167 repeat: Infinity,
168 ease: "linear",
169 }}
170 style={{
171 position: "absolute",
172 top: `${random() * 100}%`,
173 left: `${random() * 100}%`,
174 width: `2px`,
175 height: `2px`,
176 backgroundColor: "white",
177 borderRadius: "50%",
178 zIndex: 1,
179 }}
180 className="inline-block"
181 ></motion.span>
182 ))}
183 </div>
184 );
185};
186
187export const MemoizedStars = memo(Stars);
188