# Text Scramble

> Glyph-shuffling reveal on mount or hover.

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

## Install

Copy the source below into `components/bowls/text-scramble.tsx`.

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `text` | `string` | — | The final text. |
| `trigger` | `"mount" \| "hover" \| "both"` | `"both"` | When to scramble. |
| `speed` | `number` | `38` | Milliseconds per frame. |
| `glyphs` | `string` | — | Characters used while scrambling. |

## Usage

```tsx
"use client";

import { TextScramble } from "@/components/bowls/text-scramble";

export default function Demo() {
  return (
    <div className="flex h-full w-full items-center justify-center">
      <TextScramble text="Serving fresh" className="cursor-default font-mono text-[clamp(22px,3vw,34px)] font-medium" />
      <span className="animate-blink font-mono text-[clamp(22px,3vw,34px)]">_</span>
    </div>
  );
}
```

## Source

`components/bowls/text-scramble.tsx`

```tsx
"use client";

import { useCallback, useEffect, useRef, useState } from "react";

export type TextScrambleProps = {
  text: string;
  /** Run once on mount, on hover, or both. */
  trigger?: "mount" | "hover" | "both";
  /** Milliseconds per frame. */
  speed?: number;
  /** Characters used while scrambling. */
  glyphs?: string;
  className?: string;
};

const DEFAULT_GLYPHS = "!<>-_\\/[]{}—=+*^?#%€$3&@";

export function TextScramble({ text, trigger = "both", speed = 38, glyphs = DEFAULT_GLYPHS, className }: TextScrambleProps) {
  const [output, setOutput] = useState(text);
  const timer = useRef<ReturnType<typeof setInterval> | null>(null);

  const run = useCallback(() => {
    if (timer.current) clearInterval(timer.current);
    let frame = 0;
    const total = text.length * 3;
    timer.current = setInterval(() => {
      frame++;
      const revealed = Math.floor(frame / 3);
      setOutput(
        text
          .split("")
          .map((ch, i) => (i < revealed || ch === " " ? ch : glyphs[Math.floor(Math.random() * glyphs.length)]))
          .join(""),
      );
      if (frame >= total && timer.current) {
        clearInterval(timer.current);
        setOutput(text);
      }
    }, speed);
  }, [text, speed, glyphs]);

  useEffect(() => {
    if (trigger !== "hover") run();
    return () => {
      if (timer.current) clearInterval(timer.current);
    };
  }, [run, trigger]);

  return (
    <span
      aria-label={text}
      onMouseEnter={trigger !== "mount" ? run : undefined}
      className={className}
    >
      <span aria-hidden="true">{output}</span>
    </span>
  );
}
```
