1"use client"
2
3import { FC, useEffect, useRef } from "react"
4import { HTMLMotionProps, motion, useAnimation, useInView } from "motion/react"
5
6type AnimationType =
7 | "fadeIn"
8 | "fadeInUp"
9 | "popIn"
10 | "shiftInUp"
11 | "rollIn"
12 | "whipIn"
13 | "whipInUp"
14 | "calmInUp"
15
16interface Props extends HTMLMotionProps<"div"> {
17 text: string
18 type?: AnimationType
19 delay?: number
20 duration?: number
21}
22
23const animationVariants = {
24 fadeIn: {
25 container: {
26 hidden: { opacity: 0 },
27 visible: (i: number = 1) => ({
28 opacity: 1,
29 transition: { staggerChildren: 0.05, delayChildren: i * 0.3 },
30 }),
31 },
32 child: {
33 visible: {
34 opacity: 1,
35 y: 0,
36 transition: {
37 type: "spring" as const,
38 damping: 12,
39 stiffness: 100,
40 },
41 },
42 hidden: { opacity: 0, y: 10 },
43 },
44 },
45 fadeInUp: {
46 container: {
47 hidden: { opacity: 0 },
48 visible: {
49 opacity: 1,
50 transition: { staggerChildren: 0.1, delayChildren: 0.2 },
51 },
52 },
53 child: {
54 visible: { opacity: 1, y: 0, transition: { duration: 0.5 } },
55 hidden: { opacity: 0, y: 20 },
56 },
57 },
58 popIn: {
59 container: {
60 hidden: { scale: 0 },
61 visible: {
62 scale: 1,
63 transition: { staggerChildren: 0.05, delayChildren: 0.2 },
64 },
65 },
66 child: {
67 visible: {
68 opacity: 1,
69 scale: 1.1,
70 transition: { type: "spring" as const, damping: 15, stiffness: 400 },
71 },
72 hidden: { opacity: 0, scale: 0 },
73 },
74 },
75 calmInUp: {
76 container: {
77 hidden: {},
78 visible: (i: number = 1) => ({
79 transition: { staggerChildren: 0.01, delayChildren: 0.2 * i },
80 }),
81 },
82 child: {
83 hidden: {
84 y: "200%",
85 transition: {
86 ease: [0.455, 0.03, 0.515, 0.955] as const,
87 duration: 0.85,
88 },
89 },
90 visible: {
91 y: 0,
92 transition: {
93 ease: [0.125, 0.92, 0.69, 0.975] as const, // Drawing attention to dynamic content or interactive elements, where the animation needs to be engaging but not abrupt
94 duration: 0.75,
95 // ease: [0.455, 0.03, 0.515, 0.955], // smooth and gradual acceleration followed by a steady deceleration towards the end of the animation
96 // ease: [0.115, 0.955, 0.655, 0.939], // smooth and gradual acceleration followed by a steady deceleration towards the end of the animation
97 // ease: [0.09, 0.88, 0.68, 0.98], // Very Gentle Onset, Swift Mid-Section, Soft Landing
98 // ease: [0.11, 0.97, 0.64, 0.945], // Minimal Start, Energetic Acceleration, Smooth Closure
99 },
100 },
101 },
102 },
103 shiftInUp: {
104 container: {
105 hidden: {},
106 visible: (i: number = 1) => ({
107 transition: { staggerChildren: 0.01, delayChildren: 0.2 * i },
108 }),
109 },
110 child: {
111 hidden: {
112 y: "100%", // Starting from below but not too far to ensure a dramatic but manageable shift.
113 transition: {
114 ease: [0.75, 0, 0.25, 1] as const, // Starting quickly
115 duration: 0.6, // Shortened duration for a more dramatic start
116 },
117 },
118 visible: {
119 y: 0,
120 transition: {
121 duration: 0.8, // Slightly longer to accommodate the slow middle and swift end
122 ease: [0.22, 1, 0.36, 1] as const, // This easing function starts quickly (dramatic shift), slows down (slow middle), and ends quickly (clean swift end)
123 },
124 },
125 },
126 },
127
128 whipInUp: {
129 container: {
130 hidden: {},
131 visible: (i: number = 1) => ({
132 transition: { staggerChildren: 0.01, delayChildren: 0.2 * i },
133 }),
134 },
135 child: {
136 hidden: {
137 y: "200%",
138 transition: {
139 ease: [0.455, 0.03, 0.515, 0.955] as const,
140 duration: 0.45,
141 },
142 },
143 visible: {
144 y: 0,
145 transition: {
146 ease: [0.5, -0.15, 0.25, 1.05] as const,
147 duration: 0.75,
148 },
149 },
150 },
151 },
152 rollIn: {
153 container: {
154 hidden: {},
155 visible: {},
156 },
157 child: {
158 hidden: {
159 opacity: 0,
160 y: `0.25em`,
161 },
162 visible: {
163 opacity: 1,
164 y: `0em`,
165 transition: {
166 duration: 0.65,
167 ease: [0.65, 0, 0.75, 1] as const, // Great! Swift Beginning, Prolonged Ease, Quick Finish
168 // ease: [0.75, 0.05, 0.85, 1], // Quick Start, Smooth Middle, Sharp End
169 // ease: [0.7, -0.25, 0.9, 1.25], // Fast Acceleration, Gentle Slowdown, Sudden Snap
170 // ease: [0.7, -0.5, 0.85, 1.5], // Quick Leap, Soft Glide, Snappy Closure
171 },
172 },
173 },
174 },
175 whipIn: {
176 container: {
177 hidden: {},
178 visible: {},
179 },
180 child: {
181 hidden: {
182 opacity: 0,
183 y: `0.35em`,
184 },
185 visible: {
186 opacity: 1,
187 y: `0em`,
188 transition: {
189 duration: 0.45,
190 // ease: [0.75, 0.05, 0.85, 1], // Quick Start, Smooth Middle, Sharp End
191 // ease: [0.7, -0.25, 0.9, 1.25], // Fast Acceleration, Gentle Slowdown, Sudden Snap
192 // ease: [0.65, 0, 0.75, 1], // Great! Swift Beginning, Prolonged Ease, Quick Finish
193 ease: [0.85, 0.1, 0.9, 1.2] as const, // Rapid Initiation, Subtle Slow, Sharp Conclusion
194 },
195 },
196 },
197 },
198}
199
200const TextAnimate: FC<Props> = ({
201 text,
202 type = "whipInUp",
203 ...props
204}: Props) => {
205 // const { ref, inView } = useInView({
206 // threshold: 0.5,
207 // triggerOnce: true,
208 // });
209
210 const ref = useRef(null)
211 const isInView = useInView(ref, { once: true })
212
213 const letters = Array.from(text)
214 const { container, child } = animationVariants[type]
215
216 const ctrls = useAnimation()
217
218 // useEffect(() => {
219 // if (isInView) {
220 // ctrls.start("visible");
221 // }
222 // if (!isInView) {
223 // ctrls.start("hidden");
224 // }
225 // }, [ctrls, isInView]);
226
227 if (type === "rollIn" || type === "whipIn") {
228 return (
229 <h2 className="mt-10 text-3xl font-black text-black dark:text-neutral-100 py-5 pb-8 px-8 md:text-5xl">
230 {text.split(" ").map((word, index) => {
231 return (
232 <motion.span
233 ref={ref}
234 className="inline-block mr-[0.25em] whitespace-nowrap"
235 aria-hidden="true"
236 key={index}
237 initial="hidden"
238 animate="visible"
239 variants={container}
240 transition={{
241 delayChildren: index * 0.13,
242 // delayChildren: index * 0.35,
243 staggerChildren: 0.025,
244 // staggerChildren: 0.05,
245 }}
246 >
247 {word.split("").map((character, index) => {
248 return (
249 <motion.span
250 aria-hidden="true"
251 key={index}
252 variants={child}
253 className="inline-block -mr-[0.01em]"
254 >
255 {character}
256 </motion.span>
257 )
258 })}
259 </motion.span>
260 )
261 })}
262 </h2>
263 )
264 }
265
266 return (
267 <motion.h2
268 style={{ display: "flex", overflow: "hidden" }}
269 role="heading"
270 variants={container}
271 initial="hidden"
272 animate="visible"
273 className="mt-10 text-4xl font-black text-black dark:text-neutral-100 py-5 pb-8 px-8 md:text-5xl"
274 {...props}
275 >
276 {letters.map((letter, index) => (
277 <motion.span key={index} variants={child}>
278 {letter === " " ? "\u00A0" : letter}
279 </motion.span>
280 ))}
281 </motion.h2>
282 )
283}
284
285export { TextAnimate }
286export default TextAnimate
287