1"use client"
2
3import { useEffect, useState } from "react"
4import { animate, motion, useMotionValue, useTransform } from "motion/react"
5
6export interface ITypewriterProps {
7 delay: number
8 texts: string[]
9 baseText?: string
10}
11
12export function Typewriter({ delay, texts, baseText = "" }: ITypewriterProps) {
13 const [animationComplete, setAnimationComplete] = useState(false)
14 const count = useMotionValue(0)
15 const rounded = useTransform(count, (latest) => Math.round(latest))
16 const displayText = useTransform(rounded, (latest) =>
17 baseText.slice(0, latest)
18 )
19
20 useEffect(() => {
21 const controls = animate(count, baseText.length, {
22 type: "tween",
23 delay,
24 duration: 1,
25 ease: [0.42, 0, 0.58, 1] as const,
26 onComplete: () => setAnimationComplete(true),
27 })
28 return () => {
29 controls.stop && controls.stop()
30 }
31 }, [count, baseText.length, delay])
32
33 return (
34 <span>
35 <motion.span>{displayText}</motion.span>
36 {animationComplete && (
37 <RepeatedTextAnimation texts={texts} delay={delay + 1} />
38 )}
39 <BlinkingCursor />
40 </span>
41 )
42}
43
44export interface IRepeatedTextAnimationProps {
45 delay: number
46 texts: string[]
47}
48
49const defaultTexts = [
50 "quiz page with questions and answers",
51 "blog Article Details Page Layout",
52 "ecommerce dashboard with a sidebar",
53 "ui like platform.openai.com....",
54 "buttttton",
55 "aop that tracks non-standard split sleep cycles",
56 "transparent card to showcase achievements of a user",
57]
58function RepeatedTextAnimation({
59 delay,
60 texts = defaultTexts,
61}: IRepeatedTextAnimationProps) {
62 const textIndex = useMotionValue(0)
63
64 const baseText = useTransform(textIndex, (latest) => texts[latest] || "")
65 const count = useMotionValue(0)
66 const rounded = useTransform(count, (latest) => Math.round(latest))
67 const displayText = useTransform(rounded, (latest) =>
68 baseText.get().slice(0, latest)
69 )
70 const updatedThisRound = useMotionValue(true)
71
72 useEffect(() => {
73 const animation = animate(count, 60, {
74 type: "tween",
75 delay,
76 duration: 1,
77 ease: [0.42, 0, 1, 1] as const,
78 repeat: Infinity,
79 repeatType: "reverse",
80 repeatDelay: 1,
81 onUpdate(latest) {
82 if (updatedThisRound.get() && latest > 0) {
83 updatedThisRound.set(false)
84 } else if (!updatedThisRound.get() && latest === 0) {
85 textIndex.set((textIndex.get() + 1) % texts.length)
86 updatedThisRound.set(true)
87 }
88 },
89 })
90 return () => {
91 animation.stop && animation.stop()
92 }
93 }, [count, delay, textIndex, texts, updatedThisRound])
94
95 return <motion.span className="inline">{displayText}</motion.span>
96}
97
98const cursorVariants = {
99 blinking: {
100 opacity: [0, 0, 1, 1],
101 transition: {
102 duration: 1,
103 repeat: Infinity,
104 repeatDelay: 0,
105 ease: [0, 0, 1, 1] as const,
106 times: [0, 0.5, 0.5, 1],
107 },
108 },
109}
110
111function BlinkingCursor() {
112 return (
113 <motion.div
114 variants={cursorVariants}
115 animate="blinking"
116 className="inline-block h-5 w-[1px] translate-y-1 bg-neutral-900"
117 />
118 )
119}
120