# Integrate `Rift` from Motion Garden

You are helping me add the **Rift** 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:** Rift
- **Category:** Micro-interactions
- **Summary:** A draggable tear between two overlaid scenes. Push past an edge and it resists, then snaps back.
- **Reference:** https://motion-garden.netlify.app/components/rift
- **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 {
  --font-display: "Space Mono", ui-monospace, "SFMono-Regular", monospace;
}

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

:root {
  --sg-panel: #ffffff;
  --sg-hairline: #dde2e7;
  --sg-ink: #16191d;
  --sg-accent: #7e22ce;
}

.dark {
  --sg-panel: #101215;
  --sg-hairline: #2a2f36;
  --sg-ink: #e2e6e9;
  --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.

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

```tsx
"use client";

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

export interface RiftProps {
  before?: ReactNode;
  after?: ReactNode;
  className?: string;
  position?: number;
  handleWidth?: number;
  rubberBand?: number;
  angle?: number;
  snapBack?: boolean;
  glow?: number;
  accentColor?: string;
  reducedMotion?: boolean;
}

const RIFT_CSS = `
@property --sg-rift-pos { syntax: "<number>"; inherits: true; initial-value: 50; }
`;

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

function resist(raw: number, rubberBand: number): number {
  if (raw >= 0 && raw <= 100) return raw;
  const edge = raw < 0 ? 0 : 100;
  const over = raw - edge;
  const give = 18 * rubberBand;
  if (give <= 0) return edge;
  return edge + give * Math.tanh(over / give);
}

export default function Rift({
  before,
  after,
  className,
  position = 50,
  handleWidth = 2,
  rubberBand = 0.35,
  angle = 0,
  snapBack = false,
  glow = 0.4,
  accentColor = "#a855f7",
  reducedMotion = false,
}: RiftProps) {
  const rootRef = useRef<HTMLDivElement>(null);
  const [pos, setPos] = useState(position);
  const [dragging, setDragging] = useState(false);

  const fromClientX = useCallback((clientX: number) => {
    const el = rootRef.current;
    if (!el) return null;
    const rect = el.getBoundingClientRect();
    return ((clientX - rect.left) / rect.width) * 100;
  }, []);

  const onPointerDown = useCallback(
    (e: React.PointerEvent<HTMLDivElement>) => {
      const raw = fromClientX(e.clientX);
      if (raw === null) return;
      e.currentTarget.setPointerCapture(e.pointerId);
      setDragging(true);
      setPos(resist(raw, rubberBand));
    },
    [fromClientX, rubberBand],
  );

  const onPointerMove = useCallback(
    (e: React.PointerEvent<HTMLDivElement>) => {
      if (!dragging) return;
      const raw = fromClientX(e.clientX);
      if (raw === null) return;
      setPos(resist(raw, rubberBand));
    },
    [dragging, fromClientX, rubberBand],
  );

  const onPointerUp = useCallback(
    (e: React.PointerEvent<HTMLDivElement>) => {
      if (!dragging) return;
      e.currentTarget.releasePointerCapture(e.pointerId);
      setDragging(false);
      setPos((p) => (snapBack ? position : Math.min(100, Math.max(0, p))));
    },
    [dragging, snapBack, position],
  );

  const onKeyDown = useCallback((e: React.KeyboardEvent<HTMLDivElement>) => {
    const step = e.shiftKey ? 10 : 2;
    if (e.key === "ArrowLeft") {
      e.preventDefault();
      setPos((p) => Math.max(0, p - step));
    } else if (e.key === "ArrowRight") {
      e.preventDefault();
      setPos((p) => Math.min(100, p + step));
    } else if (e.key === "Home") {
      e.preventDefault();
      setPos(0);
    } else if (e.key === "End") {
      e.preventDefault();
      setPos(100);
    }
  }, []);

  const lean = angle * 0.4;
  const clip = `polygon(0% 0%, calc((var(--sg-rift-pos) + ${lean}) * 1%) 0%, calc((var(--sg-rift-pos) - ${lean}) * 1%) 100%, 0% 100%)`;

  const settle =
    dragging || reducedMotion
      ? "none"
      : `--sg-rift-pos 420ms ${EASE}`;

  return (
    <div
      ref={rootRef}
      onPointerDown={onPointerDown}
      onPointerMove={onPointerMove}
      onPointerUp={onPointerUp}
      onPointerCancel={onPointerUp}
      className={cn(
        "relative isolate touch-none select-none overflow-hidden",
        dragging ? "cursor-grabbing" : "cursor-ew-resize",
        className,
      )}
      style={
        {
          "--sg-rift-pos": pos,
          transition: settle,
        } as React.CSSProperties
      }
    >
      <style>{RIFT_CSS}</style>

      <div className="absolute inset-0 z-0">{after}</div>

      <div
        className="absolute inset-0 z-10"
        style={{ clipPath: clip, WebkitClipPath: clip }}
      >
        {before}
      </div>

      <div
        role="slider"
        tabIndex={0}
        aria-label="Reveal position"
        aria-valuemin={0}
        aria-valuemax={100}
        aria-valuenow={Math.round(Math.min(100, Math.max(0, pos)))}
        onKeyDown={onKeyDown}
        className="group absolute inset-y-0 z-20 outline-none"
        style={{
          left: "calc(var(--sg-rift-pos) * 1%)",
          width: `${Math.max(handleWidth, 2)}px`,
          transform: `translateX(-50%) rotate(${angle}deg)`,
        }}
      >
        <span
          aria-hidden
          className="absolute inset-0"
          style={{
            background: accentColor,
            boxShadow:
              glow > 0
                ? `0 0 ${(glow * 24).toFixed(0)}px ${(glow * 4).toFixed(0)}px ${accentColor}`
                : undefined,
            opacity: 0.9,
          }}
        />
        <span
          aria-hidden
          className="absolute left-1/2 top-1/2 flex size-9 -translate-x-1/2 -translate-y-1/2 items-center justify-center rounded-full border border-hairline bg-panel/90 font-display text-[11px] text-ink backdrop-blur"
        >
          ⇄
        </span>
        <span className="pointer-events-none absolute left-1/2 top-1/2 size-11 -translate-x-1/2 -translate-y-1/2 rounded-full ring-accent transition-shadow group-focus-visible:ring-2" />
      </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 |
| --- | --- | --- | --- |
| `position` | `number` | `50` | Resting position of the tear. Range 0–100 %. |
| `handleWidth` | `number` | `2` | Thickness of the divider line. Range 1–8 px. |
| `rubberBand` | `number` | `0.35` | Give when dragged past an edge, hard stop → loose. Range 0–1. |
| `angle` | `number` | `0` | Lean of the tear off vertical. Range -20–20 °. |
| `snapBack` | `boolean` | `false` | Return to the resting position on release. |
| `glow` | `number` | `0.4` | Bloom along the divider. Range 0–1. |
| `accentColor` | `string (hex)` | `"#a855f7"` | Amethyst divider. |

## Usage

```tsx
import Rift from "@/components/registry/rift/rift";

<Rift
  position={50}
  handleWidth={2}
  rubberBand={0.35}
  angle={0}
  snapBack={false}
  glow={0.4}
  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`.
- The `touch-action: none` on the hit target is load-bearing — without it a finger drag scrolls the page instead of driving the effect. Do not strip it as a stray utility class.
- `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.
