{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "file-upload1",
  "title": "File upload",
  "description": "An upload area that accepts files through drag and drop or the file browser. Every file upload variant works with both Radix UI and Base UI setups.",
  "dependencies": [
    "lucide-react@0"
  ],
  "registryDependencies": [
    "button"
  ],
  "files": [
    {
      "path": "contents/components/file-upload/1/index.tsx",
      "content": "\"use client\";\n\nimport { ImageUpIcon, XIcon } from \"lucide-react\";\n\nimport { Button } from \"@/components/ui/button\";\nimport { useFileUpload } from \"@/hooks/use-file-upload\";\nimport { cn } from \"@/lib/utils\";\n\nexport default function Component() {\n  const maxSizeMB = 5;\n  const maxSize = maxSizeMB * 1024 * 1024;\n\n  const [\n    { files, isDragging, errors },\n    {\n      handleDragEnter,\n      handleDragLeave,\n      handleDragOver,\n      handleDrop,\n      openFileDialog,\n      removeFile,\n      getInputProps\n    }\n  ] = useFileUpload({\n    accept: \"image/*\",\n    maxSize\n  });\n\n  const previewUrl = files[0]?.preview || null;\n\n  return (\n    <div className=\"w-full max-w-lg space-y-2\">\n      <div\n        role=\"button\"\n        tabIndex={0}\n        onClick={openFileDialog}\n        onKeyDown={(event) => {\n          if (event.key === \"Enter\" || event.key === \" \") {\n            event.preventDefault();\n            openFileDialog();\n          }\n        }}\n        onDragEnter={handleDragEnter}\n        onDragLeave={handleDragLeave}\n        onDragOver={handleDragOver}\n        onDrop={handleDrop}\n        data-dragging={isDragging || undefined}\n        className={cn(\n          \"border-input hover:bg-accent/40 data-[dragging=true]:bg-accent/40 focus-visible:border-ring focus-visible:ring-ring/50 relative flex min-h-56 w-full flex-col items-center justify-center rounded-xl border border-dashed p-6 transition-colors outline-none focus-visible:ring-[3px]\"\n        )}\n        aria-label={previewUrl ? \"Change image\" : \"Upload image\"}>\n        <input {...getInputProps()} className=\"sr-only\" aria-label=\"Upload image file\" />\n\n        {previewUrl ? (\n          <img\n            src={previewUrl}\n            alt={files[0]?.file?.name || \"Uploaded image\"}\n            className=\"mx-auto max-h-32 rounded-lg object-contain\"\n          />\n        ) : (\n          <div className=\"flex flex-col items-center gap-4 text-center\">\n            <div className=\"border-input text-muted-foreground flex size-14 items-center justify-center rounded-full border\">\n              <ImageUpIcon className=\"size-4\" />\n            </div>\n            <div className=\"space-y-1 text-sm\">\n              <p className=\"font-medium\">Drop your image here or click to browse</p>\n              <p className=\"text-muted-foreground \">Max size: {maxSizeMB}MB</p>\n            </div>\n          </div>\n        )}\n\n        {previewUrl && (\n          <Button\n            type=\"button\"\n            size=\"icon\"\n            variant=\"secondary\"\n            className=\"absolute end-3 top-3 size-7 rounded-full\"\n            onClick={(event) => {\n              event.stopPropagation();\n              removeFile(files[0]?.id);\n            }}\n            aria-label=\"Remove image\">\n            <XIcon className=\"size-4\" />\n          </Button>\n        )}\n      </div>\n\n      {errors.length > 0 && (\n        <p className=\"text-destructive text-sm\" role=\"alert\">\n          {errors[0]}\n        </p>\n      )}\n    </div>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/file-upload1.tsx"
    },
    {
      "path": "hooks/use-file-upload.ts",
      "content": "\"use client\";\n\nimport {\n  type ChangeEvent,\n  type DragEvent,\n  type InputHTMLAttributes,\n  useCallback,\n  useRef,\n  useState\n} from \"react\";\n\nexport type FileMetadata = {\n  name: string;\n  size: number;\n  type: string;\n  url: string;\n  id: string;\n};\n\nexport type FileWithPreview = {\n  file: File | FileMetadata;\n  id: string;\n  preview?: string;\n};\n\nexport type FileUploadOptions = {\n  maxFiles?: number; // Only used when multiple is true, defaults to Infinity\n  maxSize?: number; // in bytes\n  accept?: string;\n  multiple?: boolean; // Defaults to false\n  initialFiles?: FileMetadata[];\n  onFilesChange?: (files: FileWithPreview[]) => void; // Callback when files change\n  onFilesAdded?: (addedFiles: FileWithPreview[]) => void; // Callback when new files are added\n};\n\nexport type FileUploadState = {\n  files: FileWithPreview[];\n  isDragging: boolean;\n  errors: string[];\n};\n\nexport type FileUploadActions = {\n  addFiles: (files: FileList | File[]) => void;\n  removeFile: (id: string) => void;\n  clearFiles: () => void;\n  clearErrors: () => void;\n  handleDragEnter: (e: DragEvent<HTMLElement>) => void;\n  handleDragLeave: (e: DragEvent<HTMLElement>) => void;\n  handleDragOver: (e: DragEvent<HTMLElement>) => void;\n  handleDrop: (e: DragEvent<HTMLElement>) => void;\n  handleFileChange: (e: ChangeEvent<HTMLInputElement>) => void;\n  openFileDialog: () => void;\n  getInputProps: (\n    props?: InputHTMLAttributes<HTMLInputElement>\n  ) => InputHTMLAttributes<HTMLInputElement> & {\n    // Use `any` here to avoid cross-React ref type conflicts across packages\n    // biome-ignore lint/suspicious/noExplicitAny: intentional\n    ref: any;\n  };\n};\n\nexport const useFileUpload = (\n  options: FileUploadOptions = {}\n): [FileUploadState, FileUploadActions] => {\n  const {\n    maxFiles = Number.POSITIVE_INFINITY,\n    maxSize = Number.POSITIVE_INFINITY,\n    accept = \"*\",\n    multiple = false,\n    initialFiles = [],\n    onFilesChange,\n    onFilesAdded\n  } = options;\n\n  const [state, setState] = useState<FileUploadState>({\n    errors: [],\n    files: initialFiles.map((file) => ({\n      file,\n      id: file.id,\n      preview: file.url\n    })),\n    isDragging: false\n  });\n\n  const inputRef = useRef<HTMLInputElement>(null);\n\n  const validateFile = useCallback(\n    (file: File | FileMetadata): string | null => {\n      if (file instanceof File) {\n        if (file.size > maxSize) {\n          return `File \"${file.name}\" exceeds the maximum size of ${formatBytes(maxSize)}.`;\n        }\n      } else {\n        if (file.size > maxSize) {\n          return `File \"${file.name}\" exceeds the maximum size of ${formatBytes(maxSize)}.`;\n        }\n      }\n\n      if (accept !== \"*\") {\n        const acceptedTypes = accept.split(\",\").map((type) => type.trim());\n        const fileType = file instanceof File ? file.type || \"\" : file.type;\n        const fileExtension = `.${file instanceof File ? file.name.split(\".\").pop() : file.name.split(\".\").pop()}`;\n\n        const isAccepted = acceptedTypes.some((type) => {\n          if (type.startsWith(\".\")) {\n            return fileExtension.toLowerCase() === type.toLowerCase();\n          }\n          if (type.endsWith(\"/*\")) {\n            const baseType = type.split(\"/\")[0];\n            return fileType.startsWith(`${baseType}/`);\n          }\n          return fileType === type;\n        });\n\n        if (!isAccepted) {\n          return `File \"${file instanceof File ? file.name : file.name}\" is not an accepted file type.`;\n        }\n      }\n\n      return null;\n    },\n    [accept, maxSize]\n  );\n\n  const createPreview = useCallback((file: File | FileMetadata): string | undefined => {\n    if (file instanceof File) {\n      return URL.createObjectURL(file);\n    }\n    return file.url;\n  }, []);\n\n  const generateUniqueId = useCallback((file: File | FileMetadata): string => {\n    if (file instanceof File) {\n      return `${file.name}-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;\n    }\n    return file.id;\n  }, []);\n\n  const clearFiles = useCallback(() => {\n    setState((prev) => {\n      // Clean up object URLs\n      for (const file of prev.files ?? []) {\n        if (file.preview && file.file instanceof File && file.file.type.startsWith(\"image/\")) {\n          URL.revokeObjectURL(file.preview);\n        }\n      }\n\n      if (inputRef.current) {\n        inputRef.current.value = \"\";\n      }\n\n      const newState = {\n        ...prev,\n        errors: [],\n        files: []\n      };\n\n      onFilesChange?.(newState.files);\n      return newState;\n    });\n  }, [onFilesChange]);\n\n  const addFiles = useCallback(\n    (newFiles: FileList | File[]) => {\n      if (!newFiles || newFiles.length === 0) return;\n\n      const newFilesArray = Array.from(newFiles);\n      const errors: string[] = [];\n\n      // Clear existing errors when new files are uploaded\n      setState((prev) => ({ ...prev, errors: [] }));\n\n      // In single file mode, clear existing files first\n      if (!multiple) {\n        clearFiles();\n      }\n\n      // Check if adding these files would exceed maxFiles (only in multiple mode)\n      if (\n        multiple &&\n        maxFiles !== Number.POSITIVE_INFINITY &&\n        state.files.length + newFilesArray.length > maxFiles\n      ) {\n        errors.push(`You can only upload a maximum of ${maxFiles} files.`);\n        setState((prev) => ({ ...prev, errors }));\n        return;\n      }\n\n      const validFiles: FileWithPreview[] = [];\n\n      for (const file of newFilesArray) {\n        if (multiple) {\n          const isDuplicate = state.files.some(\n            (existingFile) =>\n              existingFile.file.name === file.name && existingFile.file.size === file.size\n          );\n\n          if (isDuplicate) {\n            continue;\n          }\n        }\n\n        if (file.size > maxSize) {\n          errors.push(\n            multiple\n              ? `Some files exceed the maximum size of ${formatBytes(maxSize)}.`\n              : `File exceeds the maximum size of ${formatBytes(maxSize)}.`\n          );\n          continue;\n        }\n\n        const error = validateFile(file);\n\n        if (error) {\n          errors.push(error);\n          continue;\n        }\n\n        validFiles.push({\n          file,\n          id: generateUniqueId(file),\n          preview: createPreview(file)\n        });\n      }\n\n      // Only update state if we have valid files to add\n      if (validFiles.length > 0) {\n        // Call the onFilesAdded callback with the newly added valid files\n        onFilesAdded?.(validFiles);\n\n        setState((prev) => {\n          const newFiles = !multiple ? validFiles : [...prev.files, ...validFiles];\n          onFilesChange?.(newFiles);\n          return {\n            ...prev,\n            errors,\n            files: newFiles\n          };\n        });\n      } else if (errors.length > 0) {\n        setState((prev) => ({\n          ...prev,\n          errors\n        }));\n      }\n\n      // Reset input value after handling files\n      if (inputRef.current) {\n        inputRef.current.value = \"\";\n      }\n    },\n    [\n      state.files,\n      maxFiles,\n      multiple,\n      maxSize,\n      validateFile,\n      createPreview,\n      generateUniqueId,\n      clearFiles,\n      onFilesChange,\n      onFilesAdded\n    ]\n  );\n\n  const removeFile = useCallback(\n    (id: string) => {\n      setState((prev) => {\n        const fileToRemove = prev.files.find((file) => file.id === id);\n        if (\n          fileToRemove?.preview &&\n          fileToRemove.file instanceof File &&\n          fileToRemove.file.type.startsWith(\"image/\")\n        ) {\n          URL.revokeObjectURL(fileToRemove.preview);\n        }\n\n        const newFiles = prev.files.filter((file) => file.id !== id);\n        onFilesChange?.(newFiles);\n\n        return {\n          ...prev,\n          errors: [],\n          files: newFiles\n        };\n      });\n    },\n    [onFilesChange]\n  );\n\n  const clearErrors = useCallback(() => {\n    setState((prev) => ({\n      ...prev,\n      errors: []\n    }));\n  }, []);\n\n  const handleDragEnter = useCallback((e: DragEvent<HTMLElement>) => {\n    e.preventDefault();\n    e.stopPropagation();\n    setState((prev) => ({ ...prev, isDragging: true }));\n  }, []);\n\n  const handleDragLeave = useCallback((e: DragEvent<HTMLElement>) => {\n    e.preventDefault();\n    e.stopPropagation();\n\n    if (e.currentTarget.contains(e.relatedTarget as Node)) {\n      return;\n    }\n\n    setState((prev) => ({ ...prev, isDragging: false }));\n  }, []);\n\n  const handleDragOver = useCallback((e: DragEvent<HTMLElement>) => {\n    e.preventDefault();\n    e.stopPropagation();\n  }, []);\n\n  const handleDrop = useCallback(\n    (e: DragEvent<HTMLElement>) => {\n      e.preventDefault();\n      e.stopPropagation();\n      setState((prev) => ({ ...prev, isDragging: false }));\n\n      // Don't process files if the input is disabled\n      if (inputRef.current?.disabled) {\n        return;\n      }\n\n      if (e.dataTransfer.files && e.dataTransfer.files.length > 0) {\n        // In single file mode, only use the first file\n        if (!multiple) {\n          const file = e.dataTransfer.files[0];\n          addFiles([file]);\n        } else {\n          addFiles(e.dataTransfer.files);\n        }\n      }\n    },\n    [addFiles, multiple]\n  );\n\n  const handleFileChange = useCallback(\n    (e: ChangeEvent<HTMLInputElement>) => {\n      if (e.target.files && e.target.files.length > 0) {\n        addFiles(e.target.files);\n      }\n    },\n    [addFiles]\n  );\n\n  const openFileDialog = useCallback(() => {\n    if (inputRef.current) {\n      inputRef.current.click();\n    }\n  }, []);\n\n  const getInputProps = useCallback(\n    (props: InputHTMLAttributes<HTMLInputElement> = {}) => {\n      return {\n        ...props,\n        accept: props.accept || accept,\n        multiple: props.multiple !== undefined ? props.multiple : multiple,\n        onChange: handleFileChange,\n        // Cast to `any` to prevent mismatched React ref type errors across workspaces\n        // biome-ignore lint/suspicious/noExplicitAny: Intentional\n        ref: inputRef as any,\n        type: \"file\" as const\n      };\n    },\n    [accept, multiple, handleFileChange]\n  );\n\n  return [\n    state,\n    {\n      addFiles,\n      clearErrors,\n      clearFiles,\n      getInputProps,\n      handleDragEnter,\n      handleDragLeave,\n      handleDragOver,\n      handleDrop,\n      handleFileChange,\n      openFileDialog,\n      removeFile\n    }\n  ];\n};\n\n// Helper function to format bytes to human-readable format\nexport const formatBytes = (bytes: number, decimals = 2): string => {\n  if (bytes === 0) return \"0 Bytes\";\n\n  const k = 1024;\n  const dm = decimals < 0 ? 0 : decimals;\n  const sizes = [\"Bytes\", \"KB\", \"MB\", \"GB\", \"TB\", \"PB\", \"EB\", \"ZB\", \"YB\"];\n\n  const i = Math.floor(Math.log(bytes) / Math.log(k));\n\n  return Number.parseFloat((bytes / k ** i).toFixed(dm)) + sizes[i];\n};\n",
      "type": "registry:hook",
      "target": "hooks/use-file-upload.ts"
    }
  ],
  "type": "registry:component"
}