{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"mosaic-flip","type":"registry:ui","title":"Mosaic Flip","description":"A card that dissolves into a random pixel grid on hover and reassembles as its second face.","dependencies":[],"registryDependencies":["utils"],"files":[{"path":"registry/ui/mosaic-flip.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef, type ReactNode } from \"react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface MosaicFlipProps {\n  /** Face shown at rest. */\n  children: ReactNode;\n  /** Face revealed on hover, focus or tap. */\n  back: ReactNode;\n  /** Pixels per row and column. */\n  gridSize?: number;\n  pixelColor?: string;\n  /** Total ms for cover + reveal. */\n  duration?: number;\n  /** Accessible label for the card. */\n  label?: string;\n  className?: string;\n}\n\nconst shuffle = (n: number) => {\n  const a = Array.from({ length: n }, (_, i) => i);\n  for (let i = n - 1; i > 0; i--) {\n    const j = Math.floor(Math.random() * (i + 1));\n    [a[i], a[j]] = [a[j], a[i]];\n  }\n  return a;\n};\n\nexport function MosaicFlip({\n  children,\n  back,\n  gridSize = 12,\n  pixelColor = \"#c6ff3d\",\n  duration = 700,\n  label = \"Flip card\",\n  className,\n}: MosaicFlipProps) {\n  const grid = useRef<HTMLDivElement>(null);\n  const front = useRef<HTMLDivElement>(null);\n  const rear = useRef<HTMLDivElement>(null);\n  const timer = useRef<ReturnType<typeof setTimeout>>(undefined);\n  const showing = useRef(false);\n\n  useEffect(() => () => clearTimeout(timer.current), []);\n\n  const swapFaces = (toBack: boolean) => {\n    front.current!.style.visibility = toBack ? \"hidden\" : \"visible\";\n    rear.current!.style.visibility = toBack ? \"visible\" : \"hidden\";\n  };\n\n  const run = (toBack: boolean) => {\n    if (showing.current === toBack) return;\n    showing.current = toBack;\n    clearTimeout(timer.current);\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) return swapFaces(toBack);\n\n    const pixels = Array.from(grid.current!.children) as HTMLElement[];\n    const half = duration / 2;\n    // Each pixel pops in at a random step, the face swaps under full cover, then pixels pop out in a new order.\n    shuffle(pixels.length).forEach((rank, i) => {\n      pixels[i].style.transitionDelay = `${(rank / pixels.length) * half}ms`;\n      pixels[i].style.opacity = \"1\";\n    });\n    timer.current = setTimeout(() => {\n      swapFaces(toBack);\n      shuffle(pixels.length).forEach((rank, i) => {\n        pixels[i].style.transitionDelay = `${(rank / pixels.length) * half}ms`;\n        pixels[i].style.opacity = \"0\";\n      });\n    }, half);\n  };\n\n  return (\n    <div\n      role=\"button\"\n      tabIndex={0}\n      aria-label={label}\n      className={cn(\n        \"relative isolate overflow-hidden rounded-2xl border border-border bg-card outline-none focus-visible:ring-2 focus-visible:ring-ring\",\n        className,\n      )}\n      onPointerEnter={(e) => e.pointerType === \"mouse\" && run(true)}\n      onPointerLeave={(e) => e.pointerType === \"mouse\" && run(false)}\n      onClick={() => run(!showing.current)}\n      onKeyDown={(e) => {\n        if (e.key === \"Enter\" || e.key === \" \") {\n          e.preventDefault();\n          run(!showing.current);\n        }\n      }}\n    >\n      <div ref={front} className=\"absolute inset-0\">\n        {children}\n      </div>\n      <div ref={rear} className=\"absolute inset-0\" style={{ visibility: \"hidden\" }}>\n        {back}\n      </div>\n      <div\n        ref={grid}\n        aria-hidden\n        className=\"pointer-events-none absolute inset-0 z-10 grid\"\n        style={{ gridTemplateColumns: `repeat(${gridSize}, 1fr)`, gridTemplateRows: `repeat(${gridSize}, 1fr)` }}\n      >\n        {Array.from({ length: gridSize * gridSize }, (_, i) => (\n          // Slight overdraw hides hairline seams between neighbouring pixels.\n          <span key={i} style={{ background: pixelColor, opacity: 0, transition: \"opacity 0s\", margin: -0.5 }} />\n        ))}\n      </div>\n    </div>\n  );\n}\n"}]}