# Text Roll

> Characters roll in and out with a 3D perspective flip — each letter animates independently for a mechanical, tactile reveal effect.

Docs: https://ui.cnippet.dev/motion/text-animations/text-roll

```tsx
"use client";

import { TextRoll } from "@/registry/default/motion/text-roll";

export default function TextRollHero() {
  return (
    <div className="flex min-h-50 flex-col items-center justify-center gap-3 px-6">
      <p className="font-medium text-muted-foreground text-sm uppercase tracking-widest">
        Introducing
      </p>
      <h1 className="text-center font-bold text-4xl text-foreground tracking-tight sm:text-5xl">
        <TextRoll duration={0.5} getEnterDelay={(i) => i * 0.04}>
          Design with motion
        </TextRoll>
      </h1>
    </div>
  );
}
```

## Installation

**CLI**

```bash
npx shadcn@latest add @cnippet/text-roll
```

**Manual**

1. Install the following dependencies:

```bash
npm install motion
```

2. Copy and paste the following code into your project.

`components/motion/text-roll.tsx`

```tsx
"use client";

import {
  motion,
  type Target,
  type TargetAndTransition,
  type Transition,
  useReducedMotion,
  type VariantLabels,
} from "motion/react";

export type TextRollProps = {
  children: string;
  duration?: number;
  getEnterDelay?: (index: number) => number;
  getExitDelay?: (index: number) => number;
  className?: string;
  transition?: Transition;
  variants?: {
    enter: {
      initial: Target | VariantLabels | boolean;
      animate: TargetAndTransition | VariantLabels;
    };
    exit: {
      initial: Target | VariantLabels | boolean;
      animate: TargetAndTransition | VariantLabels;
    };
  };
  onAnimationComplete?: () => void;
};

const defaultVariants = {
  enter: {
    animate: { rotateX: 90 },
    initial: { rotateX: 0 },
  },
  exit: {
    animate: { rotateX: 0 },
    initial: { rotateX: 90 },
  },
} as const;

export function TextRoll({
  children,
  duration = 0.5,
  getEnterDelay = (i) => i * 0.1,
  getExitDelay = (i) => i * 0.1 + 0.2,
  className,
  transition = { ease: "easeIn" },
  variants,
  onAnimationComplete,
}: TextRollProps) {
  const reduceMotion = useReducedMotion();
  const letters = children.split("");

  return (
    <span className={className}>
      {letters.map((letter, i) => (
        <span
          aria-hidden="true"
          className="perspective-[10000px] transform-3d relative inline-block w-auto"
          key={i}
        >
          <motion.span
            animate={variants?.enter?.animate ?? defaultVariants.enter.animate}
            className="backface-hidden absolute inline-block origin-[50%_25%]"
            initial={variants?.enter?.initial ?? defaultVariants.enter.initial}
            transition={
              reduceMotion
                ? { duration: 0 }
                : { ...transition, delay: getEnterDelay(i), duration }
            }
          >
            {letter === " " ? " " : letter}
          </motion.span>
          <motion.span
            animate={variants?.exit?.animate ?? defaultVariants.exit.animate}
            className="backface-hidden absolute inline-block origin-[50%_100%]"
            initial={variants?.exit?.initial ?? defaultVariants.exit.initial}
            onAnimationComplete={
              letters.length === i + 1 ? onAnimationComplete : undefined
            }
            transition={
              reduceMotion
                ? { duration: 0 }
                : { ...transition, delay: getExitDelay(i), duration }
            }
          >
            {letter === " " ? " " : letter}
          </motion.span>
          <span className="invisible">{letter === " " ? " " : letter}</span>
        </span>
      ))}
      <span className="sr-only">{children}</span>
    </span>
  );
}
```

3. Update the import paths to match your project setup.

## Usage

```tsx
import { TextRoll } from "@/components/motion/text-roll";
```

```tsx
<TextRoll>Design with motion</TextRoll>
```

## Anatomy

```tsx
<TextRoll>Design with motion</TextRoll> {/* <span> of letters that roll up and back */}
```

## Good to know

- `children` must be a plain string. It's split into letters.
- It plays once when it mounts. `getEnterDelay` and `getExitDelay` set each letter's delay from its index.
- `onAnimationComplete` fires when the last letter finishes.
- Screen readers read the full text once; the individual letters are hidden from them.
- When the user has turned on reduced motion, the roll finishes instantly; `onAnimationComplete` still fires.

## Props

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `children` | `string` | — | The text to animate letter by letter |
| `duration` | `number` | `0.5` | Duration of each letter's roll animation (seconds) |
| `getEnterDelay` | `(index: number) => number` | `(i) => i * 0.1` | Delay for the entering letter at position `i` |
| `getExitDelay` | `(index: number) => number` | `(i) => i * 0.1 + 0.2` | Delay for the exiting letter at position `i` |
| `transition` | `Transition` | `{ ease: "easeIn" }` | Motion transition shared by all letters |
| `variants` | `{ enter: { initial, animate }, exit: { initial, animate } }` | — | Override the enter/exit rotation variants |
| `onAnimationComplete` | `() => void` | — | Callback fired when the last letter finishes |
| `className` | `string` | — | Class applied to the wrapping `<span>` |

## Examples

### Hero Headline

Letter-by-letter roll reveal for a landing page heading. Each character flips in sequentially with a tight stagger.

```tsx
"use client";

import { TextRoll } from "@/registry/default/motion/text-roll";

export default function TextRollHero() {
  return (
    <div className="flex min-h-50 flex-col items-center justify-center gap-3 px-6">
      <p className="font-medium text-muted-foreground text-sm uppercase tracking-widest">
        Introducing
      </p>
      <h1 className="text-center font-bold text-4xl text-foreground tracking-tight sm:text-5xl">
        <TextRoll duration={0.5} getEnterDelay={(i) => i * 0.04}>
          Design with motion
        </TextRoll>
      </h1>
    </div>
  );
}
```

### Stats Row

Three `TextRoll` instances with offset `getEnterDelay` functions animate a row of key metrics simultaneously. A replay button re-mounts the animation.

```tsx
"use client";

import { useState } from "react";
import { TextRoll } from "@/registry/default/motion/text-roll";

const metrics = [
  { label: "Components", value: "164+" },
  { label: "Downloads", value: "98K" },
  { label: "Stars", value: "4.2K" },
];

export default function TextRollStats() {
  const [key, setKey] = useState(0);

  return (
    <div className="flex min-h-50 flex-col items-center justify-center gap-8 px-6">
      <div className="grid grid-cols-3 gap-10 text-center">
        {metrics.map((metric, i) => (
          <div key={metric.label}>
            <p className="font-bold text-3xl text-foreground tabular-nums sm:text-4xl">
              <TextRoll
                duration={0.4}
                getEnterDelay={(j) => i * 0.15 + j * 0.04}
                key={key}
              >
                {metric.value}
              </TextRoll>
            </p>
            <p className="mt-1 text-muted-foreground text-sm">{metric.label}</p>
          </div>
        ))}
      </div>
      <button
        className="rounded-md bg-accent px-4 py-1.5 font-medium text-foreground text-sm hover:bg-accent/80"
        onClick={() => setKey((k) => k + 1)}
        type="button"
      >
        Replay
      </button>
    </div>
  );
}
```

### Navigation Links

Staggered text-roll applied to nav links on mount. Each link starts with a small group offset so the entire nav rolls in left to right.

```tsx
"use client";

import { TextRoll } from "@/registry/default/motion/text-roll";

const links = ["Home", "About", "Work", "Blog", "Contact"];

export default function TextRollNav() {
  return (
    <div className="flex min-h-50 items-center justify-center px-6">
      <nav className="flex items-center gap-6">
        {links.map((link, i) => (
          <a
            className="group cursor-pointer font-medium text-muted-foreground text-sm hover:text-foreground"
            href="#"
            key={link}
            onClick={(e) => e.preventDefault()}
          >
            <TextRoll
              className="block"
              duration={0.35}
              getEnterDelay={(j) => i * 0.05 + j * 0.025}
              transition={{ ease: "easeOut" }}
            >
              {link}
            </TextRoll>
          </a>
        ))}
      </nav>
    </div>
  );
}
```
