# Integrate `Tilt` from Motion Garden

You are helping me add the **Tilt** 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:** Tilt
- **Category:** Micro-interactions
- **Summary:** A machined card that leans toward the pointer in 3D while a specular sheet slides across its face.
- **Reference:** https://motion-garden.netlify.app/components/tilt
- **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 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-hairline: var(--sg-hairline);
}

:root {
  --sg-panel: #ffffff;
  --sg-raised: #eceaef;
  --sg-hairline: #dde2e7;
}

.dark {
  --sg-panel: #101215;
  --sg-raised: #191d21;
  --sg-hairline: #2a2f36;
}
```

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

```tsx
"use client";

import { useCallback, useRef, type ReactNode } from "react";
import { cn } from "@/lib/utils";

export interface TiltProps {
  children?: ReactNode;
  className?: string;
  maxTilt?: number;
  perspective?: number;
  glare?: number;
  glareSize?: number;
  hoverScale?: number;
  smoothing?: number;
  glareColor?: string;
  reducedMotion?: boolean;
}

const TILT_CSS = `
@property --sg-tilt-gx { syntax: "<length>"; inherits: true; initial-value: 50%; }
@property --sg-tilt-gy { syntax: "<length>"; inherits: true; initial-value: 50%; }
`;

const TILT_EASE = "cubic-bezier(0.22, 1, 0.36, 1)";

export default function Tilt({
  children,
  className,
  maxTilt = 10,
  perspective = 900,
  glare = 0.35,
  glareSize = 0.9,
  hoverScale = 1.03,
  smoothing = 0.28,
  glareColor = "#c4b5fd",
  reducedMotion = false,
}: TiltProps) {
  const rootRef = useRef<HTMLDivElement>(null);

  const apply = useCallback(
    (clientX: number, clientY: number, active: boolean) => {
      const el = rootRef.current;
      if (!el) return;
      const rect = el.getBoundingClientRect();
      const nx = (clientX - rect.left) / rect.width - 0.5;
      const ny = (clientY - rect.top) / rect.height - 0.5;
      el.style.setProperty("--sg-tilt-rx", `${(-ny * 2 * maxTilt).toFixed(3)}deg`);
      el.style.setProperty("--sg-tilt-ry", `${(nx * 2 * maxTilt).toFixed(3)}deg`);
      el.style.setProperty("--sg-tilt-gx", `${((nx + 0.5) * 100).toFixed(2)}%`);
      el.style.setProperty("--sg-tilt-gy", `${((ny + 0.5) * 100).toFixed(2)}%`);
      el.style.setProperty("--sg-tilt-scale", active ? `${hoverScale}` : "1");
      el.style.setProperty("--sg-tilt-glare", active ? `${glare}` : "0");
    },
    [maxTilt, hoverScale, glare],
  );

  const rest = useCallback(() => {
    const el = rootRef.current;
    if (!el) return;
    el.style.setProperty("--sg-tilt-rx", "0deg");
    el.style.setProperty("--sg-tilt-ry", "0deg");
    el.style.setProperty("--sg-tilt-gx", "50%");
    el.style.setProperty("--sg-tilt-gy", "50%");
    el.style.setProperty("--sg-tilt-scale", "1");
    el.style.setProperty("--sg-tilt-glare", "0");
  }, []);

  const onPointerMove = useCallback(
    (e: React.PointerEvent<HTMLDivElement>) => {
      if (reducedMotion || e.pointerType === "touch") return;
      apply(e.clientX, e.clientY, true);
    },
    [apply, reducedMotion],
  );

  const lag = Math.min(Math.max(smoothing, 0.05), 1).toFixed(3);

  return (
    <div
      ref={rootRef}
      onPointerMove={onPointerMove}
      onPointerLeave={rest}
      onPointerCancel={rest}
      className={cn("[transform-style:preserve-3d]", className)}
      style={
        {
          perspective: `${perspective}px`,
          "--sg-tilt-rx": "0deg",
          "--sg-tilt-ry": "0deg",
          "--sg-tilt-scale": "1",
          "--sg-tilt-glare": "0",
          transition: `--sg-tilt-gx ${lag}s ${TILT_EASE}, --sg-tilt-gy ${lag}s ${TILT_EASE}`,
        } as React.CSSProperties
      }
    >
      <style>{TILT_CSS}</style>

      <div
        className="h-full rounded-[2rem] border border-hairline bg-raised/50 p-1.5 will-change-transform"
        style={{
          transform:
            "rotateX(var(--sg-tilt-rx)) rotateY(var(--sg-tilt-ry)) scale(var(--sg-tilt-scale))",
          transformStyle: "preserve-3d",
          transition: `transform ${lag}s ${TILT_EASE}`,
        }}
      >
        <div
          className="relative h-full overflow-hidden rounded-[calc(2rem-0.375rem)] bg-panel"
          style={{ boxShadow: "inset 0 1px 1px rgb(255 255 255 / 0.09)" }}
        >
          {children}

          <div
            aria-hidden
            className="pointer-events-none absolute inset-0"
            style={{
              background: `radial-gradient(circle ${Math.round(glareSize * 100)}% at var(--sg-tilt-gx) var(--sg-tilt-gy), ${glareColor}, transparent 70%)`,
              mixBlendMode: "plus-lighter",
              opacity: "var(--sg-tilt-glare)",
              transition: `opacity ${lag}s ${TILT_EASE}`,
            }}
          />
        </div>
      </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 |
| --- | --- | --- | --- |
| `maxTilt` | `number` | `10` | Maximum lean at the card's edge. Range 0–25 °. |
| `perspective` | `number` | `900` | Depth of the 3D projection — lower exaggerates it. Range 300–2000 px. |
| `glare` | `number` | `0.35` | Opacity of the specular sheet, matte → mirror. Range 0–1. |
| `glareSize` | `number` | `0.9` | Glare radius as a fraction of card width. Range 0.3–2. |
| `hoverScale` | `number` | `1.03` | Scale applied while the pointer is over the card. Range 1–1.15. |
| `smoothing` | `number` | `0.28` | How long the card takes to catch up to the pointer. Range 0.05–1 s. |
| `glareColor` | `string (hex)` | `"#c4b5fd"` | Tint of the specular sheet. |

## Usage

```tsx
import Tilt from "@/components/registry/tilt/tilt";

<Tilt
  maxTilt={10}
  perspective={900}
  glare={0.35}
  glareSize={0.9}
  hoverScale={1.03}
  smoothing={0.28}
  glareColor="#c4b5fd"
/>
```

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.
