{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "code-block-web",
  "title": "Code Block (Web Components)",
  "description": "A code block with its language, a wrap toggle and a copy button.",
  "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",
    "@kind-ui/internal-scroll-area-web"
  ],
  "files": [
    {
      "path": "packages/agent-ui/src/web/code-block.ts",
      "content": "import {\n  type CodeBlockApi,\n  type CodeBlockOptions,\n  type CodeBlockState,\n  type CodeBlockWrapChangeReason,\n  codeBlockPrimitive,\n  type Token,\n} from \"@kind-ui/agent-ui/core\";\nimport { createParts, wrappedButton, wrappedChild } from \"@ui-sdk/web\";\nimport type { AttributeProperties, UiChangeEvent, UiElement } from \"@ui-sdk/web/events\";\nimport { defineInternalScrollArea } from \"./internal/scroll-area\";\n\nconst scrollAreaParts = `\n  <kind-internal-scroll-area>\n    <ui-scroll-area>\n      <ui-scroll-area-viewport>\n        <ui-scroll-area-content><kind-code-block-code><pre><code></code></pre></kind-code-block-code></ui-scroll-area-content>\n      </ui-scroll-area-viewport>\n      <ui-scroll-area-scrollbar orientation=\"horizontal\"><ui-scroll-area-thumb></ui-scroll-area-thumb></ui-scroll-area-scrollbar>\n      <ui-scroll-area-scrollbar orientation=\"vertical\"><ui-scroll-area-thumb></ui-scroll-area-thumb></ui-scroll-area-scrollbar>\n    </ui-scroll-area>\n  </kind-internal-scroll-area>`;\n\nconst parts = `\n  <kind-code-block-header>\n    <kind-code-block-language></kind-code-block-language>\n    <kind-code-block-wrap><button></button></kind-code-block-wrap>\n    <kind-code-block-copy><button></button></kind-code-block-copy>\n  </kind-code-block-header>${scrollAreaParts}`;\n\nconst sameTokens = (a: Token[] | undefined, b: Token[]) =>\n  a?.length === b.length &&\n  a.every((token, i) => {\n    const other = b[i] as Token;\n    return (\n      token.content === other.content && token.color === other.color && token.fontStyle === other.fontStyle\n    );\n  });\n\nfunction tokenElement(token: Token): HTMLElement {\n  const span = document.createElement(\"span\");\n  if (token.fontStyle) span.dataset.fontStyle = token.fontStyle;\n  if (token.color) span.style.setProperty(\"--kind-code-block-token\", token.color);\n  span.textContent = token.content;\n  return span;\n}\n\n/**\n * `<kind-code-block code=\"…\" language=\"ts\" aria-label=\"Code\">` fills in its header and scroll area when it has\n * no children, or only its scroll area with the `headerless` attribute. Set `highlight` as a property.\n */\nexport function defineCodeBlock(): void {\n  if (customElements.get(\"kind-code-block\")) return;\n  defineInternalScrollArea();\n  createParts(codeBlockPrimitive, {\n    root: {\n      tag: \"kind-code-block\",\n      state: {\n        wrap: {\n          get: (api) => api.isWrapped(),\n          set: (api, wrap) => api.setWrap(!!wrap),\n        },\n      },\n      api: [\"copy\", \"isWrapped\", \"setWrap\", \"toggleWrap\"],\n      render: (el, instance) => {\n        if (!el.firstElementChild) el.innerHTML = el.hasAttribute(\"headerless\") ? scrollAreaParts : parts;\n        const label = el.querySelector(\"kind-code-block-language\");\n        const scrollArea = el.querySelector(\"ui-scroll-area\");\n        const code = el.querySelector(\"kind-code-block-code code\");\n        let drawn: Token[][] = [];\n        const draw = () => {\n          const { language, lines, streaming } = instance.get() as CodeBlockState;\n          if (label && label.textContent !== (language ?? \"\")) label.textContent = language ?? \"\";\n          scrollArea?.toggleAttribute(\"stick-to-end\", streaming);\n          if (!code || lines === drawn) return;\n          lines.forEach((tokens, index) => {\n            if (sameTokens(drawn[index], tokens)) return;\n            const line = code.children[index] ?? code.appendChild(document.createElement(\"span\"));\n            line.setAttribute(\"data-line\", \"\");\n            line.replaceChildren(...tokens.map(tokenElement));\n          });\n          while (code.children.length > lines.length) code.lastElementChild?.remove();\n          drawn = lines;\n        };\n        draw();\n        return instance.subscribe(draw);\n      },\n    },\n    header: \"kind-code-block-header\",\n    language: \"kind-code-block-language\",\n    wrap: { tag: \"kind-code-block-wrap\", target: wrappedButton, display: \"contents\" },\n    copy: { tag: \"kind-code-block-copy\", target: wrappedButton, display: \"contents\" },\n    code: { tag: \"kind-code-block-code\", target: wrappedChild(\"pre\"), display: \"contents\" },\n  });\n}\n\nexport type KindCodeBlockEvents = { wrapchange: UiChangeEvent<boolean, CodeBlockWrapChangeReason> };\n\nexport interface KindCodeBlockElement\n  extends UiElement<KindCodeBlockEvents>,\n    AttributeProperties<typeof codeBlockPrimitive, \"api\" | \"highlight\" | \"wrap\"> {\n  wrap: boolean;\n  highlight?: CodeBlockOptions[\"highlight\"];\n  api: Pick<CodeBlockApi, \"copy\" | \"isWrapped\" | \"setWrap\" | \"toggleWrap\">;\n}\n\ndeclare global {\n  interface HTMLElementTagNameMap {\n    \"kind-code-block\": KindCodeBlockElement;\n    \"kind-code-block-header\": HTMLElement;\n    \"kind-code-block-language\": HTMLElement;\n    \"kind-code-block-wrap\": HTMLElement;\n    \"kind-code-block-copy\": HTMLElement;\n    \"kind-code-block-code\": HTMLElement;\n  }\n}\n",
      "type": "registry:ui",
      "target": "@ui/web/code-block.ts"
    }
  ],
  "meta": {
    "layer": "web"
  },
  "type": "registry:ui"
}
