{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "image-compare",
  "title": "Image Compare",
  "description": "Astro before-and-after pictures with a pointer and keyboard divider.",
  "dependencies": [
    "@lucide/astro",
    "cn"
  ],
  "registryDependencies": [],
  "files": [
    {
      "path": "packages/ui/src/components/ui/image-compare/image-compare-handle.astro",
      "content": "---\nimport type { HTMLAttributes } from \"astro/types\"\n\nimport { cn } from \"cn\"\nimport GripVerticalIcon from \"@lucide/astro/icons/grip-vertical\"\n\n/**\n * Pointer drag surface and divider between the two pictures.\n * Div attributes such as `class` pass through.\n * The default slot replaces the grip icon.\n * Keyboard input stays on the range control in `ImageCompare`.\n */\ntype Props = HTMLAttributes<\"div\">\n\nconst { class: className, ...props } = Astro.props\n---\n\n<div\n  data-slot=\"image-compare-handle\"\n  aria-hidden=\"true\"\n  class={cn(\n    \"pointer-events-auto absolute inset-y-0 left-(--image-compare-position,50%) z-30 flex w-8 -translate-x-1/2 cursor-grab touch-none items-center justify-center\",\n    \"group-data-[dragging=true]/image-compare:cursor-grabbing\",\n    className,\n  )}\n  {...props}\n>\n  <span\n    class=\"block w-1 h-full bg-background absolute inset-0 mx-auto\"\n    aria-hidden=\"true\"\n    data-slot=\"image-compare-handle-line\"\n  />\n  <span\n    class=\"absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 rounded-full bg-background px-0.5 py-2 [&_svg:not([class*='size-'])]:size-4\"\n    title=\"Slide to compare images\"\n  >\n    <slot>\n      <GripVerticalIcon class=\"text-foreground/72 group-hover/image-compare:text-foreground\" />\n    </slot>\n  </span>\n</div>\n",
      "type": "registry:file",
      "target": "~/src/components/ui/image-compare/image-compare-handle.astro"
    },
    {
      "path": "packages/ui/src/components/ui/image-compare/image-compare-image.astro",
      "content": "---\nimport type { HTMLAttributes } from \"astro/types\"\n\nimport { cn } from \"cn\"\nimport type { ImageCompareImageProps } from \"./types.ts\"\n\n/** Image attributes pass through. `src`, `alt`, and `clip` come from `ImageCompareImageProps`. */\ntype Props = Omit<HTMLAttributes<\"img\">, \"src\" | \"alt\"> & ImageCompareImageProps\n\nconst {\n  class: className,\n  src,\n  alt,\n  width,\n  height,\n  clip = false,\n  draggable = false,\n  ...props\n} = Astro.props\n\n/**\n * An imported picture carries its own pixel size.\n * A plain address uses the 16:9 fallback unless the caller sets a size.\n * `draggable` is the string \"true\" or \"false\". A boolean false would omit the attribute.\n */\nconst resolvedSrc = typeof src === \"string\" ? src : src.src\nconst resolvedWidth = width ?? (typeof src === \"string\" ? 1200 : src.width)\nconst resolvedHeight = height ?? (typeof src === \"string\" ? 675 : src.height)\nconst imageDraggable =\n  draggable === true || draggable === \"true\" ? \"true\" : \"false\"\n---\n\n<img\n  data-slot=\"image-compare-image\"\n  data-clip={clip ? \"true\" : undefined}\n  src={resolvedSrc}\n  alt={alt}\n  width={resolvedWidth}\n  height={resolvedHeight}\n  draggable={imageDraggable}\n  class={cn(\n    \"pointer-events-none absolute inset-0 size-full object-cover\",\n    clip ? \"image-compare-clip z-10\" : \"z-0\",\n    className,\n  )}\n  {...props}\n/>\n\n<style>\n  /* Reveal the left portion. The root sets --image-compare-position. */\n  .image-compare-clip {\n    clip-path: inset(0 calc(100% - var(--image-compare-position, 50%)) 0 0);\n  }\n</style>\n",
      "type": "registry:file",
      "target": "~/src/components/ui/image-compare/image-compare-image.astro"
    },
    {
      "path": "packages/ui/src/components/ui/image-compare/image-compare.astro",
      "content": "---\nimport type { HTMLAttributes } from \"astro/types\"\n\nimport { cn } from \"cn\"\nimport type { ImageCompareProps } from \"./types.ts\"\n\n/**\n * Comparison frame.\n * Place two `ImageCompareImage` parts and one `ImageCompareHandle` in the slot.\n * Div attributes pass through. `defaultValue` and `label` come from `ImageCompareProps`.\n */\ntype Props = HTMLAttributes<\"div\"> & ImageCompareProps\n\nconst {\n  class: className,\n  defaultValue = 50,\n  label = \"Amount of the clipped image shown\",\n  style,\n  ...props\n} = Astro.props\n\n/**\n * Keep the divider inside the frame.\n * A non-numeric value falls back to the center.\n */\nconst position = Number.isFinite(defaultValue)\n  ? Math.min(100, Math.max(0, defaultValue))\n  : 50\n\n/** String form so a value of 0 stays on the range control. */\nconst positionValue = String(position)\nconst positionStyle = `--image-compare-position:${positionValue}%`\nconst mergedStyle =\n  typeof style === \"string\"\n    ? `${style};${positionStyle}`\n    : { ...style, \"--image-compare-position\": `${positionValue}%` }\n---\n\n<div\n  data-slot=\"image-compare\"\n  data-dragging=\"false\"\n  style={mergedStyle}\n  class={cn(\n    \"group/image-compare relative block aspect-video w-full touch-pan-y overflow-hidden rounded-2xl bg-background select-none\",\n    className,\n  )}\n  {...props}\n>\n  <slot />\n  <input\n    data-slot=\"image-compare-input\"\n    type=\"range\"\n    min={0}\n    max={100}\n    step={0.01}\n    value={positionValue}\n    aria-label={label}\n    class=\"pointer-events-none sr-only\"\n  />\n</div>\n\n<script>\n  import { type ImageCompareController, create } from \"./image-compare.ts\"\n\n  const controllers: ImageCompareController[] = []\n\n  const initialize = () => controllers.push(...create())\n\n  initialize()\n  document.addEventListener(\"astro:page-load\", initialize)\n  document.addEventListener(\"astro:before-swap\", () => {\n    for (const controller of controllers) controller.destroy()\n    controllers.length = 0\n  })\n</script>\n",
      "type": "registry:file",
      "target": "~/src/components/ui/image-compare/image-compare.astro"
    },
    {
      "path": "packages/ui/src/components/ui/image-compare/image-compare.ts",
      "content": "const rootSelector = '[data-slot=\"image-compare\"]'\nconst controllers = new WeakMap<HTMLElement, ImageCompareController>()\n\nexport interface ImageCompareController {\n    destroy(): void\n    updatePosition(): void\n    startDrag(event: PointerEvent): void\n    endDrag(event: PointerEvent): void\n    moveDrag(event: PointerEvent): void\n}\n\n/** Bind each unbound comparison in the scope. */\nexport function create(scope: ParentNode = document): ImageCompareController[] {\n    const roots = [...scope.querySelectorAll<HTMLElement>(rootSelector)]\n    if (scope instanceof HTMLElement && scope.matches(rootSelector))\n        roots.unshift(scope)\n    return roots\n        .filter((root) => !controllers.has(root))\n        .map(createImageCompare)\n}\n\n/** Bind one comparison. Pointer input starts only on its own handle. */\nexport function createImageCompare(root: HTMLElement): ImageCompareController {\n    const existing = controllers.get(root)\n    if (existing) return existing\n\n    const ownedPart = <T extends HTMLElement>(selector: string) =>\n        [...root.querySelectorAll<T>(selector)].find((part) => {\n            const owner = part.closest(rootSelector)\n            return !owner || owner === root || !root.contains(owner)\n        })\n    const input = ownedPart<HTMLInputElement>(\n        '[data-slot=\"image-compare-input\"]',\n    )\n    const handle = ownedPart<HTMLElement>('[data-slot=\"image-compare-handle\"]')\n    if (!input || !handle)\n        throw new Error('ImageCompare requires an input and a handle.')\n\n    let destroyed = false\n    let pointerId: number | null = null\n    let startX = 0\n    let startValue = 50\n\n    const updatePosition = () => {\n        if (destroyed) return\n        const parsed = Number(input.value)\n        const value = Number.isFinite(parsed)\n            ? Math.min(100, Math.max(0, parsed))\n            : 50\n        input.value = String(value)\n        root.style.setProperty('--image-compare-position', `${value}%`)\n    }\n\n    const resetDrag = () => {\n        const id = pointerId\n        pointerId = null\n        root.dataset.dragging = 'false'\n        if (id !== null && handle.hasPointerCapture(id))\n            handle.releasePointerCapture(id)\n    }\n\n    const startDrag = (event: PointerEvent) => {\n        if (\n            destroyed ||\n            event.defaultPrevented ||\n            event.button !== 0 ||\n            !event.isPrimary ||\n            pointerId !== null\n        )\n            return\n        // Keep a click or a press off-center on the handle from moving the image.\n        startX = event.clientX\n        startValue = Number(input.value)\n        pointerId = event.pointerId\n        handle.setPointerCapture(pointerId)\n        root.dataset.dragging = 'true'\n        event.preventDefault()\n        input.focus({ preventScroll: true })\n    }\n\n    const endDrag = (event: PointerEvent) => {\n        if (event.pointerId === pointerId) resetDrag()\n    }\n\n    const moveDrag = (event: PointerEvent) => {\n        if (\n            destroyed ||\n            event.pointerId !== pointerId ||\n            root.clientWidth === 0\n        )\n            return\n        const delta = ((event.clientX - startX) / root.clientWidth) * 100\n        const value = Math.min(\n            100,\n            Math.max(0, Math.round((startValue + delta) * 100) / 100),\n        )\n        if (value === Number(input.value)) return\n        input.value = String(value)\n        input.dispatchEvent(new Event('input', { bubbles: true }))\n    }\n\n    const events = new AbortController()\n    const { signal } = events\n    input.addEventListener('input', updatePosition, { signal })\n    handle.addEventListener('pointerdown', startDrag, { signal })\n    handle.addEventListener('pointermove', moveDrag, { signal })\n    handle.addEventListener('pointerup', endDrag, { signal })\n    handle.addEventListener('pointercancel', endDrag, { signal })\n    handle.addEventListener('lostpointercapture', endDrag, { signal })\n    root.dataset.dragging = 'false'\n    updatePosition()\n\n    const controller: ImageCompareController = {\n        updatePosition,\n        startDrag,\n        endDrag,\n        moveDrag,\n        destroy() {\n            if (destroyed) return\n            destroyed = true\n            events.abort()\n            resetDrag()\n            controllers.delete(root)\n        },\n    }\n    controllers.set(root, controller)\n    return controller\n}\n",
      "type": "registry:file",
      "target": "~/src/components/ui/image-compare/image-compare.ts"
    },
    {
      "path": "packages/ui/src/components/ui/image-compare/index.ts",
      "content": "export { default as ImageCompareHandle } from './image-compare-handle.astro'\nexport { default as ImageCompareImage } from './image-compare-image.astro'\nexport { default as ImageCompare } from './image-compare.astro'\n\nexport type { ImageCompareImageProps, ImageCompareProps } from './types.ts'\n",
      "type": "registry:file",
      "target": "~/src/components/ui/image-compare/index.ts"
    },
    {
      "path": "packages/ui/src/components/ui/image-compare/types.ts",
      "content": "import type { ImageMetadata } from 'astro'\n\n/**\n * Props for the comparison frame.\n * Div attributes such as `class` and `id` pass through.\n * Put both pictures and the handle in the default slot.\n */\nexport interface ImageCompareProps {\n    /**\n     * Divider position at first render, from 0 to 100.\n     * `0` hides the clipped picture. `100` covers the frame with it.\n     * @default 50\n     */\n    defaultValue?: number\n    /**\n     * Accessible name for the range control.\n     * @default \"Amount of the clipped image shown\"\n     */\n    label?: string\n}\n\n/**\n * Props for one picture in a comparison.\n * Image attributes such as `class`, `width`, `height`, and `loading` pass through.\n */\nexport interface ImageCompareImageProps {\n    /**\n     * Picture file or address.\n     */\n    src: ImageMetadata | string\n    /**\n     * Text alternative for this picture.\n     */\n    alt: string\n    /**\n     * Show only the part of this picture to the left of the divider.\n     * Set this on one picture. Leave it unset on the picture that fills the frame.\n     * @default false\n     */\n    clip?: boolean\n}\n",
      "type": "registry:file",
      "target": "~/src/components/ui/image-compare/types.ts"
    }
  ],
  "meta": {
    "framework": "astro",
    "source": "@gnostic/ui",
    "testedDependencies": {
      "@lucide/astro": "^1.68.0",
      "cn": "0.5.2"
    }
  },
  "docs": "https://gnosticai.com/ui/components/image-compare",
  "categories": [
    "ui"
  ],
  "type": "registry:ui"
}
