{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "pinch-zoom",
  "title": "Pinch Zoom",
  "description": "A zoom and pan container for maps, diagrams and photos: pinch, double tap, Ctrl or Cmd plus wheel, drag, +/-/0 keys and on-screen buttons, with bounds so content never leaves the box.",
  "dependencies": [
    "@use-gesture/react",
    "lucide-react"
  ],
  "files": [
    {
      "path": "registry/tealmac/pinch-zoom.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { useGesture } from \"@use-gesture/react\"\nimport { MinusIcon, PlusIcon, RotateCcwIcon } from \"lucide-react\"\n\nimport { cn } from \"@/lib/utils\"\n\nexport interface PinchZoomHandle {\n  zoomIn: () => void\n  zoomOut: () => void\n  reset: () => void\n  /** Current scale, 1 = fit. */\n  scale: () => number\n}\n\nexport interface PinchZoomProps extends Omit<\n  React.ComponentProps<\"div\">,\n  \"onChange\"\n> {\n  minScale?: number\n  maxScale?: number\n  /** Scale a double tap or double click zooms to. Default 2.5. */\n  doubleTapScale?: number\n  onScaleChange?: (scale: number) => void\n  /** Show zoom out, reset and zoom in buttons. Default true. */\n  controls?: boolean\n  /** Accessible name of the zoomable area. */\n  label: string\n}\n\ninterface View {\n  x: number\n  y: number\n  s: number\n}\n\nfunction clamp(n: number, lo: number, hi: number) {\n  return Math.min(hi, Math.max(lo, n))\n}\n\n/**\n * A container whose content can be pinched, double-tapped, wheeled\n * (Ctrl or Cmd + wheel), dragged and keyboard zoomed (+, -, 0 and the arrow\n * keys). Zoom is around the fingers; dragging is kept inside the content.\n * Single-finger drags only pan when zoomed in, so the page still scrolls and\n * parents can still handle swipes at scale 1 (see `onScaleChange`).\n */\nexport const PinchZoom = React.forwardRef<PinchZoomHandle, PinchZoomProps>(\n  function PinchZoom(\n    {\n      minScale = 1,\n      maxScale = 5,\n      doubleTapScale = 2.5,\n      onScaleChange,\n      controls = true,\n      label,\n      className,\n      children,\n      ...props\n    },\n    ref\n  ) {\n    const boxRef = React.useRef<HTMLDivElement>(null)\n    const [view, setView] = React.useState<View>({ x: 0, y: 0, s: 1 })\n    const [active, setActive] = React.useState(false)\n    const viewRef = React.useRef(view)\n\n    const apply = React.useCallback(\n      (next: View) => {\n        const box = boxRef.current\n        const s = clamp(next.s, minScale, maxScale)\n        let { x, y } = next\n        if (box) {\n          // Keep the content covering the box.\n          const maxX = (box.clientWidth * (s - 1)) / 2\n          const maxY = (box.clientHeight * (s - 1)) / 2\n          x = clamp(x, -maxX, maxX)\n          y = clamp(y, -maxY, maxY)\n        }\n        if (s === 1) {\n          x = 0\n          y = 0\n        }\n        const out = { x, y, s }\n        viewRef.current = out\n        setView(out)\n        return out\n      },\n      [minScale, maxScale]\n    )\n\n    // Report the scale after it changes.\n    const reported = React.useRef(1)\n    React.useEffect(() => {\n      if (reported.current !== view.s) {\n        reported.current = view.s\n        onScaleChange?.(view.s)\n      }\n    }, [view.s, onScaleChange])\n\n    /** Zoom to `s`, keeping the point (px, py), relative to the box centre, still. */\n    const zoomAt = React.useCallback(\n      (s: number, px = 0, py = 0) => {\n        const cur = viewRef.current\n        const target = clamp(s, minScale, maxScale)\n        const ratio = target / cur.s\n        apply({\n          s: target,\n          x: px - (px - cur.x) * ratio,\n          y: py - (py - cur.y) * ratio,\n        })\n      },\n      [apply, minScale, maxScale]\n    )\n\n    React.useImperativeHandle(\n      ref,\n      () => ({\n        zoomIn: () => zoomAt(viewRef.current.s * 1.5),\n        zoomOut: () => zoomAt(viewRef.current.s / 1.5),\n        reset: () => apply({ x: 0, y: 0, s: 1 }),\n        scale: () => viewRef.current.s,\n      }),\n      [apply, zoomAt]\n    )\n\n    const centre = (clientX: number, clientY: number) => {\n      const rect = boxRef.current?.getBoundingClientRect()\n      if (!rect) return [0, 0]\n      return [\n        clientX - (rect.left + rect.width / 2),\n        clientY - (rect.top + rect.height / 2),\n      ]\n    }\n\n    // Double tap and double click: two quick presses that barely move.\n    const lastTap = React.useRef({ time: 0, x: 0, y: 0 })\n    const downAt = React.useRef({ time: 0, x: 0, y: 0 })\n    const onPointerDown = (event: React.PointerEvent) => {\n      downAt.current = { time: Date.now(), x: event.clientX, y: event.clientY }\n    }\n    const onPointerUp = (event: React.PointerEvent) => {\n      const down = downAt.current\n      const quick = Date.now() - down.time < 300\n      const still =\n        Math.hypot(event.clientX - down.x, event.clientY - down.y) < 8\n      if (!quick || !still) return\n      const prev = lastTap.current\n      const now = Date.now()\n      if (\n        now - prev.time < 300 &&\n        Math.hypot(event.clientX - prev.x, event.clientY - prev.y) < 30\n      ) {\n        const [px, py] = centre(event.clientX, event.clientY)\n        if (viewRef.current.s > 1) apply({ x: 0, y: 0, s: 1 })\n        else zoomAt(doubleTapScale, px, py)\n        lastTap.current = { time: 0, x: 0, y: 0 }\n      } else lastTap.current = { time: now, x: event.clientX, y: event.clientY }\n    }\n\n    const bind = useGesture(\n      {\n        onPinchStart: () => setActive(true),\n        onPinchEnd: () => setActive(false),\n        onPinch: ({ offset: [scale], origin: [ox, oy], first, memo }) => {\n          const [px, py] = centre(ox, oy)\n          const start = first ? viewRef.current : (memo as View)\n          const ratio = scale / start.s\n          apply({\n            s: scale,\n            x: px - (px - start.x) * ratio,\n            y: py - (py - start.y) * ratio,\n          })\n          return start\n        },\n        onDragStart: () => setActive(true),\n        onDragEnd: () => setActive(false),\n        onDrag: ({\n          pinching,\n          cancel,\n          offset: [dx, dy],\n          first,\n          memo,\n          event,\n        }) => {\n          if (event.type.startsWith(\"key\")) return\n          if (pinching) return cancel()\n          if (viewRef.current.s <= 1) return\n          const start = first ? viewRef.current : (memo as View)\n          apply({ ...viewRef.current, x: start.x + dx, y: start.y + dy })\n          return start\n        },\n        onWheel: ({ event, delta: [, dy], ctrlKey, metaKey }) => {\n          if (!ctrlKey && !metaKey) return\n          event.preventDefault()\n          const e = event as WheelEvent\n          const [px, py] = centre(e.clientX, e.clientY)\n          zoomAt(viewRef.current.s * Math.exp(-dy / 200), px, py)\n        },\n      },\n      {\n        drag: {\n          from: () => [0, 0],\n          pointer: { touch: true, capture: false },\n          threshold: 4,\n        },\n        pinch: {\n          scaleBounds: { min: minScale, max: maxScale },\n          from: () => [viewRef.current.s, 0],\n          eventOptions: { passive: false },\n        },\n        wheel: { eventOptions: { passive: false } },\n      }\n    )\n\n    const onKeyDown = (event: React.KeyboardEvent) => {\n      const step = 40\n      const cur = viewRef.current\n      switch (event.key) {\n        case \"+\":\n        case \"=\":\n          zoomAt(cur.s * 1.5)\n          break\n        case \"-\":\n        case \"_\":\n          zoomAt(cur.s / 1.5)\n          break\n        case \"0\":\n          apply({ x: 0, y: 0, s: 1 })\n          break\n        case \"ArrowLeft\":\n          if (cur.s <= 1) return\n          apply({ ...cur, x: cur.x + step })\n          break\n        case \"ArrowRight\":\n          if (cur.s <= 1) return\n          apply({ ...cur, x: cur.x - step })\n          break\n        case \"ArrowUp\":\n          if (cur.s <= 1) return\n          apply({ ...cur, y: cur.y + step })\n          break\n        case \"ArrowDown\":\n          if (cur.s <= 1) return\n          apply({ ...cur, y: cur.y - step })\n          break\n        default:\n          return\n      }\n      event.preventDefault()\n    }\n\n    const pct = Math.round(view.s * 100)\n\n    return (\n      <div\n        data-slot=\"pinch-zoom\"\n        className={cn(\"relative overflow-hidden\", className)}\n        {...props}\n      >\n        {/* The focusable zoom area handles + - 0 and arrows itself. */}\n        {/* eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions */}\n        <div\n          ref={boxRef}\n          role=\"group\"\n          aria-roledescription=\"zoomable area\"\n          aria-label={`${label}, ${pct}%. Press plus and minus to zoom, zero to reset.`}\n          // eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex\n          tabIndex={0}\n          {...bind()}\n          onPointerDownCapture={onPointerDown}\n          onPointerUpCapture={onPointerUp}\n          onKeyDown={onKeyDown}\n          className=\"focus-visible:ring-ring flex size-full touch-none items-center justify-center outline-none select-none focus-visible:ring-2 focus-visible:ring-inset\"\n        >\n          <div\n            data-active={active || undefined}\n            style={{\n              transform: `translate(${view.x}px, ${view.y}px) scale(${view.s})`,\n            }}\n            className={cn(\n              \"origin-center will-change-transform\",\n              !active &&\n                \"transition-transform duration-200 motion-reduce:transition-none\"\n            )}\n          >\n            {children}\n          </div>\n        </div>\n        {controls && (\n          <div className=\"bg-background/80 absolute end-2 bottom-2 flex items-center rounded-full border p-0.5 shadow backdrop-blur\">\n            <button\n              type=\"button\"\n              aria-label=\"Zoom out\"\n              disabled={view.s <= minScale}\n              onClick={() => zoomAt(view.s / 1.5)}\n              className=\"focus-visible:ring-ring grid size-9 place-items-center rounded-full outline-none focus-visible:ring-2 disabled:opacity-40\"\n            >\n              <MinusIcon className=\"size-4\" />\n            </button>\n            <button\n              type=\"button\"\n              aria-label=\"Reset zoom\"\n              disabled={view.s === 1}\n              onClick={() => apply({ x: 0, y: 0, s: 1 })}\n              className=\"focus-visible:ring-ring grid size-9 place-items-center rounded-full outline-none focus-visible:ring-2 disabled:opacity-40\"\n            >\n              <RotateCcwIcon className=\"size-4\" />\n            </button>\n            <button\n              type=\"button\"\n              aria-label=\"Zoom in\"\n              disabled={view.s >= maxScale}\n              onClick={() => zoomAt(view.s * 1.5)}\n              className=\"focus-visible:ring-ring grid size-9 place-items-center rounded-full outline-none focus-visible:ring-2 disabled:opacity-40\"\n            >\n              <PlusIcon className=\"size-4\" />\n            </button>\n          </div>\n        )}\n      </div>\n    )\n  }\n)\n",
      "type": "registry:ui"
    }
  ],
  "type": "registry:ui"
}