# Typewriter

> Types a phrase, pauses, backspaces and types the next, with a human rhythm and a caret that blinks only at rest.

Category: Text · Free · Updated 2026-10-20 · Live demo: https://www.motionkitui.com/components/typewriter

## When to use it

Hero lines that cycle through audiences or use cases, chat-style product demos and terminal-themed pages.

## How it works

A timer types one character at a time, waits, deletes and moves on to the next phrase. In human mode, each delay varies between half and one and a half times the average, with a beat after spaces and punctuation. The caret holds steady while typing and blinks only at rest. Screen readers get each phrase once.

## Installation

```bash
npx shadcn@latest add https://www.motionkitui.com/r/typewriter.json
```

Or copy `components/motionkit/typewriter.tsx` below. Requires react, motion and Tailwind CSS v4. Free for personal projects; commercial use needs MotionKit Plus (https://www.motionkitui.com/pricing).

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `words` | `string[]` |  | Phrases to type, in order. |
| `typeSpeed` | `number` | `70` | Average milliseconds per typed character. |
| `deleteSpeed` | `number` | `35` | Milliseconds per deleted character. |
| `pause` | `number` | `1600` | Milliseconds a finished phrase stays. |
| `loop` | `boolean` | `true` | Start again after the last phrase. |
| `humanize` | `boolean` | `true` | Vary the rhythm like a person typing. |
| `caret` | `boolean` | `true` | Show the caret. |

## Accessibility

- **Keyboard: Not applicable.** Text only, nothing to focus.
- **Screen readers: Pass.** Screen readers get every phrase once, never a character at a time.
- **Reduced motion: Pass.** With reduced motion on, the first phrase shows in full and nothing types.
- **Colour contrast: Not applicable.** Uses whatever text colour you pass in className.

## Source

```tsx
"use client";

import { useEffect, useRef, useState, type ElementType } from "react";
import { motion, useInView, useReducedMotion } from "motion/react";

type TypewriterProps = {
  /** Phrases to type, in order. */
  words: string[];
  /** Milliseconds per typed character, on average. */
  typeSpeed?: number;
  /** Milliseconds per deleted character. */
  deleteSpeed?: number;
  /** Milliseconds a finished phrase stays before it is deleted. */
  pause?: number;
  /** Go back to the first phrase after the last; otherwise stop on the last. */
  loop?: boolean;
  /** Vary the rhythm the way a person types: quicker runs, a beat after punctuation. */
  humanize?: boolean;
  caret?: boolean;
  /** Element to render, e.g. "span". */
  as?: ElementType;
  className?: string;
};

export function Typewriter({
  words,
  typeSpeed = 70,
  deleteSpeed = 35,
  pause = 1600,
  loop = true,
  humanize = true,
  caret = true,
  as: Tag = "span",
  className = "",
}: TypewriterProps) {
  const ref = useRef<HTMLElement>(null);
  const inView = useInView(ref, { once: true });
  const reduceMotion = useReducedMotion();
  const [state, setState] = useState({ word: 0, length: 0, deleting: false });
  const phrase = words[state.word % words.length] ?? "";

  useEffect(() => {
    if (!inView || reduceMotion || !words.length) return;
    const typed = state.length === phrase.length;
    const isLast = state.word === words.length - 1;
    if (typed && !state.deleting && isLast && !loop) return;

    const previous = phrase[state.length - 1];
    let delay: number;
    if (state.deleting) delay = deleteSpeed;
    else if (typed) delay = pause;
    else if (!humanize) delay = typeSpeed;
    // between half and one-and-a-half the average, with a short beat after punctuation and spaces
    else delay = typeSpeed * (0.5 + Math.random()) + (previous && /[.,!?]/.test(previous) ? typeSpeed * 4 : previous === " " ? typeSpeed * 0.6 : 0);

    const timer = window.setTimeout(() => {
      setState((current) => {
        if (current.deleting)
          return current.length === 0
            ? { word: (current.word + 1) % words.length, length: 0, deleting: false }
            : { ...current, length: current.length - 1 };
        if (current.length === phrase.length) return { ...current, deleting: true };
        return { ...current, length: current.length + 1 };
      });
    }, delay);
    return () => window.clearTimeout(timer);
  }, [inView, reduceMotion, state, phrase, words.length, typeSpeed, deleteSpeed, pause, loop, humanize]);

  // reduced motion shows the first phrase whole, with no typing
  const shown = reduceMotion ? (words[0] ?? "") : phrase.slice(0, state.length);

  return (
    <Tag ref={ref} className={className}>
      {/* screen readers get every phrase once, never a character at a time */}
      <span className="sr-only">{words.join(", ")}</span>
      <span aria-hidden>
        {shown}
        {caret && (
          <motion.span
            className="ml-[0.05em] inline-block w-[0.08em] translate-y-[0.1em] bg-current"
            style={{ height: "1em" }}
            // the caret holds steady while typing and only blinks when the typist rests
            animate={reduceMotion || (!state.deleting && state.length < phrase.length) ? { opacity: 1 } : { opacity: [1, 1, 0, 0] }}
            transition={{ duration: 1, repeat: Infinity, times: [0, 0.5, 0.5, 1] }}
          />
        )}
      </span>
    </Tag>
  );
}
```
