{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "approval-mode-web",
  "title": "Approval Mode (Web Components)",
  "description": "A button showing how much the agent may do without asking, opening a menu of modes with a risky one marked.",
  "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/approval-mode.ts",
      "content": "import {\n  APPROVAL_MODE_GROUP,\n  APPROVAL_MODE_LEARN_MORE,\n  type ApprovalModeApi,\n  type ApprovalModeChangeReason,\n  type ApprovalModeContentState,\n  type ApprovalModeOptions,\n  type ApprovalModeRow,\n  type ApprovalModeState,\n  approvalModePrimitive,\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-approval-mode-trigger><ui-menu-trigger><button type=\"button\" data-agent-hidden></button></ui-menu-trigger></kind-approval-mode-trigger><kind-approval-mode-content><ui-menu-content side-offset=\"6\" align=\"start\"></ui-menu-content></kind-approval-mode-content></ui-menu></kind-internal-menu>`;\n\n// Buttons and contents this element filled in, and so draws. Ones someone wrote keep theirs, as React's children do.\n// Kept across instances, because UI SDK rebuilds one when the element returns to the page.\nconst owned = new WeakSet<Element>();\nconst drawnTrigger = new WeakMap<Element, string>();\nconst drawnContent = new WeakMap<Element, ApprovalModeContentState>();\n\ntype Selectable = HTMLElement & { value: string | null };\n\nconst warningMark = (document: Document) =>\n  element(document, \"span\", { \"data-status-mark\": \"\", \"data-status\": \"warning\", \"aria-hidden\": \"true\" });\n\nconst drawTrigger = (button: Element, mode: ApprovalModeRow | undefined) => {\n  const document = button.ownerDocument;\n  const children: Node[] = [\n    element(document, \"span\", { \"data-menu-trigger-icon\": \"\", \"aria-hidden\": \"true\" }),\n  ];\n  if (mode) children.push(element(document, \"span\", { \"data-approval-mode-value\": \"\" }, mode.label));\n  if (mode?.danger) children.push(warningMark(document));\n  button.replaceChildren(...children);\n};\n\nconst drawHeader = (group: Element, state: ApprovalModeContentState) => {\n  const document = group.ownerDocument;\n  let header = group.querySelector(\":scope > [data-approval-mode-header]\");\n  if (!header) {\n    header = element(document, \"div\", { \"data-approval-mode-header\": \"\" });\n    header.append(element(document, \"ui-menu-group-label\"));\n  }\n  place(group, header, group.firstElementChild);\n  const title = header.querySelector(\":scope > ui-menu-group-label\");\n  if (title && title.textContent !== state.label) title.textContent = state.label;\n};\n\n/** Learn more, after the modes behind a separator, so opening the menu with a key lands on the modes. */\nconst drawLearnMore = (content: Element, group: Element, state: ApprovalModeContentState) => {\n  const document = content.ownerDocument;\n  let separator = content.querySelector(\":scope > ui-menu-separator[data-approval-mode-learn-more]\");\n  let learnMore = content.querySelector(\":scope > ui-menu-item[data-approval-mode-learn-more]\");\n  const link = state.learnMoreHref !== undefined;\n  if (learnMore && (!state.learnMore || learnMore.hasAttribute(\"as-child\") !== link)) {\n    learnMore.remove();\n    learnMore = null;\n  }\n  if (!state.learnMore) {\n    separator?.remove();\n    return;\n  }\n  separator ??= element(document, \"ui-menu-separator\", { \"data-approval-mode-learn-more\": \"\" });\n  if (!learnMore) {\n    // With `as-child` the link is the item, so the inset goes on it.\n    const attributes = { value: APPROVAL_MODE_LEARN_MORE, \"data-approval-mode-learn-more\": \"\" };\n    learnMore = link\n      ? element(document, \"ui-menu-item\", { \"as-child\": \"\", ...attributes })\n      : element(document, \"ui-menu-item\", { ...attributes, \"data-menu-inset\": \"\" }, \"Learn more\");\n    if (link) learnMore.append(element(document, \"a\", { \"data-menu-inset\": \"\" }, \"Learn more\"));\n  }\n  place(content, separator, group.nextElementSibling);\n  place(content, learnMore, separator.nextElementSibling);\n  const anchor = learnMore.querySelector(\":scope > a\");\n  if (anchor && state.learnMoreHref && anchor.getAttribute(\"href\") !== state.learnMoreHref)\n    anchor.setAttribute(\"href\", state.learnMoreHref);\n};\n\nconst drawRow = (document: Document, el: Element | undefined, row: ApprovalModeRow) => {\n  let item = el;\n  if (!item) {\n    item = element(document, \"ui-menu-radio-item\", { value: row.value, \"close-on-select\": \"\" });\n    const indicator = element(document, \"ui-menu-item-indicator\", { \"keep-mounted\": \"\" });\n    indicator.append(element(document, \"span\", { \"data-menu-check\": \"\", \"aria-hidden\": \"true\" }));\n    item.append(indicator);\n  }\n  item.setAttribute(\"label\", row.label);\n  item.toggleAttribute(\"data-danger\", row.danger);\n  const indicator = item.querySelector(\":scope > ui-menu-item-indicator\");\n  let risk = item.querySelector(\":scope > [data-approval-mode-risk]\");\n  // Clears the item when the label or description changed, so the indicator and the risk badge go back after.\n  drawMenuItemContent(item, row);\n  if (indicator) place(item, indicator, item.firstElementChild);\n  if (row.danger) {\n    if (!risk) {\n      risk = element(document, \"span\", { \"data-approval-mode-risk\": \"\" }, \"Risky\");\n    }\n    place(item, risk, null);\n  } else risk?.remove();\n  return item;\n};\n\n/**\n * Draws the header and the rows into the radio group, reusing each radio item by its `value` and moving only what is\n * out of place, so a person's focus and highlight survive a change of `modes` or `value` while the menu is open.\n */\nconst drawContent = (content: Element, state: ApprovalModeContentState) => {\n  const document = content.ownerDocument;\n  let group = content.querySelector<Selectable>(\":scope > ui-menu-radio-group\");\n  if (!group) {\n    group = element(document, \"ui-menu-radio-group\", { name: APPROVAL_MODE_GROUP }) as Selectable;\n    content.append(group);\n  }\n  drawHeader(group, state);\n  const existing = new Map<string, Element>();\n  for (const item of group.querySelectorAll(\":scope > ui-menu-radio-item\"))\n    existing.set(item.getAttribute(\"value\") ?? \"\", item);\n  let previous = group.querySelector(\":scope > [data-approval-mode-header]\");\n  for (const row of state.rows) {\n    const item = drawRow(document, existing.get(row.value), row);\n    existing.delete(row.value);\n    place(group, item, previous ? previous.nextElementSibling : group.firstElementChild);\n    previous = item;\n  }\n  for (const item of existing.values()) item.remove();\n  if (group.value !== state.value) group.value = state.value;\n  drawLearnMore(content, group, state);\n};\n\n/**\n * `<kind-approval-mode>` fills in its trigger and menu when it has no children. Set `modes` as a property and `value`\n * as a property or attribute; a person's choice fires `modechange`. With `can-learn-more`, Learn more fires\n * `learnmore`. Markup you write puts the `ui-menu` inside a `kind-internal-menu`, which styles it, and an empty\n * `ui-menu-content` in `kind-approval-mode-content` gets the header, the modes and Learn more.\n */\nexport function defineApprovalMode(): void {\n  if (customElements.get(\"kind-approval-mode\")) return;\n  defineInternalMenu();\n  createParts(approvalModePrimitive, {\n    root: {\n      tag: \"kind-approval-mode\",\n      kinds: { canLearnMore: \"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 KindApprovalModeElement).canLearnMore ? {} : { onLearnMore: undefined }),\n      defaults: { agentConfirm: true },\n      state: {\n        open: {\n          get: (api) => api.isOpen(),\n          set: (api, open) => api.setOpen(!!open),\n        },\n        value: {\n          get: (api) => api.getValue(),\n          set: (api, value) => api.setValue?.(String(value)),\n        },\n      },\n      // The inner menu fires a `valuechange` and an `action` of its own.\n      events: { onValueChange: \"modechange\", onLearnMore: \"learnmore\" },\n      api: [\"isOpen\", \"setOpen\", \"getValue\"],\n      render: (el, instance) => {\n        if (!el.firstElementChild) el.innerHTML = DEFAULT_PARTS;\n        const api = instance.api as ApprovalModeApi;\n        const draw = () => {\n          const state = instance.get() as ApprovalModeState;\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-approval-mode-trigger > ui-menu-trigger\");\n          const props = api.triggerState(state);\n          trigger?.toggleAttribute(\"disabled\", props.disabled);\n          const button = trigger?.querySelector(\"button\");\n          if (button) {\n            if (button.ariaLabel !== props[\"aria-label\"]) button.ariaLabel = props[\"aria-label\"];\n            button.toggleAttribute(\"data-kind-trigger\", props[\"data-kind-trigger\"] === \"\");\n            if (!button.firstChild) owned.add(button);\n            // By content, since every new `modes` array makes new rows.\n            const { mode } = props;\n            const key = mode ? `${mode.danger}:${mode.label}` : \"\";\n            if (owned.has(button) && drawnTrigger.get(button) !== key) {\n              drawnTrigger.set(button, key);\n              drawTrigger(button, props.mode);\n            }\n          }\n          const content = el.querySelector(\"kind-approval-mode-content > ui-menu-content\");\n          const contentState = api.contentState(state);\n          if (content && !content.firstChild) owned.add(content);\n          if (content && owned.has(content) && drawnContent.get(content) !== contentState) {\n            drawnContent.set(content, contentState);\n            drawContent(content, contentState);\n          }\n        };\n        draw();\n        return instance.subscribe(draw);\n      },\n    },\n    trigger: { tag: \"kind-approval-mode-trigger\", display: \"contents\" },\n    content: { tag: \"kind-approval-mode-content\", display: \"contents\" },\n  });\n}\n\nexport type KindApprovalModeEvents = {\n  modechange: UiChangeEvent<string, ApprovalModeChangeReason>;\n  learnmore: UiChangeEvent<string | undefined, \"item-press\">;\n  openchange: UiChangeEvent<boolean, MenuOpenChangeReason>;\n};\n\nexport interface KindApprovalModeElement\n  extends UiElement<KindApprovalModeEvents>,\n    AttributeProperties<typeof approvalModePrimitive, \"api\" | \"modes\" | \"value\" | \"open\"> {\n  modes?: NonNullable<ApprovalModeOptions[\"modes\"]>;\n  /** The chosen mode's `value`. Setting it chooses that mode without firing `modechange`. */\n  get value(): string | undefined;\n  set value(value: string);\n  open: boolean;\n  /** Whether the menu offers Learn more without a `learn-more-href`, which fires `learnmore`. */\n  canLearnMore: boolean;\n  api: Pick<ApprovalModeApi, \"isOpen\" | \"setOpen\" | \"getValue\">;\n}\n\ndeclare global {\n  interface HTMLElementTagNameMap {\n    \"kind-approval-mode\": KindApprovalModeElement;\n    \"kind-approval-mode-trigger\": HTMLElement;\n    \"kind-approval-mode-content\": HTMLElement;\n  }\n}\n",
      "type": "registry:ui",
      "target": "@ui/web/approval-mode.ts"
    }
  ],
  "meta": {
    "layer": "web"
  },
  "type": "registry:ui"
}
