{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "add-menu-web",
  "title": "Add Menu (Web Components)",
  "description": "A plus button that opens a menu of what can be added, with icons, descriptions, groups and submenus.",
  "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-menu-web"
  ],
  "files": [
    {
      "path": "packages/agent-ui/src/web/add-menu.ts",
      "content": "import {\n  type AddMenuActionReason,\n  type AddMenuApi,\n  type AddMenuContentState,\n  type AddMenuOptions,\n  type AddMenuRow,\n  type AddMenuSection,\n  type AddMenuState,\n  addMenuPrimitive,\n} from \"@kind-ui/agent-ui/core\";\nimport type { MenuOpenChangeReason } from \"@ui-sdk/core/menu\";\nimport { createParts } from \"@ui-sdk/web\";\nimport type { AttributeProperties, UiChangeEvent, UiElement } from \"@ui-sdk/web/events\";\nimport {\n  defineInternalMenu,\n  drawMenuItemContent,\n  menuElement as element,\n  placeMenuChild as place,\n} from \"./internal/menu\";\n\nconst DEFAULT_PARTS = `<kind-internal-menu><ui-menu><kind-add-menu-trigger><ui-menu-trigger><button type=\"button\" data-agent-hidden><span data-menu-trigger-icon aria-hidden=\"true\"></span></button></ui-menu-trigger></kind-add-menu-trigger><kind-add-menu-content><ui-menu-content side-offset=\"6\" align=\"start\"></ui-menu-content></kind-add-menu-content></ui-menu></kind-internal-menu>`;\n\n// Contents this element filled in, and so draws the rows of. A content someone wrote keeps its rows, as React's\n// children do. Kept across instances, because UI SDK rebuilds one when the element returns to the page.\nconst owned = new WeakSet<Element>();\nconst drawn = new WeakMap<Element, AddMenuContentState>();\n\ntype AddMenuIcon = string | Element;\n\n/**\n * Draws a level's rows into `parent`, reusing each row's element by its `id` and moving only what is out of place, so\n * a person's focus and highlight survive a change of `items` while the menu is open.\n */\nconst drawLevel = (parent: Element, sections: AddMenuSection[]) => {\n  const document = parent.ownerDocument;\n  const rows = new Map<string, Element>();\n  for (const row of parent.querySelectorAll(\":scope > [data-row], :scope > ui-menu-group > [data-row]\"))\n    rows.set(row.getAttribute(\"data-row\") ?? \"\", row);\n  const groups = new Map<string, Element>();\n  for (const group of parent.querySelectorAll(\":scope > ui-menu-group\"))\n    groups.set(group.getAttribute(\"data-section\") ?? \"\", group);\n  const separators = [...parent.querySelectorAll(\":scope > ui-menu-separator\")];\n  const kept = new Set<Element>();\n  let previous: Element | null = null;\n  const next = (container: Element, after: Element | null) =>\n    after ? after.nextElementSibling : container.firstElementChild;\n\n  const drawRow = (row: AddMenuRow) => {\n    let el = rows.get(row.id);\n    rows.delete(row.id);\n    const opens = row.sections !== undefined;\n    if (el && (el.localName === \"ui-menu-sub\") !== opens) {\n      el.remove();\n      el = undefined;\n    }\n    if (!opens) {\n      el ??= element(document, \"ui-menu-item\", { \"data-row\": row.id });\n      el.setAttribute(\"value\", row.id);\n      el.setAttribute(\"label\", row.label);\n      el.toggleAttribute(\"disabled\", row.disabled);\n      drawMenuItemContent(el, row);\n      return el;\n    }\n    if (!el) {\n      el = element(document, \"ui-menu-sub\", { \"data-row\": row.id });\n      el.append(element(document, \"ui-menu-sub-trigger\"), element(document, \"ui-menu-sub-content\"));\n    }\n    const trigger = el.querySelector(\":scope > ui-menu-sub-trigger\");\n    if (trigger) {\n      trigger.toggleAttribute(\"disabled\", row.disabled);\n      drawMenuItemContent(trigger, row, [element(document, \"span\", { \"data-menu-chevron\": \"\" })]);\n    }\n    const sub = el.querySelector(\":scope > ui-menu-sub-content\");\n    if (sub && row.sections) drawLevel(sub, row.sections);\n    return el;\n  };\n\n  for (const [index, section] of sections.entries()) {\n    if (index > 0) {\n      const separator = separators.shift() ?? element(document, \"ui-menu-separator\");\n      place(parent, separator, next(parent, previous));\n      kept.add(separator);\n      previous = separator;\n    }\n    if (section.label === undefined) {\n      for (const row of section.rows) {\n        const el = drawRow(row);\n        place(parent, el, next(parent, previous));\n        kept.add(el);\n        previous = el;\n      }\n      continue;\n    }\n    let group = groups.get(section.label);\n    if (!group) {\n      group = element(document, \"ui-menu-group\", { \"data-section\": section.label });\n      group.append(element(document, \"ui-menu-group-label\", {}, section.label));\n    }\n    place(parent, group, next(parent, previous));\n    kept.add(group);\n    previous = group;\n    let last = group.querySelector(\":scope > ui-menu-group-label\");\n    for (const row of section.rows) {\n      const el = drawRow(row);\n      place(group, el, next(group, last));\n      last = el;\n    }\n  }\n  for (const child of [...parent.children]) if (!kept.has(child)) child.remove();\n  for (const row of rows.values()) row.remove();\n};\n\n/**\n * `<kind-add-menu>` fills in its trigger and menu when it has no children. Set `items` as a property; with `can-run`,\n * choosing an item fires `addaction` with its `id` as `detail.value`. Markup you write puts the `ui-menu` inside a\n * `kind-internal-menu`, which styles it, and an empty `ui-menu-content` in `kind-add-menu-content` gets the rows.\n */\nexport function defineAddMenu(): void {\n  if (customElements.get(\"kind-add-menu\")) return;\n  defineInternalMenu();\n  createParts(addMenuPrimitive, {\n    root: {\n      tag: \"kind-add-menu\",\n      kinds: { canRun: \"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 KindAddMenuElement).canRun ? {} : { onAction: undefined }),\n      state: {\n        open: {\n          get: (api) => api.isOpen(),\n          set: (api, open) => api.setOpen(!!open),\n        },\n      },\n      // The inner menu fires an `action` of its own.\n      events: { onAction: \"addaction\" },\n      api: [\"isOpen\", \"setOpen\"],\n      render: (el, instance) => {\n        if (!el.firstElementChild) el.innerHTML = DEFAULT_PARTS;\n        const api = instance.api as AddMenuApi;\n        const draw = () => {\n          const state = instance.get() as AddMenuState;\n          const { controller, presentation, open } = api.partState.menu(state);\n          const menu = el.querySelector(\"ui-menu\");\n          if (menu) {\n            if (menu.controller !== controller) menu.controller = controller;\n            if (menu.presentation !== presentation) menu.presentation = presentation;\n            if (menu.open !== open) menu.open = open;\n          }\n          const trigger = el.querySelector(\"kind-add-menu-trigger > ui-menu-trigger\");\n          const props = api.partState.trigger(state);\n          trigger?.toggleAttribute(\"disabled\", props.disabled);\n          const button = trigger?.querySelector(\"button\");\n          if (button && button.ariaLabel !== props[\"aria-label\"]) button.ariaLabel = props[\"aria-label\"];\n          button?.toggleAttribute(\"data-kind-trigger\", props[\"data-kind-trigger\"] === \"\");\n          button?.toggleAttribute(\"data-kind-icon-button\", props[\"data-kind-icon-button\"] === \"\");\n          const content = el.querySelector(\"kind-add-menu-content > ui-menu-content\");\n          const rows = api.contentState(state);\n          // A content written empty fills itself in, as React's bare `AddMenuContent` does.\n          if (content && !content.firstChild) owned.add(content);\n          if (content && owned.has(content) && drawn.get(content) !== rows) {\n            drawn.set(content, rows);\n            drawLevel(content, rows.sections);\n          }\n        };\n        draw();\n        return instance.subscribe(draw);\n      },\n    },\n    trigger: { tag: \"kind-add-menu-trigger\", display: \"contents\" },\n    content: { tag: \"kind-add-menu-content\", display: \"contents\" },\n  });\n}\n\nexport type KindAddMenuEvents = {\n  addaction: UiChangeEvent<string, AddMenuActionReason>;\n  openchange: UiChangeEvent<boolean, MenuOpenChangeReason>;\n};\n\nexport interface KindAddMenuElement\n  extends UiElement<KindAddMenuEvents>,\n    AttributeProperties<typeof addMenuPrimitive, \"api\" | \"items\" | \"open\"> {\n  /**\n   * An entry's `icon` is an image URL, or an element (such as an `svg`) copied into its row. A copy keeps the\n   * element's ids, so pass an icon that uses `defs` (gradients, masks) as an image URL.\n   */\n  items?: (Omit<NonNullable<AddMenuOptions[\"items\"]>[number], \"icon\" | \"items\"> & {\n    icon?: AddMenuIcon;\n    items?: NonNullable<KindAddMenuElement[\"items\"]>;\n  })[];\n  open: boolean;\n  /** Whether a person can run the items, which fires `addaction`. Without it every item is disabled. */\n  canRun: boolean;\n  api: Pick<AddMenuApi, \"isOpen\" | \"setOpen\">;\n}\n\ndeclare global {\n  interface HTMLElementTagNameMap {\n    \"kind-add-menu\": KindAddMenuElement;\n    \"kind-add-menu-trigger\": HTMLElement;\n    \"kind-add-menu-content\": HTMLElement;\n  }\n}\n",
      "type": "registry:ui",
      "target": "@ui/web/add-menu.ts"
    }
  ],
  "meta": {
    "layer": "web"
  },
  "type": "registry:ui"
}
