{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "toggle-group",
  "type": "registry:ui",
  "title": "Toggle Group",
  "description": "A row of toggles that belong together: single (a radio group) or multiple, one tab stop with arrow-key movement.",
  "dependencies": [],
  "registryDependencies": [
    "https://ui.klarhimmel.com/r/theme.json",
    "https://ui.klarhimmel.com/r/toggle.json",
    "https://ui.klarhimmel.com/r/utils.json"
  ],
  "files": [
    {
      "path": "src/ui/toggle-group.tsx",
      "type": "registry:file",
      "target": "components/klarhimmel/ui/toggle-group.tsx",
      "content": "import { createContext, useContext, useLayoutEffect, useRef, useState, type KeyboardEvent, type ReactNode } from 'react';\nimport { cn, moveFocus } from '../lib/utils';\nimport { toggleClass } from './toggle';\n\nexport type ToggleGroupVariant = 'segmented' | 'chips';\n\ninterface Ctx {\n  multiple: boolean;\n  variant: ToggleGroupVariant;\n  isOn: (v: string) => boolean;\n  toggle: (v: string) => void;\n  /** The one item in the tab order: the focused one, else the pressed one (single), else the first. */\n  stop: string | undefined;\n  focusItem: (v: string) => void;\n}\nconst GroupCtx = createContext<Ctx | null>(null);\n\ntype Props = (\n  | { type: 'single'; value?: string; defaultValue?: string; onValueChange?: (value: string) => void }\n  | { type: 'multiple'; value?: string[]; defaultValue?: string[]; onValueChange?: (value: string[]) => void }\n) & {\n  /** Required: names the group (\"Text alignment\"). */\n  label: string;\n  /** 'segmented' (default): buttons on one quiet track, for a view or an alignment. 'chips': separate pills that wrap, for a filter or\n   *  a set of options (room type, amenities); an on chip is the soft accent with an accent ring, in light and dark alike. */\n  variant?: ToggleGroupVariant;\n  className?: string;\n  children: ReactNode;\n};\n\nconst asList = (v: string | string[] | undefined) => (v === undefined || v === '' ? [] : Array.isArray(v) ? v : [v]);\n\n/**\n * A row of toggles that belong together. `type=\"single\"` picks one (alignment, view) and is a radio group;\n * `type=\"multiple\"` allows any number (bold, italic) and is a group of pressed buttons. One tab stop; the arrow keys,\n * Home and End move between the buttons. `variant=\"chips\"` draws them as wrapping filter chips instead of a segmented track.\n */\nexport function ToggleGroup(props: Props) {\n  const { label, className, children, type, variant = 'segmented' } = props;\n  const multiple = type === 'multiple';\n  const [own, setOwn] = useState<string[]>(asList(props.defaultValue));\n  const on = props.value !== undefined ? asList(props.value) : own;\n  const root = useRef<HTMLDivElement>(null);\n  const [first, setFirst] = useState<string>();\n  const [focused, setFocused] = useState<string>();\n  // The first enabled item is the tab stop until something is pressed or focused.\n  useLayoutEffect(() => { setFirst(root.current?.querySelector<HTMLElement>('[data-kh-toggle-item]:not(:disabled)')?.dataset.khValue); });\n  const toggle = (v: string) => {\n    const next = multiple ? (on.includes(v) ? on.filter((x) => x !== v) : [...on, v]) : on[0] === v ? [] : [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  const onKeyDown = (e: KeyboardEvent<HTMLDivElement>) => {\n    if (!root.current || !(e.target as HTMLElement).matches('[data-kh-toggle-item]')) return;\n    if (moveFocus(root.current, e.target as HTMLElement, e.key, 'horizontal', '[data-kh-toggle-item]')) e.preventDefault();\n  };\n  return (\n    <GroupCtx.Provider value={{ multiple, variant, isOn: (v) => on.includes(v), toggle, stop: focused ?? (multiple ? undefined : on[0]) ?? first, focusItem: setFocused }}>\n      <div\n        ref={root}\n        role={multiple ? 'group' : 'radiogroup'}\n        aria-label={label}\n        onKeyDown={onKeyDown}\n        data-kh-variant={variant}\n        className={cn(variant === 'chips' ? 'flex flex-wrap items-center gap-2' : 'inline-flex items-center gap-1 rounded-kh-lg bg-kh-surface-2 p-1', className)}\n      >\n        {children}\n      </div>\n    </GroupCtx.Provider>\n  );\n}\n\n/** A chip: a pill with a hairline ring when off; on, the soft accent fill with an accent ring, a non-colour cue (the ring's\n *  weight) beside the fill. Never the ink inverted: a near-white pill glares in dark mode (KB-353). */\nconst chipClass =\n  'rounded-full px-3.5 ring-1 ring-inset ring-kh-line-strong hover:bg-kh-surface-2 ' +\n  'data-[state=on]:bg-kh-accent-soft data-[state=on]:text-kh-ink data-[state=on]:ring-2 data-[state=on]:ring-kh-accent ' +\n  'data-[state=on]:hover:bg-kh-accent-soft';\n\n/** One button in a ToggleGroup. An icon-only item needs an aria-label. */\nexport function ToggleGroupItem({ value, className, disabled, children, ...rest }: { value: string; disabled?: boolean; children: ReactNode; className?: string; 'aria-label'?: string }) {\n  const ctx = useContext(GroupCtx);\n  if (!ctx) throw new Error('ToggleGroupItem must sit inside a ToggleGroup');\n  const pressed = ctx.isOn(value);\n  const aria = ctx.multiple ? { 'aria-pressed': pressed } : { role: 'radio' as const, 'aria-checked': pressed };\n  // One tab stop for the whole group (APG roving tabindex). Before the first layout pass every item stays tabbable.\n  const tabIndex = ctx.stop === undefined ? undefined : ctx.stop === value ? 0 : -1;\n  return (\n    <button\n      type=\"button\"\n      data-kh-toggle-item=\"\"\n      data-kh-value={value}\n      onFocus={() => ctx.focusItem(value)}\n      data-state={pressed ? 'on' : 'off'}\n      tabIndex={tabIndex}\n      disabled={disabled}\n      onClick={() => ctx.toggle(value)}\n      className={cn(toggleClass, 'min-h-9', ctx.variant === 'chips' ? chipClass : 'data-[state=on]:bg-(--kh-selected) data-[state=on]:shadow-kh-1', className)}\n      {...aria}\n      {...rest}\n    >\n      {children}\n    </button>\n  );\n}\n"
    }
  ]
}
