{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "masonry-grid",
  "title": "Masonry Grid",
  "description": "A masonry layout for tiles of different heights: each tile drops into the shortest column while keeping left-to-right order, the column count follows the container's width, and tiles spring to their new places when it changes.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/masonry-grid/masonry-grid.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { motion, useReducedMotion } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport interface MasonryGridProps {\n  /** The tiles. Any content works; heights are measured. */\n  children?: React.ReactNode;\n  /** A fixed number of columns. Leave out to fit as many as minColumnWidth allows. */\n  columns?: number;\n  minColumnWidth?: number;\n  maxColumns?: number;\n  gap?: number;\n  /** Tiles fade up one after another the first time they're placed. */\n  animateIn?: boolean;\n  className?: string;\n}\n\n// Demo tiles: photos at different shapes plus a couple of text cards, so the heights vary.\nconst DEMO: { src?: string; ratio?: string; title: string; meta: string; quote?: string }[] = [\n  { src: \"/demo/34.webp\", ratio: \"4 / 5\", title: \"Road Cycling\", meta: \"42 km · Zone 3\" },\n  { src: \"/demo/31.webp\", ratio: \"4 / 3\", title: \"Track Sprints\", meta: \"8 × 200 m\" },\n  { title: \"Coach's note\", meta: \"Week 6\", quote: \"Easy days easy, hard days hard. Most people get both wrong.\" },\n  { src: \"/demo/12.webp\", ratio: \"1 / 1\", title: \"Alpine Skiing\", meta: \"Carving drills\" },\n  { src: \"/demo/28.webp\", ratio: \"3 / 4\", title: \"Rowing\", meta: \"45 min steady\" },\n  { src: \"/demo/33.webp\", ratio: \"16 / 10\", title: \"Open Water\", meta: \"1,500 m\" },\n  { src: \"/demo/24.webp\", ratio: \"4 / 5\", title: \"Barbell\", meta: \"5 × 5\" },\n  { title: \"Recovery\", meta: \"Sunday\", quote: \"Sleep is the only supplement that works every time.\" },\n  { src: \"/demo/29.webp\", ratio: \"4 / 3\", title: \"5K to 10K\", meta: \"Week 3 of 9\" },\n];\n\nfunction DemoTile({ tile }: { tile: (typeof DEMO)[number] }) {\n  if (tile.quote) {\n    return (\n      <div className=\"flex flex-col gap-4 rounded-2xl border border-white/10 bg-[#121212] p-5 text-[#F5F4F1]\">\n        <p className=\"m-0 text-[16px] leading-snug font-medium tracking-[-0.01em]\">“{tile.quote}”</p>\n        <span className=\"text-[12.5px] text-white/50\">\n          {tile.title} · {tile.meta}\n        </span>\n      </div>\n    );\n  }\n  return (\n    <figure className=\"group relative m-0 overflow-hidden rounded-2xl bg-[#151515]\" style={{ aspectRatio: tile.ratio }}>\n      {/* eslint-disable-next-line @next/next/no-img-element */}\n      <img src={tile.src} alt={tile.title} className=\"h-full w-full object-cover transition-transform duration-700 group-hover:scale-[1.04]\" draggable={false} />\n      <figcaption className=\"absolute inset-x-0 bottom-0 flex flex-col px-4 pt-8 pb-3\" style={{ background: \"linear-gradient(to top, rgba(0,0,0,0.65), transparent)\" }}>\n        <span className=\"text-[14px] font-semibold text-white\">{tile.title}</span>\n        <span className=\"text-[12px] text-white/70\">{tile.meta}</span>\n      </figcaption>\n    </figure>\n  );\n}\n\nexport function MasonryGrid({ children, columns, minColumnWidth = 200, maxColumns = 4, gap = 12, animateIn = true, className }: MasonryGridProps) {\n  const reduce = Boolean(useReducedMotion());\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const itemRefs = React.useRef<Map<number, HTMLDivElement>>(new Map());\n  const [width, setWidth] = React.useState(0);\n  const [heights, setHeights] = React.useState<number[]>([]);\n  // 0: measuring (hidden). 1: first placement, set instantly and faded up in place. 2: live, moves spring.\n  const [phase, setPhase] = React.useState<0 | 1 | 2>(0);\n\n  const items = children !== undefined ? React.Children.toArray(children) : DEMO.map((t, i) => <DemoTile key={i} tile={t} />);\n  const count = items.length;\n\n  const cols = Math.max(1, columns ?? Math.min(maxColumns, Math.floor((width + gap) / (minColumnWidth + gap)) || 1));\n  const colW = width ? (width - gap * (cols - 1)) / cols : 0;\n\n  // Watch the container's width and every tile's height (images loading, text wrapping).\n  React.useLayoutEffect(() => {\n    const root = rootRef.current;\n    if (!root) return;\n    const measure = () => {\n      setWidth(root.clientWidth);\n      const hs = Array.from({ length: count }, (_, i) => itemRefs.current.get(i)?.offsetHeight ?? 0);\n      setHeights(hs);\n      // Only place once the tiles were measured at their real column width.\n      if (colW > 0 && hs.every((h) => h > 0)) setPhase((ph) => (ph === 0 ? 1 : ph));\n    };\n    const ro = new ResizeObserver(measure);\n    ro.observe(root);\n    itemRefs.current.forEach((el) => ro.observe(el));\n    measure();\n    return () => ro.disconnect();\n  }, [count, colW]);\n\n  React.useEffect(() => {\n    if (phase !== 1) return;\n    const id = window.setTimeout(() => setPhase(2), 60);\n    return () => window.clearTimeout(id);\n  }, [phase]);\n\n  // Shortest-column placement, taken in order so tiles still read left to right, top to bottom.\n  const colH = new Array(cols).fill(0);\n  const pos = items.map((_, i) => {\n    const c = colH.indexOf(Math.min(...colH));\n    const at = { x: c * (colW + gap), y: colH[c] };\n    colH[c] += (heights[i] ?? 0) + gap;\n    return at;\n  });\n  const ready = phase > 0;\n  const total = Math.max(0, Math.max(...colH) - gap);\n\n  return (\n    <div ref={rootRef} className={cn(\"relative w-full\", className)} style={{ height: ready ? total : undefined, minHeight: ready ? undefined : 200, transition: reduce ? undefined : \"height 0.4s cubic-bezier(0.22, 1, 0.36, 1)\" }}>\n      {items.map((item, i) => (\n        <motion.div\n          key={i}\n          ref={(el) => {\n            if (el) itemRefs.current.set(i, el);\n            else itemRefs.current.delete(i);\n          }}\n          className=\"absolute top-0 left-0\"\n          style={{ width: colW ? undefined : `${100 / cols}%` }}\n          initial={false}\n          animate={{ x: pos[i].x, y: pos[i].y, opacity: ready ? 1 : 0, ...(colW ? { width: colW } : {}) }}\n          transition={{\n            // Width moves with position, so tiles never overlap while the columns change.\n            width: phase === 2 && !reduce ? { type: \"spring\", stiffness: 260, damping: 32 } : { duration: 0 },\n            x: phase === 2 && !reduce ? { type: \"spring\", stiffness: 260, damping: 32 } : { duration: 0 },\n            y: phase === 2 && !reduce ? { type: \"spring\", stiffness: 260, damping: 32 } : { duration: 0 },\n            opacity: { duration: reduce ? 0 : 0.4, delay: animateIn && phase === 1 && !reduce ? Math.min(i, 12) * 0.05 : 0 },\n          }}\n        >\n          <motion.div initial={false} animate={{ y: ready || !animateIn || reduce ? 0 : 16 }} transition={{ duration: reduce ? 0 : 0.5, delay: animateIn && !reduce ? Math.min(i, 12) * 0.05 : 0, ease: [0.22, 1, 0.36, 1] }}>\n            {item}\n          </motion.div>\n        </motion.div>\n      ))}\n    </div>\n  );\n}\n\nexport default MasonryGrid;\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}