{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "comp-564",
  "type": "registry:component",
  "title": "Comp 564",
  "description": "Comp 564",
  "files": [
    {
      "path": "registry/ui-basic/comp-564.tsx",
      "content": "\"use client\";\r\n\r\nimport { useCallback, useEffect, useState } from \"react\";\r\n\r\nimport { Button } from \"@/components/ui/button\";\r\nimport {\r\n\tCropper,\r\n\tCropperCropArea,\r\n\tCropperDescription,\r\n\tCropperImage,\r\n} from \"@/components/ui/cropper\";\r\n\r\n// Define type for pixel crop area\r\ntype Area = { x: number; y: number; width: number; height: number };\r\n\r\n// --- Start: Copied Helper Functions ---\r\nconst createImage = (url: string): Promise<HTMLImageElement> =>\r\n\tnew Promise((resolve, reject) => {\r\n\t\tconst image = new Image();\r\n\t\timage.addEventListener(\"load\", () => resolve(image));\r\n\t\timage.addEventListener(\"error\", (error) => reject(error));\r\n\t\timage.setAttribute(\"crossOrigin\", \"anonymous\"); // Needed for canvas Tainted check\r\n\t\timage.src = url;\r\n\t});\r\n\r\nasync function getCroppedImg(\r\n\timageSrc: string,\r\n\tpixelCrop: Area,\r\n\toutputWidth: number = pixelCrop.width, // Optional: specify output size\r\n\toutputHeight: number = pixelCrop.height\r\n): Promise<Blob | null> {\r\n\ttry {\r\n\t\tconst image = await createImage(imageSrc);\r\n\t\tconst canvas = document.createElement(\"canvas\");\r\n\t\tconst ctx = canvas.getContext(\"2d\");\r\n\r\n\t\tif (!ctx) {\r\n\t\t\treturn null;\r\n\t\t}\r\n\r\n\t\t// Set canvas size to desired output size\r\n\t\tcanvas.width = outputWidth;\r\n\t\tcanvas.height = outputHeight;\r\n\r\n\t\t// Draw the cropped image onto the canvas\r\n\t\tctx.drawImage(\r\n\t\t\timage,\r\n\t\t\tpixelCrop.x,\r\n\t\t\tpixelCrop.y,\r\n\t\t\tpixelCrop.width,\r\n\t\t\tpixelCrop.height,\r\n\t\t\t0,\r\n\t\t\t0,\r\n\t\t\toutputWidth, // Draw onto the output size\r\n\t\t\toutputHeight\r\n\t\t);\r\n\r\n\t\t// Convert canvas to blob\r\n\t\treturn new Promise((resolve) => {\r\n\t\t\tcanvas.toBlob((blob) => {\r\n\t\t\t\tresolve(blob);\r\n\t\t\t}, \"image/jpeg\"); // Specify format and quality if needed\r\n\t\t});\r\n\t} catch (error) {\r\n\t\tconsole.error(\"Error in getCroppedImg:\", error);\r\n\t\treturn null;\r\n\t}\r\n}\r\n// --- End: Copied Helper Functions ---\r\n\r\nconst ORIGINAL_IMAGE_URL =\r\n\t\"https://raw.githubusercontent.com/origin-space/origin-images/refs/heads/main/cropper-10_k24zxk.jpg\";\r\n\r\nexport default function Component() {\r\n\tconst [croppedAreaPixels, setCroppedAreaPixels] = useState<Area | null>(\r\n\t\tnull\r\n\t);\r\n\tconst [croppedImageUrl, setCroppedImageUrl] = useState<string | null>(null);\r\n\r\n\t// Callback to update crop area state\r\n\tconst handleCropChange = useCallback((pixels: Area | null) => {\r\n\t\tsetCroppedAreaPixels(pixels);\r\n\t}, []);\r\n\r\n\t// Function to handle the crop button click\r\n\tconst handleCrop = async () => {\r\n\t\tif (!croppedAreaPixels) {\r\n\t\t\tconsole.error(\"No crop area selected.\");\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\ttry {\r\n\t\t\tconst croppedBlob = await getCroppedImg(\r\n\t\t\t\tORIGINAL_IMAGE_URL,\r\n\t\t\t\tcroppedAreaPixels\r\n\t\t\t);\r\n\t\t\tif (!croppedBlob) {\r\n\t\t\t\tthrow new Error(\"Failed to generate cropped image blob.\");\r\n\t\t\t}\r\n\r\n\t\t\t// Create a new object URL\r\n\t\t\tconst newCroppedUrl = URL.createObjectURL(croppedBlob);\r\n\r\n\t\t\t// Revoke the old URL if it exists\r\n\t\t\tif (croppedImageUrl) {\r\n\t\t\t\tURL.revokeObjectURL(croppedImageUrl);\r\n\t\t\t}\r\n\r\n\t\t\t// Set the new URL\r\n\t\t\tsetCroppedImageUrl(newCroppedUrl);\r\n\t\t} catch (error) {\r\n\t\t\tconsole.error(\"Error during cropping:\", error);\r\n\t\t\t// Optionally: Clear the old image URL on error\r\n\t\t\tif (croppedImageUrl) {\r\n\t\t\t\tURL.revokeObjectURL(croppedImageUrl);\r\n\t\t\t}\r\n\t\t\tsetCroppedImageUrl(null);\r\n\t\t}\r\n\t};\r\n\r\n\t// Effect for cleaning up the object URL\r\n\tuseEffect(() => {\r\n\t\t// This is the cleanup function that runs when the component unmounts\r\n\t\t// or when croppedImageUrl changes before the next effect runs.\r\n\t\tconst currentUrl = croppedImageUrl;\r\n\t\treturn () => {\r\n\t\t\tif (currentUrl && currentUrl.startsWith(\"blob:\")) {\r\n\t\t\t\tURL.revokeObjectURL(currentUrl);\r\n\t\t\t\tconsole.log(\"Revoked URL:\", currentUrl); // Optional: for debugging\r\n\t\t\t}\r\n\t\t};\r\n\t}, [croppedImageUrl]); // Dependency array ensures cleanup runs when URL changes\r\n\r\n\treturn (\r\n\t\t<div className=\"flex flex-col items-center gap-2\">\r\n\t\t\t<div className=\"flex w-full flex-col gap-4 md:flex-row\">\r\n\t\t\t\t<Cropper\r\n\t\t\t\t\tclassName=\"h-64 md:flex-1\"\r\n\t\t\t\t\timage={ORIGINAL_IMAGE_URL}\r\n\t\t\t\t\tonCropChange={handleCropChange}\r\n\t\t\t\t>\r\n\t\t\t\t\t<CropperDescription />\r\n\t\t\t\t\t<CropperImage />\r\n\t\t\t\t\t<CropperCropArea />\r\n\t\t\t\t</Cropper>\r\n\t\t\t\t<div className=\"flex w-26 flex-col gap-4\">\r\n\t\t\t\t\t<Button onClick={handleCrop} disabled={!croppedAreaPixels}>\r\n\t\t\t\t\t\tCrop preview\r\n\t\t\t\t\t</Button>\r\n\t\t\t\t\t{/* Display Area */}\r\n\t\t\t\t\t<div className=\"aspect-square w-full shrink-0 overflow-hidden rounded-lg border\">\r\n\t\t\t\t\t\t{croppedImageUrl ? (\r\n\t\t\t\t\t\t\t<img\r\n\t\t\t\t\t\t\t\tsrc={croppedImageUrl}\r\n\t\t\t\t\t\t\t\talt=\"Cropped result\"\r\n\t\t\t\t\t\t\t\tclassName=\"h-full w-full object-cover\"\r\n\t\t\t\t\t\t\t/>\r\n\t\t\t\t\t\t) : (\r\n\t\t\t\t\t\t\t<div className=\"bg-muted text-muted-foreground/80 flex h-full w-full items-center justify-center p-2 text-center text-xs\">\r\n\t\t\t\t\t\t\t\tImage preview\r\n\t\t\t\t\t\t\t</div>\r\n\t\t\t\t\t\t)}\r\n\t\t\t\t\t</div>\r\n\t\t\t\t</div>\r\n\t\t\t</div>\r\n\r\n\t\t\t<p\r\n\t\t\t\taria-live=\"polite\"\r\n\t\t\t\trole=\"region\"\r\n\t\t\t\tclassName=\"text-muted-foreground mt-2 text-xs\"\r\n\t\t\t>\r\n\t\t\t\tCropper with image preview ∙{\" \"}\r\n\t\t\t\t<a\r\n\t\t\t\t\thref=\"https://github.com/origin-space/image-cropper\"\r\n\t\t\t\t\tclassName=\"hover:text-foreground underline\"\r\n\t\t\t\t\ttarget=\"_blank\"\r\n\t\t\t\t\trel=\"noopener noreferrer\"\r\n\t\t\t\t>\r\n\t\t\t\t\tAPI\r\n\t\t\t\t</a>\r\n\t\t\t</p>\r\n\t\t</div>\r\n\t);\r\n}\r\n",
      "type": "registry:ui"
    },
    {
      "path": "components/ui/button.tsx",
      "content": "import * as React from \"react\";\r\n\r\nimport { cn } from \"@/registry/utilities/cn\";\r\nimport { Slot } from \"@radix-ui/react-slot\";\r\nimport { cva, type VariantProps } from \"class-variance-authority\";\r\n\r\nconst buttonVariants = cva(\r\n\t\"inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm text-white hover:text-gray-400 font-medium ring-offset-background transition-colors focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50\",\r\n\t{\r\n\t\tvariants: {\r\n\t\t\tvariant: {\r\n\t\t\t\tsimple:\r\n\t\t\t\t\t\"bg-secondary relative z-10 bg-transparent hover:border-secondary hover:bg-secondary/50  border border-transparent dark:text-white text-sm md:text-sm transition font-medium duration-200  rounded-md px-4 py-2  flex items-center justify-center\",\r\n\t\t\t\tdefault: \"bg-primary text-primary-foreground hover:bg-primary/90\",\r\n\t\t\t\tdestructive:\r\n\t\t\t\t\t\"bg-destructive text-destructive-foreground hover:bg-destructive/90\",\r\n\t\t\t\toutline:\r\n\t\t\t\t\t\"border border-input bg-background hover:bg-accent hover:text-accent-foreground\",\r\n\t\t\t\tsecondary:\r\n\t\t\t\t\t\"bg-secondary text-secondary-foreground hover:bg-secondary/80\",\r\n\t\t\t\tghost: \"hover:bg-accent hover:text-black hover:stroke-black dark:text-white text-black\",\r\n\t\t\t\tlink: \"text-primary underline-offset-4 hover:underline\",\r\n\t\t\t},\r\n\t\t\tsize: {\r\n\t\t\t\tdefault: \"h-10 px-4 py-2\",\r\n\t\t\t\tsm: \"h-9 rounded-md px-3\",\r\n\t\t\t\tlg: \"h-11 rounded-md px-8\",\r\n\t\t\t\ticon: \"h-10 w-10\",\r\n\t\t\t},\r\n\t\t},\r\n\t\tdefaultVariants: {\r\n\t\t\tvariant: \"default\",\r\n\t\t\tsize: \"default\",\r\n\t\t},\r\n\t}\r\n);\r\n\r\nexport interface ButtonProps\r\n\textends React.ButtonHTMLAttributes<HTMLButtonElement>,\r\n\t\tVariantProps<typeof buttonVariants> {\r\n\tasChild?: boolean;\r\n}\r\n\r\nconst Button = React.forwardRef<HTMLButtonElement, ButtonProps>(\r\n\t({ className, variant, size, asChild = false, ...props }, ref) => {\r\n\t\tconst Comp = asChild ? Slot : \"button\";\r\n\t\treturn (\r\n\t\t\t<Comp\r\n\t\t\t\tclassName={cn(buttonVariants({ variant, size, className }))}\r\n\t\t\t\tref={ref}\r\n\t\t\t\t{...props}\r\n\t\t\t/>\r\n\t\t);\r\n\t}\r\n);\r\nButton.displayName = \"Button\";\r\n\r\nexport { Button, buttonVariants };\r\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/utilities/cn.ts",
      "content": "import { ClassValue, clsx } from \"clsx\";\r\nimport { twMerge } from \"tailwind-merge\";\r\n\r\nexport function cn(...inputs: ClassValue[]) {\r\n\treturn twMerge(clsx(inputs));\r\n}\r\n",
      "type": "registry:ui"
    }
  ]
}