Skip to documentation content

Typewriter

Type a phrase one character at a time, or type, erase, and cycle through several, with a blinking caret.

typewriter-demo

Overview

Typewriter reveals text one character at a time behind a caret. With one phrase it types once and stops, which suits a command line or a generated answer. With several, it types each one, holds it, erases it, and moves on to the next, a common way to show the range of a product in a heading.

The caret only blinks while the text is idle. A caret that blinks in the middle of typing looks like a stall.

Installation

npx honestui@latest add typewriter

Usage

import { Typewriter } from "@/components/animated/typewriter"

<h2>
  Interfaces that feel{" "}
  <Typewriter text={["calm.", "precise.", "honest."]} />
</h2>

Type a single line once, after a short delay:

<Typewriter text="npx honestui add typewriter" startDelay={600} />

Keep the line from reflowing as text is typed by giving the container room for the longest phrase, such as a min-w-[12ch] on the parent.

Accessibility

Screen readers receive every phrase once, joined with commas, from a visually hidden copy. The animated text and caret are hidden from them, so they never hear partial words. Don't put the only copy of important instructions in a cycling typewriter: sighted readers see each phrase for only a few seconds.

With reduced motion enabled, each phrase appears whole instead of being typed character by character, and the caret stops blinking.

API Reference

PropTypeDefaultDescription
text*string | string[]–One phrase, or several to type, erase, and cycle through.
asElementType"span"Element to render.
typeSpeednumber55Milliseconds per typed character.
deleteSpeednumber28Milliseconds per erased character.
pausenumber1600Milliseconds a finished phrase stays before it is erased.
startDelaynumber0Milliseconds before typing starts.
loopboolean–Cycles back to the first phrase. Defaults to true when text is an array.
startOnViewbooleanfalseWaits until the element scrolls into view.
cursorbooleantrueShows the caret.
keepCursorbooleantrueKeeps the caret after the last phrase is typed.
cursorClassNamestring–Class for the caret, such as a color or width.
onPhraseComplete(index: number) => void–Called after each phrase finishes typing.
classNamestring–Class for the root element.