Typing Animation
Installation
Copy and paste the following code into your project.
components/magicui/typing-animation.tsx
"use client";
import { cn } from "@/lib/utils";
import { useEffect, useState } from "react";
interface TypingAnimationProps {
text: string;
duration?: number;
className?: string;
}
export default function TypingAnimation({
text,
duration = 200,
className,
}: TypingAnimationProps) {
const [displayedText, setDisplayedText] = useState<string>("");
const [i, setI] = useState<number>(0);
useEffect(() => {
const typingEffect = setInterval(() => {
if (i < text.length) {
setDisplayedText((prevState) => prevState + text.charAt(i));
setI(i + 1);
} else {
clearInterval(typingEffect);
}
}, duration);
return () => {
clearInterval(typingEffect);
};
}, [duration, i]);
return (
<h1
className={cn(
"font-display text-center text-4xl font-bold leading-[5rem] tracking-[-0.02em] drop-shadow-sm",
className,
)}
>
{displayedText ? displayedText : text}
</h1>
);
}
Props
Prop | Type | Description | Default |
---|---|---|---|
className | string | The class name to be applied to the component | |
duration | number | Duration to wait in between each char type. | 200 |
text | string | Text to animate | "" |