Smart Typewriter Component

Smart Typewriter Component

A React typewriter that cycles through phrases with realistic variable typing speed, natural backspace pauses, and a CSS cursor blink that stops while typing.

Helpful?

Case trace

3 creative leaps

Step 1 — Core typing hookClaudeSonnet 4.6

Write a React hook useTypewriter(phrases, options) that types each phrase, pauses, backspaces it, then loops. Add ±30% jitter so it does not feel robotic.

tsx
export function useTypewriter(phrases, options = {}) {
  const { typeSpeed = 80, deleteSpeed = 45,
          pauseAfterType = 1800, pauseAfterDelete = 400 } = options;
  const [displayText, setDisplayText] = useState("");
  const [isTyping, setIsTyping] = useState(true);
  const [isDeleting, setIsDeleting] = useState(false);
  const phraseIndex = useRef(0);
  const charIndex = useRef(0);
  const timeout = useRef();

  const jitter = (b) => b * (0.7 + Math.random() * 0.6);

  useEffect(() => {
    const tick = () => {
      const phrase = phrases[phraseIndex.current];
      if (!isDeleting) {
        if (charIndex.current < phrase.length) {
          charIndex.current++;
          setDisplayText(phrase.slice(0, charIndex.current));
          timeout.current = setTimeout(tick, jitter(typeSpeed));
        } else {
          setIsTyping(false);
          timeout.current = setTimeout(() => setIsDeleting(true), pauseAfterType);
        }
      } else {
        if (charIndex.current > 0) {
          charIndex.current--;
          setDisplayText(phrase.slice(0, charIndex.current));
          timeout.current = setTimeout(tick, jitter(deleteSpeed));
        } else {
          setIsDeleting(false); setIsTyping(true);
          phraseIndex.current = (phraseIndex.current + 1) % phrases.length;
          timeout.current = setTimeout(tick, pauseAfterDelete);
        }
      }
    };
    timeout.current = setTimeout(tick, typeSpeed);
    return () => clearTimeout(timeout.current);
  }, [phrases, isDeleting]);

  return { displayText, isTyping, isDeleting };
}
Step 2 — Cursor blink logicClaudeSonnet 4.6

Add a CSS cursor element that blinks when idle but stays solid while text is being typed or deleted.

css
.typewriter-cursor {
  display: inline-block;
  width: 2px;
  height: 1.1em;
  background: currentColor;
  margin-left: 2px;
  vertical-align: text-bottom;
  border-radius: 1px;
}

.typewriter-cursor.blinking {
  animation: cursor-blink 1s step-end infinite;
}

@keyframes cursor-blink {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0; }
}
Step 3 — Final componentClaudeSonnet 4.6

Compose a <Typewriter> using the hook and cursor CSS. Cursor should blink only when not typing or deleting.

Published 3w ago

Category

Code

Tools used

Claude

Details

Tech stack

React
LanguageJavaScript

Tags

typewriteranimationreacttext

More from Jabid Hasan

Dust on the Windowsill
100% helpful

Dust on the Windowsill

A nostalgic ballad about a forgotten childhood home, with a soaring chorus and a wistful piano melody, evoking a sense of longing and warmth…

AIMusicGen.AINostalgic Ballad
The Mind of the Wild

The Mind of the Wild

A cinematic dolly-in shot through a three dimensional network of glowing electric purple code structures. Shimmering violet data streams pul…

Leonardo AIFlow (Google)720p
The Gilded Aegis
100% helpful

The Gilded Aegis

A stunning, high-contrast macro shot of a fragile human silhouette crafted from deep obsidian blown-glass. The figure is encased in a jagged…

Leonardo AI

View similar

Animated Envelope

Animated Envelope

Create the HTML structure for an animated envelope component. Use nested divs for the envelope parts: back-fold, letter, top-fold (flap), bo…

CursorHTMLCSS
Gravity Ink

Gravity Ink

Build a fully playable Gravity Painter game as a single React component (.jsx) using Matter.js for physics.Tech StackReact with hooks (useSt…

CursorReactTailwind
TypeBreaker

TypeBreaker

Build a fully playable Brick Breaker game in a single HTML/CSS/JS file with the following features:Pre-Game ScreenDisplay a centered input f…

v0
Liquid Magnetic Button

Liquid Magnetic Button

I want to build a button component in React that behaves like a magnet — when the cursor is within 100px of the button, the button smoothly …

ClaudeReactTailwind
Animated Gradient Mesh Hero

Animated Gradient Mesh Hero

Create a full-viewport div with an animated CSS mesh gradient background. Use 4 radial gradients in purple, cyan, rose, and amber. The gradi…

v0ClaudeReact
3D Bohr Atom Model — CSS Animation

3D Bohr Atom Model — CSS Animation

Requirements:A central glowing nucleus positioned in the middle.Three elliptical or circular orbits arranged in 3D space using CSS transform…

Claude