# Integrate `ParallaxRail` from Motion Garden

You are helping me add the **ParallaxRail** 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:** ParallaxRail
- **Category:** Power-User Systems
- **Summary:** A pinned section where scrolling down drives content sideways across depth planes moving at different rates.
- **Reference:** https://motion-garden.netlify.app/components/parallax-rail
- **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/parallax-rail/parallax-rail.tsx`

```tsx
"use client";

import { useEffect, useRef, useState, type ReactNode, type RefObject } from "react";
import { motion, useScroll, useSpring, useTransform } from "motion/react";
import { cn } from "@/lib/utils";

export interface ParallaxRailProps {
  layers?: ReactNode[];
  className?: string;
  planes?: number;
  travel?: number;
  depthSpread?: number;
  damping?: number;
  fadeEdges?: number;
  railGap?: number;
  pinHeight?: string;
  scrollContainerRef?: RefObject<HTMLElement | null>;
  reducedMotion?: boolean;
}

export default function ParallaxRail({
  layers = [],
  className,
  planes = 3,
  travel = 1,
  depthSpread = 0.45,
  damping = 30,
  fadeEdges = 0.12,
  railGap = 24,
  pinHeight,
  scrollContainerRef,
  reducedMotion = false,
}: ParallaxRailProps) {
  const trackRef = useRef<HTMLDivElement>(null);
  const portRef = useRef<HTMLDivElement>(null);
  const railRef = useRef<HTMLDivElement>(null);

  const [overflow, setOverflow] = useState(0);
  const [measuredPin, setMeasuredPin] = useState<number | null>(null);

  useEffect(() => {
    const port = portRef.current;
    const rail = railRef.current;
    const scroller = scrollContainerRef?.current ?? null;
    if (!port || !rail) return;

    const measure = () => {
      setOverflow(Math.max(0, rail.scrollWidth - port.clientWidth));
      setMeasuredPin(scroller ? scroller.clientHeight : null);
    };

    const observer = new ResizeObserver(measure);
    observer.observe(port);
    observer.observe(rail);
    if (scroller) observer.observe(scroller);
    measure();
    return () => observer.disconnect();
  }, [scrollContainerRef, planes]);

  const { scrollYProgress } = useScroll({
    target: trackRef,
    ...(scrollContainerRef ? { container: scrollContainerRef } : {}),
    offset: ["start start", "end end"],
  });

  const smooth = useSpring(scrollYProgress, {
    damping,
    stiffness: 220,
    restDelta: 0.0005,
  });

  const visible = layers.slice(0, Math.max(1, planes));
  const depth = Math.max(1, visible.length - 1);

  const mask = `linear-gradient(to right, transparent, #000 ${(fadeEdges * 100).toFixed(1)}%, #000 ${(100 - fadeEdges * 100).toFixed(1)}%, transparent)`;

  return (
    <div ref={trackRef} className={cn("relative", className)}>
      <div
        ref={portRef}
        className="sticky top-0 flex flex-col justify-center overflow-hidden"
        style={{
          height: measuredPin ? `${measuredPin}px` : (pinHeight ?? "100svh"),
          gap: `${railGap}px`,
          ...(fadeEdges > 0
            ? { maskImage: mask, WebkitMaskImage: mask }
            : {}),
        }}
      >
        {visible.map((layer, i) => (
          <Plane
            key={i}
            progress={smooth}
            factor={1 - depthSpread * ((visible.length - 1 - i) / depth)}
            overflow={overflow}
            travel={travel}
            still={reducedMotion}
            innerRef={i === visible.length - 1 ? railRef : undefined}
          >
            {layer}
          </Plane>
        ))}
      </div>
    </div>
  );
}

function Plane({
  children,
  progress,
  factor,
  overflow,
  travel,
  still,
  innerRef,
}: {
  children: ReactNode;
  progress: ReturnType<typeof useSpring>;
  factor: number;
  overflow: number;
  travel: number;
  still: boolean;
  innerRef?: RefObject<HTMLDivElement | null>;
}) {
  const x = useTransform(progress, [0, 1], [0, -overflow * travel * factor]);

  return (
    <motion.div
      ref={innerRef}
      className="flex w-max shrink-0 items-center will-change-transform"
      style={still ? undefined : { x }}
    >
      {children}
    </motion.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 |
| --- | --- | --- | --- |
| `planes` | `number` | `3` | How many depth planes to render. Range 1–3. |
| `travel` | `number` | `1` | Multiplier on the front rail's travel across the pin. Range 0.3–2.5 ×. |
| `depthSpread` | `number` | `0.45` | How much slower the backmost plane moves than the front. Range 0–1. |
| `damping` | `number` | `30` | Spring damping on the scrub — higher tracks scroll exactly. Range 10–90. |
| `fadeEdges` | `number` | `0.12` | Width of the soft mask at each rail edge. Range 0–0.4. |
| `railGap` | `number` | `24` | Vertical gap between planes. Range 8–64 px. |

## Usage

```tsx
import ParallaxRail from "@/components/registry/parallax-rail/parallax-rail";

<ParallaxRail
  planes={3}
  travel={1}
  depthSpread={0.45}
  damping={30}
  fadeEdges={0.12}
  railGap={24}
/>
```

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. 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.
