{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "core-dropdown",
  "type": "registry:lib",
  "title": "core/dropdown",
  "description": "Framework-free behaviour shared by the ui-* components (dropdown); no DOM, no lit. 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": [],
  "registryDependencies": [
    "@kanok-ui/core-listbox",
    "@kanok-ui/core-typeahead"
  ],
  "files": [
    {
      "path": "src/core/dropdown.ts",
      "type": "registry:lib",
      "target": "components/ui/core/dropdown.ts",
      "content": "/**\n * Keyboard of a single-select list box (Carbon Dropdown, traced on react.carbondesignsystem.com):\n * the next state for a key, as data. Closed: ↓ / Enter / Space open on the chosen option (else the\n * first), ↑ does nothing (consumed), Home / End open on the first / last, a letter opens on the\n * match without picking it. Open: arrows / Page / Home / End move the highlight (no wrap),\n * Enter / Space / Alt+↑ pick it and close, Esc closes, Tab closes without picking (and moves on),\n * letters move the highlight (typeahead).\n */\nimport { firstEnabled, lastEnabled, stepEnabled, type LabelledOption } from './listbox.ts';\nimport type { KeyInput, Typeahead } from './typeahead.ts';\n\n/** Rows Page Up / Page Down move. */\nexport const PAGE = 10;\n\nexport interface DropdownState {\n    open: boolean;\n    /** Highlighted row; -1 for none. */\n    active: number;\n    /** Chosen row; -1 for none. */\n    selected: number;\n}\n\nexport interface DropdownStep {\n    open: boolean;\n    active: number;\n    /** Pick this row (the renderer commits it unless it is disabled or already chosen). */\n    pick?: number;\n    /** The key was used: prevent its default (Tab is used but not prevented: focus moves on). */\n    handled: boolean;\n}\n\n/** Where a closed list opens from the keyboard: the chosen row, else the first enabled one. */\nexport const openingRow = (options: readonly LabelledOption[], selected: number) => (selected >= 0 ? selected : firstEnabled(options));\n\nconst enabled = (o: LabelledOption) => !o.disabled;\n\n/** A key on the closed list: what opens it, and where. */\nfunction closedStep(state: DropdownState, options: readonly LabelledOption[], key: KeyInput, typeahead: Typeahead, now: number): DropdownStep | undefined {\n    const open = (active: number): DropdownStep => ({ open: true, active, handled: true });\n    switch (key.key) {\n        case 'ArrowDown':\n        case 'Enter':\n        case ' ':\n            return open(openingRow(options, state.selected));\n        case 'ArrowUp':\n            return { ...state, handled: true }; // Carbon: ↑ doesn't open (consumed so the page doesn't scroll)\n        case 'Home':\n            return open(firstEnabled(options));\n        case 'End':\n            return open(lastEnabled(options));\n        default: {\n            // Carbon: a letter opens on the match without picking it\n            const match = typeahead.match(key, options, state.selected, (o) => o.label, { enabled, now });\n            if (match === undefined) return undefined;\n            return match >= 0 ? open(match) : { ...state, handled: true };\n        }\n    }\n}\n\n/** The next state for `key`; undefined when the key isn't the list's (leave it alone). */\nexport function dropdownKey(state: DropdownState, options: readonly LabelledOption[], key: KeyInput, typeahead: Typeahead, now = Date.now()): DropdownStep | undefined {\n    if (!state.open) return closedStep(state, options, key, typeahead, now);\n    const close = (pick?: number): DropdownStep => ({ open: false, active: -1, pick, handled: true });\n    const pickActive = state.active >= 0 ? state.active : undefined;\n    if (key.key === 'Tab') return { open: false, active: -1, handled: false }; // Carbon: Tab doesn't pick\n    if (key.key === 'ArrowUp' && key.altKey) return close(pickActive);\n    const moves: Record<string, () => number> = {\n        ArrowDown: () => stepEnabled(options, state.active, 1),\n        ArrowUp: () => stepEnabled(options, state.active, -1),\n        PageDown: () => stepEnabled(options, state.active, PAGE),\n        PageUp: () => stepEnabled(options, state.active, -PAGE),\n        Home: () => firstEnabled(options),\n        End: () => lastEnabled(options),\n    };\n    if (key.key in moves) return { open: true, active: moves[key.key](), handled: true };\n    if (key.key === 'Enter' || key.key === ' ') return close(pickActive);\n    if (key.key === 'Escape') return close();\n    const match = typeahead.match(key, options, state.active, (o) => o.label, { enabled, now });\n    if (match === undefined) return undefined;\n    return { open: true, active: match >= 0 ? match : state.active, handled: true };\n}\n"
    }
  ]
}
