# Integrate `FocusGrid` from Motion Garden

You are helping me add the **FocusGrid** 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:** FocusGrid
- **Category:** Micro-interactions
- **Summary:** A five-tile bento where any tile you pick takes the large slot and the rest slide back into order around it.
- **Reference:** https://motion-garden.netlify.app/components/focus-grid
- **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 inline {
  --color-panel: var(--sg-panel);
  --color-raised: var(--sg-raised);
  --color-accent: var(--sg-accent);
}

:root {
  --sg-panel: #ffffff;
  --sg-raised: #eceaef;
  --sg-accent: #7e22ce;
}

.dark {
  --sg-panel: #101215;
  --sg-raised: #191d21;
  --sg-accent: #a855f7;
}
```

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.

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

```tsx
"use client";

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

export interface FocusGridItem {
  key: string;
  title: string;
  description?: string;
  image: string;
  alt: string;
  imagePosition?: string;
}

type Side = "left" | "right";

export interface FocusGridProps {
  items?: FocusGridItem[];
  defaultActive?: string;
  className?: string;
  gap?: number;
  radius?: number;
  stiffness?: number;
  damping?: number;
  captions?: boolean;
  heroSide?: Side;
  reducedMotion?: boolean;
}

type Slot = readonly [number, number, number, number];
const SLOTS: readonly Slot[] = [
  [1, 3, 1, 3],
  [3, 5, 1, 2],
  [5, 6, 1, 2],
  [3, 4, 2, 3],
  [4, 6, 2, 3],
];

const mirror = ([c0, c1, r0, r1]: Slot): Slot => [7 - c1, 7 - c0, r0, r1];

const SCRIM_LIGHT =
  "linear-gradient(to top, rgba(0,0,0,0.45) 0%, rgba(0,0,0,0) 38%)";
const SCRIM_HERO =
  "linear-gradient(to top, rgba(0,0,0,0.78) 0%, rgba(0,0,0,0.35) 32%, rgba(0,0,0,0) 58%)";

const EASE_OUT: [number, number, number, number] = [0.22, 1, 0.36, 1];
const EASE_RISE: [number, number, number, number] = [0.2, 0.7, 0.1, 1];
const EASE_LIFT: [number, number, number, number] = [0.25, 0.1, 0.25, 1];

const INTRO_DELAY = 0.28;

export default function FocusGrid({
  items = [],
  defaultActive,
  className,
  gap = 8,
  radius = 16,
  stiffness = 260,
  damping = 32,
  captions = true,
  heroSide = "left",
  reducedMotion = false,
}: FocusGridProps) {
  const osReduce = useReducedMotion();
  const reduce = Boolean(osReduce) || reducedMotion;

  const tiles = items.slice(0, 5);
  const [picked, setPicked] = useState(defaultActive ?? tiles[0]?.key);
  const active = tiles.some((t) => t.key === picked) ? picked : tiles[0]?.key;

  const slotOf = new Map<string, number>();
  slotOf.set(active ?? "", 0);
  let next = 1;
  for (const t of tiles) if (t.key !== active) slotOf.set(t.key, next++);

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

  const captionIn = reduce
    ? { duration: 0 }
    : { duration: 0.4, delay: INTRO_DELAY, ease: EASE_OUT };
  const captionOut = reduce ? { duration: 0 } : { duration: 0.12 };

  return (
    <MotionConfig transition={spring}>
      <div
        className={cn("grid h-full w-full grid-cols-5 grid-rows-2", className)}
        style={{ gap: `${gap}px`, containerType: "inline-size" }}
      >
        {tiles.map((tile) => {
          const slot = slotOf.get(tile.key) ?? 0;
          const hero = slot === 0;
          const [c0, c1, r0, r1] =
            heroSide === "right" ? mirror(SLOTS[slot]) : SLOTS[slot];
          const focus = tile.imagePosition ?? "50% 40%";

          return (
            <motion.button
              key={tile.key}
              type="button"
              layout
              onClick={() => setPicked(tile.key)}
              aria-pressed={hero}
              aria-label={tile.title}
              style={{
                gridColumn: `${c0} / ${c1}`,
                gridRow: `${r0} / ${r1}`,
                borderRadius: radius,
                zIndex: hero ? 2 : 1,
              }}
              className={cn(
                "group relative min-h-0 min-w-0 overflow-hidden bg-raised text-left outline-none",
                "focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-2 focus-visible:ring-offset-panel",
                hero ? "cursor-default" : "cursor-pointer",
              )}
            >
              <span className="absolute inset-0 transition-[filter] duration-300 group-hover:brightness-110">
                <motion.img
                  layout
                  src={tile.image}
                  alt={tile.alt}
                  draggable={false}
                  decoding="async"
                  style={{ objectPosition: focus }}
                  className="absolute inset-0 h-full w-full select-none object-cover"
                />
              </span>

              <span
                className="pointer-events-none absolute inset-0"
                style={{ backgroundImage: SCRIM_LIGHT }}
              />
              <motion.span
                className="pointer-events-none absolute inset-0"
                style={{ backgroundImage: SCRIM_HERO }}
                initial={false}
                animate={{ opacity: hero ? 1 : 0 }}
                transition={hero ? captionIn : captionOut}
              />

              <AnimatePresence>
                {hero ? (
                  <motion.span
                    key="hero"
                    layout="position"
                    initial={{ opacity: 1 }}
                    animate={{ opacity: 1 }}
                    exit={{ opacity: 0, transition: captionOut }}
                    className="pointer-events-none absolute inset-x-0 bottom-0 flex flex-col gap-1.5 p-[clamp(0.75rem,2.4cqw,1.25rem)] text-white"
                  >
                    <span
                      className="font-medium leading-[1.05] tracking-tight"
                      style={{ fontSize: "clamp(1.1rem, 3.4cqw, 2rem)" }}
                    >
                      {tile.title.split(" ").map((word, i) => (
                        <span key={i}>
                          {i > 0 && " "}
                          <span className="mb-[-0.12em] inline-block overflow-hidden pb-[0.12em] align-bottom">
                            <motion.span
                              className="inline-block"
                              initial={{ y: "105%" }}
                              animate={{ y: 0 }}
                              transition={
                                reduce
                                  ? { duration: 0 }
                                  : { duration: 0.9, delay: INTRO_DELAY + i * 0.12, ease: EASE_RISE }
                              }
                            >
                              {word}
                            </motion.span>
                          </span>
                        </span>
                      ))}
                    </span>
                    {tile.description && (
                      <motion.span
                        className="max-w-[34ch] text-[clamp(0.65rem,1.3cqw,0.8rem)] leading-snug text-white/80"
                        initial={{ opacity: 0, y: "0.75rem" }}
                        animate={{ opacity: 1, y: 0 }}
                        transition={
                          reduce
                            ? { duration: 0 }
                            : { duration: 0.8, delay: INTRO_DELAY + 0.42, ease: EASE_LIFT }
                        }
                      >
                        {tile.description}
                      </motion.span>
                    )}
                  </motion.span>
                ) : captions ? (
                  <motion.span
                    key="label"
                    layout="position"
                    initial={{ opacity: 0 }}
                    animate={{ opacity: 1, transition: captionIn }}
                    exit={{ opacity: 0, transition: captionOut }}
                    className="pointer-events-none absolute bottom-0 left-0 p-[clamp(0.5rem,1.4cqw,0.75rem)] text-[clamp(0.6rem,1.1cqw,0.7rem)] font-medium text-white/90"
                  >
                    {tile.title}
                  </motion.span>
                ) : null}
              </AnimatePresence>
            </motion.button>
          );
        })}
      </div>
    </MotionConfig>
  );
}
```

### 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 |
| --- | --- | --- | --- |
| `gap` | `number` | `8` | Space between tiles. Range 0–24 px. |
| `radius` | `number` | `16` | Corner radius of each tile. Range 0–32 px. |
| `stiffness` | `number` | `260` | Spring stiffness — higher lands faster. Range 60–600. |
| `damping` | `number` | `32` | Spring damping — lower overshoots and settles. Range 8–60. |
| `captions` | `boolean` | `true` | Show the small label on the supporting tiles. |
| `heroSide` | `"left" | "right"` | `"left"` | Which edge the large slot sits against. |

## Usage

```tsx
import FocusGrid from "@/components/registry/focus-grid/focus-grid";

<FocusGrid
  gap={8}
  radius={16}
  stiffness={260}
  damping={32}
  captions={true}
  heroSide="left"
/>
```

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.
