{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "file-upload",
  "type": "registry:ui",
  "title": "File Upload",
  "description": "Drag-and-drop file upload with validation, animated progress, and a completion callback.",
  "dependencies": [
    "@remixicon/react@^4.9.0"
  ],
  "registryDependencies": [
    "cx",
    "globals",
    "template-copy"
  ],
  "files": [
    {
      "path": "components/base/file-upload/file-upload.tsx",
      "content": "\"use client\";\n\nimport { useTemplateCopy } from \"@/components/foundations/template-copy/template-copy\";\n\nimport { useEffect, useRef, useState, type ReactNode } from \"react\";\nimport {\n  RiFileExcel2Line,\n  RiFileImageLine,\n  RiFileTextLine,\n  RiUploadCloud2Line,\n} from \"@remixicon/react\";\nimport { cx } from \"@/utils/cx\";\n\n/**\n * Figma sources: Board UI → \"Upload file\" (node 4105:25269),\n * \"Upload in progress\" (node 4107:27192), and the complete state\n * (node 4107:30456).\n *\n * Interactive drag-and-drop file upload with validation, a perimeter progress\n * indicator, animated content transitions, and a completion callback. The\n * upload is simulated so the component is backend-agnostic; connect\n * `onUploadComplete` to the application's upload/storage layer.\n */\n\nconst DEFAULT_MAX_BYTES = 8 * 1024 * 1024;\nconst DEFAULT_EXTENSIONS = [\"pdf\", \"jpg\", \"jpeg\", \"png\", \"xlsx\"] as const;\n\ntype UploadPhase = \"idle\" | \"uploading\" | \"complete\";\ntype StaggerState = \"shown\" | \"hiding\" | \"hidden\";\n\nexport interface FileUploadProps {\n  /** Called after the progress and success states finish. */\n  onUploadComplete?: (file: File) => void;\n  /** Accepted filename extensions without dots. */\n  allowedExtensions?: readonly string[];\n  /** Maximum accepted file size in bytes. */\n  maxBytes?: number;\n  /** Overrides the icon shown while a file uploads. */\n  renderFileIcon?: (file: File) => ReactNode;\n  className?: string;\n}\n\nexport function formatFileSize(bytes: number) {\n  if (bytes >= 1024 * 1024) {\n    const mb = bytes / (1024 * 1024);\n    return `${mb >= 10 ? Math.round(mb) : Math.round(mb * 10) / 10} MB`;\n  }\n  return `${Math.max(1, Math.round(bytes / 1024))} KB`;\n}\n\nfunction extensionFor(fileName: string) {\n  return fileName.split(\".\").pop()?.toLowerCase() ?? \"\";\n}\n\nfunction DefaultFileIcon({ file }: { file: File }) {\n  const extension = extensionFor(file.name);\n  const Icon = extension === \"xlsx\"\n    ? RiFileExcel2Line\n    : [\"jpg\", \"jpeg\", \"png\"].includes(extension)\n      ? RiFileImageLine\n      : RiFileTextLine;\n\n  return <Icon className=\"size-6 shrink-0 text-foreground-icon-secondary\" aria-hidden />;\n}\n\n/** Drives the Transitions.dev text-reveal states defined in globals.css. */\nfunction useStaggerState(active: boolean): StaggerState {\n  const [prevActive, setPrevActive] = useState(active);\n  const [hiding, setHiding] = useState(false);\n\n  if (prevActive !== active) {\n    setPrevActive(active);\n    setHiding(!active);\n  }\n\n  useEffect(() => {\n    if (!hiding) return;\n    const timer = setTimeout(() => setHiding(false), 200);\n    return () => clearTimeout(timer);\n  }, [hiding]);\n\n  return active ? \"shown\" : hiding ? \"hiding\" : \"hidden\";\n}\n\nconst staggerContainer = (state: StaggerState) =>\n  cx(\"t-stagger\", state === \"shown\" && \"is-shown\", state === \"hiding\" && \"is-hiding\");\n\nconst staggerLine = (state: StaggerState) =>\n  cx(\n    state === \"shown\" &&\n      \"translate-y-0 opacity-100 blur-0 [transition:opacity_var(--stagger-dur)_var(--stagger-ease),translate_var(--stagger-dur)_var(--stagger-ease),filter_var(--stagger-dur)_var(--stagger-ease)]\",\n    state === \"hiding\" && \"translate-y-0 opacity-0 blur-0 [transition:opacity_200ms_ease]\",\n    state === \"hidden\" && \"translate-y-3 opacity-0 blur-[3px] transition-none\",\n  );\n\n/** Rounded-rect progress path beginning at top-center and running clockwise. */\nfunction ringPath(width: number, height: number, inset: number, radius: number) {\n  const x0 = inset;\n  const y0 = inset;\n  const x1 = width - inset;\n  const y1 = height - inset;\n  const arc = (endX: number, endY: number) =>\n    `A ${radius} ${radius} 0 0 1 ${endX} ${endY}`;\n\n  return [\n    `M ${width / 2} ${y0}`,\n    `H ${x1 - radius}`,\n    arc(x1, y0 + radius),\n    `V ${y1 - radius}`,\n    arc(x1 - radius, y1),\n    `H ${x0 + radius}`,\n    arc(x0, y1 - radius),\n    `V ${y0 + radius}`,\n    arc(x0 + radius, y0),\n    \"Z\",\n  ].join(\" \");\n}\n\nexport function FileUpload({\n  onUploadComplete,\n  allowedExtensions = DEFAULT_EXTENSIONS,\n  maxBytes = DEFAULT_MAX_BYTES,\n  renderFileIcon,\n  className,\n}: FileUploadProps) {\n  const localize = useTemplateCopy();\n  const [phase, setPhase] = useState<UploadPhase>(\"idle\");\n  const [progress, setProgress] = useState(0);\n  const [file, setFile] = useState<File | null>(null);\n  const [rejection, setRejection] = useState<string | null>(null);\n  const [dragOver, setDragOver] = useState(false);\n\n  const inputRef = useRef<HTMLInputElement>(null);\n  const boxRef = useRef<HTMLDivElement>(null);\n  const [box, setBox] = useState({ width: 533, height: 164 });\n  const timers = useRef<ReturnType<typeof setTimeout>[]>([]);\n\n  useEffect(() => {\n    const element = boxRef.current;\n    if (!element) return;\n    const observer = new ResizeObserver(([entry]) => {\n      const { width, height } = entry.contentRect;\n      if (width > 0) setBox({ width, height });\n    });\n    observer.observe(element);\n    return () => observer.disconnect();\n  }, []);\n\n  useEffect(() => {\n    const pending = timers.current;\n    return () => pending.forEach(clearTimeout);\n  }, []);\n\n  const startUpload = (nextFile: File) => {\n    const extension = extensionFor(nextFile.name);\n    if (!allowedExtensions.map((value) => value.toLowerCase()).includes(extension)) {\n      setRejection(`Only ${allowedExtensions.map((value) => value.toUpperCase()).join(\", \")} files are supported`);\n      timers.current.push(setTimeout(() => setRejection(null), 2600));\n      return;\n    }\n    if (nextFile.size > maxBytes) {\n      setRejection(`That file is larger than ${formatFileSize(maxBytes)}`);\n      timers.current.push(setTimeout(() => setRejection(null), 2600));\n      return;\n    }\n\n    setFile(nextFile);\n    setProgress(0);\n    setPhase(\"uploading\");\n\n    let value = 0;\n    const tick = () => {\n      value = Math.min(100, value + 2 + Math.random() * 5);\n      setProgress(Math.round(value));\n      if (value < 100) {\n        timers.current.push(setTimeout(tick, 90));\n        return;\n      }\n\n      setPhase(\"complete\");\n      timers.current.push(\n        setTimeout(() => {\n          onUploadComplete?.(nextFile);\n          setPhase(\"idle\");\n          setFile(null);\n        }, 1600),\n      );\n    };\n    timers.current.push(setTimeout(tick, 250));\n  };\n\n  const busy = phase !== \"idle\";\n  const progressPath = ringPath(box.width, box.height, 1, 15);\n  const idleReveal = useStaggerState(!busy);\n  const busyReveal = useStaggerState(busy);\n  const uploadingLine = useStaggerState(phase === \"uploading\");\n  const completeLine = useStaggerState(phase === \"complete\");\n  const allowedLabel = allowedExtensions\n    .filter((extension) => extension.toLowerCase() !== \"jpeg\")\n    .map((extension) => extension.toUpperCase())\n    .join(\", \");\n\n  return localize(\n    <div\n      ref={boxRef}\n      role=\"button\"\n      tabIndex={busy ? -1 : 0}\n      aria-label=\"Upload a file\"\n      onClick={() => !busy && inputRef.current?.click()}\n      onKeyDown={(event) => {\n        if (!busy && (event.key === \"Enter\" || event.key === \" \")) {\n          event.preventDefault();\n          inputRef.current?.click();\n        }\n      }}\n      onDragOver={(event) => {\n        event.preventDefault();\n        if (!busy) setDragOver(true);\n      }}\n      onDragLeave={() => setDragOver(false)}\n      onDrop={(event) => {\n        event.preventDefault();\n        setDragOver(false);\n        const droppedFile = event.dataTransfer.files?.[0];\n        if (droppedFile && !busy) startUpload(droppedFile);\n      }}\n      className={cx(\n        \"group relative h-[164px] w-full shrink-0 rounded-2xl outline-none\",\n        \"transition-colors duration-300 ease-out\",\n        busy ? \"bg-background-primary-default\" : \"cursor-pointer bg-background-secondary-default\",\n        \"focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-border-focus-ring\",\n        className,\n      )}\n    >\n      <input\n        ref={inputRef}\n        type=\"file\"\n        aria-label=\"Choose a file to upload\"\n        accept={allowedExtensions.map((extension) => `.${extension}`).join(\",\")}\n        className=\"sr-only\"\n        tabIndex={-1}\n        onChange={(event) => {\n          const selectedFile = event.target.files?.[0];\n          event.target.value = \"\";\n          if (selectedFile) startUpload(selectedFile);\n        }}\n      />\n\n      <div\n        aria-hidden\n        className={cx(\n          \"pointer-events-none absolute inset-0 rounded-2xl border-2 border-dashed\",\n          dragOver\n            ? \"border-border-button-active\"\n            : \"border-border-checkbox-default group-hover:border-border-button-active\",\n          \"transition-[opacity,border-color] duration-300 ease-out\",\n          busy ? \"opacity-0\" : \"opacity-100\",\n        )}\n      />\n\n      <svg\n        aria-hidden\n        viewBox={`0 0 ${box.width} ${box.height}`}\n        preserveAspectRatio=\"none\"\n        className={cx(\n          \"pointer-events-none absolute inset-0 size-full transition-opacity duration-300 ease-out\",\n          busy ? \"opacity-100\" : \"opacity-0\",\n        )}\n      >\n        <rect\n          x={1}\n          y={1}\n          width={box.width - 2}\n          height={box.height - 2}\n          rx={15}\n          fill=\"none\"\n          stroke=\"var(--color-border-button-default)\"\n          strokeWidth={2}\n        />\n        <path\n          d={progressPath}\n          fill=\"none\"\n          stroke=\"var(--color-accent-400)\"\n          strokeWidth={2}\n          strokeLinecap=\"round\"\n          pathLength={100}\n          strokeDasharray={`${busy ? (progress >= 100 ? 102 : progress) : 0} 200`}\n          className=\"transition-[stroke-dasharray] duration-200 ease-linear\"\n        />\n      </svg>\n\n      <div\n        aria-hidden\n        className={cx(\n          \"absolute -top-[9.5px] left-1/2 -translate-x-1/2 rounded-md bg-accent-400 px-1.5 py-0.5\",\n          \"text-caption-1-medium whitespace-nowrap text-white tabular-nums\",\n          \"[transition:opacity_160ms_ease-out,filter_160ms_ease-out,translate_300ms_ease-out]\",\n          busy ? \"translate-y-0 opacity-100 blur-0\" : \"-translate-y-2.5 opacity-0 blur-[2px]\",\n        )}\n      >\n        {progress}%\n      </div>\n\n      <div\n        className={cx(\n          \"absolute inset-0 flex flex-col items-center justify-center gap-3.5\",\n          staggerContainer(idleReveal),\n          busy && \"pointer-events-none\",\n        )}\n      >\n        <span className=\"t-stagger-line t-stagger-line--1 flex size-10 items-center justify-center rounded-full bg-file-upload-icon-background p-2.5\">\n          <RiUploadCloud2Line\n            className=\"size-6 shrink-0 text-file-upload-icon-foreground transition-colors duration-150 ease group-hover:text-file-upload-icon-foreground-hover\"\n            aria-hidden\n          />\n        </span>\n        <div className=\"flex flex-col items-center gap-2 text-center\">\n          <p\n            className={cx(\n              \"t-stagger-line t-stagger-line--2 text-body-medium\",\n              rejection ? \"text-text-error-primary\" : \"text-text-secondary\",\n            )}\n          >\n            {rejection ?? (\n              <>\n                Drag and drop to upload or <span className=\"text-accent-500\">select</span>\n              </>\n            )}\n          </p>\n          <p className=\"t-stagger-line t-stagger-line--3 text-body-2-regular text-text-tertiary\">\n            {allowedLabel} (max {formatFileSize(maxBytes)})\n          </p>\n        </div>\n      </div>\n\n      <div\n        className={cx(\n          \"absolute inset-0 flex flex-col items-center justify-center\",\n          staggerContainer(busyReveal),\n          !busy && \"pointer-events-none\",\n        )}\n      >\n        <span className=\"t-stagger-line t-stagger-line--1 flex size-10 items-center justify-center rounded-full border border-border-button-default bg-background-primary-default p-2\">\n          {file && (renderFileIcon ? renderFileIcon(file) : <DefaultFileIcon file={file} />)}\n        </span>\n        <p className=\"t-stagger-line t-stagger-line--2 mt-3.5 max-w-[90%] truncate text-body-medium text-text-primary\">\n          {file?.name}\n        </p>\n        <div className=\"t-stagger-line t-stagger-line--3 relative mt-1 h-[18px] w-full\">\n          <p\n            className={cx(\n              \"absolute inset-x-0 text-center text-body-2-regular text-text-secondary\",\n              staggerLine(uploadingLine),\n            )}\n          >\n            Uploading {file ? formatFileSize(file.size) : \"\"}\n            ...\n          </p>\n          <p\n            className={cx(\n              \"absolute inset-x-0 text-center text-body-2-regular text-text-secondary\",\n              staggerLine(completeLine),\n            )}\n          >\n            Uploaded successfully!\n          </p>\n        </div>\n      </div>\n    </div>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/base/file-upload/file-upload.tsx"
    }
  ],
  "meta": {
    "docsUrl": "https://www.boardui.com/components/file-upload",
    "example": "import { FileUpload } from \"@/components/base/file-upload/file-upload\";\n\nexport function Example() {\n  return (\n    <FileUpload\n      onUploadComplete={(file) => {\n        console.log(\"Ready to persist:\", file);\n      }}\n    />\n  );\n}",
    "snippets": [
      {
        "label": "validation",
        "code": "<FileUpload\n  allowedExtensions={[\"pdf\", \"png\"]}\n  maxBytes={4 * 1024 * 1024}\n  onUploadComplete={saveFile}\n/>"
      },
      {
        "label": "icon",
        "code": "import { RiFileCloudLine } from \"@remixicon/react\";\n\n<FileUpload\n  renderFileIcon={() => (\n    <RiFileCloudLine\n      className=\"size-6 text-foreground-icon-secondary\"\n      aria-hidden\n    />\n  )}\n/>"
      }
    ]
  }
}
