{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "shared-listbox",
  "type": "registry:lib",
  "title": "shared/listbox",
  "description": "Internal module shared by the ui-* components (listbox). Copied from @purpurabog/ui 2.3.0; the package is the maintained source, this copy is yours to change. Icons from the Carbon Design System (Apache-2.0, © IBM Corp.; see THIRD-PARTY-NOTICES.md in the package).",
  "dependencies": [
    "lit@^3.3.0"
  ],
  "registryDependencies": [
    "@kanok-ui/tokens"
  ],
  "files": [
    {
      "path": "src/primitives/shared/listbox.ts",
      "type": "registry:lib",
      "target": "components/ui/primitives/shared/listbox.ts",
      "content": "import { css, html, type TemplateResult } from 'lit';\n\n/** One choice read from an `<option>` child — shared by ui-dropdown and ui-combobox. */\nexport interface OptionData {\n    value: string;\n    label: string;\n    disabled: boolean;\n    /** Optional second line, from `<option data-description>`. Not searched by filtering or type-ahead. */\n    description: string;\n}\n\nexport const readOption = (o: HTMLOptionElement): OptionData => ({\n    value: o.value,\n    label: o.label || o.textContent?.trim() || '',\n    disabled: o.disabled,\n    description: o.dataset.description ?? '',\n});\n\n/** The text of a row: one line, or label over description when the option has one. */\nexport function renderOptionText(o: OptionData): TemplateResult {\n    if (!o.description) return html`<span class=\"text\">${o.label}</span>`;\n    return html`<span class=\"text two-line\">\n        <span class=\"line\">${o.label}</span>\n        <span class=\"line description\" part=\"option-description\">${o.description}</span>\n    </span>`;\n}\n\n/** Options as authored: loose `<option>`s form an unlabelled group, each `<optgroup>` a labelled one. */\nexport interface OptionGroup {\n    label: string | null;\n    options: OptionData[];\n}\n\nexport function readOptionGroups(host: Element): OptionGroup[] {\n    const groups: OptionGroup[] = [];\n    let loose: OptionGroup | null = null;\n    for (const child of host.children) {\n        if (child instanceof HTMLOptGroupElement) {\n            const options = [...child.querySelectorAll('option')].map((o) => ({ ...readOption(o), disabled: o.disabled || child.disabled }));\n            groups.push({ label: child.label, options });\n            loose = null;\n        } else if (child instanceof HTMLOptionElement) {\n            loose ??= { label: null, options: [] };\n            if (!groups.includes(loose)) groups.push(loose);\n            loose.options.push(readOption(child));\n        }\n    }\n    return groups;\n}\n\nexport const flattenGroups = (groups: OptionGroup[]) => groups.flatMap((g) => g.options);\n\n/** Two options are the same one when their values are (the options lifecycle finds the highlight again by it). */\nexport const sameOption = (a: OptionData, b: OptionData) => a.value === b.value;\n\nexport const hasLabelledGroup = (groups: OptionGroup[]) => groups.some((g) => g.label !== null);\n\n/**\n * Renders rows group by group with one running index, so keyboard code can stay flat.\n * Labelled groups become a `role=\"group\"` inside a presentational item (APG listbox grouping; axe doesn't\n * allow the group role on an `<li>`); unlabelled rows render bare.\n */\nexport function renderGroupedRows(\n    groups: OptionGroup[],\n    fieldId: string,\n    renderRow: (option: OptionData, index: number) => TemplateResult,\n): TemplateResult[] {\n    let i = 0;\n    return groups.map((g, gi) => {\n        const rows = g.options.map((o) => renderRow(o, i++));\n        if (g.label === null) return html`${rows}`;\n        const id = `${fieldId}-group-${gi}`;\n        return html`<li role=\"presentation\">\n            <div role=\"group\" aria-labelledby=${id}>\n                <div class=\"group-label\" part=\"group-label\" id=${id}>${g.label}</div>\n                <ul role=\"presentation\">\n                    ${rows}\n                </ul>\n            </div>\n        </li>`;\n    });\n}\n\n/**\n * The floating list (Carbon `list-box__menu`): host must define `--_h` (row height).\n * Rows are `.option`; `.active` is the keyboard highlight, `aria-selected` the committed choice.\n */\nexport const listboxStyles = css`\n    .menu {\n        position: absolute; /* document coordinates in the top layer: scrolls with the page, see position.ts */\n        inset: auto;\n        box-sizing: border-box;\n        margin: 0;\n        padding: var(--ui-menu-padding, 0); /* Carbon: rows edge to edge; a design can inset them */\n        /* Carbon: square, shadow only; --ui-menu-* let a design round and outline it */\n        border: var(--ui-menu-border, none);\n        border-radius: var(--ui-menu-radius, 0);\n        list-style: none;\n        background-color: var(--ui-overlay-background, var(--ui-surface-1)); /* a design can raise floating surfaces (--ui-overlay-background) */\n        color: var(--ui-ink);\n        font: var(--ui-font-body-sm);\n        letter-spacing: var(--ui-tracking-body);\n        box-shadow: var(--ui-menu-shadow, 0 2px 6px rgb(0 0 0 / 0.3));\n        /* no taller than the room left beside its field (core/popup placePopup writes it while open): the list scrolls instead */\n        max-height: min(calc(var(--_h) * var(--ui-menu-max-rows, 5.5)), var(--ui-available-height, 100vh));\n        overflow-y: auto;\n        overscroll-behavior: contain; /* scrolling past the end doesn't scroll the page (which would move the list off its field) */\n    }\n\n    .menu ul {\n        margin: 0;\n        padding: 0;\n        list-style: none;\n    }\n\n    .group-label {\n        display: flex;\n        align-items: center;\n        box-sizing: border-box;\n        height: var(--_h);\n        padding: 0 var(--ui-space-md);\n        border-top: 1px solid var(--ui-hairline);\n        color: var(--ui-ink-helper);\n        font: var(--ui-font-caption);\n        letter-spacing: var(--ui-tracking-caption);\n        text-transform: none;\n    }\n\n    /*\n     * Carbon list-box rows (measured on react.carbondesignsystem.com): text-secondary, a 1px border-subtle-01 divider\n     * inset 16px each side; hover = layer-hover with text-primary; selected = layer-selected (hover: layer-selected-hover);\n     * keyboard highlight = 2px focus outline — the mouse never moves it (Carbon web components: hover only tints the row;\n * the outline stays on the highlighted row). Dividers next to a hovered, highlighted or selected row are hidden.\n     */\n    .option {\n        position: relative;\n        display: flex;\n        align-items: center;\n        box-sizing: border-box;\n        min-height: var(--_h);\n        padding: 0 calc(var(--ui-space-md) * 2 + 16px) 0 var(--ui-space-md);\n        /* --ui-menu-item-*: Carbon rows are square, secondary text, divided; a design can round them and drop the rules */\n        border-radius: var(--ui-menu-item-radius, 0);\n        color: var(--ui-menu-item-ink, var(--ui-ink-muted));\n        cursor: pointer;\n        outline: var(--ui-focus-width, 2px) solid transparent;\n        outline-offset: -2px;\n        /* Carbon list-box__menu-item: background; its __option: text colour and divider, all at fast-01 */\n        transition:\n            background-color var(--ui-duration-fast) var(--ui-easing),\n            color var(--ui-duration-fast) var(--ui-easing);\n    }\n\n    .option + .option::before {\n        position: absolute;\n        inset-block-start: 0;\n        inset-inline: var(--ui-space-md);\n        block-size: 1px;\n        background-color: var(--ui-menu-divider, var(--ui-row-divider)); /* border-subtle-01, -02, -03 by layer */\n        content: '';\n        transition: background-color var(--ui-duration-fast) var(--ui-easing);\n    }\n\n    .option:hover:not([aria-disabled='true']) {\n        background-color: var(--ui-ghost-hover); /* layer-hover */\n        color: var(--ui-ink);\n    }\n\n    .option:active:not([aria-disabled='true']) {\n        background-color: var(--ui-menu-item-pressed, var(--ui-surface-2)); /* layer-selected; a design can make it a tint */\n    }\n\n    /* the keyboard highlight: Carbon's 2px focus outline; a design can show it as a fill instead (--ui-menu-item-active-*) */\n    .option.active {\n        outline-color: var(--ui-menu-item-active-outline, var(--ui-focus));\n        color: var(--ui-ink);\n    }\n\n    /* opened by a press: the highlighted row has no keyboard ring until a key is pressed (shared/quiet-focus; Kanok, --ui-pointer-focus) */\n    :host([data-quiet-focus]) .option.active {\n        outline-color: var(--ui-pointer-focus, var(--ui-menu-item-active-outline, var(--ui-focus)));\n    }\n\n    .option.active:not(:hover, [aria-selected='true']) {\n        background-color: var(--ui-menu-item-active-background, transparent);\n    }\n\n    .option[aria-selected='true'] {\n        background-color: var(--ui-menu-item-selected, var(--ui-surface-2)); /* layer-selected */\n        color: var(--ui-ink);\n    }\n\n    /* --ui-menu-item-selected-hover: a design's selected row a shade deeper under the pointer (Carbon: layer-selected-hover) */\n    .option[aria-selected='true']:hover {\n        background-color: var(--ui-menu-item-selected-hover, var(--ui-layer-selected-hover));\n    }\n\n    .option:hover:not([aria-disabled='true'])::before,\n    .option.active::before,\n    .option:hover:not([aria-disabled='true']) + .option::before,\n    .option.active + .option::before,\n    .option[aria-selected='true'] + .option::before {\n        background-color: transparent;\n    }\n\n    .option[aria-disabled='true'] {\n        color: var(--ui-ink-disabled);\n        cursor: not-allowed;\n        background-color: transparent;\n    }\n\n    .option .text {\n        overflow: hidden;\n        white-space: nowrap;\n        text-overflow: ellipsis;\n    }\n\n    .option .two-line {\n        display: flex;\n        flex-direction: column;\n        padding: var(--ui-space-xs) 0;\n    }\n\n    .option .line {\n        overflow: hidden;\n        text-overflow: ellipsis;\n    }\n\n    .option .description {\n        color: var(--ui-ink-helper);\n        font: var(--ui-font-caption);\n        letter-spacing: var(--ui-tracking-caption);\n    }\n\n    .option[aria-disabled='true'] .description {\n        color: inherit;\n    }\n\n    .check {\n        position: absolute;\n        inset-inline-end: var(--ui-space-md);\n        width: 16px;\n        height: 16px;\n        fill: currentColor;\n    }\n\n    @media (forced-colors: active) {\n        .menu {\n            border: 1px solid CanvasText;\n        }\n        .option.active {\n            outline-color: Highlight;\n        }\n        .option[aria-selected='true'] {\n            forced-color-adjust: none;\n            background-color: Highlight;\n            color: HighlightText;\n        }\n        .check {\n            fill: FieldText;\n        }\n    }\n`;\n\n\n/** The list's only row while it is loading or finds nothing (\"Loading…\", \"Searching…\", \"No matching items\"). */\nexport const listboxEmptyStyles = css`\n    .empty {\n        display: flex;\n        align-items: center;\n        box-sizing: border-box;\n        height: var(--_h);\n        padding: 0 var(--ui-space-md);\n        color: var(--ui-ink-helper);\n    }\n`;\n\n/**\n * The end-of-field controls of a text box with a list (Carbon ComboBox, filterable MultiSelect): status icon, × and\n * chevron in `.controls`, the input's end padding, and the \"searching\" / \"no results\" row (`.empty`). The host puts\n * `.has-clear` / `.has-status` / `.open` on the element around `input` and `.controls`.\n */\nexport const listboxControlStyles = css`\n    /* Carbon ComboBox end padding: 48, 70 with ×, 64 with a status icon, 105 with both */\n    .has-clear input {\n        padding-inline-end: 70px;\n    }\n\n    .has-status input {\n        padding-inline-end: 64px;\n    }\n\n    .has-status.has-clear input {\n        padding-inline-end: 105px;\n    }\n\n    .controls {\n        position: absolute;\n        inset-block: 0;\n        inset-inline-end: 0;\n        display: flex;\n        align-items: center;\n    }\n\n    /*\n     * Carbon geometry from the end (measured on react.carbondesignsystem.com): chevron icon 16–32px,\n     * × a 24px box at 45–69px with a 1px border-subtle divider 41px from the end (and one 74px from the end\n     * after a status icon), status icon 40–56px, or 82–98px beside the ×.\n     */\n    .status {\n        display: flex;\n        align-items: center;\n        margin-inline-end: -8px;\n        pointer-events: none;\n    }\n\n    .has-clear .status {\n        margin-inline-end: 0;\n        padding-inline-end: 13px;\n    }\n\n    .clear,\n    .toggle {\n        display: grid;\n        place-items: center;\n        height: 100%;\n        padding: 0;\n        border: none;\n        border-radius: var(--ui-icon-control-radius, 0); /* round in a design that rounds icon controls (Kanok) */\n        background: transparent;\n        color: var(--ui-ink);\n        cursor: pointer;\n    }\n\n    .clear {\n        position: relative;\n        z-index: 1;\n        width: 24px;\n        height: 24px;\n        margin-inline-end: -3px;\n    }\n\n    .clear::after,\n    .has-status .clear::before {\n        position: absolute;\n        inset-block-start: 4px;\n        inline-size: 1px;\n        block-size: 16px;\n        background-color: var(--ui-border-subtle-2); /* border-subtle-01 */\n        content: '';\n    }\n\n    .clear::after {\n        inset-inline-start: 28px;\n    }\n\n    .has-status .clear::before {\n        inset-inline-start: -5px;\n    }\n\n    .toggle {\n        width: 48px;\n    }\n\n    /* Carbon FluidComboBox: chevron 12px from the end */\n    :host([data-fluid]) .toggle {\n        width: 40px;\n    }\n\n    .clear:focus-visible,\n    .toggle:focus-visible {\n        outline: var(--ui-focus-width, 2px) solid var(--ui-focus);\n        outline-offset: -2px;\n    }\n\n    .clear svg,\n    .toggle svg {\n        width: 16px;\n        height: 16px;\n        fill: currentColor;\n    }\n\n    .toggle svg {\n        transition: transform var(--ui-duration-fast) var(--ui-easing);\n    }\n\n    .open .toggle svg {\n        transform: rotate(180deg);\n    }\n\n    :host([readonly]) .toggle {\n        color: var(--ui-ink-subtle);\n    }\n\n    .toggle:disabled {\n        color: var(--ui-ink-disabled);\n        cursor: default;\n    }\n\n    @media (forced-colors: active) {\n        .clear svg,\n        .toggle svg {\n            fill: FieldText;\n        }\n    }\n\n    /* Carbon fluid combo box: the 40px toggle 22px down, its chevron 34px down and 12px from the end */\n    :host([data-fluid]) .controls {\n        --ui-fluid-icon-top: 50px;\n        --ui-fluid-icon-end: var(--ui-space-md);\n        inset-block: 22px auto;\n        block-size: 40px;\n    }\n`;\n"
    }
  ]
}
