{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "dropzone",
  "title": "Dropzone",
  "type": "registry:component",
  "description": "File upload dropzone with per-file progress, retry and aggregate status",
  "dependencies": [
    "motion@^12.43.0"
  ],
  "files": [
    {
      "path": "src/kavyn/dropzone.jsx",
      "type": "registry:file",
      "target": "@components/kavyn/dropzone.jsx",
      "content": "'use client'\n\nimport { useCallback, useEffect, useRef, useState } from 'react'\nimport { AnimatePresence, motion } from 'motion/react'\n\nconst RED = '#e5484d'\n\nconst EXT_COLORS = {\n  pdf: '#e5484d',\n  png: '#c8a27a',\n  jpg: '#c8a27a',\n  jpeg: '#c8a27a',\n  gif: '#c8a27a',\n  svg: '#c8a27a',\n  xlsx: '#30a46c',\n  xls: '#30a46c',\n  csv: '#30a46c',\n  doc: '#a1a1a6',\n  docx: '#a1a1a6',\n  pptx: '#e5793c',\n  mp4: '#d6409f',\n  mov: '#d6409f',\n  mp3: '#b07fd6',\n  zip: '#8b8b96',\n  txt: '#8b8b96',\n  json: '#8b8b96',\n}\nconst extOf = (name) => (name.includes('.') ? name.split('.').pop().toLowerCase() : 'file')\nconst extColor = (ext) => EXT_COLORS[ext] || '#8b8b96'\n\nlet uid = 0\n// The unit follows the total (\"120 of 980 KB\", \"1.2 of 4.2 MB\") so a pair never mixes units.\nconst fmtSize = (b, total = b) => (total < 1e6 ? `${Math.max(b > 0 ? 1 : 0, Math.round(b / 1e3))}` : (b / 1e6).toFixed(1))\nconst unit = (total) => (total < 1e6 ? 'KB' : 'MB')\n// Deterministic per-name upload rate so the demo tells the same story every load.\nconst rateOf = (name) => {\n  let h = 0\n  for (let i = 0; i < name.length; i++) h = (h * 31 + name.charCodeAt(i)) >>> 0\n  return 0.14 + (h % 100) / 1000 // 4.5-7s per file\n}\n\nconst DEMO_FILES = [\n  { name: 'Q3-Board-Report.pdf', size: 4.2e6 },\n  { name: 'Hero-Shot.png', size: 2.6e6 },\n  { name: 'Budget-2027.xlsx', size: 1.3e6, failOnce: true },\n  { name: 'Product-Demo.mp4', size: 31.5e6 },\n]\n\nfunction DocIcon({ ext, w = 38, h = 44 }) {\n  const c = extColor(ext)\n  const label = ext === 'file' ? 'FILE' : ext.toUpperCase().slice(0, 4)\n  return (\n    <svg width={w} height={h} viewBox=\"0 0 38 44\" fill=\"none\" aria-hidden=\"true\" className=\"shrink-0\">\n      <path\n        d=\"M6 1.5h16.5L33 12v26a4.5 4.5 0 0 1-4.5 4.5H6A4.5 4.5 0 0 1 1.5 38V6A4.5 4.5 0 0 1 6 1.5Z\"\n        className=\"fill-panel2 stroke-line2\"\n      />\n      <path d=\"M22.5 1.5 33 12h-9a1.5 1.5 0 0 1-1.5-1.5V1.5Z\" className=\"fill-line stroke-line2\" />\n      <rect x=\"5.5\" y=\"29.5\" width=\"27\" height=\"11\" rx=\"3\" fill={c} fillOpacity=\"0.14\" />\n      <text x=\"19\" y=\"38\" textAnchor=\"middle\" fontSize={label.length > 3 ? '6.4' : '7.6'} fontWeight=\"800\" fill={c} style={{ letterSpacing: '0.02em' }}>\n        {label}\n      </text>\n    </svg>\n  )\n}\n\nfunction UploadGlyph() {\n  return (\n    <svg width=\"20\" height=\"20\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n      <path d=\"M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4\" />\n      <polyline points=\"17 8 12 3 7 8\" />\n      <line x1=\"12\" x2=\"12\" y1=\"3\" y2=\"15\" />\n    </svg>\n  )\n}\n\nfunction FanIcon({ active }) {\n  return (\n    <div className={`relative mb-4 h-[62px] w-[92px] transition-transform duration-300 ${active ? '-translate-y-0.5' : ''}`} aria-hidden=\"true\">\n      <div className={`absolute left-0 top-[15px] transition-all duration-300 ${active ? '-translate-x-1 -rotate-[14deg]' : '-rotate-12'}`}>\n        <DocIcon ext=\"pdf\" w={30} h={35} />\n      </div>\n      <div className={`absolute right-0 top-[15px] transition-all duration-300 ${active ? 'translate-x-1 rotate-[14deg]' : 'rotate-12'}`}>\n        <DocIcon ext=\"png\" w={30} h={35} />\n      </div>\n      <div\n        className={`absolute left-1/2 top-0 grid h-[46px] w-[46px] -translate-x-1/2 place-items-center rounded-[13px] border transition-all duration-300 ${\n          active ? 'border-tx bg-tx text-bg shadow-[0_10px_24px_rgba(0,0,0,0.35)]' : 'border-line2 bg-panel2 text-tx shadow-sm'\n        }`}\n      >\n        <UploadGlyph />\n      </div>\n    </div>\n  )\n}\n\nconst CheckIcon = () => (\n  <svg width=\"15\" height=\"15\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2.5\" strokeLinecap=\"round\" strokeLinejoin=\"round\" className=\"mt-0.5 shrink-0 text-faint\" aria-hidden=\"true\">\n    <polyline points=\"20 6 9 17 4 12\" />\n  </svg>\n)\nconst XIcon = () => (\n  <svg width=\"16\" height=\"16\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n    <path d=\"M18 6 6 18\" /><path d=\"m6 6 12 12\" />\n  </svg>\n)\nconst RetryIcon = () => (\n  <svg width=\"15\" height=\"15\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n    <path d=\"M3 12a9 9 0 0 1 9-9 9.75 9.75 0 0 1 6.74 2.74L21 8\" /><path d=\"M21 3v5h-5\" /><path d=\"M21 12a9 9 0 0 1-9 9 9.75 9.75 0 0 1-6.74-2.74L3 16\" /><path d=\"M8 16H3v5\" />\n  </svg>\n)\nconst WarnIcon = () => (\n  <svg width=\"14\" height=\"14\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n    <path d=\"m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3Z\" /><path d=\"M12 9v4\" /><path d=\"M12 17h.01\" />\n  </svg>\n)\n\n/** @param {{ maxSizeMB?: any, typesLabel?: any, autoDemo?: any, onUpload?: any, className?: any }} props */\nexport default function Dropzone({\n  maxSizeMB = 25,\n  typesLabel = undefined,\n  autoDemo = false,\n  onUpload = undefined,\n  className = '',\n}) {\n  const label = typesLabel ?? 'PDF, PNG, JPG and more'\n  const [files, setFiles] = useState([])\n  const [drag, setDrag] = useState(false)\n  const [dragCount, setDragCount] = useState(0)\n  const inputRef = useRef(null)\n  const depth = useRef(0)\n  const demoRan = useRef(false)\n  const realStarted = useRef(new Set())\n\n  const patch = (id, p) => setFiles((fs) => fs.map((f) => (f.id === id ? { ...f, ...p } : f)))\n\n  const addFiles = useCallback((list) => {\n    const items = [...list].map((src) => {\n      const name = src.name\n      const size = src.size ?? 0\n      const over = size > maxSizeMB * 1e6\n      return {\n        id: ++uid,\n        name,\n        size,\n        ext: extOf(name),\n        file: src.file || null,\n        failOnce: !!src.failOnce,\n        status: over ? 'toolarge' : 'uploading',\n        loaded: 0,\n        rate: rateOf(name),\n      }\n    })\n    if (items.length) setFiles((fs) => [...fs, ...items])\n  }, [maxSizeMB])\n\n  // Simulated upload engine. Real backends plug in via onUpload instead.\n  useEffect(() => {\n    if (onUpload) return\n    const t = setInterval(() => {\n      setFiles((fs) => {\n        if (!fs.some((f) => f.status === 'uploading')) return fs\n        return fs.map((f) => {\n          if (f.status !== 'uploading') return f\n          const loaded = Math.min(f.size, f.loaded + f.size * f.rate * 0.12)\n          // Scripted one-time failure so the retry state is reachable in demos.\n          if (f.failOnce && loaded / f.size >= 0.45) return { ...f, loaded, status: 'error', errorMsg: 'Connection lost', failOnce: false }\n          if (loaded >= f.size) return { ...f, loaded: f.size, status: 'done' }\n          return { ...f, loaded }\n        })\n      })\n    }, 120)\n    return () => clearInterval(t)\n  }, [onUpload])\n\n  useEffect(() => {\n    if (!onUpload) return\n    files.forEach((f) => {\n      if (f.status !== 'uploading' || realStarted.current.has(f.id)) return\n      realStarted.current.add(f.id)\n      Promise.resolve(onUpload(f, (loaded) => patch(f.id, { loaded: Math.min(loaded, f.size) })))\n        .then(() => patch(f.id, { status: 'done', loaded: f.size }))\n        .catch((err) => patch(f.id, { status: 'error', errorMsg: err?.message || 'Upload failed' }))\n    })\n  }, [files, onUpload])\n\n  // Showcase script: drop the reference files, one fails once and auto-retries.\n  useEffect(() => {\n    if (!autoDemo || demoRan.current) return\n    demoRan.current = true\n    const timers = []\n    DEMO_FILES.forEach((f, i) => timers.push(setTimeout(() => addFiles([f]), 700 + i * 350)))\n    timers.push(setTimeout(() => setFiles((fs) => fs.map((f) => (f.status === 'error' ? { ...f, status: 'uploading', loaded: 0 } : f))), 700 + DEMO_FILES.length * 350 + 5200))\n    return () => timers.forEach(clearTimeout)\n  }, [autoDemo, addFiles])\n\n  const retry = (id) => {\n    realStarted.current.delete(id)\n    patch(id, { status: 'uploading', loaded: 0, errorMsg: undefined })\n  }\n  const remove = (id) => setFiles((fs) => fs.filter((f) => f.id !== id))\n  const clear = () => setFiles([])\n\n  // Never let a missed drop fall through to the browser (it would open the file and leave the page).\n  useEffect(() => {\n    const stop = (e) => e.preventDefault()\n    window.addEventListener('dragover', stop)\n    window.addEventListener('drop', stop)\n    return () => { window.removeEventListener('dragover', stop); window.removeEventListener('drop', stop) }\n  }, [])\n\n  const onDragEnter = (e) => { e.preventDefault(); depth.current++; setDragCount(e.dataTransfer?.items?.length || 0); setDrag(true) }\n  const onDragOver = (e) => { e.preventDefault() }\n  const onDragLeave = (e) => { e.preventDefault(); if (--depth.current <= 0) { depth.current = 0; setDrag(false) } }\n  const onDrop = (e) => {\n    e.preventDefault(); depth.current = 0; setDrag(false)\n    addFiles([...e.dataTransfer.files].map((file) => ({ name: file.name, size: file.size, file })))\n  }\n  const pick = () => inputRef.current?.click()\n\n  const active = files.filter((f) => f.status === 'uploading')\n  const doneCount = files.filter((f) => f.status === 'done').length\n  const failed = files.some((f) => f.status === 'error' || f.status === 'toolarge')\n  const loadedSum = active.reduce((a, f) => a + f.loaded, 0)\n  const sizeSum = active.reduce((a, f) => a + f.size, 0)\n\n  return (\n    <div\n      className={`w-full max-w-xl overflow-hidden rounded-[28px] border border-line bg-panel shadow-sm ${className}`}\n      onDragEnter={onDragEnter}\n      onDragOver={onDragOver}\n      onDragLeave={onDragLeave}\n      onDrop={onDrop}\n    >\n      <div className=\"p-3\">\n        <div\n          role=\"button\"\n          tabIndex={0}\n          onClick={pick}\n          onKeyDown={(e) => (e.key === 'Enter' || e.key === ' ') && pick()}\n          className={`grid cursor-pointer place-items-center rounded-[20px] border-2 px-6 py-10 text-center outline-none transition-all duration-300 ${\n            drag ? 'border-solid border-tx bg-panel2 shadow-[0_0_0_4px_color-mix(in_oklab,var(--tx)_10%,transparent)]' : 'border-dashed border-line2 hover:border-faint/60'\n          }`}\n        >\n          <input ref={inputRef} type=\"file\" multiple className=\"hidden\" onChange={(e) => { addFiles([...e.target.files].map((file) => ({ name: file.name, size: file.size, file }))); e.target.value = '' }} />\n          <FanIcon active={drag} />\n          <div className=\"text-[17px] font-semibold text-tx\">\n            {drag ? (\n              <>Release to upload {dragCount > 0 ? `${dragCount} file${dragCount === 1 ? '' : 's'}` : 'files'}</>\n            ) : (\n              <>Drop files here or <span className=\"font-semibold underline decoration-line2 underline-offset-4\">browse</span></>\n            )}\n          </div>\n          <div className=\"mt-1.5 text-sm text-mute\">{label} · up to {maxSizeMB} MB each</div>\n        </div>\n\n        {files.length > 0 && (\n          <div className=\"divide-y divide-line\">\n            <AnimatePresence initial={false}>\n              {files.map((f) => (\n                <motion.div\n                  key={f.id}\n                  layout\n                  initial={{ opacity: 0, y: -10 }}\n                  animate={{ opacity: 1, y: 0 }}\n                  exit={{ opacity: 0, transition: { duration: 0.15 } }}\n                  transition={{ type: 'spring', stiffness: 320, damping: 28 }}\n                  className=\"flex items-center gap-3.5 px-2 py-4\"\n                >\n                  <DocIcon ext={f.ext} />\n                  <div className=\"min-w-0 flex-1\">\n                    <div className=\"flex items-center gap-1.5\">\n                      <span className=\"truncate text-[15px] font-medium text-tx\">{f.name}</span>\n                      {f.status === 'done' && <CheckIcon />}\n                    </div>\n                    <div className={`mt-0.5 text-[13px] ${f.status === 'error' || f.status === 'toolarge' ? '' : 'text-mute'}`} style={f.status === 'error' || f.status === 'toolarge' ? { color: RED } : undefined}>\n                      {f.status === 'uploading' && (\n                        <>{fmtSize(f.loaded, f.size)} of {fmtSize(f.size)} {unit(f.size)} · {f.size - f.loaded < f.size * f.rate * 1.5 ? 'almost done' : `${Math.ceil((f.size - f.loaded) / (f.size * f.rate))}s left`}</>\n                      )}\n                      {f.status === 'done' && <>{fmtSize(f.size)} {unit(f.size)}</>}\n                      {f.status === 'error' && (f.errorMsg || 'Upload failed')}\n                      {f.status === 'toolarge' && `Too large – max ${maxSizeMB} MB`}\n                    </div>\n                    <AnimatePresence initial={false}>\n                    {(f.status === 'uploading' || f.status === 'error') && (\n                      <motion.div key=\"bar\" initial={{ opacity: 0, height: 0, marginTop: 0 }} animate={{ opacity: 1, height: 6, marginTop: 10 }} exit={{ opacity: 0, height: 0, marginTop: 0 }} transition={{ duration: 0.25, ease: [0.22, 1, 0.36, 1] }} className=\"overflow-hidden rounded-full bg-line2/70\">\n                        <motion.div\n                          className={`h-full origin-left rounded-full ${f.status === 'error' ? '' : 'bg-tx'}`}\n                          style={f.status === 'error' ? { backgroundColor: RED } : undefined}\n                          initial={{ scaleX: 0 }}\n                          animate={{ scaleX: f.size ? f.loaded / f.size : 0 }}\n                          transition={{ type: 'spring', stiffness: 90, damping: 22, restDelta: 0.001 }}\n                        />\n                      </motion.div>\n                    )}\n                    </AnimatePresence>\n                  </div>\n                  {f.status === 'error' && (\n                    <button type=\"button\" onClick={() => retry(f.id)} aria-label=\"Retry upload\" className=\"shrink-0 text-faint transition-colors hover:text-tx\"><RetryIcon /></button>\n                  )}\n                  <button type=\"button\" onClick={() => remove(f.id)} aria-label={`Remove ${f.name}`} className=\"shrink-0 text-faint transition-colors hover:text-tx\"><XIcon /></button>\n                </motion.div>\n              ))}\n            </AnimatePresence>\n          </div>\n        )}\n      </div>\n\n      <div className=\"flex items-center justify-between bg-panel2 px-5 py-3.5 text-[13px]\">\n        {files.length === 0 ? (\n          <span className=\"text-faint\">No files yet</span>\n        ) : active.length > 0 ? (\n          <span className=\"text-mute\">Uploading {active.length} of {files.length} · {fmtSize(loadedSum, sizeSum)} of {fmtSize(sizeSum)} {unit(sizeSum)}</span>\n        ) : (\n          <>\n            <span className=\"flex items-center gap-1.5 text-mute\">\n              {failed && <WarnIcon />}\n              {doneCount} of {files.length} uploaded\n            </span>\n            <button type=\"button\" onClick={clear} className=\"font-medium text-mute transition-colors hover:text-tx\">Clear</button>\n          </>\n        )}\n      </div>\n    </div>\n  )\n}\n"
    }
  ],
  "cssVars": {
    "theme": {
      "color-bg": "var(--bg)",
      "color-panel": "var(--panel)",
      "color-panel2": "var(--panel2)",
      "color-line": "var(--line)",
      "color-line2": "var(--line2)",
      "color-tx": "var(--tx)",
      "color-mute": "var(--mute)",
      "color-faint": "var(--faint)",
      "color-acc": "var(--acc)",
      "color-acc-fg": "var(--acc-fg)"
    },
    "light": {
      "bg": "#ffffff",
      "panel": "#f5f5f7",
      "panel2": "#e8e8ed",
      "line": "#e5e5ea",
      "line2": "#d2d2d7",
      "tx": "#1d1d1f",
      "mute": "#6e6e73",
      "faint": "#a1a1a6",
      "acc": "#1d1d1f",
      "acc-fg": "#ffffff"
    },
    "dark": {
      "bg": "#000000",
      "panel": "#141416",
      "panel2": "#1f1f22",
      "line": "#262628",
      "line2": "#3a3a3d",
      "tx": "#f5f5f7",
      "mute": "#a1a1a6",
      "faint": "#6e6e73",
      "acc": "#f5f5f7",
      "acc-fg": "#000000"
    }
  },
  "css": {
    ".light": {
      "--bg": "#ffffff",
      "--panel": "#f5f5f7",
      "--panel2": "#e8e8ed",
      "--line": "#e5e5ea",
      "--line2": "#d2d2d7",
      "--tx": "#1d1d1f",
      "--mute": "#6e6e73",
      "--faint": "#a1a1a6",
      "--acc": "#1d1d1f",
      "--acc-fg": "#ffffff"
    }
  }
}
