{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "image-zoom",
  "title": "Image zoom",
  "description": "ImageZoom: an image that zooms in to fill the screen when clicked, built on react-medium-image-zoom, and Lightbox, which browses the zoomable images inside it in one viewer.",
  "dependencies": [
    "cn",
    "@fuma-translate/react@^1.0.2",
    "fumadocs-core@^16.17.8",
    "lucide-react",
    "react-medium-image-zoom"
  ],
  "registryDependencies": [
    "button"
  ],
  "files": [
    {
      "path": "registry/base/docs/components/image-zoom.tsx",
      "content": "// Adapted from Fumadocs UI (https://github.com/fuma-nama/fumadocs)\n// Copyright (c) 2023 Fuma, MIT License\n\"use client\";\n\nimport { Image, type ImageProps } from \"fumadocs-core/framework\";\nimport {\n  type ComponentProps,\n  createContext,\n  type CSSProperties,\n  type ReactNode,\n  use,\n  useRef,\n  useState,\n} from \"react\";\nimport { createPortal, flushSync } from \"react-dom\";\nimport Zoom, { type UncontrolledProps } from \"react-medium-image-zoom\";\nimport { ChevronLeft, ChevronRight, X } from \"lucide-react\";\nimport { useTranslations } from \"@fuma-translate/react\";\nimport { cn } from \"cn\";\nimport { Button } from \"@/components/ui/button\";\n\nexport type ImageZoomProps = ImageProps & {\n  /**\n   * Image props when zoom in\n   */\n  zoomInProps?: ComponentProps<\"img\">;\n\n  /**\n   * Props for `react-medium-image-zoom`, unused inside `<Lightbox />`\n   */\n  rmiz?: UncontrolledProps;\n\n  /**\n   * Caption in `<Lightbox />`\n   *\n   * @defaultValue `title`\n   */\n  caption?: ReactNode;\n};\n\ninterface Item {\n  element: HTMLElement;\n  image: ComponentProps<\"img\">;\n  caption?: ReactNode;\n}\n\ninterface Slide extends Item {\n  style?: CSSProperties;\n}\n\nconst LightboxContext = createContext<{\n  register: (item: Item) => () => void;\n  open: (element: HTMLElement) => void;\n} | null>(null);\n\nfunction getImageSrc(src: ImageProps[\"src\"]): string {\n  if (typeof src === \"string\") return src;\n\n  if (typeof src === \"object\") {\n    // Next.js\n    if (\"default\" in src)\n      return (src as { default: { src: string } }).default.src;\n    return src.src;\n  }\n\n  return \"\";\n}\n\nexport function ImageZoom({\n  zoomInProps,\n  children,\n  rmiz,\n  caption,\n  ...props\n}: ImageZoomProps) {\n  const lightbox = use(LightboxContext);\n  const zoomImg = {\n    src: getImageSrc(props.src),\n    sizes: undefined,\n    ...zoomInProps,\n  };\n  const image = children ?? (\n    <Image\n      sizes=\"(max-width: 768px) 100vw, (max-width: 1200px) 70vw, 900px\"\n      alt=\"\"\n      {...props}\n    />\n  );\n\n  if (lightbox) {\n    return (\n      <span\n        role=\"button\"\n        tabIndex={0}\n        aria-haspopup=\"dialog\"\n        ref={(element) => {\n          if (!element) return;\n          return lightbox.register({\n            element,\n            image: { alt: props.alt, ...zoomImg },\n            caption: caption ?? props.title,\n          });\n        }}\n        onClick={(e) => {\n          if (e.target !== e.currentTarget) lightbox.open(e.currentTarget);\n        }}\n        onKeyDown={(e) => {\n          if (e.key !== \"Enter\" && e.key !== \" \") return;\n          e.preventDefault();\n          lightbox.open(e.currentTarget);\n        }}\n        className=\"block outline-none [&_img]:cursor-zoom-in [&:focus-visible_img]:ring-2 [&:focus-visible_img]:ring-ring\"\n      >\n        {image}\n      </span>\n    );\n  }\n\n  return (\n    <Zoom zoomMargin={20} wrapElement=\"span\" {...rmiz} zoomImg={zoomImg}>\n      {image}\n    </Zoom>\n  );\n}\n\n/**\n * Browse the zoomable images inside in one viewer\n */\nexport function Lightbox({ children }: { children: ReactNode }) {\n  const t = useTranslations({ note: \"image zoom\" });\n  const [slides, setSlides] = useState<Slide[] | null>(null);\n  const scrollerRef = useRef<HTMLDivElement>(null);\n  const [context] = useState(() => {\n    const items = new Set<Item>();\n\n    return {\n      register(item: Item) {\n        items.add(item);\n        return () => void items.delete(item);\n      },\n      open(element: HTMLElement) {\n        const list: Slide[] = [];\n        for (const item of items) {\n          if (!item.element.checkVisibility()) continue;\n          const img = item.element.querySelector(\"img\");\n          list.push({ ...item, style: img ? getSlideStyle(img) : undefined });\n        }\n        list.sort((a, b) =>\n          a.element.compareDocumentPosition(b.element) &\n          Node.DOCUMENT_POSITION_FOLLOWING\n            ? -1\n            : 1,\n        );\n        const index = list.findIndex((slide) => slide.element === element);\n\n        transition(getImage(element), () => {\n          flushSync(() => setSlides(list));\n          const slide = scrollerRef.current!.children[index]!;\n          slide.scrollIntoView({\n            block: \"nearest\",\n            inline: \"start\",\n            behavior: \"instant\",\n          });\n          return slide.querySelector(\"img\")!;\n        });\n      },\n    };\n  });\n\n  function close() {\n    const scroller = scrollerRef.current;\n    if (!scroller || !slides) return;\n    const index = getIndex(scroller);\n    const slide = slides[index];\n    if (!slide) return;\n    const { element } = slide;\n\n    transition(scroller.children[index]!.querySelector(\"img\")!, () => {\n      flushSync(() => setSlides(null));\n      element.focus({ preventScroll: true });\n      const image = getImage(element);\n      image.scrollIntoView({ block: \"nearest\", behavior: \"instant\" });\n      return image;\n    });\n  }\n\n  function navigate(step: number) {\n    const scroller = scrollerRef.current!;\n    scroller.children[getIndex(scroller) + step]?.scrollIntoView({\n      block: \"nearest\",\n      inline: \"start\",\n      behavior: \"smooth\",\n    });\n  }\n\n  return (\n    <LightboxContext value={context}>\n      {children}\n      {slides &&\n        createPortal(\n          <dialog\n            ref={initDialog}\n            tabIndex={-1}\n            aria-label={t(\"Image Viewer\", { note: \"aria-label\" })}\n            onCancel={(e) => {\n              e.preventDefault();\n              close();\n            }}\n            onClose={() => setSlides(null)}\n            onKeyDown={(e) => {\n              if (e.key !== \"ArrowLeft\" && e.key !== \"ArrowRight\") return;\n              e.preventDefault();\n              const rtl = getComputedStyle(e.currentTarget).direction === \"rtl\";\n              navigate((e.key === \"ArrowRight\") !== rtl ? 1 : -1);\n            }}\n            className=\"m-0 size-full max-h-none max-w-none overflow-hidden border-none bg-background p-0 text-foreground outline-none\"\n          >\n            <Button\n              variant=\"ghost\"\n              size=\"icon-sm\"\n              aria-label={t(\"Close\", { note: \"aria-label\" })}\n              onClick={close}\n              className=\"absolute inset-e-2.5 top-3.25\"\n            >\n              <X />\n            </Button>\n            <div\n              ref={scrollerRef}\n              onClick={(e) => {\n                if (!(e.target as Element).closest(\"figcaption\")) close();\n              }}\n              className=\"flex size-full snap-x snap-mandatory [scrollbar-width:none] overflow-x-auto overscroll-x-contain\"\n            >\n              {slides.map((slide, i) => (\n                <figure\n                  key={i}\n                  className=\"flex w-full shrink-0 snap-start snap-always flex-col items-center pb-6\"\n                >\n                  <p className=\"flex h-14 w-full items-center px-4 text-sm text-muted-foreground tabular-nums\">\n                    {slides.length > 1 && `${i + 1} / ${slides.length}`}\n                  </p>\n                  <div className=\"[container-type:size] flex min-h-0 w-full flex-1 items-center justify-center px-4 md:px-16\">\n                    {/* the zoomed image, `zoomInProps` of `<ImageZoom />` like `react-medium-image-zoom` */}\n                    {/* eslint-disable-next-line @next/next/no-img-element */}\n                    <img\n                      alt=\"\"\n                      loading=\"lazy\"\n                      {...slide.image}\n                      style={slide.style}\n                      className={cn(\n                        \"max-h-full cursor-zoom-out bg-cover\",\n                        slide.image.className,\n                      )}\n                    />\n                  </div>\n                  {slide.caption && (\n                    <figcaption className=\"docs-typeset mt-4 px-4 text-center text-sm text-muted-foreground md:px-16\">\n                      {slide.caption}\n                    </figcaption>\n                  )}\n                </figure>\n              ))}\n            </div>\n            {slides.length > 1 && (\n              <>\n                <Button\n                  variant=\"ghost\"\n                  size=\"icon\"\n                  aria-label={t(\"Previous Image\", { note: \"aria-label\" })}\n                  onClick={() => navigate(-1)}\n                  className=\"absolute inset-s-2.5 top-1/2 -translate-y-1/2 max-md:hidden\"\n                >\n                  <ChevronLeft className=\"rtl:-scale-x-100\" />\n                </Button>\n                <Button\n                  variant=\"ghost\"\n                  size=\"icon\"\n                  aria-label={t(\"Next Image\", { note: \"aria-label\" })}\n                  onClick={() => navigate(1)}\n                  className=\"absolute inset-e-2.5 top-1/2 -translate-y-1/2 max-md:hidden\"\n                >\n                  <ChevronRight className=\"rtl:-scale-x-100\" />\n                </Button>\n              </>\n            )}\n          </dialog>,\n          document.body,\n        )}\n    </LightboxContext>\n  );\n}\n\nfunction initDialog(element: HTMLDialogElement) {\n  // Strict Mode runs ref callbacks twice\n  if (!element.open) element.showModal();\n  element.focus();\n  const { style } = document.documentElement;\n  const overflow = style.overflow;\n  style.overflow = \"hidden\";\n  return () => {\n    style.overflow = overflow;\n  };\n}\n\nfunction getIndex(scroller: HTMLElement) {\n  return Math.round(Math.abs(scroller.scrollLeft) / scroller.clientWidth);\n}\n\nfunction getImage(element: HTMLElement): HTMLElement {\n  return element.querySelector(\"img\") ?? element;\n}\n\n// sized from the page image, so the slide has its final box before loading\nfunction getSlideStyle(img: HTMLImageElement): CSSProperties {\n  const ratio =\n    (img.naturalWidth || img.width) / (img.naturalHeight || img.height);\n\n  return {\n    width: ratio ? `min(100cqw, ${ratio} * 100cqh)` : undefined,\n    aspectRatio: ratio ? `auto ${ratio}` : undefined,\n    backgroundImage: img.currentSrc\n      ? `url(${JSON.stringify(img.currentSrc)})`\n      : undefined,\n  };\n}\n\n/**\n * morph `from` into the element returned by `update`\n */\nfunction transition(from: HTMLElement, update: () => HTMLElement) {\n  if (\n    !(\"startViewTransition\" in document) ||\n    matchMedia(\"(prefers-reduced-motion: reduce)\").matches\n  ) {\n    update();\n    return;\n  }\n\n  let to: HTMLElement | undefined;\n  from.style.viewTransitionName = \"fd-lightbox\";\n  document\n    .startViewTransition(() => {\n      from.style.viewTransitionName = \"\";\n      to = update();\n      to.style.viewTransitionName = \"fd-lightbox\";\n    })\n    .finished.finally(() => {\n      if (to) to.style.viewTransitionName = \"\";\n    });\n}\n",
      "type": "registry:component",
      "target": "@components/docs/components/image-zoom.tsx"
    }
  ],
  "css": {
    "@layer components": {
      "[data-rmiz]": {
        "display": "block",
        "position": "relative"
      },
      "[data-rmiz-ghost]": {
        "position": "absolute",
        "pointer-events": "none"
      },
      "[data-rmiz-btn-zoom], [data-rmiz-btn-unzoom]": {
        "display": "none"
      },
      "[data-rmiz-content=\"found\"] img": {
        "cursor": "zoom-in"
      },
      "[data-rmiz-modal][open]": {
        "position": "fixed",
        "width": "100dvw",
        "max-width": "none",
        "height": "100dvh",
        "max-height": "none",
        "margin": "0",
        "padding": "0",
        "overflow": "hidden",
        "background-color": "transparent"
      },
      "[data-rmiz-modal]:focus-visible": {
        "outline": "none"
      },
      "[data-rmiz-modal-overlay]": {
        "position": "absolute",
        "inset": "0",
        "transition": "background-color 0.3s"
      },
      "[data-rmiz-modal-overlay=\"visible\"]": {
        "background-color": "var(--background)"
      },
      "[data-rmiz-modal-overlay=\"hidden\"]": {
        "background-color": "transparent"
      },
      "[data-rmiz-modal-content]": {
        "position": "relative",
        "width": "100%",
        "height": "100%"
      },
      "[data-rmiz-modal]::backdrop": {
        "display": "none"
      },
      "[data-rmiz-modal-img]": {
        "position": "absolute",
        "transform-origin": "0 0",
        "cursor": "zoom-out",
        "image-rendering": "high-quality",
        "transition": "transform 0.3s"
      },
      "@media (prefers-reduced-motion: reduce)": {
        "[data-rmiz-modal-overlay], [data-rmiz-modal-img]": {
          "transition-duration": "0.01ms !important"
        }
      },
      "::view-transition-old(fd-lightbox), ::view-transition-new(fd-lightbox)": {
        "height": "100%",
        "object-fit": "cover"
      }
    }
  },
  "type": "registry:component"
}