{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "aspect-ratio",
  "title": "Aspect Ratio",
  "description": "Holds the space for media before it arrives, so the page does not jump, with a mat, an overlaid caption or an empty slot.",
  "dependencies": [
    "radix-ui",
    "lucide-react",
    "motion"
  ],
  "registryDependencies": [
    "utils",
    "https://miseui.junsu4621.workers.dev/r/motion.json"
  ],
  "files": [
    {
      "path": "registry/miseui/ui/aspect-ratio.tsx",
      "content": "import type * as React from \"react\"\nimport { useEffect, useRef, useState } from \"react\"\nimport { AspectRatio as AspectRatioPrimitive } from \"radix-ui\"\nimport { ImageIcon } from \"lucide-react\"\nimport { motion, useReducedMotion } from \"motion/react\"\nimport { SPRING_DEFAULT } from \"@/registry/miseui/lib/motion\"\nimport { cn } from \"@/registry/miseui/lib/utils\"\n\ntype AspectRatioVariant = \"plain\" | \"framed\" | \"overlay\" | \"placeholder\"\ntype AspectRatioEffect = \"reveal\"\n\nconst INSTANT = { duration: 0 } as const\nconst MEDIA_CLASS = \"size-full [&>img]:size-full [&>img]:object-cover [&>video]:size-full [&>video]:object-cover\"\nconst MAX_DENOMINATOR = 16\nconst RATIO_TOLERANCE = 0.005\n\n/** Prefers the small whole numbers people say out loud; a cinema 2.39 stays a decimal rather than becoming 43:18. */\nfunction ratioLabel(ratio: number) {\n  if (!Number.isFinite(ratio) || ratio <= 0) return \"\"\n  for (let denominator = 1; denominator <= MAX_DENOMINATOR; denominator += 1) {\n    const numerator = Math.round(ratio * denominator)\n    if (numerator > 0 && Math.abs(numerator / denominator - ratio) < RATIO_TOLERANCE) return `${numerator}:${denominator}`\n  }\n  return `${ratio.toFixed(2)}:1`\n}\n\nfunction useMediaReady(enabled: boolean) {\n  const ref = useRef<HTMLDivElement>(null)\n  const [ready, setReady] = useState(false)\n  useEffect(() => {\n    const root = ref.current\n    if (!enabled || !root) return\n    const finish = () => setReady(true)\n    const media = root.querySelector<HTMLImageElement | HTMLVideoElement>(\"img, video\")\n    // A cached or broken image fired its event before this effect ran, and nothing arrives for a media-less slot.\n    const settled = !media || (media instanceof HTMLImageElement ? media.complete : media.readyState >= 2)\n    if (settled) {\n      const frame = requestAnimationFrame(finish)\n      return () => cancelAnimationFrame(frame)\n    }\n    // Media events do not bubble but they do pass through the capture phase. An error must also reveal, or a broken image stays invisible.\n    const events = [\"load\", \"loadeddata\", \"error\"]\n    events.forEach((name) => root.addEventListener(name, finish, true))\n    return () => events.forEach((name) => root.removeEventListener(name, finish, true))\n  }, [enabled])\n  return { ref, ready }\n}\n\nfunction RevealMedia({ children }: { children: React.ReactNode }) {\n  const reduceMotion = useReducedMotion() ?? false\n  const { ref, ready } = useMediaReady(true)\n  const hidden = reduceMotion ? { opacity: 0 } : { opacity: 0, scale: 1.02, filter: \"blur(8px)\" }\n  const shown = reduceMotion ? { opacity: 1 } : { opacity: 1, scale: 1, filter: \"blur(0px)\" }\n  return (\n    <motion.div\n      ref={ref}\n      data-slot=\"aspect-ratio-media\"\n      data-ready={ready ? \"true\" : \"false\"}\n      className={MEDIA_CLASS}\n      initial={false}\n      animate={ready ? shown : hidden}\n      transition={reduceMotion ? INSTANT : SPRING_DEFAULT}\n    >\n      {children}\n    </motion.div>\n  )\n}\n\nfunction Media({ effect, children }: { effect?: AspectRatioEffect; children: React.ReactNode }) {\n  if (effect === \"reveal\") return <RevealMedia>{children}</RevealMedia>\n  return (\n    <div data-slot=\"aspect-ratio-media\" className={MEDIA_CLASS}>\n      {children}\n    </div>\n  )\n}\n\ntype AspectRatioProps = Omit<React.ComponentProps<\"div\">, \"children\"> & {\n  /** Width divided by height, such as 16 / 9. */\n  ratio?: number\n  /** `plain` clipped media, `framed` a mat with the caption below, `overlay` the caption on the media, `placeholder` an empty slot. */\n  variant?: AspectRatioVariant\n  /** `reveal` keeps the media hidden until it has loaded, then brings it into focus. */\n  effect?: AspectRatioEffect\n  /** Shown by `framed` and `overlay`. */\n  caption?: React.ReactNode\n  /** What `placeholder` says; defaults to the ratio itself. */\n  label?: string\n  children?: React.ReactNode\n}\n\nfunction EmptySlot({ ratio, label }: { ratio: number; label: string }) {\n  return (\n    <AspectRatioPrimitive.Root ratio={ratio}>\n      <div className=\"flex size-full flex-col items-center justify-center gap-2\">\n        <ImageIcon aria-hidden=\"true\" className=\"size-5\" />\n        <span className=\"text-[13px] tabular-nums\">{label}</span>\n      </div>\n    </AspectRatioPrimitive.Root>\n  )\n}\n\nfunction AspectRatio({\n  ratio = 16 / 9,\n  variant = \"plain\",\n  effect,\n  caption,\n  label,\n  className,\n  children,\n  ...props\n}: AspectRatioProps) {\n  const root = { ...props, \"data-slot\": \"aspect-ratio\", \"data-variant\": variant, \"data-effect\": effect }\n  const box = (\n    <AspectRatioPrimitive.Root ratio={ratio}>\n      <Media effect={effect}>{children}</Media>\n    </AspectRatioPrimitive.Root>\n  )\n  if (variant === \"framed\") {\n    return (\n      <figure {...root} className={cn(\"m-0 grid min-w-0 gap-2 rounded-xl border bg-card p-2\", className)}>\n        <div className=\"overflow-hidden rounded-md bg-muted\">{box}</div>\n        {caption ? <figcaption className=\"px-1 pb-1 text-[13px] leading-5 text-muted-foreground\">{caption}</figcaption> : null}\n      </figure>\n    )\n  }\n  if (variant === \"overlay\") {\n    return (\n      <figure {...root} className={cn(\"relative m-0 min-w-0 overflow-hidden rounded-lg bg-muted\", className)}>\n        {box}\n        {caption ? (\n          // Fixed black and white: this sits on a photo in either theme, so theme tokens would say nothing about legibility.\n          <figcaption className=\"absolute inset-x-0 bottom-0 bg-linear-to-t from-black/70 to-transparent px-3 pt-8 pb-3 text-sm leading-5 font-medium text-white\">\n            {caption}\n          </figcaption>\n        ) : null}\n      </figure>\n    )\n  }\n  if (variant === \"placeholder\") {\n    const text = label ?? ratioLabel(ratio)\n    return (\n      <div\n        {...root}\n        role=\"img\"\n        aria-label={label ?? `Empty media slot, ${text}`}\n        className={cn(\"min-w-0 rounded-lg border border-dashed bg-muted/50 text-muted-foreground\", className)}\n      >\n        <EmptySlot ratio={ratio} label={text} />\n      </div>\n    )\n  }\n  return (\n    <div {...root} className={cn(\"min-w-0 overflow-hidden rounded-lg bg-muted\", className)}>\n      {box}\n    </div>\n  )\n}\n\nexport { AspectRatio, ratioLabel, type AspectRatioProps, type AspectRatioVariant, type AspectRatioEffect }\n",
      "type": "registry:ui"
    }
  ],
  "categories": [
    "display"
  ],
  "type": "registry:ui"
}