A typewriter-effect component from Aceternity UI.
From Aceternity UI
1"use client";
2
3import { cn } from "@/lib/utils";
4import { motion, stagger, useAnimate, useInView } from "motion/react";
5import { useEffect } from "react";
6
7export const TypewriterEffect = ({
8 words,
9 className,
10 cursorClassName,
11}: {
12 words: {
13 text: string;
14 className?: string;
15 }[];
16 className?: string;
17 cursorClassName?: string;
18}) => {
19 // split text inside of words into array of characters
20 const wordsArray = words.map((word) => {
21 return {
22 ...word,
23 text: word.text.split(""),
24 };
25 });
26
27 const [scope, animate] = useAnimate();
28 const isInView = useInView(scope);
29 useEffect(() => {
30 if (isInView) {
31 animate(
32 "span",
33 {
34 display: "inline-block",
35 opacity: 1,
36 width: "fit-content",
37 },
38 {
39 duration: 0.3,
40 delay: stagger(0.1),
41 ease: "easeInOut",
42 }
43 );
44 }
45 }, [isInView]);
46
47 const renderWords = () => {
48 return (
49 <motion.div ref={scope} className="inline">
50 {wordsArray.map((word, idx) => {
51 return (
52 <div key={`word-${idx}`} className="inline-block">
53 {word.text.map((char, index) => (
54 <motion.span
55 initial={{}}
56 key={`char-${index}`}
57 className={cn(
58 `dark:text-white text-black opacity-0 hidden`,
59 word.className
60 )}
61 >
62 {char}
63 </motion.span>
64 ))}
65
66 </div>
67 );
68 })}
69 </motion.div>
70 );
71 };
72 return (
73 <div
74 className={cn(
75 "text-base sm:text-xl md:text-3xl lg:text-5xl font-bold text-center",
76 className
77 )}
78 >
79 {renderWords()}
80 <motion.span
81 initial={{
82 opacity: 0,
83 }}
84 animate={{
85 opacity: 1,
86 }}
87 transition={{
88 duration: 0.8,
89 repeat: Infinity,
90 repeatType: "reverse",
91 }}
92 className={cn(
93 "inline-block rounded-sm w-[4px] h-4 md:h-6 lg:h-10 bg-blue-500",
94 cursorClassName
95 )}
96 ></motion.span>
97 </div>
98 );
99};
100
101export const TypewriterEffectSmooth = ({
102 words,
103 className,
104 cursorClassName,
105}: {
106 words: {
107 text: string;
108 className?: string;
109 }[];
110 className?: string;
111 cursorClassName?: string;
112}) => {
113 // split text inside of words into array of characters
114 const wordsArray = words.map((word) => {
115 return {
116 ...word,
117 text: word.text.split(""),
118 };
119 });
120 const renderWords = () => {
121 return (
122 <div>
123 {wordsArray.map((word, idx) => {
124 return (
125 <div key={`word-${idx}`} className="inline-block">
126 {word.text.map((char, index) => (
127 <span
128 key={`char-${index}`}
129 className={cn(`dark:text-white text-black `, word.className)}
130 >
131 {char}
132 </span>
133 ))}
134
135 </div>
136 );
137 })}
138 </div>
139 );
140 };
141
142 return (
143 <div className={cn("flex space-x-1 my-6", className)}>
144 <motion.div
145 className="overflow-hidden pb-2"
146 initial={{
147 width: "0%",
148 }}
149 whileInView={{
150 width: "fit-content",
151 }}
152 transition={{
153 duration: 2,
154 ease: "linear",
155 delay: 1,
156 }}
157 >
158 <div
159 className="text-xs sm:text-base md:text-xl lg:text:3xl xl:text-5xl font-bold"
160 style={{
161 whiteSpace: "nowrap",
162 }}
163 >
164 {renderWords()}{" "}
165 </div>{" "}
166 </motion.div>
167 <motion.span
168 initial={{
169 opacity: 0,
170 }}
171 animate={{
172 opacity: 1,
173 }}
174 transition={{
175 duration: 0.8,
176
177 repeat: Infinity,
178 repeatType: "reverse",
179 }}
180 className={cn(
181 "block rounded-sm w-[4px] h-4 sm:h-6 xl:h-12 bg-blue-500",
182 cursorClassName
183 )}
184 ></motion.span>
185 </div>
186 );
187};
188