{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "attachments-web",
  "title": "Attachments (Web Components)",
  "description": "A removable list of attached files.",
  "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/attachments.ts",
      "content": "import {\n  type AttachmentItem,\n  type AttachmentsApi,\n  type AttachmentsOptions,\n  attachmentDetail,\n  attachmentsPrimitive,\n} from \"@kind-ui/agent-ui/core\";\nimport { createParts } from \"@ui-sdk/web\";\nimport type {\n  AttributeProperties,\n  UiDetailedChangeEvent,\n  UiElement,\n  UiPartElement,\n} from \"@ui-sdk/web/events\";\n\nfunction text(attribute: string, content: string): HTMLSpanElement {\n  const span = document.createElement(\"span\");\n  span.setAttribute(attribute, \"\");\n  span.textContent = content;\n  return span;\n}\n\n// Fills an item's content, keeping its remove part bound.\nfunction fill(el: Element, item: AttachmentItem, preview: string | undefined): void {\n  const visual = preview === undefined ? text(\"data-icon\", \"\") : document.createElement(\"img\");\n  if (visual instanceof HTMLImageElement) {\n    visual.setAttribute(\"data-preview\", \"\");\n    visual.alt = \"\";\n    visual.src = preview as string;\n  }\n  const remove =\n    el.querySelector(\"kind-attachments-remove\") ?? document.createElement(\"kind-attachments-remove\");\n  el.replaceChildren(\n    visual,\n    text(\"data-name\", item.name),\n    text(\"data-detail\", attachmentDetail(item)),\n    remove,\n  );\n}\n\n/**\n * `<kind-attachments aria-label=\"Files\">` renders a `kind-attachments-item` for each entry of its `items`\n * property, and keeps a removed one while it fades out. The `can-remove` attribute lets items be removed, each firing\n * `remove` with the item's id. Set the `focusFallback` property to name where focus goes when the last chip leaves.\n */\nexport function defineAttachments(): void {\n  if (customElements.get(\"kind-attachments\")) return;\n  createParts(attachmentsPrimitive, {\n    root: {\n      tag: \"kind-attachments\",\n      kinds: { canRemove: \"boolean\" },\n      // The composer's own handler, which offers removal without the attribute.\n      properties: [\"removeHandler\"],\n      // Without either, the callback is left out; with only the attribute, `@ui-sdk/web` fills it in with an event.\n      options: (el) => {\n        const { canRemove, removeHandler } = el as KindAttachmentsElement;\n        if (removeHandler) return { onRemove: removeHandler };\n        return canRemove ? {} : { onRemove: undefined };\n      },\n      render: (el, instance) => {\n        const api = instance.api as AttachmentsApi;\n        const drawn = new WeakMap<Element, { item: AttachmentItem; preview: string | undefined }>();\n        const draw = () => {\n          const { rows } = instance.get();\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 [...el.children])\n            if (!ids.has(child.getAttribute(\"value\") ?? \"\")) child.remove();\n          const existing = new Map([...el.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-attachments-item\");\n              child.setAttribute(\"value\", item.id);\n            }\n            const preview = api.previewOf(item);\n            const last = drawn.get(child);\n            if (last?.item !== item || last.preview !== preview) {\n              fill(child, item, preview);\n              drawn.set(child, { item, preview });\n            }\n            if (el.children[index] !== child) el.insertBefore(child, el.children[index] ?? null);\n          });\n        };\n        draw();\n        return instance.subscribe(draw);\n      },\n    },\n    item: \"kind-attachments-item\",\n    remove: \"kind-attachments-remove\",\n  });\n}\n\nexport type KindAttachmentsEvents = {\n  remove: UiDetailedChangeEvent<string, Parameters<NonNullable<AttachmentsOptions[\"onRemove\"]>>[1]>;\n};\n\nexport interface KindAttachmentsElement\n  extends UiElement<KindAttachmentsEvents>,\n    AttributeProperties<typeof attachmentsPrimitive, \"items\"> {\n  canRemove: boolean;\n  items?: AttachmentsOptions[\"items\"];\n  removeHandler?: AttachmentsOptions[\"onRemove\"];\n  focusFallback?: AttachmentsOptions[\"focusFallback\"];\n}\n\ndeclare global {\n  interface HTMLElementTagNameMap {\n    \"kind-attachments\": KindAttachmentsElement;\n    \"kind-attachments-item\": UiPartElement<typeof attachmentsPrimitive, \"item\", \"value\">;\n    \"kind-attachments-remove\": HTMLElement;\n  }\n}\n",
      "type": "registry:ui",
      "target": "@ui/web/attachments.ts"
    }
  ],
  "meta": {
    "layer": "web"
  },
  "type": "registry:ui"
}
