# Typewriter

> Types, holds and deletes a list of words, each in its own colour.

- Category: Text effects
- Tier: Free (MIT)
- Page: https://bowls.dev/components/typewriter
- Requires: React 18+, Tailwind CSS 4

## Install

Copy the source below into `components/bowls/typewriter.tsx`.

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `words` | `{ text: string; color: string }[]` | — | Words to cycle through. |
| `typeSpeed` | `number` | `70` | ms per typed character. |
| `deleteSpeed` | `number` | `35` | ms per deleted character. |
| `hold` | `number` | `1300` | ms a finished word stays up. |

## Usage

```tsx
"use client";

import { Typewriter } from "@/components/bowls/typewriter";

const words = [
  { text: "landing pages", color: "#ff5c39" },
  { text: "dashboards", color: "#3b82f6" },
  { text: "side projects", color: "#16a34a" },
  { text: "portfolios", color: "#a855f7" },
];

export default function Demo() {
  return (
    <div className="flex h-full w-full items-center justify-center p-6">
      <p className="text-center text-[clamp(22px,2.6vw,30px)] leading-snug font-semibold tracking-[-0.03em]">
        Build better
        <br />
        <Typewriter words={words} />
      </p>
    </div>
  );
}
```

## Source

`components/bowls/typewriter.tsx`

```tsx
"use client";

import { useEffect, useState } from "react";

export type TypewriterProps = {
  words: { text: string; color: string }[];
  /** ms per typed character. */
  typeSpeed?: number;
  /** ms per deleted character. */
  deleteSpeed?: number;
  /** ms a finished word stays up. */
  hold?: number;
};

/** Types a word, holds it, deletes it, moves on. Each word brings its own colour. */
export function Typewriter({ words, typeSpeed = 70, deleteSpeed = 35, hold = 1300 }: TypewriterProps) {
  const [index, setIndex] = useState(0);
  const [length, setLength] = useState(0);
  const [deleting, setDeleting] = useState(false);
  const word = words[index % words.length];

  useEffect(() => {
    const full = length === word.text.length;
    const delay = deleting ? deleteSpeed : full ? hold : typeSpeed;
    const timer = setTimeout(() => {
      if (!deleting && full) setDeleting(true);
      else if (deleting && length === 0) {
        setDeleting(false);
        setIndex((i) => i + 1);
      } else setLength((l) => l + (deleting ? -1 : 1));
    }, delay);
    return () => clearTimeout(timer);
  }, [length, deleting, word, typeSpeed, deleteSpeed, hold]);

  return (
    <span className="inline-flex items-baseline" aria-live="polite">
      <span className="sr-only">{word.text}</span>
      <span
        aria-hidden="true"
        className="rounded-lg px-2 transition-colors duration-300"
        style={{ background: `color-mix(in srgb, ${word.color} 22%, transparent)`, color: word.color }}
      >
        {word.text.slice(0, length) || "​"}
      </span>
      <span aria-hidden="true" className="ml-0.5 inline-block h-[0.9em] w-[3px] animate-pulse self-center rounded-full" style={{ background: word.color }} />
    </span>
  );
}
```
