# Integrate `MaskBlur` from Motion Garden

You are helping me add the **MaskBlur** 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:** MaskBlur
- **Category:** Backgrounds
- **Summary:** Progressive edge blur. Stacked backdrop-filter layers dissolve content toward a chosen edge.
- **Reference:** https://motion-garden.netlify.app/components/mask-blur
- **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-accent: var(--sg-accent);
}

:root {
  --sg-accent: #7e22ce;
}

.dark {
  --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/mask-blur/mask-blur.tsx`

```tsx
"use client";

import {
  useEffect,
  useMemo,
  useRef,
  useState,
  type CSSProperties,
  type ReactNode,
} from "react";
import { useReducedMotion } from "motion/react";
import { cn } from "@/lib/utils";

type Position = "top" | "bottom" | "left" | "right";
type Curve = "linear" | "bezier" | "ease-in" | "ease-out" | "ease-in-out";
type Target = "parent" | "page";

export interface MaskBlurProps {
  position?: Position;
  strength?: number;
  height?: string;
  width?: string;
  divCount?: number;
  exponential?: boolean;
  curve?: Curve;
  opacity?: number;
  target?: Target;
  tinted?: boolean;
  animated?: boolean | "scroll";
  duration?: string;
  easing?: string;
  hoverIntensity?: number;
  zIndex?: number;
  className?: string;
  style?: CSSProperties;
  children?: ReactNode;
}

const BASE_BLUR_REM = 0.0625;

const CURVES: Record<Curve, (p: number) => number> = {
  linear: (p) => p,
  bezier: (p) => p * p * (3 - 2 * p),
  "ease-in": (p) => p * p,
  "ease-out": (p) => 1 - (1 - p) ** 2,
  "ease-in-out": (p) => (p < 0.5 ? 2 * p * p : 1 - (-2 * p + 2) ** 2 / 2),
};

const GRADIENT_TO: Record<Position, string> = {
  top: "to top",
  bottom: "to bottom",
  left: "to left",
  right: "to right",
};

const round1 = (n: number) => Math.round(n * 10) / 10;

function layerBlur(
  i: number,
  count: number,
  curve: Curve,
  exponential: boolean,
  strength: number,
): number {
  const t = CURVES[curve](i / count);
  const ramp = exponential ? 2 ** (t * 4) : t * count + 1;
  return BASE_BLUR_REM * ramp * strength;
}

function layerMask(i: number, count: number, to: string): string {
  const span = 100 / count;
  const a = round1(span * (i - 1));
  const b = round1(span * i);
  const c = round1(span * (i + 1));
  const d = round1(span * (i + 2));
  let stops = `transparent ${a}%, black ${b}%`;
  if (c <= 100) stops += `, black ${c}%`;
  if (d <= 100) stops += `, transparent ${d}%`;
  return `linear-gradient(${to}, ${stops})`;
}

function edgeStyle(
  position: Position,
  vertical: boolean,
  height: string,
  width: string | undefined,
): CSSProperties {
  const size: CSSProperties = vertical
    ? { height, width: width ?? "100%" }
    : { width: width ?? height, height: "100%" };
  switch (position) {
    case "top":
      return { ...size, top: 0, left: 0, right: 0 };
    case "bottom":
      return { ...size, bottom: 0, left: 0, right: 0 };
    case "left":
      return { ...size, left: 0, top: 0, bottom: 0 };
    case "right":
      return { ...size, right: 0, top: 0, bottom: 0 };
  }
}

export default function MaskBlur({
  position = "bottom",
  strength = 1,
  height = "6rem",
  width,
  divCount = 5,
  exponential = true,
  curve = "bezier",
  opacity = 1,
  target = "parent",
  tinted = false,
  animated = false,
  duration = "0.3s",
  easing = "ease-out",
  hoverIntensity,
  zIndex = 10,
  className,
  style,
  children,
}: MaskBlurProps) {
  const reduce = useReducedMotion() ?? false;
  const ref = useRef<HTMLDivElement>(null);
  const [hovered, setHovered] = useState(false);
  const [revealed, setRevealed] = useState(false);

  useEffect(() => {
    if (animated === false) return;
    if (animated === "scroll") {
      const el = ref.current;
      if (!el) return;
      const io = new IntersectionObserver(
        ([entry]) => setRevealed(entry.isIntersecting),
        { threshold: 0.1 },
      );
      io.observe(el);
      return () => io.disconnect();
    }
    setRevealed(true);
  }, [animated]);

  const vertical = position === "top" || position === "bottom";
  const to = GRADIENT_TO[position];
  const activeStrength =
    hovered && hoverIntensity ? strength * hoverIntensity : strength;

  const layers = useMemo(() => {
    const willTransition = animated !== false && !reduce;
    const styles: CSSProperties[] = [];
    for (let i = 1; i <= divCount; i++) {
      const blur = layerBlur(i, divCount, curve, exponential, activeStrength);
      const mask = layerMask(i, divCount, to);
      styles.push({
        position: "absolute",
        inset: 0,
        maskImage: mask,
        WebkitMaskImage: mask,
        backdropFilter: `blur(${blur.toFixed(3)}rem)`,
        WebkitBackdropFilter: `blur(${blur.toFixed(3)}rem)`,
        opacity,
        ...(tinted
          ? {
              backgroundImage: `linear-gradient(${to}, transparent, color-mix(in oklab, var(--color-accent) 12%, transparent))`,
            }
          : null),
        ...(willTransition
          ? { transition: `backdrop-filter ${duration} ${easing}` }
          : null),
      });
    }
    return styles;
  }, [
    divCount,
    curve,
    exponential,
    activeStrength,
    to,
    opacity,
    tinted,
    animated,
    reduce,
    duration,
    easing,
  ]);

  const isPage = target === "page";
  const shown = animated === false || reduce ? true : revealed;

  const containerStyle: CSSProperties = {
    position: isPage ? "fixed" : "absolute",
    pointerEvents: hoverIntensity ? "auto" : "none",
    opacity: shown ? 1 : 0,
    transition: animated && !reduce ? `opacity ${duration} ${easing}` : undefined,
    zIndex: isPage ? zIndex + 100 : zIndex,
    ...edgeStyle(position, vertical, height, width),
    ...style,
  };

  return (
    <div
      ref={ref}
      aria-hidden
      className={cn(className)}
      style={containerStyle}
      onMouseEnter={hoverIntensity ? () => setHovered(true) : undefined}
      onMouseLeave={hoverIntensity ? () => setHovered(false) : undefined}
    >
      {layers.map((layerStyle, i) => (
        <div key={i} style={layerStyle} />
      ))}
      {children}
    </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` | `"top" | "bottom" | "left" | "right"` | `"bottom"` | Edge the blur attaches to. |
| `strength` | `number` | `1` | Base blur multiplier applied to every layer. Range 0–6. |
| `divCount` | `number` | `5` | Number of stacked blur layers (higher = smoother ramp). Range 1–12. |
| `exponential` | `boolean` | `true` | Exponential progression for a stronger blur at the edge. |
| `opacity` | `number` | `1` | Opacity applied to each blur layer. Range 0–1. |
| `target` | `"parent" | "page"` | `"parent"` | Attach to the parent container or the viewport (fixed). |
| `tinted` | `boolean` | `false` | Wash the blurred edge with a faint amethyst accent. |

## Usage

```tsx
import MaskBlur from "@/components/registry/mask-blur/mask-blur";

<MaskBlur
  position="bottom"
  strength={1}
  divCount={5}
  exponential={true}
  opacity={1}
  target="parent"
  tinted={false}
/>
```

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