{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "file-upload",
  "title": "File Upload",
  "description": "A drag-and-drop file dropzone with click-to-browse, type/size/count validation, image previews, animated progress bars, retry and remove actions.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/file-upload/file-upload.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { motion, AnimatePresence } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type FileUploadSize = \"sm\" | \"md\" | \"lg\";\nexport type FileStatus = \"ready\" | \"uploading\" | \"done\" | \"error\";\n\nexport interface UploadItem {\n  id: string;\n  file: File;\n  status: FileStatus;\n  progress: number;\n  error?: string;\n  rejected?: boolean;\n  previewUrl?: string;\n}\n\nexport interface FileUploadProps {\n  accept?: string;\n  multiple?: boolean;\n  maxSize?: number;\n  maxFiles?: number;\n  upload?: (file: File, onProgress: (percent: number) => void) => Promise<void>;\n  onFilesChange?: (files: File[]) => void;\n  title?: string;\n  description?: string;\n  label?: string;\n  size?: FileUploadSize;\n  theme?: \"dark\" | \"light\";\n  width?: number | string;\n  disabled?: boolean;\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: { text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.5)\", faint: \"rgba(245,244,241,0.28)\", zone: \"rgba(255,255,255,0.03)\", zoneActive: \"rgba(255,255,255,0.08)\", dash: \"rgba(255,255,255,0.2)\", dashActive: \"rgba(245,244,241,0.85)\", card: \"rgba(255,255,255,0.04)\", border: \"rgba(255,255,255,0.1)\", hover: \"rgba(255,255,255,0.08)\", chip: \"rgba(255,255,255,0.08)\", bar: \"rgba(255,255,255,0.1)\", fill: \"#F5F4F1\", focus: \"rgba(245,244,241,0.85)\", error: \"#FF7A6B\" },\n  light: { text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.5)\", faint: \"rgba(10,10,10,0.28)\", zone: \"#FFFFFF\", zoneActive: \"rgba(10,10,10,0.05)\", dash: \"rgba(10,10,10,0.25)\", dashActive: \"rgba(10,10,10,0.85)\", card: \"#FFFFFF\", border: \"rgba(10,10,10,0.12)\", hover: \"rgba(10,10,10,0.06)\", chip: \"rgba(10,10,10,0.06)\", bar: \"rgba(10,10,10,0.1)\", fill: \"#0A0A0A\", focus: \"rgba(10,10,10,0.85)\", error: \"#E5484D\" },\n};\n\nconst SIZES: Record<FileUploadSize, { font: number; pad: number; radius: number; icon: number; thumb: number }> = {\n  sm: { font: 13, pad: 22, radius: 14, icon: 38, thumb: 34 },\n  md: { font: 14.5, pad: 32, radius: 18, icon: 46, thumb: 40 },\n  lg: { font: 16, pad: 44, radius: 22, icon: 56, thumb: 48 },\n};\n\nfunction formatBytes(bytes: number) {\n  if (bytes < 1024) return `${bytes} B`;\n  const units = [\"KB\", \"MB\", \"GB\"];\n  let n = bytes / 1024;\n  let i = 0;\n  while (n >= 1024 && i < units.length - 1) {\n    n /= 1024;\n    i++;\n  }\n  return `${n >= 10 || i === 0 ? Math.round(n) : n.toFixed(1)} ${units[i]}`;\n}\n\nfunction matchesAccept(file: File, accept?: string) {\n  if (!accept) return true;\n  const rules = accept.split(\",\").map((r) => r.trim().toLowerCase()).filter(Boolean);\n  const name = file.name.toLowerCase();\n  const type = file.type.toLowerCase();\n  return rules.some((r) => (r.startsWith(\".\") ? name.endsWith(r) : r.endsWith(\"/*\") ? type.startsWith(r.slice(0, -1)) : type === r));\n}\n\nfunction extOf(name: string) {\n  const i = name.lastIndexOf(\".\");\n  return i > 0 ? name.slice(i + 1, i + 5).toUpperCase() : \"FILE\";\n}\n\nlet counter = 0;\n\nexport function FileUpload({\n  accept,\n  multiple = true,\n  maxSize,\n  maxFiles,\n  upload,\n  onFilesChange,\n  title,\n  description,\n  label,\n  size = \"md\",\n  theme = \"dark\",\n  width = 420,\n  disabled = false,\n  className,\n}: FileUploadProps) {\n  const p = PALETTES[theme];\n  const s = SIZES[size];\n  const uid = React.useId();\n  const inputRef = React.useRef<HTMLInputElement>(null);\n  const dragDepth = React.useRef(0);\n  const [items, setItems] = React.useState<UploadItem[]>([]);\n  const [dragging, setDragging] = React.useState(false);\n  const [hovered, setHovered] = React.useState(false);\n  const itemsRef = React.useRef<UploadItem[]>([]);\n\n  React.useEffect(() => {\n    itemsRef.current = items;\n  }, [items]);\n  React.useEffect(\n    () => () => {\n      itemsRef.current.forEach((i) => i.previewUrl && URL.revokeObjectURL(i.previewUrl));\n    },\n    [],\n  );\n\n  const emit = (list: UploadItem[]) => onFilesChange?.(list.filter((i) => i.status !== \"error\").map((i) => i.file));\n\n  const patch = (id: string, change: Partial<UploadItem>) => setItems((prev) => prev.map((i) => (i.id === id ? { ...i, ...change } : i)));\n\n  const runUpload = async (item: UploadItem) => {\n    if (!upload) return;\n    patch(item.id, { status: \"uploading\", progress: 0, error: undefined });\n    try {\n      await upload(item.file, (percent) => patch(item.id, { progress: Math.max(0, Math.min(100, percent)) }));\n      patch(item.id, { status: \"done\", progress: 100 });\n    } catch (err) {\n      patch(item.id, { status: \"error\", error: err instanceof Error ? err.message : \"Upload failed\" });\n    }\n  };\n\n  const addFiles = (incoming: File[]) => {\n    if (disabled || incoming.length === 0) return;\n    const current = multiple ? items : [];\n    const room = maxFiles ? Math.max(0, maxFiles - current.filter((i) => i.status !== \"error\").length) : Infinity;\n    let accepted = 0;\n    const created: UploadItem[] = [];\n    for (const file of multiple ? incoming : incoming.slice(0, 1)) {\n      let error: string | undefined;\n      if (!matchesAccept(file, accept)) error = \"File type not allowed\";\n      else if (maxSize && file.size > maxSize) error = `Larger than ${formatBytes(maxSize)}`;\n      else if (accepted >= room) error = `Limit of ${maxFiles} files reached`;\n      if (!error) accepted++;\n      created.push({\n        id: `${uid}-${++counter}`,\n        file,\n        status: error ? \"error\" : \"ready\",\n        progress: 0,\n        error,\n        rejected: Boolean(error),\n        previewUrl: !error && file.type.startsWith(\"image/\") ? URL.createObjectURL(file) : undefined,\n      });\n    }\n    if (!multiple) current.forEach((i) => i.previewUrl && URL.revokeObjectURL(i.previewUrl));\n    const next = [...current, ...created];\n    setItems(next);\n    emit(next);\n    created.filter((i) => i.status === \"ready\").forEach(runUpload);\n  };\n\n  const remove = (id: string) => {\n    const target = items.find((i) => i.id === id);\n    if (target?.previewUrl) URL.revokeObjectURL(target.previewUrl);\n    const next = items.filter((i) => i.id !== id);\n    setItems(next);\n    emit(next);\n  };\n\n  const openPicker = () => !disabled && inputRef.current?.click();\n\n  const active = dragging && !disabled;\n\n  return (\n    <div className={cn(\"inline-flex flex-col gap-2\", className)} style={{ width, maxWidth: \"100%\", fontFamily: \"Inter, sans-serif\", opacity: disabled ? 0.5 : 1 }}>\n      {label && (\n        <span id={`${uid}-label`} className=\"font-semibold tracking-[-0.01em]\" style={{ color: p.text, fontSize: s.font }}>\n          {label}\n        </span>\n      )}\n\n      <motion.div\n        role=\"button\"\n        tabIndex={disabled ? -1 : 0}\n        aria-disabled={disabled || undefined}\n        aria-labelledby={label ? `${uid}-label` : undefined}\n        aria-label={label ? undefined : \"Upload files\"}\n        onClick={openPicker}\n        onKeyDown={(e) => {\n          if (e.key === \"Enter\" || e.key === \" \") {\n            e.preventDefault();\n            openPicker();\n          }\n        }}\n        onMouseEnter={() => setHovered(true)}\n        onMouseLeave={() => setHovered(false)}\n        onDragEnter={(e) => {\n          e.preventDefault();\n          dragDepth.current++;\n          setDragging(true);\n        }}\n        onDragOver={(e) => e.preventDefault()}\n        onDragLeave={(e) => {\n          e.preventDefault();\n          dragDepth.current = Math.max(0, dragDepth.current - 1);\n          if (dragDepth.current === 0) setDragging(false);\n        }}\n        onDrop={(e) => {\n          e.preventDefault();\n          dragDepth.current = 0;\n          setDragging(false);\n          addFiles(Array.from(e.dataTransfer.files));\n        }}\n        animate={{ scale: active ? 1.015 : 1 }}\n        transition={{ type: \"spring\", stiffness: 380, damping: 28 }}\n        className=\"flex flex-col items-center text-center outline-none focus-visible:ring-2\"\n        style={{ gap: 12, padding: `${s.pad}px 20px`, borderRadius: s.radius, border: `1.5px dashed ${active ? p.dashActive : hovered ? p.dashActive : p.dash}`, background: active ? p.zoneActive : p.zone, cursor: disabled ? \"default\" : \"pointer\", transition: \"border-color 0.18s ease, background 0.18s ease\", [\"--tw-ring-color\" as string]: p.focus }}\n      >\n        <motion.span\n          animate={{ y: active ? -5 : 0, scale: active ? 1.08 : 1 }}\n          transition={{ type: \"spring\", stiffness: 420, damping: 22 }}\n          className=\"flex items-center justify-center rounded-full\"\n          style={{ width: s.icon, height: s.icon, background: p.chip, color: p.text }}\n        >\n          <svg width={s.icon * 0.46} height={s.icon * 0.46} viewBox=\"0 0 20 20\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.7\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n            <path d=\"M10 13V3.5M6 7l4-4 4 4M3.5 13v2a1.5 1.5 0 0 0 1.5 1.5h10a1.5 1.5 0 0 0 1.5-1.5v-2\" />\n          </svg>\n        </motion.span>\n        <div className=\"flex flex-col\" style={{ gap: 4 }}>\n          <span className=\"font-semibold tracking-[-0.01em]\" style={{ color: p.text, fontSize: s.font + 1 }}>\n            {title ?? (active ? \"Drop to upload\" : multiple ? \"Drop files here or click to browse\" : \"Drop a file here or click to browse\")}\n          </span>\n          <span style={{ color: p.muted, fontSize: s.font - 1.5 }}>\n            {description ?? [accept ? accept.split(\",\").map((a) => a.trim().replace(/^.*\\//, \"\").replace(\".\", \"\").toUpperCase()).join(\", \") : \"Any file type\", maxSize ? `up to ${formatBytes(maxSize)}` : null, maxFiles && multiple ? `max ${maxFiles} files` : null].filter(Boolean).join(\" · \")}\n          </span>\n        </div>\n        <input\n          ref={inputRef}\n          type=\"file\"\n          hidden\n          tabIndex={-1}\n          accept={accept}\n          multiple={multiple}\n          disabled={disabled}\n          onChange={(e) => {\n            addFiles(Array.from(e.target.files ?? []));\n            e.target.value = \"\";\n          }}\n        />\n      </motion.div>\n\n      <ul className=\"m-0 flex list-none flex-col p-0\" style={{ gap: 8 }} aria-live=\"polite\">\n        <AnimatePresence initial={false}>\n          {items.map((item) => {\n            const isImg = Boolean(item.previewUrl);\n            const err = item.status === \"error\";\n            return (\n              <motion.li\n                key={item.id}\n                layout\n                initial={{ opacity: 0, y: 8, scale: 0.98 }}\n                animate={{ opacity: 1, y: 0, scale: 1 }}\n                exit={{ opacity: 0, scale: 0.96, transition: { duration: 0.14 } }}\n                transition={{ type: \"spring\", stiffness: 420, damping: 32 }}\n                className=\"flex items-center\"\n                style={{ gap: 12, padding: 10, borderRadius: s.radius - 4, background: p.card, border: `1px solid ${err ? p.error : p.border}` }}\n              >\n                <span className=\"relative flex shrink-0 items-center justify-center overflow-hidden font-bold\" style={{ width: s.thumb, height: s.thumb, borderRadius: s.radius - 8, background: p.chip, color: p.muted, fontSize: 10, letterSpacing: \"0.04em\" }}>\n                  {isImg ? (\n                    // eslint-disable-next-line @next/next/no-img-element\n                    <img src={item.previewUrl} alt=\"\" className=\"h-full w-full object-cover\" />\n                  ) : (\n                    extOf(item.file.name)\n                  )}\n                </span>\n                <div className=\"flex min-w-0 flex-1 flex-col\" style={{ gap: 5 }}>\n                  <div className=\"flex items-baseline justify-between\" style={{ gap: 8 }}>\n                    <span className=\"truncate font-semibold\" style={{ color: p.text, fontSize: s.font - 0.5 }}>\n                      {item.file.name}\n                    </span>\n                    <span className=\"shrink-0 tabular-nums\" style={{ color: err ? p.error : p.muted, fontSize: s.font - 2.5 }}>\n                      {err ? item.error : item.status === \"uploading\" ? `${Math.round(item.progress)}%` : item.status === \"done\" ? \"Uploaded\" : formatBytes(item.file.size)}\n                    </span>\n                  </div>\n                  {(item.status === \"uploading\" || item.status === \"done\") && (\n                    <div className=\"h-1 overflow-hidden rounded-full\" style={{ background: p.bar }} role=\"progressbar\" aria-valuemin={0} aria-valuemax={100} aria-valuenow={Math.round(item.progress)} aria-label={`Uploading ${item.file.name}`}>\n                      <motion.div className=\"h-full rounded-full\" style={{ background: p.fill }} initial={false} animate={{ width: `${item.progress}%` }} transition={{ ease: \"easeOut\", duration: 0.2 }} />\n                    </div>\n                  )}\n                </div>\n                {err && upload && !item.rejected && (\n                  <button type=\"button\" aria-label={`Retry ${item.file.name}`} onClick={() => runUpload(item)} className=\"flex shrink-0 cursor-pointer items-center justify-center rounded-lg border-none bg-transparent outline-none focus-visible:ring-2\" style={{ width: 28, height: 28, color: p.muted, [\"--tw-ring-color\" as string]: p.focus }} onMouseEnter={(e) => (e.currentTarget.style.background = p.hover)} onMouseLeave={(e) => (e.currentTarget.style.background = \"transparent\")}>\n                    <svg width=\"14\" height=\"14\" viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.6\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n                      <path d=\"M13 8a5 5 0 1 1-1.5-3.5M13 2.5V5h-2.5\" />\n                    </svg>\n                  </button>\n                )}\n                <button type=\"button\" aria-label={`Remove ${item.file.name}`} onClick={() => remove(item.id)} className=\"flex shrink-0 cursor-pointer items-center justify-center rounded-lg border-none bg-transparent outline-none focus-visible:ring-2\" style={{ width: 28, height: 28, color: p.muted, [\"--tw-ring-color\" as string]: p.focus }} onMouseEnter={(e) => (e.currentTarget.style.background = p.hover)} onMouseLeave={(e) => (e.currentTarget.style.background = \"transparent\")}>\n                  <svg width=\"12\" height=\"12\" viewBox=\"0 0 12 12\" fill=\"none\" aria-hidden=\"true\">\n                    <path d=\"M3 3L9 9M9 3L3 9\" stroke=\"currentColor\" strokeWidth=\"1.7\" strokeLinecap=\"round\" />\n                  </svg>\n                </button>\n              </motion.li>\n            );\n          })}\n        </AnimatePresence>\n      </ul>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}