{
  "name": "file-upload",
  "type": "ui:component",
  "title": "FileUpload",
  "description": "Drag-and-drop & click file upload dropzone with max-size validation and removable file list.",
  "dependencies": [
    "@lucide/icons"
  ],
  "registryDependencies": [
    "theme",
    "icon"
  ],
  "files": [
    {
      "path": "src/ui/file-upload.tsx",
      "target": "src/ui/file-upload.tsx",
      "sha256": "294439f22eeb81d717103505be54f1a3abb7848f016641130e3e8993ac3a3c84",
      "content": "import './theme.css';\nimport { FileText, UploadCloud, X } from '@lucide/icons';\nimport { scope, type Ctx } from '@fierst/aster';\nimport { Icon } from './icon';\n\nconst fuScope = scope`\n  :scope {\n    display: grid;\n    gap: 0.65rem;\n    font-family: var(--aster-font-sans);\n  }\n  .fu-label { font-size: 0.84rem; font-weight: 500; color: var(--aster-on-surface); }\n  .dropzone {\n    display: flex;\n    flex-direction: column;\n    align-items: center;\n    justify-content: center;\n    gap: 0.45rem;\n    padding: 1.5rem 1rem;\n    border-radius: var(--aster-radius-lg);\n    border: 1.5px dashed var(--aster-border-strong);\n    background: var(--aster-surface);\n    color: var(--aster-on-surface-muted);\n    text-align: center;\n    cursor: pointer;\n    transition:\n      border-color var(--aster-dur-fast),\n      background-color var(--aster-dur-fast);\n  }\n  .dropzone:hover,\n  .dropzone.is-dragover {\n    border-color: var(--aster-primary);\n    background: var(--aster-primary-subtle);\n  }\n  .dz-icon {\n    width: 38px;\n    height: 38px;\n    border-radius: var(--aster-radius-md);\n    border: 1px solid var(--aster-border);\n    background: var(--aster-surface);\n    color: var(--aster-on-surface);\n    display: inline-grid;\n    place-items: center;\n    box-shadow: var(--aster-shadow-xs);\n  }\n  .dz-title { font-size: 0.875rem; font-weight: 500; color: var(--aster-on-surface); }\n  .dz-sub { font-size: 0.78rem; color: var(--aster-on-surface-muted); }\n  .native-file { position: absolute; width: 1px; height: 1px; clip-path: inset(50%); overflow: hidden; }\n  .dropzone:focus-within { outline: 2px solid var(--aster-primary); outline-offset: 2px; }\n  .fu-error { font-size: 0.78rem; color: var(--aster-danger); font-weight: 500; }\n  .file-list { display: grid; gap: 0.4rem; }\n  .file-row {\n    display: flex;\n    align-items: center;\n    justify-content: space-between;\n    gap: 0.65rem;\n    padding: 0.5rem 0.75rem;\n    border-radius: var(--aster-radius-md);\n    border: 1px solid var(--aster-border);\n    background: var(--aster-surface);\n    font-size: 0.82rem;\n  }\n  .file-meta { display: flex; align-items: center; gap: 0.5rem; min-width: 0; }\n  .file-name { font-weight: 500; color: var(--aster-on-surface); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }\n  .file-size { color: var(--aster-on-surface-subtle); font-family: var(--aster-font-mono); font-size: 0.74rem; flex-shrink: 0; }\n  .file-rm {\n    border: none;\n    background: transparent;\n    color: var(--aster-on-surface-muted);\n    cursor: pointer;\n    padding: 0.2rem;\n    border-radius: var(--aster-radius-xs);\n    display: inline-grid;\n    place-items: center;\n  }\n  .file-rm:hover { background: var(--aster-surface-muted); color: var(--aster-on-surface); }\n`;\n\nfunction formatBytes(n: number): string {\n  if (n < 1024) return `${n} B`;\n  if (n < 1024 * 1024) return `${(n / 1024).toFixed(1)} KB`;\n  return `${(n / (1024 * 1024)).toFixed(1)} MB`;\n}\n\nlet fuSeq = 0;\n\nexport function* FileUpload(\n  props: {\n    label?: string;\n    accept?: string;\n    multiple?: boolean;\n    maxSizeBytes?: number;\n    hint?: string;\n    onChange?: (files: File[]) => void;\n    class?: string;\n  },\n  ctx: Ctx,\n) {\n  const uid = `aster-fu-${++fuSeq}`;\n  let files: File[] = [];\n  let dragOver = false;\n  let errorMsg = '';\n\n  for (props of ctx) {\n    const ingest = (incoming: FileList | null) => {\n      if (!incoming) return;\n      errorMsg = '';\n      const accepted: File[] = [];\n      const types = props.accept\n        ?.toLowerCase()\n        .split(',')\n        .map((t) => t.trim())\n        .filter(Boolean);\n      for (const f of Array.from(incoming)) {\n        if (\n          types?.length &&\n          !types.some((type) =>\n            type.startsWith('.')\n              ? f.name.toLowerCase().endsWith(type)\n              : type.endsWith('/*')\n                ? f.type.toLowerCase().startsWith(type.slice(0, -1))\n                : f.type.toLowerCase() === type,\n          )\n        ) {\n          errorMsg = f.name + ' is not an accepted file type.';\n          continue;\n        }\n        if (props.maxSizeBytes !== undefined && f.size > props.maxSizeBytes) {\n          errorMsg = `\"${f.name}\" exceeds max size of ${formatBytes(props.maxSizeBytes)}.`;\n          continue;\n        }\n        accepted.push(f);\n      }\n      if (!accepted.length) return;\n      files = props.multiple ? [...files, ...accepted] : accepted.slice(0, 1);\n      props.onChange?.(files);\n    };\n\n    const removeAt = (idx: number) => {\n      files = files.filter((_, i) => i !== idx);\n      props.onChange?.(files);\n    };\n\n    yield (\n      <div class={[fuScope, props.class].filter(Boolean).join(' ')}>\n        {props.label ? (\n          <label class=\"fu-label\" for={uid}>\n            {props.label}\n          </label>\n        ) : null}\n        <label\n          for={uid}\n          class=\"dropzone\"\n          class:is-dragover={dragOver}\n          on:dragover={(e: Event) => {\n            e.preventDefault();\n            dragOver = true;\n          }}\n          on:dragleave={() => (dragOver = false)}\n          on:drop={(e: Event) => {\n            e.preventDefault();\n            dragOver = false;\n            ingest((e as DragEvent).dataTransfer?.files ?? null);\n          }}\n        >\n          <span class=\"dz-icon\" aria-hidden=\"true\">\n            <Icon icon={UploadCloud} size={18} />\n          </span>\n          <span class=\"dz-title\">Click to upload or drag and drop</span>\n          <span class=\"dz-sub\">\n            {props.hint ??\n              `${props.accept ?? 'Any file'}${props.maxSizeBytes ? ` · Max ${formatBytes(props.maxSizeBytes)}` : ''}`}\n          </span>\n          <input\n            id={uid}\n            type=\"file\"\n            class=\"native-file\"\n            accept={props.accept}\n            multiple={props.multiple}\n            onChange={(e: Event) => {\n              const input = e.currentTarget as HTMLInputElement;\n              ingest(input.files);\n              input.value = '';\n            }}\n          />\n        </label>\n\n        {errorMsg ? (\n          <span class=\"fu-error\" role=\"alert\">\n            {errorMsg}\n          </span>\n        ) : null}\n\n        {files.length > 0 ? (\n          <div class=\"file-list\">\n            {files.map((f, idx) => (\n              <div key={`${f.name}-${idx}`} class=\"file-row\">\n                <div class=\"file-meta\">\n                  <Icon icon={FileText} size={15} />\n                  <span class=\"file-name\">{f.name}</span>\n                  <span class=\"file-size\">{formatBytes(f.size)}</span>\n                </div>\n                <button\n                  type=\"button\"\n                  class=\"file-rm\"\n                  aria-label={`Remove ${f.name}`}\n                  onClick={() => removeAt(idx)}\n                >\n                  <Icon icon={X} size={14} />\n                </button>\n              </div>\n            ))}\n          </div>\n        ) : null}\n      </div>\n    );\n  }\n}\n"
    }
  ]
}
