1"use client"
2
3import { memo } from "react"
4import {
5 AnimatePresence,
6 motion,
7 Variants,
8 type DOMMotionComponents,
9 type MotionProps,
10} from "motion/react"
11
12import { cn } from "@/lib/utils"
13
14type AnimationType = "text" | "word" | "character" | "line"
15type AnimationVariant =
16 | "fadeIn"
17 | "blurIn"
18 | "blurInUp"
19 | "blurInDown"
20 | "slideUp"
21 | "slideDown"
22 | "slideLeft"
23 | "slideRight"
24 | "scaleUp"
25 | "scaleDown"
26
27const motionElements = {
28 article: motion.article,
29 div: motion.div,
30 h1: motion.h1,
31 h2: motion.h2,
32 h3: motion.h3,
33 h4: motion.h4,
34 h5: motion.h5,
35 h6: motion.h6,
36 li: motion.li,
37 p: motion.p,
38 section: motion.section,
39 span: motion.span,
40} as const
41
42type MotionElementType = Extract<
43 keyof DOMMotionComponents,
44 keyof typeof motionElements
45>
46
47interface TextAnimateProps extends Omit<MotionProps, "children"> {
48 /**
49 * The text content to animate
50 */
51 children: string
52 /**
53 * The class name to be applied to the component
54 */
55 className?: string
56 /**
57 * The class name to be applied to each segment
58 */
59 segmentClassName?: string
60 /**
61 * The delay before the animation starts
62 */
63 delay?: number
64 /**
65 * The duration of the animation
66 */
67 duration?: number
68 /**
69 * Custom motion variants for the animation
70 */
71 variants?: Variants
72 /**
73 * The element type to render
74 */
75 as?: MotionElementType
76 /**
77 * How to split the text ("text", "word", "character")
78 */
79 by?: AnimationType
80 /**
81 * Whether to start animation when component enters viewport
82 */
83 startOnView?: boolean
84 /**
85 * Whether to animate only once
86 */
87 once?: boolean
88 /**
89 * The animation preset to use
90 */
91 animation?: AnimationVariant
92 /**
93 * Whether to enable accessibility features (default: true)
94 */
95 accessible?: boolean
96}
97
98const staggerTimings: Record<AnimationType, number> = {
99 text: 0.06,
100 word: 0.05,
101 character: 0.03,
102 line: 0.06,
103}
104
105const defaultContainerVariants = {
106 hidden: { opacity: 1 },
107 show: {
108 opacity: 1,
109 transition: {
110 delayChildren: 0,
111 staggerChildren: 0.05,
112 },
113 },
114 exit: {
115 opacity: 0,
116 transition: {
117 staggerChildren: 0.05,
118 staggerDirection: -1,
119 },
120 },
121}
122
123const defaultItemVariants: Variants = {
124 hidden: { opacity: 0 },
125 show: {
126 opacity: 1,
127 },
128 exit: {
129 opacity: 0,
130 },
131}
132
133const defaultItemAnimationVariants: Record<
134 AnimationVariant,
135 { container: Variants; item: Variants }
136> = {
137 fadeIn: {
138 container: defaultContainerVariants,
139 item: {
140 hidden: { opacity: 0, y: 20 },
141 show: {
142 opacity: 1,
143 y: 0,
144 transition: {
145 duration: 0.3,
146 },
147 },
148 exit: {
149 opacity: 0,
150 y: 20,
151 transition: { duration: 0.3 },
152 },
153 },
154 },
155 blurIn: {
156 container: defaultContainerVariants,
157 item: {
158 hidden: { opacity: 0, filter: "blur(10px)" },
159 show: {
160 opacity: 1,
161 filter: "blur(0px)",
162 transition: {
163 duration: 0.3,
164 },
165 },
166 exit: {
167 opacity: 0,
168 filter: "blur(10px)",
169 transition: { duration: 0.3 },
170 },
171 },
172 },
173 blurInUp: {
174 container: defaultContainerVariants,
175 item: {
176 hidden: { opacity: 0, filter: "blur(10px)", y: 20 },
177 show: {
178 opacity: 1,
179 filter: "blur(0px)",
180 y: 0,
181 transition: {
182 y: { duration: 0.3 },
183 opacity: { duration: 0.4 },
184 filter: { duration: 0.3 },
185 },
186 },
187 exit: {
188 opacity: 0,
189 filter: "blur(10px)",
190 y: 20,
191 transition: {
192 y: { duration: 0.3 },
193 opacity: { duration: 0.4 },
194 filter: { duration: 0.3 },
195 },
196 },
197 },
198 },
199 blurInDown: {
200 container: defaultContainerVariants,
201 item: {
202 hidden: { opacity: 0, filter: "blur(10px)", y: -20 },
203 show: {
204 opacity: 1,
205 filter: "blur(0px)",
206 y: 0,
207 transition: {
208 y: { duration: 0.3 },
209 opacity: { duration: 0.4 },
210 filter: { duration: 0.3 },
211 },
212 },
213 },
214 },
215 slideUp: {
216 container: defaultContainerVariants,
217 item: {
218 hidden: { y: 20, opacity: 0 },
219 show: {
220 y: 0,
221 opacity: 1,
222 transition: {
223 duration: 0.3,
224 },
225 },
226 exit: {
227 y: -20,
228 opacity: 0,
229 transition: {
230 duration: 0.3,
231 },
232 },
233 },
234 },
235 slideDown: {
236 container: defaultContainerVariants,
237 item: {
238 hidden: { y: -20, opacity: 0 },
239 show: {
240 y: 0,
241 opacity: 1,
242 transition: { duration: 0.3 },
243 },
244 exit: {
245 y: 20,
246 opacity: 0,
247 transition: { duration: 0.3 },
248 },
249 },
250 },
251 slideLeft: {
252 container: defaultContainerVariants,
253 item: {
254 hidden: { x: 20, opacity: 0 },
255 show: {
256 x: 0,
257 opacity: 1,
258 transition: { duration: 0.3 },
259 },
260 exit: {
261 x: -20,
262 opacity: 0,
263 transition: { duration: 0.3 },
264 },
265 },
266 },
267 slideRight: {
268 container: defaultContainerVariants,
269 item: {
270 hidden: { x: -20, opacity: 0 },
271 show: {
272 x: 0,
273 opacity: 1,
274 transition: { duration: 0.3 },
275 },
276 exit: {
277 x: 20,
278 opacity: 0,
279 transition: { duration: 0.3 },
280 },
281 },
282 },
283 scaleUp: {
284 container: defaultContainerVariants,
285 item: {
286 hidden: { scale: 0.5, opacity: 0 },
287 show: {
288 scale: 1,
289 opacity: 1,
290 transition: {
291 duration: 0.3,
292 scale: {
293 type: "spring",
294 damping: 15,
295 stiffness: 300,
296 },
297 },
298 },
299 exit: {
300 scale: 0.5,
301 opacity: 0,
302 transition: { duration: 0.3 },
303 },
304 },
305 },
306 scaleDown: {
307 container: defaultContainerVariants,
308 item: {
309 hidden: { scale: 1.5, opacity: 0 },
310 show: {
311 scale: 1,
312 opacity: 1,
313 transition: {
314 duration: 0.3,
315 scale: {
316 type: "spring",
317 damping: 15,
318 stiffness: 300,
319 },
320 },
321 },
322 exit: {
323 scale: 1.5,
324 opacity: 0,
325 transition: { duration: 0.3 },
326 },
327 },
328 },
329}
330
331const TextAnimateBase = ({
332 children,
333 delay = 0,
334 duration = 0.3,
335 variants,
336 className,
337 segmentClassName,
338 as: Component = "p",
339 startOnView = true,
340 once = false,
341 by = "word",
342 animation = "fadeIn",
343 accessible = true,
344 ...props
345}: TextAnimateProps) => {
346 const MotionComponent = motionElements[Component]
347
348 let segments: string[] = []
349 switch (by) {
350 case "word":
351 segments = children.split(/(\s+)/)
352 break
353 case "character":
354 segments = children.split("")
355 break
356 case "line":
357 segments = children.split("\n")
358 break
359 case "text":
360 default:
361 segments = [children]
362 break
363 }
364
365 const finalVariants = variants
366 ? {
367 container: {
368 hidden: { opacity: 0 },
369 show: {
370 opacity: 1,
371 transition: {
372 opacity: { duration: 0.01, delay },
373 delayChildren: delay,
374 staggerChildren: duration / segments.length,
375 },
376 },
377 exit: {
378 opacity: 0,
379 transition: {
380 staggerChildren: duration / segments.length,
381 staggerDirection: -1,
382 },
383 },
384 },
385 item: variants,
386 }
387 : animation
388 ? {
389 container: {
390 ...defaultItemAnimationVariants[animation].container,
391 show: {
392 ...defaultItemAnimationVariants[animation].container.show,
393 transition: {
394 delayChildren: delay,
395 staggerChildren: duration / segments.length,
396 },
397 },
398 exit: {
399 ...defaultItemAnimationVariants[animation].container.exit,
400 transition: {
401 staggerChildren: duration / segments.length,
402 staggerDirection: -1,
403 },
404 },
405 },
406 item: defaultItemAnimationVariants[animation].item,
407 }
408 : { container: defaultContainerVariants, item: defaultItemVariants }
409
410 return (
411 <AnimatePresence mode="popLayout">
412 <MotionComponent
413 variants={finalVariants.container as Variants}
414 initial="hidden"
415 whileInView={startOnView ? "show" : undefined}
416 animate={startOnView ? undefined : "show"}
417 exit="exit"
418 className={cn("whitespace-pre-wrap", className)}
419 viewport={{ once }}
420 aria-label={accessible ? children : undefined}
421 {...props}
422 >
423 {accessible && <span className="sr-only">{children}</span>}
424 {segments.map((segment, i) => (
425 <motion.span
426 key={`${by}-${segment}-${i}`}
427 variants={finalVariants.item}
428 custom={i * staggerTimings[by]}
429 className={cn(
430 by === "line" ? "block" : "inline-block whitespace-pre",
431 by === "character" && "",
432 segmentClassName
433 )}
434 aria-hidden={accessible ? true : undefined}
435 >
436 {segment}
437 </motion.span>
438 ))}
439 </MotionComponent>
440 </AnimatePresence>
441 )
442}
443
444// Export the memoized version
445export const TextAnimate = memo(TextAnimateBase)
446