Below is a complete React TSX component using Framer Motion to create a typewriter effect for a paragraph of text. The effect mimics a terminal response by animating the text character by character, using a monospaced font, a dark background, and a blinking cursor at the end.
Key Features:
- Typewriter Effect: Text appears one character at a time with a slight stagger.
- Terminal Styling: Monospaced font (e.g., Courier New), black background, green text, and a blinking underscore cursor.
- Framer Motion Integration: Uses variants and staggering for smooth animations.
- TypeScript Support: The component is written in TypeScript for type safety.
- Customizable: You can pass the text as a prop and adjust animation speed or styles as needed.
Installation Prerequisites:
Make sure you have Framer Motion installed:
npm install framer-motion
Or with yarn:
yarn add framer-motion
Code:
import React from 'react';
import { motion } from 'framer-motion';
interface TypewriterTextProps {
text: string; // The text to display with the typewriter effect
}
const TypewriterText: React.FC<TypewriterTextProps> = ({ text }) => {
const characters = text.split(''); // Split the text into an array of characters
// Variants for the container to handle staggering
const containerVariants = {
hidden: { opacity: 0 },
visible: {
opacity: 1,
transition: {
staggerChildren: 0.05, // Delay between each character (adjust for speed)
},
},
};
// Variants for each individual character
const characterVariants = {
hidden: { opacity: 0 }, // Start hidden
visible: { opacity: 1 }, // Fade in
};
return (
<motion.p
variants={containerVariants}
initial="hidden"
animate="visible"
style={{
fontFamily: 'Courier New, monospace', // Monospaced font for terminal look
backgroundColor: 'black', // Dark background
color: 'limegreen', // Green text like a terminal
padding: '10px',
border: '1px solid #333', // Optional border for terminal feel
whiteSpace: 'pre-wrap', // Preserve newlines and spaces
overflow: 'hidden', // Prevent overflow during animation
fontSize: '16px', // Adjust as needed
}}
>
{characters.map((char, index) => (
<motion.span key={index} variants={characterVariants}>
{char}
</motion.span>
))}
{/* Blinking cursor at the end */}
<motion.span
animate={{ opacity: [1, 0, 0, 1] }} // Cycle opacity for blinking
transition={{
duration: 0.8, // Duration of each blink cycle
repeat: Infinity, // Repeat forever
repeatType: 'loop',
}}
style={{ marginLeft: '2px' }} // Small space before cursor
>
_
</motion.span>
</motion.p>
);
};
export default TypewriterText;
How to Use the Component:
-
Import and render it in your React component:
import React from 'react';
import TypewriterText from './TypewriterText'; // Adjust the path as needed
const App: React.FC = () => {
return (
<div>
<h1>Terminal Response Example</h1>
<TypewriterText text="This is a sample terminal response. It can have multiple lines\nand will type out like a command line interface." />
</div>
);
};
export default App;
-
Explanation of the Code:
- Splitting Text: The
text prop is split into an array of characters using text.split(''). Each character is wrapped in a motion.span for individual animation.
- Container Variants: The
motion.p element uses variants to control the overall animation. The staggerChildren property ensures each character animates sequentially.
- Character Variants: Each
motion.span fades in one by one.
- Blinking Cursor: A separate
motion.span animates its opacity to create a blinking effect, simulating a terminal cursor.
- Styling: Applied directly via the
style prop for simplicity. You can extract this into CSS or a styled component if needed.
- Animation Speed: The
staggerChildren: 0.05 value controls the typing speed (in seconds per character). Lower values make it faster.
Customization Ideas:
- Speed Adjustment: Change
staggerChildren in containerVariants to make the typing faster or slower.
- Cursor Style: Modify the cursor's character (e.g., from
_ to |) or its animation in the animate prop.
- Full Terminal Simulation: Add more terminal-like features, such as a prompt (e.g., prepend
$ to the text prop).
- Responsive Design: Wrap the component in a container with fixed width for a terminal window effect.
- Error Handling: If the text is empty, you might want to add a fallback or conditional rendering.
This component should give you a solid starting point. If you need further modifications, like integrating it with state or adding more advanced features, let me know!