{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "image-crop-field",
  "title": "Image crop field",
  "author": "pqoqubbw <https://zriz.dev>",
  "description": "A form field that crops the chosen image in place and submits the crop as a file.",
  "dependencies": [
    "@zriz/react@^0.2.0",
    "@zriz/core@^0.2.0"
  ],
  "registryDependencies": [
    "button",
    "https://zriz.dev/r/cropper.json"
  ],
  "files": [
    {
      "path": "registry/new-york/blocks/image-crop-field.tsx",
      "content": "\"use client\";\n\nimport type { Cropper as CropperApi, Size } from \"@zriz/react\";\nimport { useEffect, useId, useRef, useState } from \"react\";\nimport type { DragEvent } from \"react\";\n\nimport { cn } from \"@/lib/utils\";\nimport { Button } from \"@/registry/new-york/ui/button\";\nimport {\n  Cropper,\n  CropperViewport,\n  CropperZoom,\n  CropperZoomIn,\n  CropperZoomOut,\n  cropToFile,\n} from \"@/registry/new-york/ui/cropper\";\n\ntype ImageCropFieldProps = {\n  aspect?: number;\n  className?: string;\n  defaultImage?: Blob | string | null;\n  description?: string;\n  label: string;\n  maxSize?: Size;\n  name: string;\n};\n\nconst WIDE = 16 / 9;\n\nconst MAX_SIZE: Size = { height: 1600, width: 1600 };\n\nconst ImageCropField = ({\n  aspect = WIDE,\n  className,\n  defaultImage = null,\n  description = \"The crop you see is the file the form sends.\",\n  label,\n  maxSize = MAX_SIZE,\n  name,\n}: ImageCropFieldProps) => {\n  const [image, setImage] = useState<Blob | string | null>(defaultImage);\n  const [picks, setPicks] = useState(0);\n  const [failed, setFailed] = useState(false);\n  const [dropping, setDropping] = useState(false);\n  const actions = useRef<CropperApi>(null);\n  const field = useRef<HTMLInputElement>(null);\n  const picker = useRef<HTMLInputElement>(null);\n  const running = useRef<AbortController | null>(null);\n  const id = useId();\n\n  useEffect(() => {\n    const form = field.current?.form;\n\n    const reset = (): void => {\n      running.current?.abort();\n      setFailed(false);\n      setImage(defaultImage);\n      setPicks((count) => count + 1);\n    };\n\n    form?.addEventListener(\"reset\", reset);\n\n    return () => {\n      form?.removeEventListener(\"reset\", reset);\n      running.current?.abort();\n    };\n  }, [defaultImage]);\n\n  const fill = (files: File[]): void => {\n    if (!field.current) {\n      return;\n    }\n\n    const transfer = new DataTransfer();\n\n    for (const file of files) {\n      transfer.items.add(file);\n    }\n\n    field.current.files = transfer.files;\n  };\n\n  const sync = async (): Promise<void> => {\n    if (!actions.current) {\n      return;\n    }\n\n    running.current?.abort();\n    const controller = new AbortController();\n\n    running.current = controller;\n    const file = await cropToFile(actions.current, {\n      maxSize,\n      name,\n      quality: 0.9,\n      signal: controller.signal,\n      type: \"image/jpeg\",\n    }).catch(() => null);\n\n    if (controller.signal.aborted) {\n      return;\n    }\n\n    setFailed(file === null);\n\n    if (file !== null) {\n      fill([file]);\n    }\n  };\n\n  const open = (file: File | undefined): void => {\n    if (file) {\n      setFailed(false);\n      setImage(file);\n      setPicks((count) => count + 1);\n    }\n  };\n\n  const remove = (): void => {\n    running.current?.abort();\n    fill([]);\n    setFailed(false);\n    setImage(null);\n    picker.current?.focus();\n  };\n\n  const drop = (event: DragEvent): void => {\n    event.preventDefault();\n    setDropping(false);\n    open(event.dataTransfer.files[0]);\n  };\n\n  return (\n    <div\n      className={cn(\"flex w-full flex-col gap-2\", className)}\n      data-slot=\"image-crop-field\"\n    >\n      <label className=\"text-sm leading-none font-medium\" htmlFor={id}>\n        {label}\n      </label>\n      <input hidden name={name} ref={field} type=\"file\" />\n      <input\n        accept=\"image/jpeg,image/png,image/webp\"\n        className=\"peer sr-only\"\n        id={id}\n        onChange={(event) => {\n          open(event.currentTarget.files?.[0]);\n          event.currentTarget.value = \"\";\n        }}\n        ref={picker}\n        tabIndex={image === null ? 0 : -1}\n        type=\"file\"\n      />\n      {image === null ? (\n        <button\n          className=\"flex w-full flex-col items-center justify-center gap-1 rounded-lg border border-dashed border-input px-4 text-sm text-muted-foreground outline-none peer-focus-visible:border-ring peer-focus-visible:ring-3 peer-focus-visible:ring-ring/50 hover:bg-muted/50 data-dropping:border-ring data-dropping:bg-muted/50\"\n          data-dropping={dropping ? \"\" : undefined}\n          onClick={() => {\n            picker.current?.click();\n          }}\n          onDragLeave={() => {\n            setDropping(false);\n          }}\n          onDragOver={(event) => {\n            event.preventDefault();\n            setDropping(true);\n          }}\n          onDrop={drop}\n          style={{ aspectRatio: aspect }}\n          tabIndex={-1}\n          type=\"button\"\n        >\n          <span className=\"font-medium text-foreground\">Choose an image</span>\n          <span className=\"text-xs\">or drop it here</span>\n        </button>\n      ) : (\n        <Cropper\n          actionsRef={actions}\n          aspect={aspect}\n          className=\"[--zriz-frame:transparent]\"\n          key={picks}\n          maxZoom={4}\n          onError={() => {\n            setFailed(true);\n          }}\n          onLoad={() => {\n            void sync();\n          }}\n          onValueCommit={() => {\n            void sync();\n          }}\n          src={image}\n          svg={false}\n        >\n          <CropperViewport\n            className=\"aspect-auto p-0\"\n            grid={false}\n            handles={false}\n            style={{ aspectRatio: aspect }}\n            wheel=\"none\"\n          />\n          <div className=\"flex items-center gap-1\">\n            <CropperZoomOut />\n            <CropperZoom />\n            <CropperZoomIn />\n            <Button\n              className=\"ml-2\"\n              onClick={() => {\n                picker.current?.click();\n              }}\n              size=\"sm\"\n              type=\"button\"\n              variant=\"outline\"\n            >\n              Replace\n            </Button>\n            <Button onClick={remove} size=\"sm\" type=\"button\" variant=\"ghost\">\n              Remove\n            </Button>\n          </div>\n        </Cropper>\n      )}\n      {failed ? (\n        <p className=\"text-xs text-destructive\" role=\"alert\">\n          This image did not open. Try a JPEG, PNG or WebP.\n        </p>\n      ) : (\n        <p className=\"text-xs text-muted-foreground\">{description}</p>\n      )}\n    </div>\n  );\n};\n\nexport type { ImageCropFieldProps };\nexport { ImageCropField };\n",
      "type": "registry:component"
    }
  ],
  "docs": "Needs Tailwind CSS v4, React 19 and shadcn/ui components on Radix or Base UI. Docs: https://zriz.dev/docs/shadcn",
  "categories": [
    "image",
    "cropper"
  ],
  "type": "registry:block"
}