{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "toggle-group",
  "title": "ToggleGroup",
  "description": "A row of related toggles that share one value, such as text alignment or bold, italic and underline in a toolbar.",
  "dependencies": [
    "@base-ui/react@^1.8.0"
  ],
  "registryDependencies": [
    "@ouro/lib-class-name-check",
    "@ouro/lib-cn",
    "@ouro/lib-dev",
    "@ouro/lib-env",
    "@ouro/lib-touch-target",
    "@ouro/lib-use-dev-messages",
    "@ouro/toggle"
  ],
  "files": [
    {
      "path": "registry/ouro/components/ouro/toggle/toggle-group.tsx",
      "content": "\"use client\";\n\nimport { Toggle as BaseToggle } from \"@base-ui/react/toggle\";\nimport { useId, useState, type JSX, type KeyboardEvent, type Ref } from \"react\";\nimport { useAppearanceClassNameCheck } from \"@/registry/ouro/lib/ouro/class-name-check\";\nimport { cn } from \"@/registry/ouro/lib/ouro/cn\";\nimport { devThrow } from \"@/registry/ouro/lib/ouro/dev\";\nimport { TouchTarget } from \"@/registry/ouro/lib/ouro/touch-target\";\nimport { useDevWarnings } from \"@/registry/ouro/lib/ouro/use-dev-messages\";\nimport {\n  iconClass,\n  looks,\n  shapes,\n  sizes,\n  squareClass,\n  toggleBase,\n  toggleHint,\n  toggleShapeWarning,\n  toggleSizeWarning,\n  toggleVariantWarning,\n  type ToggleIcon,\n} from \"@/registry/ouro/components/ouro/toggle/toggle-styles\";\n\n/** One toggle in a {@link ToggleGroup}. */\nexport interface ToggleGroupItem {\n  /** Identifies the toggle: `value`, `defaultValue` and `onValueChange` use it. Unique within the group. */\n  value: string;\n  /** The toggle's name, shown unless `hideLabel` is set. It stays the same while pressed. */\n  label: string;\n  /** Icon before the label, or alone with `hideLabel`. Pass the component: `icon: BoldIcon`. */\n  icon?: ToggleIcon | undefined;\n  /** Shows only the icon; the label stays the toggle's name for screen readers. */\n  hideLabel?: boolean | undefined;\n  /** Shows the toggle but keeps it from being pressed. */\n  disabled?: boolean | undefined;\n}\n\n/** Props both modes of {@link ToggleGroup} take. */\nexport interface ToggleGroupBaseProps {\n  /**\n   * Names the group (\"Text style\"). Shown above it unless `hideLabel` is set; screen readers always hear it.\n   *\n   * @claim \"Shown above it unless `hideLabel` is\" registry/ouro/components/ouro/toggle/toggle-group.test.tsx verify:testExists(registry/ouro/components/ouro/toggle/toggle-group.test.tsx::ToggleGroup > hides the group label visually with hideLabel and keeps it as the name)\n   */\n  label: string;\n  /** Hides the group's label visually; it still names the group. Set it on a toolbar whose toggles say what it is. */\n  hideLabel?: boolean | undefined;\n  /** The toggles, in order. */\n  items: readonly ToggleGroupItem[];\n  /**\n   * Disables every toggle.\n   *\n   * @claim \"Disables every toggle\" registry/ouro/components/ouro/toggle/toggle-group.test.tsx verify:testExists(registry/ouro/components/ouro/toggle/toggle-group.test.tsx::ToggleGroup > disables one item, or the whole group)\n   */\n  disabled?: boolean | undefined;\n  /**\n   * `plain` toggles have no border until pressed; `outline` ones always have one. shadcn's `default` is `plain`.\n   * @default \"plain\"\n   * @claim \"`plain` toggles have no border until\" registry/ouro/components/ouro/toggle/toggle.test.tsx verify:testExists(registry/ouro/components/ouro/toggle/toggle.test.tsx::Toggle looks > draws a border with variant outline)\n   * @claim \"`plain` toggles have no border until\" https://ui.shadcn.com/docs/components/toggle-group verify:urlOk(https://ui.shadcn.com/docs/components/toggle-group)\n   */\n  variant?: \"plain\" | \"outline\" | (string & { 'From shadcn? default is \"plain\"; use \"plain\" or \"outline\"': never }) | undefined;\n  /**\n   * Every toggle's size, as Button's, plus `xs` for a row of filter chips: 24px with a fine\n   * pointer, with a 44px touch target on phones. shadcn's `default` is `md`.\n   * @default \"md\"\n   * @claim \"Every toggle's size, as Button's, plus\" registry/ouro/components/ouro/toggle/toggle-group.test.tsx verify:testExists(registry/ouro/components/ouro/toggle/toggle-group.test.tsx::ToggleGroup > passes variant and size to every item)\n   */\n  size?: \"xs\" | \"sm\" | \"md\" | \"lg\" | (string & { 'From shadcn? default is \"md\"': never }) | undefined;\n  /**\n   * Every toggle's shape: `rect` has the theme's corners and medium text; `pill` is fully rounded\n   * with regular-weight text, for filter chips such as All, Unread and Mentions.\n   * @default \"rect\"\n   * @claim \"`pill` is fully rounded with regular-weight\" registry/ouro/components/ouro/toggle/toggle-group.test.tsx verify:testExists(registry/ouro/components/ouro/toggle/toggle-group.test.tsx::ToggleGroup > passes shape pill and size xs to every item)\n   */\n  shape?: \"rect\" | \"pill\" | (string & { 'Use \"rect\" or \"pill\"; rounded-full is \"pill\"': never }) | undefined;\n  /** Layout only: margins, grid placement (`mt-4`). Appearance classes warn in development. */\n  className?: string | undefined;\n  /** Reaches the group element (`role=\"group\"`). */\n  ref?: Ref<HTMLDivElement> | undefined;\n\n  /** Not a prop. Ouro UI uses `items`: `<ToggleGroup items={[{ value, label }]}>`. */\n  options?: { 'Use <ToggleGroup items={[{ value, label }]}>': never };\n  /** Not a prop. One toggle at a time is the default; `multiple` allows several: `<ToggleGroup multiple>`. */\n  type?: { 'Use <ToggleGroup multiple> for several; one at a time is the default': never };\n  /** Not a prop. Ouro UI uses `onValueChange`, which gets the value: `<ToggleGroup onValueChange={(value) => ...}>`. */\n  onChange?: { 'Use <ToggleGroup onValueChange={(value) => ...}>': never };\n  /** Not a prop. The toggles come from `items`: `<ToggleGroup items={[{ value: \"bold\", label: \"Bold\" }]}>`. */\n  children?: { 'Use <ToggleGroup items={[{ value: \"bold\", label: \"Bold\" }]}>': never };\n}\n\n/**\n * One toggle pressed at a time, the default: the value is one item's value, or `null` for none.\n *\n * @claim \"One toggle pressed at a time\" registry/ouro/components/ouro/toggle/toggle-group.test.tsx verify:testExists(registry/ouro/components/ouro/toggle/toggle-group.test.tsx::ToggleGroup > presses one item at a time and reports the value, or null when none is pressed)\n */\nexport type ToggleGroupSingleProps = ToggleGroupBaseProps & {\n  /** Allows several toggles pressed at once, and makes the value a list. */\n  multiple?: false | undefined;\n  /**\n   * The pressed toggle's value, or `null` for none, when you control it. Pair it with `onValueChange`.\n   *\n   * @claim \"The pressed toggle's value, or `null`\" registry/ouro/components/ouro/toggle/toggle-group.test.tsx verify:testExists(registry/ouro/components/ouro/toggle/toggle-group.test.tsx::ToggleGroup > presses one item at a time and reports the value, or null when none is pressed)\n   */\n  value?: string | null | undefined;\n  /** The toggle pressed first, when the group keeps its own state. @default null */\n  defaultValue?: string | null | undefined;\n  /** Called with the pressed toggle's value, or `null` when the pressed one is pressed again. */\n  onValueChange?: ((value: string | null) => void) | undefined;\n};\n\n/** With `multiple`, any number of toggles pressed: the value is a list of item values. */\nexport type ToggleGroupMultipleProps = ToggleGroupBaseProps & {\n  /** Allows several toggles pressed at once, and makes the value a list. */\n  multiple: true;\n  /** The pressed toggles' values, when you control them. Pair it with `onValueChange`. */\n  value?: readonly string[] | undefined;\n  /** The toggles pressed first, when the group keeps its own state. @default [] */\n  defaultValue?: readonly string[] | undefined;\n  /** Called with the pressed toggles' values, in item order. */\n  onValueChange?: ((value: string[]) => void) | undefined;\n};\n\n/** Props for {@link ToggleGroup}: `multiple` makes the value a list. */\nexport type ToggleGroupProps = ToggleGroupSingleProps | ToggleGroupMultipleProps;\n\n/** The first value that appears twice, or undefined. */\nfunction duplicateValue(items: readonly ToggleGroupItem[]): string | undefined {\n  const seen = new Set<string>();\n  for (const item of items) {\n    if (seen.has(item.value)) return item.value;\n    seen.add(item.value);\n  }\n  return undefined;\n}\n\n/** A value prop as Base UI's list. */\nfunction asList(value: string | null | readonly string[] | undefined): readonly string[] | undefined {\n  if (value === undefined) return undefined;\n  if (value === null) return [];\n  return typeof value === \"string\" ? [value] : value;\n}\n\n/**\n * A row of related toggles that share one value, such as text alignment or bold, italic and\n * underline in a toolbar. One toggle is pressed at a time unless `multiple` is set, which\n * makes the value a list. The arrow keys move between toggles, and each keeps its name with\n * `aria-pressed` for its state.\n *\n * Use `SegmentedControl` instead when exactly one choice is always selected and switches a\n * view. Use `RadioGroup` or `CheckboxGroup` instead for a form value that is submitted, and\n * one `Toggle` for a single on/off button.\n *\n * @group Actions\n * @see https://ui.ouroboros.bot/docs/toggle-group\n *\n * @example\n * <ToggleGroup\n *   label=\"Text style\"\n *   hideLabel\n *   multiple\n *   items={[\n *     { value: \"bold\", label: \"Bold\", icon: BoldIcon, hideLabel: true },\n *     { value: \"italic\", label: \"Italic\", icon: ItalicIcon, hideLabel: true },\n *   ]}\n *   value={styles}\n *   onValueChange={setStyles}\n * />\n * <ToggleGroup\n *   label=\"Show\"\n *   hideLabel\n *   variant=\"outline\"\n *   shape=\"pill\"\n *   size=\"xs\"\n *   items={[\n *     { value: \"all\", label: \"All\" },\n *     { value: \"unread\", label: \"Unread\" },\n *   ]}\n *   value={filter}\n *   onValueChange={(next) => setFilter(next ?? \"all\")}\n * />\n * @claim \"The arrow keys move between toggles\" registry/ouro/components/ouro/toggle/toggle-group.test.tsx verify:testExists(registry/ouro/components/ouro/toggle/toggle-group.test.tsx::ToggleGroup > moves focus with the arrow keys and presses with Space)\n * @claim \"The arrow keys move between toggles\" registry/ouro/components/ouro/toggle/toggle-group.test.tsx verify:testExists(registry/ouro/components/ouro/toggle/toggle-group.test.tsx::ToggleGroup > is a labeled group of toggle buttons)\n */\nexport function ToggleGroup(props: ToggleGroupProps): JSX.Element {\n  const {\n    label,\n    hideLabel,\n    items,\n    disabled,\n    variant: variantProp = \"plain\",\n    size: sizeProp = \"md\",\n    shape: shapeProp = \"rect\",\n    className,\n    ref,\n    multiple,\n    value,\n    defaultValue,\n    onValueChange,\n    // Guard props (hint types), dropped here so they never reach the DOM.\n    options,\n    type,\n    onChange,\n    children,\n  } = props;\n  const variant = Object.hasOwn(looks, variantProp) ? (variantProp as keyof typeof looks) : \"plain\";\n  const size = Object.hasOwn(sizes, sizeProp) ? (sizeProp as keyof typeof sizes) : \"md\";\n  const shape = Object.hasOwn(shapes, shapeProp) ? (shapeProp as keyof typeof shapes) : \"rect\";\n  const duplicate = duplicateValue(items);\n  if (duplicate !== undefined) {\n    devThrow(\n      \"ToggleGroup\",\n      `two items have the value \"${duplicate}\"`,\n      process.env.NODE_ENV !== \"production\" ? \"give each item its own value\" : /* istanbul ignore next @preserve -- production only */ \"\",\n    );\n  }\n  useAppearanceClassNameCheck(\n    \"ToggleGroup\",\n    className,\n    process.env.NODE_ENV !== \"production\" ? toggleHint(\"ToggleGroup\") : /* istanbul ignore next @preserve -- production only */ \"\",\n  );\n  useDevWarnings(\n    \"ToggleGroup\",\n    process.env.NODE_ENV !== \"production\"\n      ? () => {\n          const lists = Array.isArray(value) || Array.isArray(defaultValue);\n          const strings = typeof value === \"string\" || typeof defaultValue === \"string\";\n          const first = items[0]?.value ?? \"bold\";\n          const unknown = [...(asList(value) ?? []), ...(asList(defaultValue) ?? [])].find(\n            (candidate) => !items.some((item) => item.value === candidate),\n          );\n          const bare = items.find((item) => item.hideLabel && !item.icon);\n          const kind = String(type);\n          return [\n            variant !== variantProp && toggleVariantWarning(\"ToggleGroup\", String(variantProp)),\n            size !== sizeProp && toggleSizeWarning(\"ToggleGroup\", String(sizeProp)),\n            shape !== shapeProp && toggleShapeWarning(\"ToggleGroup\", String(shapeProp)),\n            !label && [\"label is empty, so the group has no name\", '<ToggleGroup label=\"Text style\" hideLabel>'],\n            !multiple && lists && [\"a list of values needs multiple\", \"<ToggleGroup multiple defaultValue={[…]}>\"],\n            multiple && strings && [\"multiple takes a list of values\", `<ToggleGroup multiple value={[\"${first}\"]}>`],\n            unknown !== undefined && [`value \"${unknown}\" matches no item`, `<ToggleGroup value=\"${first}\">`],\n            bare && [\n              `item \"${bare.value}\" hides its label without an icon`,\n              `{ value: \"${bare.value}\", label: \"${bare.label}\", icon: ${bare.label.replace(/\\W/g, \"\")}Icon, hideLabel: true }`,\n            ],\n            options !== undefined && [\"options is not a prop\", \"<ToggleGroup items={[{ value, label }]}>\"],\n            type !== undefined && [\n              `type=\"${kind}\" is not a prop`,\n              kind === \"multiple\" ? \"<ToggleGroup multiple>\" : \"<ToggleGroup> (one at a time is the default)\",\n            ],\n            onChange !== undefined && [\"onChange is not a prop\", \"<ToggleGroup onValueChange={(value) => …}>\"],\n            children !== undefined && [\n              \"children is not a prop; the toggles come from items\",\n              '<ToggleGroup items={[{ value: \"bold\", label: \"Bold\" }]}>',\n            ],\n          ];\n        }\n      : /* istanbul ignore next @preserve -- production only */ undefined,\n  );\n  const labelId = useId();\n  // Stryker disable next-line ArrayDeclaration: an empty list and a stray entry press no item\n  const [inner, setInner] = useState<readonly string[]>(() => asList(defaultValue) ?? []);\n  const pressed = asList(value) ?? inner;\n  const [focused, setFocused] = useState<string | null>(null);\n  const enabled = items.filter((item) => !disabled && !item.disabled);\n  const stop = enabled.find((item) => item.value === focused) ?? enabled.find((item) => pressed.includes(item.value)) ?? enabled[0];\n\n  const press = (itemValue: string, next: boolean): void => {\n    const list = multiple\n      ? items.filter((item) => (item.value === itemValue ? next : pressed.includes(item.value))).map((item) => item.value)\n      : next\n        ? [itemValue]\n        : [];\n    // Stryker disable next-line ConditionalExpression: a controlled group reads value, so the extra state is never shown\n    if (value === undefined) setInner(list);\n    if (multiple) (onValueChange as ToggleGroupMultipleProps[\"onValueChange\"])?.(list);\n    else (onValueChange as ToggleGroupSingleProps[\"onValueChange\"])?.(list[0] ?? null);\n  };\n\n  // Roving focus is the group's own, not Base UI's composite, whose mount reads computed style once per group.\n  const move = (event: KeyboardEvent<HTMLDivElement>): void => {\n    const buttons = Array.from(event.currentTarget.querySelectorAll<HTMLElement>(\"[data-ouro-toggle]:not(:disabled)\"));\n    const at = buttons.indexOf(event.target as HTMLElement);\n    if (at === -1 || event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return;\n    const rtl = (event.currentTarget.closest(\"[dir]\")?.getAttribute(\"dir\") ?? document.documentElement.dir) === \"rtl\";\n    const step =\n      event.key === \"ArrowRight\" ? (rtl ? -1 : 1) : event.key === \"ArrowLeft\" ? (rtl ? 1 : -1) : event.key === \"ArrowDown\" ? 1 : event.key === \"ArrowUp\" ? -1 : 0;\n    const target =\n      event.key === \"Home\" ? 0 : event.key === \"End\" ? buttons.length - 1 : step ? (at + step + buttons.length) % buttons.length : -1;\n    if (target === -1) return;\n    event.preventDefault();\n    // Stryker disable next-line OptionalChaining: target is always inside buttons\n    buttons[target]?.focus();\n  };\n\n  return (\n    <div data-ouro=\"toggle-group\" className={cn(\"box-border grid min-w-0 content-start justify-items-start gap-2\", className)}>\n      <span id={labelId} className={cn(\"m-0 text-sm leading-5 font-medium text-foreground select-none\", hideLabel && \"sr-only\")}>\n        {label}\n      </span>\n      <div\n        ref={ref}\n        role=\"group\"\n        aria-labelledby={labelId}\n        onKeyDown={move}\n        className=\"m-0 box-border inline-flex max-w-full flex-wrap items-center gap-1\"\n      >\n        {items.map(({ value: itemValue, label: itemLabel, icon: Icon, hideLabel: iconOnlyItem, disabled: itemDisabled }) => {\n          const iconOnly = Boolean(iconOnlyItem && Icon);\n          return (\n            <BaseToggle\n              key={itemValue}\n              pressed={pressed.includes(itemValue)}\n              onPressedChange={(next) => press(itemValue, next)}\n              disabled={disabled || itemDisabled}\n              tabIndex={stop?.value === itemValue ? 0 : -1}\n              onFocus={() => setFocused(itemValue)}\n              aria-label={iconOnlyItem ? itemLabel : undefined}\n              data-ouro-toggle=\"\"\n              data-ouro-control=\"toggle-group\"\n              data-ouro-part=\"item\"\n              data-variant={variant}\n              data-shape={shape}\n              data-ouro-squircle={shape === \"rect\" ? \"\" : undefined}\n              className={cn(toggleBase, shapes[shape], sizes[size], looks[variant], iconOnly && squareClass)}\n            >\n              <TouchTarget />\n              {shape === \"rect\" && <span data-ouro-surface=\"\" aria-hidden=\"true\" style={{ zIndex: -1 }} />}\n              {Icon && <Icon aria-hidden=\"true\" className={cn(iconClass, \"relative\", !iconOnlyItem && \"-ms-0.5\")} />}\n              {!iconOnly && <span className={cn(\"relative\", iconOnlyItem && \"sr-only\")}>{itemLabel}</span>}\n            </BaseToggle>\n          );\n        })}\n      </div>\n    </div>\n  );\n}\n",
      "type": "registry:ui",
      "target": "@components/ouro/toggle/toggle-group.tsx"
    }
  ],
  "docs": "A row of related toggles that share one value, such as text alignment or bold, italic and underline in a toolbar. Import: import { ToggleGroup } from \"@/components/ouro/toggle/toggle-group\", with @/components replaced by your components alias (aliases.components in components.json, such as ~/components). Also installed: toggle, the helpers they import in lib/ouro. Needs ouro-base in your stylesheet: add that item once per app, from the same place as this one, if you have not. Docs: https://ui.ouroboros.bot/docs/toggle-group.md Ouro UI v1.3.257.",
  "type": "registry:ui"
}