{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "image-cropper",
  "title": "Image Cropper",
  "description": "A crop, zoom, rotate and flip dialog that outputs a processed image file.",
  "dependencies": [
    "@uploadcn/core@^0.1.0",
    "@uploadcn/react@^0.1.0",
    "cn",
    "react-easy-crop"
  ],
  "registryDependencies": [
    "@uploadcn/upload"
  ],
  "files": [
    {
      "path": "registry/default/ui/image-cropper.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport type { ImageTransformOptions } from \"@uploadcn/core/image\"\nimport { useFilePreview, useImageCrop } from \"@uploadcn/react\"\nimport { cn } from \"cn\"\nimport Cropper from \"react-easy-crop\"\n\nimport { IconPlaceholder } from \"@/components/icon-placeholder\"\nimport {\n  NativeButton,\n  UploadModal,\n  UploadModalDescription,\n  UploadModalFooter,\n  UploadModalHeader,\n  UploadModalTitle,\n  UploadSpinner,\n} from \"@/registry/default/ui/upload\"\n\ninterface ImageCropperProps {\n  /** The image to crop. The dialog opens while a file is set. */\n  file: File | null\n  /** Called with the cropped image. */\n  onCrop: (file: File) => void\n  /** Called when the user dismisses the dialog without cropping. */\n  onCancel: () => void\n  /** Width / height. Default `1`. */\n  aspect?: number\n  cropShape?: \"rect\" | \"round\"\n  /** Output size and encoding for the cropped image. */\n  output?: Omit<\n    ImageTransformOptions,\n    \"crop\" | \"rotate\" | \"flipHorizontal\" | \"flipVertical\"\n  >\n  title?: React.ReactNode\n  description?: React.ReactNode\n  className?: string\n}\n\nfunction ImageCropper({\n  file,\n  onCrop,\n  onCancel,\n  aspect = 1,\n  cropShape = \"rect\",\n  output,\n  title = \"Crop image\",\n  description = \"Drag to reposition. Use the slider, scroll or pinch to zoom.\",\n  className,\n}: ImageCropperProps) {\n  const url = useFilePreview(file)\n  const crop = useImageCrop({ minZoom: 1, maxZoom: 4 })\n  const [isApplying, setIsApplying] = React.useState(false)\n  const { reset } = crop\n\n  React.useEffect(() => {\n    if (file) reset()\n  }, [file, reset])\n\n  async function apply() {\n    if (!file) return\n    setIsApplying(true)\n    try {\n      onCrop(\n        await crop.apply(file, { maxWidth: 2048, maxHeight: 2048, ...output })\n      )\n    } finally {\n      setIsApplying(false)\n    }\n  }\n\n  return (\n    <UploadModal\n      open={file !== null}\n      onOpenChange={(open) => {\n        if (!open && !isApplying) onCancel()\n      }}\n      data-slot=\"image-cropper\"\n      className={cn(\"sm:max-w-lg\", className)}\n    >\n      <UploadModalHeader>\n        <UploadModalTitle>{title}</UploadModalTitle>\n        <UploadModalDescription>{description}</UploadModalDescription>\n      </UploadModalHeader>\n      <div className=\"relative h-72 overflow-hidden rounded-lg bg-muted\">\n        {url ? (\n          <Cropper\n            image={url}\n            crop={crop.position}\n            zoom={crop.zoom}\n            rotation={crop.rotation}\n            aspect={aspect}\n            cropShape={cropShape}\n            minZoom={crop.minZoom}\n            maxZoom={crop.maxZoom}\n            showGrid={cropShape === \"rect\"}\n            transform={`translate(${crop.position.x}px, ${crop.position.y}px) rotate(${crop.rotation}deg) scale(${crop.zoom * (crop.flipHorizontal ? -1 : 1)}, ${crop.zoom * (crop.flipVertical ? -1 : 1)})`}\n            onCropChange={crop.setPosition}\n            onZoomChange={crop.setZoom}\n            onRotationChange={crop.setRotation}\n            onCropComplete={(_, pixels) => crop.setArea(pixels)}\n            cropperProps={{\n              \"aria-label\": \"Crop area. Use arrow keys to move.\",\n            }}\n          />\n        ) : (\n          <div className=\"flex size-full items-center justify-center\">\n            <UploadSpinner />\n          </div>\n        )}\n      </div>\n      <div className=\"flex items-center gap-3\">\n        <IconPlaceholder\n          lucide=\"ZoomOutIcon\"\n          tabler=\"IconZoomOut\"\n          hugeicons=\"ZoomOutAreaIcon\"\n          phosphor=\"MagnifyingGlassMinusIcon\"\n          remixicon=\"RiSearchEyeLine\"\n          aria-hidden\n          className=\"size-4 text-muted-foreground\"\n        />\n        {/* A native range input is fully accessible with Radix and Base UI alike. */}\n        <input\n          type=\"range\"\n          aria-label=\"Zoom\"\n          min={crop.minZoom}\n          max={crop.maxZoom}\n          step={0.01}\n          value={crop.zoom}\n          onChange={(event) => crop.setZoom(Number(event.target.value))}\n          className=\"h-1.5 w-full cursor-pointer accent-primary\"\n        />\n        <IconPlaceholder\n          lucide=\"ZoomInIcon\"\n          tabler=\"IconZoomIn\"\n          hugeicons=\"ZoomInAreaIcon\"\n          phosphor=\"MagnifyingGlassPlusIcon\"\n          remixicon=\"RiZoomInLine\"\n          aria-hidden\n          className=\"size-4 text-muted-foreground\"\n        />\n      </div>\n      <UploadModalFooter className=\"sm:justify-between\">\n        <div className=\"flex gap-1\">\n          <NativeButton\n            variant=\"outline\"\n            size=\"icon-sm\"\n            aria-label=\"Rotate left\"\n            onClick={() => crop.rotateBy(-90)}\n          >\n            <IconPlaceholder\n              lucide=\"RotateCcwIcon\"\n              tabler=\"IconRotate\"\n              hugeicons=\"ArrowTurnBackwardIcon\"\n              phosphor=\"ArrowCounterClockwiseIcon\"\n              remixicon=\"RiArrowGoBackLine\"\n            />\n          </NativeButton>\n          <NativeButton\n            variant=\"outline\"\n            size=\"icon-sm\"\n            aria-label=\"Rotate right\"\n            onClick={() => crop.rotateBy(90)}\n          >\n            <IconPlaceholder\n              lucide=\"RotateCwIcon\"\n              tabler=\"IconRotateClockwise2\"\n              hugeicons=\"Rotate01Icon\"\n              phosphor=\"ArrowClockwiseIcon\"\n              remixicon=\"RiRefreshLine\"\n            />\n          </NativeButton>\n          <NativeButton\n            variant=\"outline\"\n            size=\"icon-sm\"\n            aria-label=\"Flip horizontally\"\n            aria-pressed={crop.flipHorizontal}\n            onClick={() => crop.flip(\"horizontal\")}\n          >\n            <IconPlaceholder\n              lucide=\"FlipHorizontal2Icon\"\n              tabler=\"IconFlipVertical\"\n              hugeicons=\"FlipHorizontalIcon\"\n              phosphor=\"FlipHorizontalIcon\"\n              remixicon=\"RiFlipHorizontalLine\"\n            />\n          </NativeButton>\n        </div>\n        <div className=\"flex gap-2\">\n          <NativeButton\n            variant=\"outline\"\n            onClick={onCancel}\n            disabled={isApplying}\n          >\n            Cancel\n          </NativeButton>\n          <NativeButton onClick={apply} disabled={isApplying || !crop.area}>\n            {isApplying ? <UploadSpinner data-icon=\"inline-start\" /> : null}\n            Apply\n          </NativeButton>\n        </div>\n      </UploadModalFooter>\n    </UploadModal>\n  )\n}\n\nexport { ImageCropper, type ImageCropperProps }\n",
      "type": "registry:ui"
    }
  ],
  "categories": [
    "upload",
    "image"
  ],
  "type": "registry:ui"
}