{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "uploader-web",
  "title": "Uploader (Web Components)",
  "description": "File uploads with progress, cancel and retry.",
  "dependencies": [
    "@kind-ui/agent-ui@0.9.0",
    "@ui-sdk/core@0.1.0-beta.17",
    "@ui-sdk/web@0.1.0-beta.17"
  ],
  "registryDependencies": [
    "@kind-ui/agent-ui-setup"
  ],
  "files": [
    {
      "path": "packages/agent-ui/src/web/uploader.ts",
      "content": "import \"@ui-sdk/web/file-input\";\nimport \"@ui-sdk/web/progress\";\nimport {\n  type UploaderFilesDetails,\n  type UploaderOptions,\n  type UploaderState,\n  type UploadItem,\n  uploadDetail,\n  uploaderPrimitive,\n} from \"@kind-ui/agent-ui/core\";\nimport { createParts, wrappedButton } from \"@ui-sdk/web\";\nimport type {\n  AttributeProperties,\n  UiDetailedChangeEvent,\n  UiElement,\n  UiPartElement,\n} from \"@ui-sdk/web/events\";\n\nconst parts = `\n  <ui-file-input>\n    <ui-file-input-dropzone>\n      <span data-upload-icon></span>\n      <span data-hint>Drop files here, or</span>\n      <ui-file-input-trigger><button type=\"button\">browse</button></ui-file-input-trigger>\n    </ui-file-input-dropzone>\n  </ui-file-input>\n  <kind-uploader-list></kind-uploader-list>`;\n\nconst row = `\n  <span data-icon></span>\n  <ui-progress>\n    <ui-progress-label></ui-progress-label>\n    <ui-progress-track><ui-progress-indicator></ui-progress-indicator></ui-progress-track>\n  </ui-progress>\n  <span data-detail></span>\n  <kind-uploader-action><button></button></kind-uploader-action>`;\n\ntype Movable = Element & { moveBefore?: (node: Node, child: Node | null) => void };\n\n// `moveBefore` keeps a focused action button focused where the browser has it; `insertBefore` blurs it.\nfunction move(list: Movable, row: Element, before: Element | null): void {\n  if (list.moveBefore && row.isConnected) list.moveBefore(row, before);\n  else list.insertBefore(row, before);\n}\n\n// Updates a row in place, so its action button keeps focus and its bar moves rather than restarts.\nfunction fill(el: Element, item: UploadItem): void {\n  if (!el.firstElementChild) el.innerHTML = row;\n  const progress = el.querySelector(\"ui-progress\") as (Element & { value: number | null }) | null;\n  const label = el.querySelector(\"ui-progress-label\");\n  const detail = el.querySelector(\"[data-detail]\");\n  if (progress && progress.value !== item.progress) progress.value = item.progress;\n  if (label && label.textContent !== item.name) label.textContent = item.name;\n  const text = uploadDetail(item);\n  if (detail && detail.textContent !== text) detail.textContent = text;\n}\n\n/**\n * `<kind-uploader aria-label=\"Uploads\">` fills in a File Input drop zone and its list when it has no children, and\n * renders a `kind-uploader-item` for each entry of its `items` property, keeping a removed one while it fades out.\n * The `can-cancel` and `can-retry` attributes show those buttons, which fire `uploadcancel` and `uploadretry` with\n * the item's id. Accepted files fire `files` and turned-down ones `uploadreject`, each with `event.detail.value`.\n */\nexport function defineUploader(): void {\n  if (customElements.get(\"kind-uploader\")) return;\n  createParts(uploaderPrimitive, {\n    root: {\n      tag: \"kind-uploader\",\n      kinds: { canCancel: \"boolean\", canRetry: \"boolean\" },\n      // Without the attribute the callback is left out; with it, `@ui-sdk/web` fills it in with an event.\n      options: (el) => {\n        const { canCancel, canRetry } = el as KindUploaderElement;\n        return { ...(!canCancel && { onCancel: undefined }), ...(!canRetry && { onRetry: undefined }) };\n      },\n      // The File Input inside fires `cancel` (from its native input, bubbling) and `reject`.\n      events: { onCancel: \"uploadcancel\", onRetry: \"uploadretry\", onReject: \"uploadreject\" },\n      render: (el, instance) => {\n        if (!el.firstElementChild) el.innerHTML = parts;\n        const fileInput = el.querySelector(\"ui-file-input\") as (Element & Record<string, unknown>) | null;\n        const list = el.querySelector(\"kind-uploader-list\");\n        const drawn = new WeakMap<Element, UploadItem>();\n        const draw = () => {\n          const { rows, accept, maxSize } = instance.get() as UploaderState;\n          if (fileInput) {\n            if (!fileInput.multiple) fileInput.multiple = true;\n            if (fileInput.accept !== accept) fileInput.accept = accept;\n            if (fileInput.maxSize !== maxSize) fileInput.maxSize = maxSize;\n          }\n          if (!list) return;\n          const ids = new Set(rows.map(({ item }) => item.id));\n          // Dropped rows leave first, so placing the rest never moves (and blurs) a focused one.\n          for (const child of [...list.children])\n            if (!ids.has(child.getAttribute(\"value\") ?? \"\")) child.remove();\n          const existing = new Map([...list.children].map((child) => [child.getAttribute(\"value\"), child]));\n          rows.forEach(({ item }, index) => {\n            let child = existing.get(item.id);\n            if (!child) {\n              child = document.createElement(\"kind-uploader-item\");\n              child.setAttribute(\"value\", item.id);\n            }\n            if (drawn.get(child) !== item) {\n              fill(child, item);\n              drawn.set(child, item);\n            }\n            if (list.children[index] !== child) move(list, child, list.children[index] ?? null);\n          });\n        };\n        draw();\n        return instance.subscribe(draw);\n      },\n    },\n    list: \"kind-uploader-list\",\n    item: \"kind-uploader-item\",\n    action: { tag: \"kind-uploader-action\", target: wrappedButton, display: \"contents\" },\n  });\n}\n\nexport type KindUploaderEvents = {\n  files: UiDetailedChangeEvent<File[], UploaderFilesDetails>;\n  uploadreject: UiDetailedChangeEvent<\n    Parameters<NonNullable<UploaderOptions[\"onReject\"]>>[0],\n    UploaderFilesDetails\n  >;\n  uploadcancel: UiDetailedChangeEvent<string, Parameters<NonNullable<UploaderOptions[\"onCancel\"]>>[1]>;\n  uploadretry: UiDetailedChangeEvent<string, Parameters<NonNullable<UploaderOptions[\"onRetry\"]>>[1]>;\n};\n\nexport interface KindUploaderElement\n  extends UiElement<KindUploaderEvents>,\n    AttributeProperties<typeof uploaderPrimitive, \"items\"> {\n  canCancel: boolean;\n  canRetry: boolean;\n  items?: UploaderOptions[\"items\"];\n}\n\ndeclare global {\n  interface HTMLElementTagNameMap {\n    \"kind-uploader\": KindUploaderElement;\n    \"kind-uploader-list\": HTMLElement;\n    \"kind-uploader-item\": UiPartElement<typeof uploaderPrimitive, \"item\", \"value\">;\n    \"kind-uploader-action\": HTMLElement;\n  }\n}\n",
      "type": "registry:ui",
      "target": "@ui/web/uploader.ts"
    }
  ],
  "meta": {
    "layer": "web"
  },
  "type": "registry:ui"
}
