Animated Text
Bring text to life by adding animations to letters or words.
Welcome to the Future
import AnimatedText from "@/components/core/animated-text";
export default function Example() {
return (
<AnimatedText
text="Welcome to the Future"
className="text-4xl font-bold"
animationType="letters"
staggerDelay={0.08}
duration={0.6}
/>
);
}
Installation
Install the following dependencies:
npm install motion
Copy and paste the following code into your project:
"use client";
import { motion } from "motion/react";
interface AnimatedTextProps {
text: string;
className?: string;
animationType?: "letters" | "words";
duration?: number;
delay?: number;
staggerDelay?: number;
initialY?: number;
initialOpacity?: number;
animateY?: number;
animateOpacity?: number;
}
export default function AnimatedText({
text,
className = "text-4xl font-bold",
animationType = "letters",
duration = 0.6,
delay = 0,
staggerDelay = 0.05,
initialY = 10,
initialOpacity = 0,
animateY = 0,
animateOpacity = 1
}: AnimatedTextProps) {
const containerVariants = {
hidden: { opacity: 0 },
visible: {
opacity: 1,
transition: {
staggerChildren: staggerDelay,
delayChildren: delay
}
}
};
const itemVariants = {
hidden: {
y: initialY,
opacity: initialOpacity
},
visible: {
y: animateY,
opacity: animateOpacity,
transition: {
duration: duration,
ease: "easeOut"
}
}
};
const renderLetters = () => {
return text.split("").map((char, index) => (
<motion.span
key={`letter-${index}`}
variants={itemVariants}
className="inline-block"
style={{ whiteSpace: char === " " ? "pre" : "normal" }}>
{char}
</motion.span>
));
};
const renderWords = () => {
return text.split(" ").map((word, index) => (
<motion.span key={`word-${index}`} variants={itemVariants} className="mr-2 inline-block">
{word}
</motion.span>
));
};
return (
<motion.div
className={className}
variants={containerVariants}
initial="hidden"
animate="visible">
{animationType === "letters" ? renderLetters() : renderWords()}
</motion.div>
);
}
Update the import paths to match your project setup.
Usage
import AnimatedText from "@/components/core/animated-text";
export default function Example() {
return (
<AnimatedText
text="Welcome to the Future"
className="text-4xl font-bold"
animationType="letters"
staggerDelay={0.08}
duration={0.6}
/>
);
}
Examples
Word
WelcometotheFuture
import AnimatedText from "@/components/core/animated-text";
export default function Example() {
return (
<AnimatedText
text="Welcome to the Future"
className="text-4xl font-bold"
animationType="words"
staggerDelay={0.08}
duration={0.6}
/>
);
}
Props
Prop | Type | Default |
---|---|---|
animateOpacity? | number | 1 |
animateY? | number | 0 |
initialOpacity? | number | 0 |
initialY? | number | 10 |
staggerDelay? | number | 0.05 |
delay? | number | 0 |
duration? | number | 0.6 |
animationType? | "letters" | "words" | "letters" |
className? | string | - |
text | string | - |