{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "settings-menu-web",
  "title": "Settings Menu (Web Components)",
  "description": "An icon button that opens a menu of settings: toggles drawn as switches, and choices in submenus showing their value.",
  "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/settings-menu.ts",
      "content": "import {\n  type SettingsMenuApi,\n  type SettingsMenuChangeReason,\n  type SettingsMenuChoiceRow,\n  type SettingsMenuContentState,\n  type SettingsMenuOptions,\n  type SettingsMenuRow,\n  type SettingsMenuState,\n  settingsMenuPrimitive,\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-settings-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-settings-menu-trigger><kind-settings-menu-content><ui-menu-content side-offset=\"6\" align=\"start\"></ui-menu-content></kind-settings-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, SettingsMenuContentState>();\nconst drawnRows = new WeakMap<Element, SettingsMenuChoiceRow>();\n\ntype Checkable = HTMLElement & { checked: boolean };\ntype Selectable = HTMLElement & { value: string | null };\n\nconst drawToggle = (\n  document: Document,\n  el: Element | undefined,\n  row: SettingsMenuRow & { type: \"toggle\" },\n) => {\n  const item = (el ?? element(document, \"ui-menu-checkbox-item\", { \"data-row\": row.id })) as Checkable;\n  item.setAttribute(\"value\", row.id);\n  item.setAttribute(\"label\", row.label);\n  let indicator = item.querySelector(\":scope > [data-menu-switch]\");\n  if (!indicator) {\n    indicator = element(document, \"ui-menu-item-indicator\", {\n      \"keep-mounted\": \"\",\n      \"data-menu-switch\": \"\",\n      \"aria-hidden\": \"true\",\n    });\n    indicator.append(element(document, \"span\", { \"data-menu-switch-thumb\": \"\" }));\n  }\n  drawMenuItemContent(item, row, [indicator]);\n  if (item.checked !== row.checked) item.checked = row.checked;\n  return item;\n};\n\n/** The value's labels, one per option, reused by `value` so the row keeps its width as the value changes. */\nconst drawLabels = (document: Document, value: Element, options: SettingsMenuChoiceRow[\"options\"]) => {\n  const labels = new Map([...value.children].map((el) => [el.getAttribute(\"data-value\"), el]));\n  let previous: Element | null = null;\n  for (const option of options) {\n    const label = labels.get(option.value) ?? element(document, \"span\", { \"data-value\": option.value });\n    labels.delete(option.value);\n    if (label.textContent !== option.label) label.textContent = option.label;\n    place(value, label, previous ? previous.nextElementSibling : value.firstElementChild);\n    previous = label;\n  }\n  for (const el of labels.values()) el.remove();\n};\n\n/**\n * A choice's radio group, holding a labelled group per option `group`, then the actions, behind a separator unless\n * the row is `inline`. Radio items and actions are reused by `value`, so a focused one keeps focus.\n */\nconst drawChoices = (document: Document, content: Element, row: SettingsMenuChoiceRow) => {\n  const items = new Map<string, Element>();\n  for (const item of content.querySelectorAll(\"ui-menu-radio-item, :scope > ui-menu-item\"))\n    items.set(item.getAttribute(\"value\") ?? \"\", item);\n  let radios = content.querySelector<Selectable>(\":scope > ui-menu-radio-group\");\n  if (!radios) {\n    radios = element(document, \"ui-menu-radio-group\", { name: row.id }) as Selectable;\n    content.prepend(radios);\n  }\n  const groups = new Map<string, Element>();\n  for (const group of radios.querySelectorAll(\":scope > ui-menu-group\"))\n    groups.set(group.getAttribute(\"data-group\") ?? \"\", group);\n  let previous: Element | null = null;\n  const next = (parent: Element, child: Element, after: Element | null) => {\n    place(parent, child, after ? after.nextElementSibling : parent.firstElementChild);\n    return child;\n  };\n  for (const { label, options } of row.groups) {\n    let parent: Element = radios;\n    if (label !== undefined) {\n      let group = groups.get(label);\n      groups.delete(label);\n      if (!group) {\n        group = element(document, \"ui-menu-group\", { \"data-group\": label });\n        group.append(element(document, \"ui-menu-group-label\", {}, label));\n      }\n      previous = next(radios, group, previous);\n      parent = group;\n    }\n    let last: Element | null = parent === radios ? previous : parent.firstElementChild;\n    for (const option of options) {\n      let item = items.get(option.value);\n      items.delete(option.value);\n      item ??= element(document, \"ui-menu-radio-item\", { value: option.value });\n      let indicator = item.querySelector(\":scope > ui-menu-item-indicator\");\n      if (!indicator) {\n        indicator = element(document, \"ui-menu-item-indicator\", { \"keep-mounted\": \"\" });\n        indicator.append(element(document, \"span\", { \"data-menu-check\": \"\", \"aria-hidden\": \"true\" }));\n      }\n      item.setAttribute(\"label\", option.label);\n      item.toggleAttribute(\"disabled\", option.disabled ?? false);\n      drawMenuItemContent(item, option, [indicator]);\n      // The check comes first, as React's radio item draws it.\n      if (item.firstElementChild !== indicator) item.prepend(indicator);\n      last = next(parent, item, last);\n    }\n    if (parent === radios) previous = last;\n  }\n  if (radios.value !== (row.value ?? null)) radios.value = row.value ?? null;\n  let after: Element | null = radios;\n  let separator = content.querySelector(\n    \":scope > ui-menu-separator:not([data-section-end], [data-menu-note-separator])\",\n  );\n  if (row.actions.length && !row.inline) {\n    separator ??= element(document, \"ui-menu-separator\");\n    after = next(content, separator, after);\n  } else separator?.remove();\n  for (const action of row.actions) {\n    let item = items.get(action.value);\n    items.delete(action.value);\n    item ??= element(document, \"ui-menu-item\", { value: action.value });\n    item.setAttribute(\"label\", action.label);\n    item.toggleAttribute(\"data-menu-inset\", row.inline);\n    drawMenuItemContent(item, action);\n    after = next(content, item, after);\n  }\n  let note = content.querySelector(\":scope > [data-menu-note]\");\n  let noteSeparator = content.querySelector(\":scope > ui-menu-separator[data-menu-note-separator]\");\n  if (row.note) {\n    noteSeparator ??= element(document, \"ui-menu-separator\", { \"data-menu-note-separator\": \"\" });\n    after = next(content, noteSeparator, after);\n    // Each row's name says its cost in words, so the note is for the eye.\n    note ??= element(document, \"span\", { \"data-menu-note\": \"\", \"aria-hidden\": \"true\" });\n    if (note.textContent !== row.note) note.textContent = row.note;\n    next(content, note, after);\n  } else {\n    note?.remove();\n    noteSeparator?.remove();\n  }\n  for (const el of [...items.values(), ...groups.values()]) el.remove();\n};\n\nconst drawChoice = (\n  document: Document,\n  el: Element | undefined,\n  row: SettingsMenuRow & { type: \"choice\" },\n) => {\n  let sub = el;\n  if (!sub) {\n    sub = element(document, \"ui-menu-sub\", { \"data-row\": row.id });\n    sub.append(element(document, \"ui-menu-sub-trigger\"), element(document, \"ui-menu-sub-content\"));\n  }\n  const trigger = sub.querySelector(\":scope > ui-menu-sub-trigger\");\n  const content = sub.querySelector(\":scope > ui-menu-sub-content\");\n  if (!trigger || !content) return sub;\n  const value =\n    trigger.querySelector(\":scope > [data-settings-menu-value]\") ??\n    element(document, \"span\", { \"data-settings-menu-value\": \"\" });\n  const chevron =\n    trigger.querySelector(\":scope > [data-menu-chevron]\") ??\n    element(document, \"span\", { \"data-menu-chevron\": \"\" });\n  drawMenuItemContent(trigger, row, [value, chevron]);\n  if (drawnRows.get(sub) !== row) {\n    drawnRows.set(sub, row);\n    drawLabels(document, value, row.options);\n    drawChoices(document, content, row);\n  }\n  for (const [index, label] of [...value.children].entries())\n    label.toggleAttribute(\"data-current\", row.options[index]?.value === row.value);\n  return sub;\n};\n\n/** An inline choice: its options in the menu itself, and a separator after them unless it's the last row. */\nconst drawSection = (\n  document: Document,\n  el: Element | undefined,\n  row: SettingsMenuChoiceRow,\n  last: boolean,\n) => {\n  const section = el ?? element(document, \"div\", { \"data-row\": row.id, \"data-settings-menu-section\": \"\" });\n  if (drawnRows.get(section) !== row) {\n    drawnRows.set(section, row);\n    drawChoices(document, section, row);\n  }\n  let end = section.querySelector(\":scope > ui-menu-separator[data-section-end]\");\n  if (last) end?.remove();\n  else if (!end || end !== section.lastElementChild) {\n    end ??= element(document, \"ui-menu-separator\", { \"data-section-end\": \"\" });\n    section.append(end);\n  }\n  return section;\n};\n\n/**\n * Draws the rows into `parent`, reusing each row's element by its `id` and moving only what is out of place, so a\n * person's focus and highlight survive a change of `settings` or `values` while the menu is open. Model Control draws\n * its rows with it too.\n */\nexport const drawSettingsRows = (parent: Element, rows: SettingsMenuRow[]) => {\n  const document = parent.ownerDocument;\n  const existing = new Map<string, Element>();\n  for (const row of parent.querySelectorAll(\":scope > [data-row]\"))\n    existing.set(row.getAttribute(\"data-row\") ?? \"\", row);\n  let previous: Element | null = null;\n  for (const [index, row] of rows.entries()) {\n    let el = existing.get(row.id);\n    existing.delete(row.id);\n    const tag = row.type === \"toggle\" ? \"ui-menu-checkbox-item\" : row.inline ? \"div\" : \"ui-menu-sub\";\n    if (el && el.localName !== tag) {\n      el.remove();\n      el = undefined;\n    }\n    el =\n      row.type === \"toggle\"\n        ? drawToggle(document, el, row)\n        : row.inline\n          ? drawSection(document, el, row, index === rows.length - 1)\n          : drawChoice(document, el, row);\n    place(parent, el, previous ? previous.nextElementSibling : parent.firstElementChild);\n    previous = el;\n  }\n  for (const row of existing.values()) row.remove();\n};\n\n/**\n * `<kind-settings-menu>` fills in its trigger and menu when it has no children. Set `settings` and `values` as\n * properties; a change fires `valueschange` with every value as `detail.value` and the setting in `detail.id`. Markup\n * you write puts the `ui-menu` inside a `kind-internal-menu`, which styles it, and an empty `ui-menu-content` in\n * `kind-settings-menu-content` gets the rows.\n */\nexport function defineSettingsMenu(): void {\n  if (customElements.get(\"kind-settings-menu\")) return;\n  defineInternalMenu();\n  createParts(settingsMenuPrimitive, {\n    root: {\n      tag: \"kind-settings-menu\",\n      state: {\n        open: {\n          get: (api) => api.isOpen(),\n          set: (api, open) => api.setOpen(!!open),\n        },\n        values: {\n          get: (api) => api.getValues(),\n          set: (api, values) => api.setValues((values ?? {}) as Record<string, boolean | string>),\n        },\n      },\n      api: [\"isOpen\", \"setOpen\", \"getValues\", \"setValues\"],\n      render: (el, instance) => {\n        if (!el.firstElementChild) el.innerHTML = DEFAULT_PARTS;\n        const api = instance.api as SettingsMenuApi;\n        const draw = () => {\n          const state = instance.get() as SettingsMenuState;\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-settings-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-settings-menu-content > ui-menu-content\");\n          const rows = api.contentState(state);\n          // A content written empty fills itself in, as React's bare `SettingsMenuContent` 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            drawSettingsRows(content, rows.rows);\n          }\n        };\n        draw();\n        return instance.subscribe(draw);\n      },\n    },\n    trigger: { tag: \"kind-settings-menu-trigger\", display: \"contents\" },\n    content: { tag: \"kind-settings-menu-content\", display: \"contents\" },\n  });\n}\n\nexport type KindSettingsMenuEvents = {\n  valueschange: UiChangeEvent<Record<string, boolean | string>, SettingsMenuChangeReason> & {\n    detail: { id: string };\n  };\n  openchange: UiChangeEvent<boolean, MenuOpenChangeReason>;\n};\n\nexport interface KindSettingsMenuElement\n  extends UiElement<KindSettingsMenuEvents>,\n    AttributeProperties<typeof settingsMenuPrimitive, \"api\" | \"settings\" | \"values\" | \"open\"> {\n  /**\n   * A setting'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  settings?: (NonNullable<SettingsMenuOptions[\"settings\"]>[number] & { icon?: string | Element })[];\n  /** The values by setting `id`. Setting it changes each value that differs, firing `valueschange` for each. */\n  values: Record<string, boolean | string>;\n  open: boolean;\n  api: Pick<SettingsMenuApi, \"isOpen\" | \"setOpen\" | \"getValues\" | \"setValues\">;\n}\n\ndeclare global {\n  interface HTMLElementTagNameMap {\n    \"kind-settings-menu\": KindSettingsMenuElement;\n    \"kind-settings-menu-trigger\": HTMLElement;\n    \"kind-settings-menu-content\": HTMLElement;\n  }\n}\n",
      "type": "registry:ui",
      "target": "@ui/web/settings-menu.ts"
    }
  ],
  "meta": {
    "layer": "web"
  },
  "type": "registry:ui"
}
