Typing Effect

This component displays text with a typing animation effect.

Basic Text Scramble

Loading...

Usage Guide

Install the clsx and tailwind-merge packages:

npm install framer-motion

Copy and paste the following component source.

"use client";

import React, { useState, useEffect } from "react";
import { motion } from "framer-motion";

interface TypingEffectProps {
    children: string;
    typingSpeed?: number;
    className?: string;
}

export default function TypingEffect({
    children,
    typingSpeed = 100,
    className = ""
}: TypingEffectProps) {
    const [displayedText, setDisplayedText] = useState("");

    const text = children;

    useEffect(() => {
        let currentIndex = 0;
        const intervalId = setInterval(() => {
            if (currentIndex <= text.length) {
                setDisplayedText(text.slice(0, currentIndex));
                currentIndex++;
            } else {
                clearInterval(intervalId);
            }
        }, typingSpeed);

        return () => clearInterval(intervalId);
    }, [text, typingSpeed]);

    return (
        <div className={`font-mono ${className}`}>
            {displayedText}
            <motion.span
                animate={{ opacity: [0, 1] }}
                transition={{ duration: 0.5, repeat: Infinity, repeatType: "reverse" }}
            >
                |
            </motion.span>
        </div>
    );
}

Props

PropTypeDescriptionDefault
childrenstringThe text to display initially."Hello, World!"
classNamestringAdditional class names for styling.""
typingSpeednumberSpeed at which typing happens (ms).100