# Integrate `Unfold` from Motion Garden

You are helping me add the **Unfold** 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:** Unfold
- **Category:** Micro-interactions
- **Summary:** A row of slats that open on approach while their neighbours give up the room on a spring.
- **Reference:** https://motion-garden.netlify.app/components/unfold
- **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
```

## Design tokens this source uses

Some classes in the source are Motion Garden theme tokens, not stock Tailwind (`text-ink`, `bg-panel`, `font-display` and the like). Without them, parts of the component render unstyled. Add this to my global CSS, after the Tailwind import:

```css
@theme {
  --font-display: "Space Mono", ui-monospace, "SFMono-Regular", monospace;
}

@theme inline {
  --color-panel: var(--sg-panel);
  --color-raised: var(--sg-raised);
  --color-hairline: var(--sg-hairline);
  --color-ink: var(--sg-ink);
  --color-ink-dim: var(--sg-ink-dim);
  --color-ink-mute: var(--sg-ink-mute);
  --color-accent: var(--sg-accent);
  --color-accent-muted: var(--sg-accent-muted);
}

:root {
  --sg-panel: #ffffff;
  --sg-raised: #eceaef;
  --sg-hairline: #dde2e7;
  --sg-ink: #16191d;
  --sg-ink-dim: #545b64;
  --sg-ink-mute: #878e97;
  --sg-accent: #7e22ce;
  --sg-accent-muted: #d8b4fe;
}

.dark {
  --sg-panel: #101215;
  --sg-raised: #191d21;
  --sg-hairline: #2a2f36;
  --sg-ink: #e2e6e9;
  --sg-ink-dim: #bcc2c8;
  --sg-ink-mute: #6c737c;
  --sg-accent: #a855f7;
  --sg-accent-muted: #5b3a7a;
}
```

The values are Motion Garden's own light and dark themes. If my project already has tokens for these roles, point the `--sg-*` variables at mine instead and tell me which you mapped. `.dark` assumes class-based dark mode — adapt the selector to how my project switches themes.

`font-display` expects Space Mono, and this CSS only names it. Load the font (with `next/font/google`, for example) or put my own display face first in the stack; until then it falls back to the system monospace.

## 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/unfold/unfold.tsx`

```tsx
"use client";

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

export interface UnfoldPanel {
  key: string;
  label: string;
  meta?: string;
  children?: ReactNode;
  image?: string;
  imagePosition?: string;
}

const ART_MASK =
  "linear-gradient(to top, transparent 0%, #000 26%, #000 100%)";

const ART_SCRIM = [
  "radial-gradient(115% 85% at 0% 100%, color-mix(in oklab, var(--color-panel) 95%, transparent) 0%, color-mix(in oklab, var(--color-panel) 62%, transparent) 45%, transparent 78%)",
  "linear-gradient(to top, color-mix(in oklab, var(--color-panel) 96%, transparent) 0%, color-mix(in oklab, var(--color-panel) 72%, transparent) 26%, transparent 62%)",
].join(", ");

const ART_WASH =
  "radial-gradient(105% 80% at 72% 6%, var(--color-accent) 0%, transparent 68%)";

export interface UnfoldProps {
  items?: UnfoldPanel[];
  className?: string;
  panels?: number;
  expandRatio?: number;
  stiffness?: number;
  damping?: number;
  labelReveal?: boolean;
  gap?: number;
  useImage?: boolean;
  reducedMotion?: boolean;
}

export default function Unfold({
  items = [],
  className,
  panels = 5,
  expandRatio = 3.5,
  stiffness = 260,
  damping = 30,
  labelReveal = true,
  gap = 8,
  useImage = false,
  reducedMotion = false,
}: UnfoldProps) {
  const [active, setActive] = useState<number | null>(null);
  const visible = items.slice(0, Math.max(2, panels));

  const spring = reducedMotion
    ? { duration: 0 }
    : { type: "spring" as const, stiffness, damping };

  const artSwap = reducedMotion
    ? { duration: 0 }
    : { duration: 0.42, ease: [0.22, 1, 0.36, 1] as [number, number, number, number] };

  const n = visible.length;
  const share = expandRatio + n - 1;
  const track = `(100cqw - ${(n - 1) * gap}px)`;
  const openWidth = `calc(${track} * ${(expandRatio / share).toFixed(4)})`;
  const closedWidth = `calc(${track} * ${(1 / share).toFixed(4)})`;

  return (
    <div
      className={cn("flex h-full w-full", className)}
      style={{ gap: `${gap}px`, containerType: "inline-size" }}
      onMouseLeave={() => setActive(null)}
    >
      {visible.map((panel, i) => {
        const open = active === i;
        const revealed = open && labelReveal;
        const focus = panel.imagePosition ?? "50% 35%";
        return (
          <motion.button
            key={panel.key}
            type="button"
            onMouseEnter={() => setActive(i)}
            onFocus={() => setActive(i)}
            onClick={() => setActive(open ? null : i)}
            aria-expanded={open}
            animate={{ flexGrow: open ? expandRatio : 1 }}
            initial={false}
            transition={spring}
            style={{ flexBasis: 0, contain: "layout paint" }}
            className={cn(
              "group relative min-w-0 overflow-hidden rounded-2xl border text-left outline-none transition-colors",
              open
                ? "border-accent-muted bg-raised"
                : "border-hairline bg-panel hover:bg-raised/60",
              "focus-visible:border-accent",
            )}
          >
            <AnimatePresence initial={false}>
              {useImage && panel.image ? (
                <motion.span
                  key="art"
                  initial={{ opacity: 0 }}
                  animate={{ opacity: 1 }}
                  exit={{ opacity: 0 }}
                  transition={{ duration: reducedMotion ? 0 : 0.3 }}
                  className="pointer-events-none absolute inset-0"
                >
                  <span className="absolute inset-0 opacity-72 dark:opacity-100">
                    <motion.img
                      src={panel.image}
                      alt=""
                      aria-hidden
                      draggable={false}
                      loading="lazy"
                      decoding="async"
                      style={{
                        maskImage: ART_MASK,
                        WebkitMaskImage: ART_MASK,
                        objectPosition: focus,
                        transformOrigin: focus,
                      }}
                      className="absolute inset-0 h-full w-full select-none object-cover"
                      initial={false}
                      animate={
                        open
                          ? {
                              opacity: 0.95,
                              scale: 2.7,
                              filter: "grayscale(0) contrast(1)",
                            }
                          : {
                              opacity: 0.22,
                              scale: 1.6,
                              filter: "grayscale(1) contrast(1.2)",
                            }
                      }
                      transition={artSwap}
                    />
                  </span>

                  <span
                    className="absolute inset-0"
                    style={{ backgroundImage: ART_SCRIM }}
                  />

                  <motion.span
                    className="absolute inset-0"
                    style={{
                      backgroundImage: ART_WASH,
                      mixBlendMode: "soft-light",
                    }}
                    initial={false}
                    animate={{ opacity: open ? 1 : 0 }}
                    transition={artSwap}
                  />
                </motion.span>
              ) : null}
            </AnimatePresence>

            <motion.span
              animate={{ opacity: revealed ? 0 : 1 }}
              initial={false}
              transition={
                reducedMotion
                  ? { duration: 0 }
                  : revealed
                    ? { duration: 0.12, ease: "easeOut" }
                    : { duration: 0.22, delay: 0.06, ease: "easeOut" }
              }
              style={{ width: closedWidth }}
              className="pointer-events-none absolute inset-y-0 left-0 flex items-end justify-center pb-5"
            >
              <span
                className="font-display text-[11px] uppercase tracking-[0.25em] text-ink-dim group-hover:text-ink"
                style={{ writingMode: "vertical-rl", rotate: "180deg" }}
              >
                {panel.label}
              </span>
            </motion.span>

            <motion.span
              animate={{ opacity: revealed ? 1 : 0, y: revealed ? 0 : 8 }}
              initial={false}
              transition={
                reducedMotion
                  ? { duration: 0 }
                  : revealed
                    ? {
                        opacity: { duration: 0.28, delay: 0.06 },
                        y: { duration: 0.34, delay: 0.06, ease: [0.22, 1, 0.36, 1] },
                      }
                    : {
                        opacity: { duration: 0.14, ease: "easeOut" },
                        y: { duration: 0, delay: 0.16 },
                      }
              }
              style={{
                width: openWidth,
                paddingRight:
                  useImage && panel.image ? `calc(${closedWidth} + 1.25rem)` : undefined,
              }}
              className="pointer-events-none absolute inset-y-0 left-0 flex flex-col justify-end gap-1.5 p-5"
            >
              {panel.meta && (
                <span className="font-display text-[10px] uppercase tracking-[0.2em] text-accent">
                  {panel.meta}
                </span>
              )}
              <span className="font-display text-lg tracking-tight text-ink">
                {panel.label}
              </span>
              {panel.children && (
                <span className="block text-xs leading-relaxed text-ink-dim">
                  {panel.children}
                </span>
              )}
            </motion.span>

            <span
              style={{ width: closedWidth }}
              className="absolute left-0 top-4 text-center font-display text-[10px] text-ink-mute"
            >
              {String(i + 1).padStart(2, "0")}
            </span>
          </motion.button>
        );
      })}
    </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 |
| --- | --- | --- | --- |
| `panels` | `number` | `5` | How many slats to render. Range 2–7. |
| `expandRatio` | `number` | `3.5` | How much wider the open slat is than a closed one. Range 1.5–8 ×. |
| `stiffness` | `number` | `260` | Spring stiffness — higher snaps open faster. Range 60–500. |
| `damping` | `number` | `30` | Spring damping — lower overshoots and settles. Range 8–60. |
| `labelReveal` | `boolean` | `true` | Turn the vertical rail label horizontal on open. |
| `useImage` | `boolean` | `false` | Fade each panel's cut-out artwork in behind its slat. |
| `gap` | `number` | `8` | Space between slats. Range 0–32 px. |

## Usage

```tsx
import Unfold from "@/components/registry/unfold/unfold";

<Unfold
  panels={5}
  expandRatio={3.5}
  stiffness={260}
  damping={30}
  labelReveal={true}
  useImage={false}
  gap={8}
/>
```

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`.
- `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. Add the design tokens to my global CSS, or map them to tokens I already have.
4. Wire the component into the place I point you at, with sensible props for that context.
5. 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.
