{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "lightbox",
  "title": "Lightbox",
  "description": "Astro image zoom and media detail views with a dialog container.",
  "dependencies": [
    "class-variance-authority",
    "cn"
  ],
  "registryDependencies": [
    "@gnostic/dialog"
  ],
  "files": [
    {
      "path": "packages/ui/src/components/ui/lightbox/index.ts",
      "content": "// Dialog supplies the root behavior. Lightbox supplies the media layout.\nexport { default as Lightbox } from '@/components/ui/dialog/dialog.astro'\nexport { default as LightboxContent } from './lightbox-content.astro'\nexport { default as LightboxTrigger } from './lightbox-trigger.astro'\nexport { default as LightboxImage } from './lightbox-image.astro'\nexport type {\n    LightboxContentProps,\n    LightboxImageProps,\n    LightboxItemImage,\n    LightboxProps,\n    LightboxTriggerProps,\n} from './types.ts'\n",
      "type": "registry:file",
      "target": "~/src/components/ui/lightbox/index.ts"
    },
    {
      "path": "packages/ui/src/components/ui/lightbox/types.ts",
      "content": "import type { ImageMetadata } from 'astro'\nimport type { LocalImageProps, RemoteImageProps } from 'astro:assets'\n\nimport type { LightboxPopupVariantProps } from './lightbox-variants.ts'\nimport type { DialogOptions } from '@/components/ui/dialog/types.ts'\n\n/** Options for the dialog that shows the enlarged view. */\nexport type LightboxProps = Omit<DialogOptions, 'alertDialog'>\n\n/** Props for the thumbnail button. */\nexport interface LightboxTriggerProps {\n    /**\n     * Scale the preview on hover.\n     * Adds the class `hover:scale-105`.\n     *\n     * @default false\n     */\n    hoverZoom?: boolean\n}\n\n/** Props for the enlarged media view. */\nexport interface LightboxContentProps {\n    /** Accessible name, rendered as a visually hidden dialog title. */\n    label: string\n    /** Text below the media. The caption slot takes precedence. */\n    caption?: string\n    /** Classes for the media container. */\n    mediaClass?: string\n    /**\n     * `panel` centers the media in a dialog.\n     * `default` fills the screen and uses a dark backdrop.\n     *\n     * @default 'default'\n     */\n    variant?: LightboxPopupVariantProps['variant']\n    /**\n     * Show the close button.\n     *\n     * @default true\n     */\n    showCloseButton?: boolean\n    /** Extra classes for the backdrop. */\n    overlayClass?: string\n}\n\n/** Options for the image zoom. Astro Image props apply to the thumbnail. */\nexport interface LightboxImageProps {\n    /** Image to show on zoom. By default the zoom uses the thumbnail source. */\n    zoomImage?: LocalImageProps | RemoteImageProps\n}\n\ntype ImageSource =\n    | { src: ImageMetadata; width?: number; height?: number }\n    | { src: string; width: number; height: number; inferSize?: false }\n    | { src: string; inferSize: true; width?: number; height?: number }\n\n// Keep Astro's widths and densities options mutually exclusive.\ntype ViewProps<T = LocalImageProps> = T extends LocalImageProps\n    ? Omit<T, 'src' | 'alt'>\n    : never\n\n/** Shared source and per-view Astro Image props for a grid tile or carousel slide. */\nexport type LightboxItemImage = ImageSource & {\n    /** Description of the image and accessible name for its control. */\n    alt: string\n    /** Astro Image props for the thumbnail. Use CSS classes to crop the visible area. */\n    thumbnail?: ViewProps\n    /** Astro Image props for the enlarged view. */\n    content?: ViewProps\n}\n",
      "type": "registry:file",
      "target": "~/src/components/ui/lightbox/types.ts"
    },
    {
      "path": "packages/ui/src/components/ui/lightbox/lightbox-item-images.ts",
      "content": "import type { LocalImageProps, RemoteImageProps } from 'astro:assets'\n\nimport type { LightboxItemImage } from './types.ts'\n\n/** Resolve the Astro props for both views of a gallery image. */\nexport function getLightboxImages(\n    image: LightboxItemImage,\n    view: 'zoom' | 'dialog',\n) {\n    if (!image?.alt?.trim())\n        throw new Error('A lightbox image requires alt text.')\n    const { thumbnail, content, ...source } = image\n\n    function props(\n        options: typeof thumbnail,\n        widths: number[],\n        sizes: string,\n    ): LocalImageProps | RemoteImageProps {\n        if (options?.densities) return { ...source, ...options }\n        return {\n            ...source,\n            ...options,\n            widths: options?.widths ?? widths,\n            sizes: options?.sizes ?? sizes,\n            densities: undefined,\n        }\n    }\n\n    // A standalone zoom selects the larger candidates from the thumbnail's srcset.\n    const widths =\n        view === 'zoom' && !content\n            ? [320, 640, 960, 1200, 1920]\n            : [320, 640, 960]\n    return {\n        preview: props(thumbnail, widths, 'auto, 100vw'),\n        enlarged: props(\n            content,\n            [640, 1200, 1920],\n            view === 'zoom'\n                ? '90vw'\n                : '(min-width: 72rem) 70rem, calc(100vw - 4rem)',\n        ),\n    }\n}\n",
      "type": "registry:file",
      "target": "~/src/components/ui/lightbox/lightbox-item-images.ts"
    },
    {
      "path": "packages/ui/src/components/ui/lightbox/lightbox-image.astro",
      "content": "---\nimport type { ComponentProps } from \"astro/types\"\n/**\n * Astro Image component with image zoom lightbox functionality.\n */\nimport { Image } from \"astro:assets\"\nimport { cn } from \"cn\"\n\nimport type { LightboxImageProps } from \"./types.ts\"\n\ntype Props = ComponentProps<typeof Image> & LightboxImageProps\n\nconst {\n  zoomImage,\n  widths = Astro.props.densities ? undefined : [100, 250, 500, 1000, 1250],\n  class: className,\n  ...props\n} = Astro.props\n---\n\n<Image\n  {...({ ...props, widths } as ComponentProps<typeof Image>)}\n  data-slot=\"lightbox-image-trigger\"\n  role=\"button\"\n  tabindex={0}\n  aria-label={`Enlarge image: ${props.alt}`}\n  aria-expanded=\"false\"\n  class={cn(\n    \"focus-visible:outline-ring cursor-zoom-in focus-visible:outline-2 focus-visible:outline-offset-4 aria-expanded:opacity-0\",\n    className,\n  )}\n/>\n\n{zoomImage && (\n  <template data-lightbox-zoom>\n    <Image {...zoomImage} />\n  </template>\n)}\n\n<script>\n  // Image-only zoom. Dialog remains the container for captions and carousels.\n  type LightboxInstance = {\n    close: (immediate?: boolean, restoreFocus?: boolean) => void\n  }\n  const selector = '[data-slot=\"lightbox-image-trigger\"]'\n  // only one image can be zoomed at a time\n  let active: LightboxInstance | undefined\n\n  /**\n   * Open the lightbox with the given image\n   */\n  function open(image: HTMLImageElement) {\n    // check the loading status of the image before opening the lightbox\n    if (!image.complete || !image.naturalWidth) return\n    active?.close(true, false)\n\n    const rect = image.getBoundingClientRect()\n    if (!rect.width || !rect.height) return\n\n    // create the overlay element as a button so clicking outside the zoomed image closes it\n    const overlay = document.createElement(\"button\")\n    overlay.type = \"button\"\n    overlay.dataset.slot = \"lightbox-overlay\"\n    overlay.setAttribute(\"aria-label\", \"Close enlarged image\")\n    overlay.className =\n      \"fixed inset-0 z-[100] m-0 size-full cursor-zoom-out border-0 bg-transparent p-0 text-foreground focus-visible:outline-none\"\n\n    // Fade only the backdrop so the image stays opaque throughout the zoom.\n    const backdrop = document.createElement(\"span\")\n    backdrop.dataset.slot = \"lightbox-backdrop\"\n    backdrop.setAttribute(\"aria-hidden\", \"true\")\n    backdrop.className =\n      \"pointer-events-none absolute inset-0 bg-background/95 backdrop-blur-sm\"\n\n    // Do not copy thumbnail classes, IDs, or responsive layout constraints.\n    const template = image.nextElementSibling\n    const fullImage =\n      template instanceof HTMLTemplateElement &&\n      template.hasAttribute(\"data-lightbox-zoom\")\n        ? template.content.querySelector(\"img\")\n        : null\n    const zoomed = fullImage\n      ? document.importNode(fullImage, true)\n      : document.createElement(\"img\")\n    if (!fullImage) {\n      zoomed.alt = image.alt\n      zoomed.sizes = \"90vw\"\n      zoomed.srcset = image.srcset\n      zoomed.src = image.currentSrc || image.src\n    }\n    zoomed.dataset.slot = \"lightbox-image\"\n    zoomed.draggable = false\n    zoomed.classList.add(\n      \"absolute\",\n      \"m-0\",\n      \"max-h-none\",\n      \"max-w-none\",\n      \"origin-top-left\",\n      \"object-contain\",\n    )\n\n    const ratio =\n      fullImage?.width && fullImage.height\n        ? fullImage.width / fullImage.height\n        : image.naturalWidth / image.naturalHeight\n    const width = Math.min(\n      window.innerWidth * 0.9,\n      window.innerHeight * 0.9 * ratio,\n    )\n    const height = width / ratio\n    const left = (window.innerWidth - width) / 2\n    const top = (window.innerHeight - height) / 2\n\n    Object.assign(zoomed.style, {\n      width: `${width}px`,\n      height: `${height}px`,\n      left: `${left}px`,\n      top: `${top}px`,\n    })\n\n    // scale the thumbnail to the zoomed image\n    const thumbnailTransform = () => {\n      const bounds = image.getBoundingClientRect()\n      return `translate(${bounds.left - left}px, ${bounds.top - top}px) scale(${bounds.width / width}, ${bounds.height / height})`\n    }\n    const { matches: isReducedMotion } = window.matchMedia(\n      \"(prefers-reduced-motion: reduce)\",\n    )\n    const timing: KeyframeAnimationOptions = {\n      duration: isReducedMotion ? 0 : 300,\n      easing: \"cubic-bezier(0.2, 0, 0, 1)\",\n      fill: \"both\",\n    }\n\n    const events = new AbortController()\n    let closing = false\n    let removed = false\n\n    overlay.append(backdrop, zoomed)\n    document.body.append(overlay)\n    image.setAttribute(\"aria-expanded\", \"true\")\n    overlay.focus({ preventScroll: true })\n\n    let movement = zoomed.animate(\n      [{ transform: thumbnailTransform() }, { transform: \"none\" }],\n      timing,\n    )\n    let fade = backdrop.animate([{ opacity: 0 }, { opacity: 1 }], timing)\n\n    function remove(restoreFocus: boolean) {\n      if (removed) return\n      removed = true\n      events.abort()\n      movement.cancel()\n      fade.cancel()\n      overlay.remove()\n      image.setAttribute(\"aria-expanded\", \"false\")\n      if (active === instance) active = undefined\n      if (restoreFocus && image.isConnected)\n        image.focus({ preventScroll: true })\n    }\n\n    function close(immediate = false, restoreFocus = true) {\n      if (immediate) return remove(restoreFocus)\n      if (closing) return\n      closing = true\n      const transform = getComputedStyle(zoomed).transform\n      const opacity = getComputedStyle(backdrop).opacity\n      movement.cancel()\n      fade.cancel()\n      movement = zoomed.animate(\n        [{ transform }, { transform: thumbnailTransform() }],\n        timing,\n      )\n      fade = backdrop.animate([{ opacity }, { opacity: 0 }], timing)\n      void movement.finished.then(\n        () => remove(restoreFocus),\n        () => {},\n      )\n    }\n\n    const instance = { close }\n    active = instance\n    const { signal } = events\n    overlay.addEventListener(\"click\", () => close(), { signal })\n    document.addEventListener(\n      \"keydown\",\n      (event) => {\n        if (event.key === \"Escape\") {\n          event.preventDefault()\n          close()\n        } else if (event.key === \"Tab\") {\n          // Return to the thumbnail before the browser moves to the next control.\n          close(true)\n        }\n      },\n      { signal },\n    )\n    document.addEventListener(\n      \"scroll\",\n      () => {\n        // A scroll queued before opening must not close the new zoom.\n        const bounds = image.getBoundingClientRect()\n        if (bounds.top !== rect.top || bounds.left !== rect.left) close(true)\n      },\n      {\n        capture: true,\n        passive: true,\n        signal,\n      },\n    )\n    window.addEventListener(\"resize\", () => close(true), { signal })\n  }\n\n  // Delegation also covers examples inserted after the first page load.\n  document.addEventListener(\"click\", (event) => {\n    const image = event.target\n    if (image instanceof HTMLImageElement && image.matches(selector))\n      open(image)\n  })\n  document.addEventListener(\"keydown\", (event) => {\n    const image = event.target\n    if (!(image instanceof HTMLImageElement) || !image.matches(selector)) return\n    if (event.key === \"Enter\" || event.key === \" \") {\n      event.preventDefault()\n      if (!event.repeat) open(image)\n    }\n  })\n  document.addEventListener(\"astro:before-swap\", () =>\n    active?.close(true, false),\n  )\n</script>\n",
      "type": "registry:file",
      "target": "~/src/components/ui/lightbox/lightbox-image.astro"
    },
    {
      "path": "packages/ui/src/components/ui/lightbox/lightbox-content.astro",
      "content": "---\nimport type { HTMLAttributes } from \"astro/types\"\n\nimport { cn } from \"cn\"\nimport {\n  DialogClose,\n  DialogDescription,\n  DialogOverlay,\n  DialogPortal,\n  DialogTitle,\n} from \"@/components/ui/dialog\"\nimport {\n  lightboxOverlayVariants,\n  lightboxPopupVariants,\n} from \"./lightbox-variants.ts\"\nimport type { LightboxContentProps } from \"./types.ts\"\n\ntype Props = HTMLAttributes<\"div\"> & LightboxContentProps\n\nconst {\n  class: className,\n  label,\n  caption,\n  mediaClass,\n  overlayClass,\n  showCloseButton = true,\n  style: styleProp,\n  variant = \"default\",\n  ...props\n} = Astro.props\n\nif (!label?.trim()) throw new Error(\"LightboxContent requires a label.\")\n\nconst style =\n  typeof styleProp === \"string\"\n    ? `z-index: calc(var(--dialog-content-stack-index, 0) * 2 + 101); ${styleProp}`\n    : {\n        \"z-index\": \"calc(var(--dialog-content-stack-index, 0) * 2 + 101)\",\n        ...styleProp,\n      }\n---\n\n<DialogPortal>\n  <DialogOverlay\n    class={cn(lightboxOverlayVariants({ variant }), overlayClass)}\n  />\n  <div\n    data-slot=\"dialog-content\"\n    data-variant={variant}\n    hidden\n    class={cn(lightboxPopupVariants({ variant }), className)}\n    style={style}\n    {...props}\n  >\n    {showCloseButton && (\n      <DialogClose\n        variant=\"ghost\"\n        size=\"icon-sm\"\n        class={cn(\n          \"pointer-events-auto absolute top-4 right-4\",\n          variant === \"default\" && \"text-white hover:bg-white/12\",\n        )}\n      />\n    )}\n    <DialogTitle class=\"sr-only\" data-lightbox-title>\n      {label}\n    </DialogTitle>\n    <div\n      data-slot=\"lightbox-media\"\n      class={cn(\n        \"size-full *:pointer-events-auto *:mx-auto *:h-auto *:max-h-[calc(100dvh-10rem)] *:w-full *:object-contain\",\n        \"has-[>:where(img,picture,video)]:mx-auto has-[>:where(img,picture,video)]:size-fit has-[>:where(img,picture,video)]:max-w-[min(100%,90dvw)]\",\n        \"[&>:where(img,picture,video)]:w-auto [&>:where(img,picture,video)]:max-w-full\",\n        \"[&>picture]:block [&>picture>img]:h-auto [&>picture>img]:max-h-[calc(100dvh-10rem)] [&>picture>img]:w-auto [&>picture>img]:max-w-full\",\n        mediaClass,\n      )}\n    >\n      <slot />\n    </div>\n    {(caption || Astro.slots.has(\"caption\")) && (\n      <DialogDescription\n        class={cn(\n          \"pointer-events-auto shrink-0 text-center\",\n          variant === \"default\" && \"mx-auto w-fit max-w-full text-white/80\",\n        )}\n      >\n        <slot name=\"caption\">{caption}</slot>\n      </DialogDescription>\n    )}\n  </div>\n</DialogPortal>\n",
      "type": "registry:file",
      "target": "~/src/components/ui/lightbox/lightbox-content.astro"
    },
    {
      "path": "packages/ui/src/components/ui/lightbox/lightbox-trigger.astro",
      "content": "---\nimport type { HTMLAttributes } from \"astro/types\"\n\nimport { cn } from \"cn\"\nimport type { LightboxTriggerProps } from \"./types.ts\"\n\ntype Props = Omit<HTMLAttributes<\"button\">, \"type\"> & LightboxTriggerProps\n\nconst { hoverZoom = false, class: className, ...props } = Astro.props\n\n// The button clips the scale.\n// hoverZoom adds hover:scale-105 on the preview.\n---\n\n<button\n  data-slot=\"dialog-trigger\"\n  class={cn(\n    \"bg-muted grid w-full cursor-zoom-in overflow-hidden rounded-xl border-0 p-0 focus-visible:outline-2 focus-visible:outline-offset-3 focus-visible:outline-ring\",\n    className,\n  )}\n  {...props}\n  type=\"button\"\n>\n  <span\n    data-slot=\"lightbox-preview\"\n    class={cn(\n      \"min-h-0 w-full [&_img]:pointer-events-none [&_img]:block [&_img]:max-w-full\",\n      hoverZoom &&\n        \"transition-transform duration-300 hover:scale-105 ease-in-out motion-reduce:transform-none motion-reduce:transition-none\",\n    )}\n  >\n    <slot />\n  </span>\n</button>\n",
      "type": "registry:file",
      "target": "~/src/components/ui/lightbox/lightbox-trigger.astro"
    },
    {
      "path": "packages/ui/src/components/ui/lightbox/lightbox-variants.ts",
      "content": "import { cva, type VariantProps } from 'class-variance-authority'\n\n/**\n * Classes for the enlarged panel.\n * `panel` centers the media. `default` fills the screen.\n */\nexport const lightboxPopupVariants = cva(\n    'data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95 duration-100 outline-none motion-reduce:animate-none!',\n    {\n        defaultVariants: {\n            variant: 'default',\n        },\n        variants: {\n            variant: {\n                panel: 'bg-popover text-popover-foreground ring-foreground/10 @container/dialog fixed top-1/2 left-1/2 z-50 grid max-h-[calc(100dvh-2rem)] w-[calc(100%-2rem)] max-w-6xl -translate-x-1/2 -translate-y-1/2 gap-3 overflow-y-auto rounded-xl p-4 pt-14 text-sm ring-1 sm:max-w-6xl',\n                default:\n                    'pointer-events-none fixed inset-0 flex flex-col justify-center gap-4 overflow-visible bg-transparent px-3 py-16 text-white shadow-none ring-0',\n            },\n        },\n    },\n)\n\n/**\n * Extra classes for the backdrop.\n * DialogOverlay supplies the base backdrop. These classes replace its color.\n * `panel` keeps that base. `default` uses a dark backdrop.\n */\nexport const lightboxOverlayVariants = cva('', {\n    defaultVariants: {\n        variant: 'default',\n    },\n    variants: {\n        variant: {\n            panel: '',\n            default: 'bg-black/90',\n        },\n    },\n})\n\nexport type LightboxPopupVariantProps = VariantProps<\n    typeof lightboxPopupVariants\n>\nexport type LightboxOverlayVariantProps = VariantProps<\n    typeof lightboxOverlayVariants\n>\n",
      "type": "registry:file",
      "target": "~/src/components/ui/lightbox/lightbox-variants.ts"
    }
  ],
  "meta": {
    "framework": "astro",
    "source": "@gnostic/ui",
    "testedDependencies": {
      "class-variance-authority": "^0.7.1",
      "cn": "0.5.2"
    }
  },
  "docs": "https://gnosticai.com/ui/components/lightbox",
  "categories": [
    "ui"
  ],
  "type": "registry:ui"
}
