# Integrate `Preloader` from Motion Garden

You are helping me add the **Preloader** 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:** Preloader
- **Category:** Micro-interactions
- **Summary:** A percentage-counting load overlay. A spinning ring, an eased counter, and a fade-out that reveals the page beneath.
- **Reference:** https://motion-garden.netlify.app/components/preloader
- **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 gsap 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/preloader/preloader.tsx`

```tsx
"use client";

import { useEffect, useRef } from "react";
import gsap from "gsap";
import { cn } from "@/lib/utils";

export interface PreloaderProps {
	simulate?: boolean;
	duration?: number;
	holdDelay?: number;
	fadeDuration?: number;
	ringColor?: string;
	overlayColor?: string;
	ringSize?: number;
	spinSpeed?: number;
	label?: string;
	showLabel?: boolean;
	progress?: number;
	isComplete?: boolean;
	onComplete?: () => void;
	reducedMotion?: boolean;
	className?: string;
}

export default function Preloader({
	simulate = false,
	duration = 3,
	holdDelay = 0.4,
	fadeDuration = 0.6,
	ringColor = "#a855f7",
	overlayColor = "#000000",
	ringSize = 128,
	spinSpeed = 1.5,
	label = "Loading Experience",
	showLabel = true,
	progress = 0,
	isComplete = false,
	onComplete,
	reducedMotion = false,
	className,
}: PreloaderProps) {
	const rootRef = useRef<HTMLDivElement>(null);
	const counterRef = useRef<HTMLSpanElement>(null);
	const onCompleteRef = useRef(onComplete);
	onCompleteRef.current = onComplete;

	const writeCount = (v: number) => {
		if (counterRef.current) {
			counterRef.current.textContent = String(Math.round(v));
		}
	};

	useEffect(() => {
		if (!simulate || reducedMotion) return;
		const ctx = gsap.context(() => {
			const proxy = { v: 0 };
			const tl = gsap.timeline();
			tl.to(proxy, {
				v: 100,
				duration,
				ease: "power2.inOut",
				onUpdate: () => writeCount(proxy.v),
			});
			tl.to(rootRef.current, {
				opacity: 0,
				duration: fadeDuration,
				ease: "power2.inOut",
				delay: holdDelay,
				onComplete: () => {
					if (rootRef.current) rootRef.current.style.display = "none";
					onCompleteRef.current?.();
				},
			});
		}, rootRef);
		return () => ctx.revert();
	}, [simulate, reducedMotion, duration, holdDelay, fadeDuration]);

	useEffect(() => {
		if (simulate || reducedMotion) return;
		const proxy = { v: Number(counterRef.current?.textContent) || 0 };
		const tween = gsap.to(proxy, {
			v: Math.min(100, Math.max(0, progress)),
			duration: 0.5,
			ease: "power2.out",
			onUpdate: () => writeCount(proxy.v),
		});
		return () => {
			tween.kill();
		};
	}, [simulate, reducedMotion, progress]);

	useEffect(() => {
		if (simulate || reducedMotion || !isComplete) return;
		const tween = gsap.to(rootRef.current, {
			opacity: 0,
			duration: fadeDuration,
			ease: "power2.inOut",
			onComplete: () => {
				if (rootRef.current) {
					rootRef.current.style.pointerEvents = "none";
					rootRef.current.style.display = "none";
				}
				onCompleteRef.current?.();
			},
		});
		return () => {
			tween.kill();
		};
	}, [simulate, reducedMotion, isComplete, fadeDuration]);

	const staticCount = reducedMotion
		? simulate
			? 100
			: Math.round(progress)
		: 0;

	return (
		<div
			ref={rootRef}
			className={cn(
				"absolute inset-0 z-40 flex items-center justify-center",
				className,
			)}
			style={{ background: overlayColor }}
		>
			<div className="flex flex-col items-center gap-6">
				<div
					className="relative"
					style={{ width: ringSize, height: ringSize }}
				>
					<div className="absolute inset-0 rounded-full border-2 border-white/20" />
					<div
						className="absolute inset-0 animate-spin rounded-full border-2"
						style={{
							borderColor: ringColor,
							borderTopColor: "transparent",
							animationDuration: `${spinSpeed}s`,
							...(reducedMotion && { animation: "none" }),
						}}
					/>
				</div>
				<div className="flex flex-col items-center gap-2">
					<div
						className="text-6xl font-bold tabular-nums"
						style={{ color: ringColor }}
					>
						<span ref={counterRef}>{staticCount}</span>%
					</div>
					{showLabel && (
						<p
							className="text-sm uppercase tracking-wider"
							style={{
								color: `color-mix(in srgb, ${ringColor} 60%, transparent)`,
							}}
						>
							{label}
						</p>
					)}
				</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 |
| --- | --- | --- | --- |
| `duration` | `number` | `3` | Simulated load time. Range 1–8 s. |
| `ringColor` | `string (hex)` | `"#a855f7"` | Color of the spinner, counter, and label. |
| `overlayColor` | `string (hex)` | `"#000000"` | Overlay background color. |
| `ringSize` | `number` | `128` | Ring diameter. Range 64–200 px. |
| `spinSpeed` | `number` | `1.5` | Seconds per spinner revolution. Range 0.5–4 s. |
| `holdDelay` | `number` | `0.4` | Pause at 100% before the fade. Range 0–2 s. |
| `fadeDuration` | `number` | `0.6` | Duration of the fade-out. Range 0.2–1.5 s. |
| `showLabel` | `boolean` | `true` | Show the caption under the counter. |

## Usage

```tsx
import Preloader from "@/components/registry/preloader/preloader";

<Preloader
  duration={3}
  ringColor="#a855f7"
  overlayColor="#000000"
  ringSize={128}
  spinSpeed={1.5}
  holdDelay={0.4}
  fadeDuration={0.6}
  showLabel={true}
/>
```

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.
