{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "accordion",
  "type": "registry:ui",
  "title": "Accordion",
  "description": "A stack of headings that each open and close a panel: one open at a time or several, arrow-key navigation between headings.",
  "dependencies": [
    "lucide-react@^1.49.0"
  ],
  "registryDependencies": [
    "https://ui.klarhimmel.com/r/theme.json",
    "https://ui.klarhimmel.com/r/utils.json"
  ],
  "files": [
    {
      "path": "src/ui/accordion.tsx",
      "type": "registry:file",
      "target": "components/klarhimmel/ui/accordion.tsx",
      "content": "import { createContext, useCallback, useContext, useId, useRef, useState, type KeyboardEvent, type ReactNode } from 'react';\nimport { ChevronDown } from 'lucide-react';\nimport { cn, moveFocus } from '../lib/utils';\n\ninterface Ctx {\n  isOpen: (v: string) => boolean;\n  toggle: (v: string) => void;\n  /** Single mode without `collapsible`: the one open item cannot be closed. */\n  locked: (v: string) => boolean;\n}\nconst AccordionCtx = createContext<Ctx | null>(null);\n\ntype AccordionProps = (\n  | { type?: 'single'; value?: string; defaultValue?: string; onValueChange?: (value: string) => void; collapsible?: boolean }\n  | { type: 'multiple'; value?: string[]; defaultValue?: string[]; onValueChange?: (value: string[]) => void; collapsible?: never }\n) & { children: ReactNode; className?: string };\n\nconst asList = (v: string | string[] | undefined) => (v === undefined || v === '' ? [] : Array.isArray(v) ? v : [v]);\n\n/**\n * A stack of headings that each open and close a panel (WAI-ARIA accordion). `type=\"multiple\"` lets several stay open;\n * single, the default, keeps one open and lets it close unless `collapsible={false}`.\n */\nexport function Accordion(props: AccordionProps) {\n  const { children, className, type = 'single' } = props;\n  const multiple = type === 'multiple';\n  const [own, setOwn] = useState<string[]>(asList(props.defaultValue));\n  const open = props.value !== undefined ? asList(props.value) : own;\n  const collapsible = props.collapsible ?? true;\n  const root = useRef<HTMLDivElement>(null);\n\n  const toggle = useCallback(\n    (v: string) => {\n      const was = open.includes(v);\n      const next = multiple ? (was ? open.filter((x) => x !== v) : [...open, v]) : was ? [] : [v];\n      setOwn(next);\n      if (multiple) (props.onValueChange as ((v: string[]) => void) | undefined)?.(next);\n      else (props.onValueChange as ((v: string) => void) | undefined)?.(next[0] ?? '');\n    },\n    [open, multiple, props.onValueChange], // eslint-disable-line react-hooks/exhaustive-deps\n  );\n\n  const onKeyDown = (e: KeyboardEvent<HTMLDivElement>) => {\n    const t = e.target as HTMLElement;\n    if (!t.matches('[data-kh-accordion-trigger]') || !root.current) return;\n    if (moveFocus(root.current, t, e.key, 'vertical', '[data-kh-accordion-trigger]')) e.preventDefault();\n  };\n\n  return (\n    <AccordionCtx.Provider value={{ isOpen: (v) => open.includes(v), toggle, locked: (v) => !multiple && !collapsible && open.includes(v) }}>\n      <div ref={root} onKeyDown={onKeyDown} className={cn('divide-y divide-kh-line rounded-kh-lg border border-kh-line bg-kh-surface', className)}>\n        {children}\n      </div>\n    </AccordionCtx.Provider>\n  );\n}\n\n/** One heading and its panel. `level` is the heading level, to fit the page outline. */\nexport function AccordionItem({ value, title, level = 3, disabled, children }: { value: string; title: ReactNode; level?: 2 | 3 | 4 | 5; disabled?: boolean; children: ReactNode }) {\n  const ctx = useContext(AccordionCtx);\n  if (!ctx) throw new Error('AccordionItem must sit inside an Accordion');\n  const id = useId();\n  const open = ctx.isOpen(value);\n  const Heading = `h${level}` as const;\n  return (\n    <div data-state={open ? 'open' : 'closed'}>\n      <Heading className=\"m-0\">\n        <button\n          type=\"button\"\n          id={`${id}-t`}\n          data-kh-accordion-trigger=\"\"\n          aria-expanded={open}\n          aria-controls={`${id}-p`}\n          aria-disabled={ctx.locked(value) || undefined}\n          disabled={disabled}\n          onClick={() => !ctx.locked(value) && ctx.toggle(value)}\n          className={cn(\n            'flex min-h-12 w-full items-center justify-between gap-4 px-4 py-3 text-left text-kh-body font-medium text-kh-ink transition-colors duration-(--kh-dur)',\n            'hover:bg-kh-surface-2 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-(--kh-focus) disabled:cursor-not-allowed disabled:text-kh-muted disabled:hover:bg-transparent',\n          )}\n        >\n          <span>{title}</span>\n          <ChevronDown aria-hidden className={cn('size-4 shrink-0 text-kh-muted transition-transform duration-(--kh-dur)', open && 'rotate-180')} />\n        </button>\n      </Heading>\n      <div id={`${id}-p`} role=\"region\" aria-labelledby={`${id}-t`} hidden={!open} className=\"px-4 pb-4 text-kh-body text-kh-muted\">\n        {children}\n      </div>\n    </div>\n  );\n}\n"
    }
  ]
}
