# Integrate `Reflow` from Motion Garden

You are helping me add the **Reflow** component to an existing project.
Everything you need is below — full source, dependencies, props and the
behavioural contract. Read it all before writing anything.

- **Component:** Reflow
- **Category:** Text Animations
- **Summary:** Text whose words keep their identity when the line breaks move. Narrow the measure and the sentence travels instead of re-wrapping.
- **Reference:** https://motion-garden.netlify.app/components/reflow
- **Full catalog (for you, the agent):** https://motion-garden.netlify.app/llms.txt

## Stack this source assumes

- React 19 with the Next.js App Router (never the `pages/` directory)
- TypeScript, strict
- Tailwind CSS v4, CSS-first config (utilities are inline; no `tailwind.config` needed)

If my project differs, adapt the integration — but do not rewrite the animation math to suit a different stack. Ask me first.

## Dependencies

```bash
bun add motion clsx tailwind-merge
```

## Files to create

2 files — all of them are required. Paths are as shipped; the component may live anywhere in my project, but keep every peer file at the exact path shown so the import aliases resolve.

### 1. `components/registry/reflow/reflow.tsx`

```tsx
"use client";

import { useEffect, useMemo, useState } from "react";
import { AnimatePresence, motion } from "motion/react";
import { cn } from "@/lib/utils";

export type ReflowAlign = "left" | "center" | "right";

export interface ReflowProps {
  phrases?: string[];
  index?: number;
  columnWidth?: number;
  stiffness?: number;
  damping?: number;
  stagger?: number;
  enterScale?: number;
  wordGap?: number;
  lineHeight?: number;
  align?: ReflowAlign;
  auto?: boolean;
  interval?: number;
  traceMoves?: boolean;
  accentColor?: string;
  paused?: boolean;
  reducedMotion?: boolean;
  className?: string;
}

const STAGGER_CAP = 20;

interface WordProps {
  text: string;
  animateLayout: boolean;
  spring: { type: "spring"; stiffness: number; damping: number };
  delay: number;
  enterScale: number;
  wordGap: number;
  traceMoves: boolean;
  accentColor: string;
}

function Word({
  text,
  animateLayout,
  spring,
  delay,
  enterScale,
  wordGap,
  traceMoves,
  accentColor,
}: WordProps) {
  const [moving, setMoving] = useState(false);

  return (
    <motion.span
      layout={animateLayout ? "position" : false}
      onLayoutAnimationStart={() => traceMoves && setMoving(true)}
      onLayoutAnimationComplete={() => setMoving(false)}
      initial={{ opacity: 0, scale: enterScale }}
      animate={{ opacity: 1, scale: 1 }}
      exit={{ opacity: 0, scale: enterScale, transition: { duration: 0.15 } }}
      transition={
        animateLayout
          ? {
              layout: { ...spring, delay },
              opacity: { duration: 0.22 },
              scale: { duration: 0.26, ease: [0.22, 1, 0.36, 1] },
            }
          : { duration: 0 }
      }
      className="inline-block"
      style={{
        marginRight: `${wordGap}em`,
        color: moving && traceMoves ? accentColor : undefined,
        transition: traceMoves ? "color 260ms ease-out" : undefined,
      }}
    >
      {text}
    </motion.span>
  );
}

export default function Reflow({
  phrases = [],
  index,
  columnWidth = 100,
  stiffness = 340,
  damping = 34,
  stagger = 14,
  enterScale = 0.82,
  wordGap = 0.3,
  lineHeight = 1.6,
  align = "left",
  auto = true,
  interval = 3600,
  traceMoves = false,
  accentColor = "#a855f7",
  paused = false,
  reducedMotion = false,
  className,
}: ReflowProps) {
  const [self, setSelf] = useState(0);
  const controlled = index !== undefined;
  const current = controlled ? index : self;
  const halted = paused || reducedMotion;

  useEffect(() => {
    if (controlled || !auto || halted || phrases.length < 2) return;
    const id = setTimeout(
      () => setSelf((n) => (n + 1) % phrases.length),
      Math.max(200, interval),
    );
    return () => clearTimeout(id);
  }, [controlled, auto, halted, interval, phrases.length, current]);

  const text = phrases[current % Math.max(1, phrases.length)] ?? "";

  const words = useMemo(() => {
    const seen = new Map<string, number>();
    return text
      .split(/\s+/)
      .filter(Boolean)
      .map((word) => {
        const nth = (seen.get(word) ?? 0) + 1;
        seen.set(word, nth);
        return { key: `${word}#${nth}`, text: word };
      });
  }, [text]);

  const spring = {
    type: "spring" as const,
    stiffness,
    damping,
  };

  return (
    <div
      className={cn("w-full", className)}
      style={{
        textAlign: align,
        lineHeight,
      }}
    >
      <div
        className={cn(
          align === "center" ? "mx-auto" : align === "right" ? "ml-auto" : null,
        )}
        style={{ width: `${columnWidth}%` }}
      >
        <AnimatePresence mode="popLayout" initial={false}>
          {words.map((word, i) => (
            <Word
              key={word.key}
              text={word.text}
              animateLayout={!reducedMotion}
              spring={spring}
              delay={(Math.min(i, STAGGER_CAP) * stagger) / 1000}
              enterScale={enterScale}
              wordGap={wordGap}
              traceMoves={traceMoves}
              accentColor={accentColor}
            />
          ))}
        </AnimatePresence>
      </div>
    </div>
  );
}
```

### 2. `lib/utils.ts` — peer util, required

```tsx
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}
```


## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `columnWidth` | `number` | `100` | Width of the text column as a percentage of its container — the measure. Dragging this is the reflow, with not one word changing. Range 30–100 %. |
| `stiffness` | `number` | `340` | Spring tension on the travel. Range 40–900. |
| `damping` | `number` | `34` | Spring friction on the travel. Range 6–80. |
| `stagger` | `number` | `14` | Delay between one word starting its travel and the next. The index is capped at twenty, so a long paragraph cannot leave its tail sitting still in plain sight. Range 0–120 ms. |
| `enterScale` | `number` | `0.82` | Size a word enters at and leaves at. Range 0.2–1. |
| `wordGap` | `number` | `0.3` | Space between words. A margin rather than a space character, so a word leaving the sentence does not strand the gap that followed it. Range 0.1–0.9 em. |
| `lineHeight` | `number` | `1.6` | Leading. Range 1–2.6. |
| `align` | `"left" | "center" | "right"` | `"left"` | Text alignment. |
| `auto` | `boolean` | `true` | Advance through the phrases on a timer. |
| `interval` | `number` | `3600` | Time on each phrase. Range 800–9000 ms. Ignored while `auto` is `false`. |
| `traceMoves` | `boolean` | `false` | Tint a word for as long as it is travelling. A debugging affordance that earned its place — it is the only way to prove a word moved rather than being destroyed and re-created somewhere else. |
| `accentColor` | `string (hex)` | `"#a855f7"` | The trace tint. Ignored while `traceMoves` is `false`. |

## Usage

```tsx
import Reflow from "@/components/registry/reflow/reflow";

<Reflow
  columnWidth={100}
  stiffness={340}
  damping={34}
  stagger={14}
  enterScale={0.82}
  wordGap={0.3}
  lineHeight={1.6}
  align="left"
  auto={true}
  interval={3600}
  traceMoves={false}
  accentColor="#a855f7"
/>
```

The import path matches the file path above; change it if you move the component. Every value shown is the documented default — pass only what you actually want to change. The snippet lists the tunable props only: if the component's props type in the source requires others (content, handlers, data), pass those too.

## Integration rules

- Keep the `"use client"` directive. This is a client component and will not render as a Server Component.
- It fills its parent. Give the wrapper an explicit height (or a definite grid area) — against a height-auto parent it collapses to nothing.
- It takes a `reducedMotion` prop and quiesces when true. Wire it to a `prefers-reduced-motion` listener rather than hardcoding `false`.
- It runs a free-running animation loop. Halt it when off-screen or backgrounded if you care about battery.
- `lib/utils.ts` is required, not optional. It exports the `cn` helper the component imports from `@/lib/utils` — keep it at that path, or rewrite every `cn` import to wherever you already keep yours. If your project is shadcn-based you almost certainly have this file already; keep yours and drop mine.

## Your task

1. Tell me where in my project this should live and why.
2. Create the files above verbatim.
3. Wire the component into the place I point you at, with sensible props for that context.
4. Flag anything in my setup that conflicts with the stack assumptions above.

Do not restyle the component or alter the animation math unless I ask.
