{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "suggestion-web",
  "title": "Suggestion (Web Components)",
  "description": "A row of prompts a person can send in one press, which scrolls sideways when they don't fit.",
  "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-carousel-web"
  ],
  "files": [
    {
      "path": "packages/agent-ui/src/web/suggestion.ts",
      "content": "import {\n  type SuggestionApi,\n  type SuggestionLayout,\n  type SuggestionOptions,\n  type SuggestionState,\n  suggestionPrimitive,\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\";\nimport { defineInternalCarousel } from \"./internal/carousel\";\n\n// Lucide \"chevron-left\" and \"chevron-right\", see THIRD_PARTY_NOTICES.md.\nconst chevron = (path: string) =>\n  `<svg viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" aria-hidden=\"true\"><path d=\"${path}\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/></svg>`;\n\n// Previous and Next are hidden from agents, which takes the Carousel out of their snapshot: an agent picks any\n// suggestion by its id with the Suggestion's own `pick`, wherever it has scrolled.\nconst DEFAULT_PARTS = `<kind-internal-carousel><ui-carousel slides-per-page=\"auto\" drag-free><ui-carousel-viewport><ui-carousel-content></ui-carousel-content></ui-carousel-viewport><div data-suggestion-controls><ui-carousel-previous><button aria-label=\"Previous suggestions\" data-agent-hidden>${chevron(\"m15 18-6-6 6-6\")}</button></ui-carousel-previous><ui-carousel-next><button aria-label=\"Next suggestions\" data-agent-hidden>${chevron(\"m9 18 6-6-6-6\")}</button></ui-carousel-next></div></ui-carousel></kind-internal-carousel>`;\n\n// Elements this element filled in, by the layout it filled them for, and so draws the suggestions of. One someone\n// wrote keeps its suggestions, as React's children do. Kept across instances, because UI SDK rebuilds one when the\n// element returns to the page.\nconst filled = new WeakMap<Element, SuggestionLayout>();\ntype Movable = Element & { moveBefore?: (node: Node, child: Node | null) => void };\n\nconst createSuggestion = (doc: Document, id: string, slide: boolean) => {\n  const item = doc.createElement(\"kind-suggestion-item\");\n  item.setAttribute(\"value\", id);\n  item.innerHTML = `<button><span data-suggestion-label></span> <span data-suggestion-description></span></button>`;\n  if (!slide) return item;\n  const carouselItem = doc.createElement(\"ui-carousel-item\");\n  carouselItem.append(item);\n  return carouselItem;\n};\n\nconst setText = (el: Element | null, text: string) => {\n  if (el && el.textContent !== text) el.textContent = text;\n};\n\n/**\n * Draws a slide (or, when the row wraps, a chip) for each suggestion, reusing the one of a suggestion with the same\n * id and moving only what is out of place, so a focused suggestion keeps focus. `moveBefore` doesn't take focus away,\n * where the browser has it.\n */\nconst drawSuggestions = (row: Movable, suggestions: SuggestionState[\"suggestions\"], slides: boolean) => {\n  const existing = new Map<string, Element>();\n  for (const slide of row.querySelectorAll(\n    `:scope > ${slides ? \"ui-carousel-item\" : \"kind-suggestion-item\"}`,\n  ))\n    existing.set(\n      (slides ? slide.querySelector(\"kind-suggestion-item\") : slide)?.getAttribute(\"value\") ?? \"\",\n      slide,\n    );\n  let previous: Element | null = null;\n  for (const { id, label, description } of suggestions) {\n    const slide = existing.get(id) ?? createSuggestion(row.ownerDocument, id, slides);\n    existing.delete(id);\n    setText(slide.querySelector(\"[data-suggestion-label]\"), label);\n    setText(slide.querySelector(\"[data-suggestion-description]\"), description ?? \"\");\n    const before: Element | null = previous ? previous.nextElementSibling : row.firstElementChild;\n    if (before !== slide) {\n      if (row.moveBefore && slide.isConnected) row.moveBefore(slide, before);\n      else row.insertBefore(slide, before);\n    }\n    previous = slide;\n  }\n  for (const slide of existing.values()) slide.remove();\n};\n\n/**\n * `<kind-suggestion can-pick>` fills in its row when it has no children. Set `suggestions` as a property; the\n * `can-pick` attribute lets a person choose one, which fires `pick` with its id as `detail.value`. A row you write\n * yourself is the internal Carousel's markup, `kind-suggestion-item`s included, and keeps what you write in it. With\n * `layout=\"wrap\"` there's no Carousel: the `kind-suggestion-item`s are its children.\n */\nexport function defineSuggestion(): void {\n  if (customElements.get(\"kind-suggestion\")) return;\n  defineInternalCarousel();\n  createParts(suggestionPrimitive, {\n    root: {\n      tag: \"kind-suggestion\",\n      kinds: { canPick: \"boolean\" },\n      // Without the attribute the callback is left out; with it, `@ui-sdk/web` fills it in with an event.\n      options: (el) => ((el as KindSuggestionElement).canPick ? {} : { onPick: undefined }),\n      api: [\"getSuggestions\"],\n      render: (el, instance) => {\n        const draw = () => {\n          const { label, layout, suggestions } = instance.get() as SuggestionState;\n          if (!el.firstElementChild && !filled.has(el)) filled.set(el, layout);\n          const own = filled.get(el);\n          if (own !== undefined && (own !== layout || !el.firstElementChild)) {\n            filled.set(el, layout);\n            if (layout === \"wrap\") el.replaceChildren();\n            else el.innerHTML = DEFAULT_PARTS;\n          }\n          if (layout === \"wrap\") {\n            if (filled.has(el)) drawSuggestions(el, suggestions, false);\n            return;\n          }\n          const carousel = el.querySelector(\"ui-carousel\");\n          if (carousel && carousel.ariaLabel !== label) carousel.ariaLabel = label;\n          const row = el.querySelector(\"ui-carousel-content\");\n          if (row && filled.has(el)) drawSuggestions(row, suggestions, true);\n        };\n        draw();\n        return instance.subscribe(draw);\n      },\n    },\n    item: { tag: \"kind-suggestion-item\", target: wrappedButton, display: \"contents\" },\n  });\n}\n\nexport type KindSuggestionEvents = {\n  pick: UiDetailedChangeEvent<string, Parameters<NonNullable<SuggestionOptions[\"onPick\"]>>[1]>;\n};\n\nexport interface KindSuggestionElement\n  extends UiElement<KindSuggestionEvents>,\n    AttributeProperties<typeof suggestionPrimitive, \"api\" | \"suggestions\"> {\n  canPick: boolean;\n  suggestions?: SuggestionOptions[\"suggestions\"];\n  api: Pick<SuggestionApi, \"getSuggestions\">;\n}\n\ndeclare global {\n  interface HTMLElementTagNameMap {\n    \"kind-suggestion\": KindSuggestionElement;\n    \"kind-suggestion-item\": UiPartElement<typeof suggestionPrimitive, \"item\", \"value\">;\n  }\n}\n",
      "type": "registry:ui",
      "target": "@ui/web/suggestion.ts"
    }
  ],
  "meta": {
    "layer": "web"
  },
  "type": "registry:ui"
}
