# Integrate `Barcode` from Motion Garden

You are helping me add the **Barcode** 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:** Barcode
- **Category:** Backgrounds
- **Summary:** A procedural barcode whose bars light up like piano notes, looping on autoplay or scrubbed by scroll.
- **Reference:** https://motion-garden.netlify.app/components/barcode
- **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
```

## 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-ink: var(--sg-ink);
}

:root {
  --sg-ink: #16191d;
}

.dark {
  --sg-ink: #e2e6e9;
}
```

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

One file. 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/barcode/barcode.tsx`

```tsx
"use client";

import gsap from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
import { useEffect, useMemo, useRef, type RefObject } from "react";

if (typeof window !== "undefined") {
	gsap.registerPlugin(ScrollTrigger);
}

const BARCODE_CONFIG = { diagonalTilt: 22, diagonalGap: 5 };
const TIMELINE_LENGTH = 10;

function mulberry32(seed: number) {
	return () => {
		seed |= 0;
		seed = (seed + 0x6d2b79f5) | 0;
		let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
		t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
		return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
	};
}

interface BarLine {
	x: number;
	width: number;
	type?: "diagonal";
}

interface BarcodeProps {
	mode?: "autoplay" | "scrub";
	restOpacity?: number;
	peakOpacity?: number;
	noteDuration?: number;
	speed?: number;
	color?: string;
	className?: string;
	scrollerRef?: RefObject<HTMLElement | null>;
	paused?: boolean;
}

export default function Barcode({
	mode = "autoplay",
	restOpacity = 0.6,
	peakOpacity = 1,
	noteDuration = 1.6,
	speed = 1,
	color = "#a855f7",
	className,
	scrollerRef,
	paused = false,
}: BarcodeProps) {
	const containerRef = useRef<HTMLDivElement>(null);
	const topGroupRef = useRef<SVGGElement>(null);
	const botGroupRef = useRef<SVGGElement>(null);

	const topLines = useMemo(() => {
		const result: BarLine[] = [];
		let x = 20;
		[4, 1.5, 3, 2, 1.5].forEach((w) => {
			result.push({ x, width: w });
			x += w + 10;
		});
		x += BARCODE_CONFIG.diagonalGap;
		result.push({ x, width: 3, type: "diagonal" });
		x += 3 + BARCODE_CONFIG.diagonalTilt + BARCODE_CONFIG.diagonalGap + 12;
		const pattern = [2, 1.5, 3, 1.5, 1.5, 4, 1.5, 3, 1.5];
		let p = 0;
		while (x < 1440) {
			const w = pattern[p++ % pattern.length];
			result.push({ x, width: w });
			x += w + 10;
		}
		return result;
	}, []);

	const bottomLines = useMemo(() => {
		const result: BarLine[] = [];
		let x = 20;
		const pattern = [1.5, 3, 1.5, 1.5, 2, 1.5, 3, 1.5, 2, 1.5, 3, 1.5, 1.5, 2];
		let p = 0;
		while (x < 1320) {
			const w = pattern[p++ % pattern.length];
			result.push({ x, width: w });
			x += w + 10;
		}
		x += BARCODE_CONFIG.diagonalGap;
		result.push({ x, width: 3, type: "diagonal" });
		x += 3 + BARCODE_CONFIG.diagonalTilt + BARCODE_CONFIG.diagonalGap + 10;
		[3, 1.5, 4, 1.5, 3].forEach((w) => {
			result.push({ x, width: w });
			x += w + 10;
		});
		while (x < 1440) {
			result.push({ x, width: 1.5 });
			x += 11.5;
		}
		return result;
	}, []);

	useEffect(() => {
		const container = containerRef.current;
		if (!container) return;

		const ctx = gsap.context(() => {
			const topBars = topGroupRef.current
				? Array.from(topGroupRef.current.children)
				: [];
			const botBars = botGroupRef.current
				? Array.from(botGroupRef.current.children)
				: [];
			if (!topBars.length || !botBars.length) return;

			const allBars = [...topBars, ...botBars];

			if (paused) {
				gsap.set(allBars, { opacity: restOpacity });
				return;
			}

			gsap.set(allBars, { opacity: 0.08 });

			const rand = mulberry32(1337);
			const buildPianoTl = (bars: Element[]) => {
				const tl = gsap.timeline();
				bars.forEach((bar) => {
					const strike = rand() * (TIMELINE_LENGTH * 0.85);
					const half = noteDuration / 2;
					tl.fromTo(
						bar,
						{ opacity: restOpacity },
						{ opacity: peakOpacity, duration: half, ease: "power2.out" },
						strike,
					).to(
						bar,
						{ opacity: restOpacity, duration: half, ease: "power2.in" },
						strike + half,
					);
				});
				return tl;
			};

			const master = gsap
				.timeline({ repeat: mode === "autoplay" ? -1 : 0 })
				.add(buildPianoTl(topBars))
				.add(buildPianoTl(botBars), 0);

			if (mode === "autoplay") {
				master.timeScale(speed);
			} else {
				master.pause();
				ScrollTrigger.create({
					trigger: container,
					scroller: scrollerRef?.current ?? undefined,
					start: "top 90%",
					end: "bottom 50%",
					scrub: 1.5,
					animation: master,
				});
			}
		}, container);

		return () => ctx.revert();
	}, [mode, restOpacity, peakOpacity, noteDuration, speed, scrollerRef, paused]);

	const barClass =
		"hover:opacity-100! hover:text-ink! cursor-crosshair transition-colors duration-200";

	return (
		<div
			ref={containerRef}
			className={className ?? "relative h-full w-full overflow-hidden"}
			style={{ color }}
		>
			<svg
				width="100%"
				height="100%"
				viewBox="0 0 1440 180"
				preserveAspectRatio="none"
				className="block"
			>
				<g ref={topGroupRef}>
					{topLines.map((line, i) =>
						line.type === "diagonal" ? (
							<polygon
								key={`top-${i}`}
								points={`${line.x},20 ${line.x + line.width},20 ${line.x + BARCODE_CONFIG.diagonalTilt + line.width},90 ${line.x + BARCODE_CONFIG.diagonalTilt},90`}
								fill="currentColor"
								className={barClass}
							/>
						) : (
							<rect
								key={`top-${i}`}
								x={line.x}
								y={20}
								width={line.width}
								height={70}
								fill="currentColor"
								className={barClass}
							/>
						),
					)}
				</g>

				<g ref={botGroupRef}>
					{bottomLines.map((line, i) =>
						line.type === "diagonal" ? (
							<polygon
								key={`bot-${i}`}
								points={`${line.x + BARCODE_CONFIG.diagonalTilt},90 ${line.x + BARCODE_CONFIG.diagonalTilt + line.width},90 ${line.x + line.width},160 ${line.x},160`}
								fill="currentColor"
								className={barClass}
							/>
						) : (
							<rect
								key={`bot-${i}`}
								x={line.x}
								y={90}
								width={line.width}
								height={70}
								fill="currentColor"
								className={barClass}
							/>
						),
					)}
				</g>

				<line
					x1="0"
					y1="90"
					x2="1440"
					y2="90"
					stroke="currentColor"
					strokeWidth="0.5"
					opacity="0.1"
				/>
			</svg>
		</div>
	);
}
```


## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `mode` | `"autoplay" | "scrub"` | `"autoplay"` | Autoplay loops the note timeline; scrub ties it to scroll position. |
| `restOpacity` | `number` | `0.6` | Opacity a bar settles at after its note. Range 0.1–1. |
| `peakOpacity` | `number` | `1` | Opacity at the peak of a note strike. Range 0.5–1. |
| `noteDuration` | `number` | `1.6` | Length of one note's swell and fade. Range 0.4–4 s. |
| `speed` | `number` | `1` | Playback rate of the autoplay loop. Range 0.25–3. Ignored while `mode` is `scrub`. |
| `color` | `string (hex)` | `"#a855f7"` | Bar color. |

## Usage

```tsx
import Barcode from "@/components/registry/barcode/barcode";

<Barcode
  mode="autoplay"
  restOpacity={0.6}
  peakOpacity={1}
  noteDuration={1.6}
  speed={1}
  color="#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.

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