{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "toggle-group",
  "title": "Toggle Group",
  "description": "Single (radiogroup) or multiple (aria-pressed) toggle buttons in outline, soft, solid, segmented, neo and glass styles with a sliding, fading or popping active marker, text or icon items, tones, sizes, shapes, shadows, attached or spaced, vertical, full width, disabled items and roving arrow-key focus that flips in RTL.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://goodui.dev/r/goodui-motion.json"
  ],
  "files": [
    {
      "path": "registry/components/core/toggle-group.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { motion, useReducedMotion } from \"motion/react\"\nimport { cn } from \"@/lib/utils\"\nimport { GooduiTokens, spring } from \"@/lib/goodui-motion\"\n\n/*\n * Toggle group, independent layers: every axis combines with every other.\n *   type       single (pick one, a radiogroup) or multiple (each button toggles, aria-pressed)\n *   variant    outline, soft, solid, segmented, neo, glass      tone       active color\n *   animation  slide (the active marker glides; single only), fade, pop, none\n *   shape, size, shadow, orientation, attached (outline and neo share borders), fullWidth\n * Keyboard: one tab stop (roving tabindex). Arrows move between enabled items and wrap, flipped under dir=\"rtl\";\n * Home / End jump to the ends. In single mode arrows also select (the radio pattern); in multiple mode Space or\n * Enter toggles the focused item.\n */\n\nexport type ToggleGroupVariant = \"outline\" | \"soft\" | \"solid\" | \"segmented\" | \"neo\" | \"glass\"\nexport type ToggleGroupTone = \"primary\" | \"neutral\" | \"success\" | \"warning\" | \"danger\" | \"info\"\nexport type ToggleGroupSize = \"sm\" | \"md\" | \"lg\"\nexport type ToggleGroupShape = \"square\" | \"rounded\" | \"pill\"\nexport type ToggleGroupShadow = \"none\" | \"sm\" | \"md\" | \"glow\"\nexport type ToggleGroupAnimation = \"none\" | \"slide\" | \"fade\" | \"pop\"\n\nexport type ToggleGroupOption = {\n  value: string\n  label?: React.ReactNode\n  icon?: React.ReactNode\n  disabled?: boolean\n  /** Required when the item has only an icon. */\n  \"aria-label\"?: string\n  /** Native tooltip, handy for icon-only items. */\n  title?: string\n}\n\ntype Base = Omit<React.ComponentProps<\"div\">, \"defaultValue\" | \"onChange\" | \"dir\"> & {\n  /** Shortcut: renders a ToggleGroupItem per option. Or pass <ToggleGroupItem> children. */\n  items?: ToggleGroupOption[]\n  /** How the group is painted. outline and neo draw joined buttons; soft, solid, segmented and glass sit in a track. */\n  variant?: ToggleGroupVariant\n  /** Color of the active item. */\n  tone?: ToggleGroupTone\n  size?: ToggleGroupSize\n  shape?: ToggleGroupShape\n  /** Group shadow; glow puts a tone-colored glow under the active marker instead. */\n  shadow?: ToggleGroupShadow\n  /** slide glides one marker between items (single only; multiple falls back to fade). Reduced motion snaps. */\n  animation?: ToggleGroupAnimation\n  orientation?: \"horizontal\" | \"vertical\"\n  /** outline and neo: buttons share borders in one box. false spaces them apart. */\n  attached?: boolean\n  /** Items share the full width equally. */\n  fullWidth?: boolean\n  /** Disables every item. */\n  disabled?: boolean\n  /** Form field name (renders hidden inputs with the selected values). */\n  name?: string\n}\nexport type ToggleGroupSingleProps = Base & {\n  /** single: one value (a radiogroup). multiple: any number (each item has aria-pressed). */\n  type?: \"single\"\n  /** Selected value (controlled): a string or null; string[] with type=\"multiple\". */\n  value?: string | null\n  /** Initial value when uncontrolled. */\n  defaultValue?: string | null\n  /** Called with the new value (string | null, or string[] with type=\"multiple\"). */\n  onValueChange?: (value: string | null) => void\n  /** single: clicking the active item clears the value. */\n  deselectable?: boolean\n}\nexport type ToggleGroupMultipleProps = Base & {\n  type: \"multiple\"\n  value?: string[]\n  defaultValue?: string[]\n  onValueChange?: (value: string[]) => void\n  deselectable?: never\n}\nexport type ToggleGroupProps = ToggleGroupSingleProps | ToggleGroupMultipleProps\n\ntype Ctx = {\n  multiple: boolean\n  selected: string[]\n  toggle: (v: string) => void\n  stop: string | null\n  onItemFocus: (v: string) => void\n  uid: string\n  variant: ToggleGroupVariant\n  size: ToggleGroupSize\n  shape: ToggleGroupShape\n  shadow: ToggleGroupShadow\n  animation: ToggleGroupAnimation\n  attached: boolean\n  fullWidth: boolean\n  vertical: boolean\n  disabled: boolean\n  slide: boolean\n  touched: boolean\n}\nconst ToggleGroupContext = React.createContext<Ctx | null>(null)\n\nconst tones = (c: string) => `\n.${c}{--pc-c:var(--primary);--pc-fg:var(--primary-foreground)}\n.${c}[data-tone=neutral]{--pc-c:var(--foreground);--pc-fg:var(--background)}\n.${c}[data-tone=success]{--pc-c:oklch(0.53 0.13 155);--pc-fg:#fff}\n.${c}[data-tone=warning]{--pc-c:oklch(0.75 0.16 70);--pc-fg:oklch(0.25 0.05 70)}\n.${c}[data-tone=danger]{--pc-c:oklch(0.56 0.21 27);--pc-fg:#fff}\n.${c}[data-tone=info]{--pc-c:oklch(0.55 0.17 250);--pc-fg:#fff}\n.dark .${c}[data-tone=success]{--pc-c:oklch(0.7 0.16 155);--pc-fg:oklch(0.2 0.05 155)}\n.dark .${c}[data-tone=danger]{--pc-c:oklch(0.68 0.2 25);--pc-fg:oklch(0.2 0.05 25)}\n.dark .${c}[data-tone=info]{--pc-c:oklch(0.7 0.15 245);--pc-fg:oklch(0.2 0.05 245)}\n.${c}{--pc-tint:color-mix(in oklab,var(--pc-c) 14%,transparent);--pc-ink:color-mix(in oklab,var(--pc-c) 82%,var(--foreground))}\n.${c}[data-tone=warning]{--pc-ink:color-mix(in oklab,var(--pc-c) 62%,var(--foreground))}\n`\n\nconst R = \".pc-tg\"\nconst I = \".pc-tg-item\"\nconst ON = \"[data-state=on]\"\nconst CSS = `${tones(\"pc-tg\")}\n${R}{--pc-muted:color-mix(in oklab,var(--muted-foreground) 82%,var(--foreground))}\n${I}{color:var(--pc-muted);-webkit-tap-highlight-color:transparent}\n${I}:hover:not(${ON}):not(:disabled){color:var(--foreground);background:color-mix(in oklab,var(--foreground) 5%,transparent)}\n${I}${ON}{color:var(--foreground)}\n.pc-tg-ind{position:absolute;inset:0;pointer-events:none;border-radius:inherit}\n\n${R}[data-variant=outline][data-attached]{background:var(--background);border:1px solid var(--border)}\n${R}[data-variant=outline][data-attached] ${I}+${I}{border-inline-start:1px solid var(--border)}\n${R}[data-variant=outline][data-attached][data-vertical] ${I}+${I}{border-inline-start:0;border-top:1px solid var(--border)}\n${R}[data-variant=outline]:not([data-attached]) ${I}{background:var(--background);border:1px solid var(--border)}\n${R}[data-variant=outline] ${I}${ON}{color:var(--pc-ink)}\n${R}[data-variant=outline] .pc-tg-ind{background:var(--pc-tint);box-shadow:inset 0 0 0 1px color-mix(in oklab,var(--pc-c) 45%,transparent)}\n\n${R}[data-variant=soft] ${I}${ON}{color:var(--pc-ink)}\n${R}[data-variant=soft] .pc-tg-ind{background:var(--pc-tint)}\n\n${R}:is([data-variant=solid],[data-variant=segmented]){background:var(--muted)}\n${R}[data-variant=solid] ${I}${ON}{color:var(--pc-fg)}\n${R}[data-variant=solid] .pc-tg-ind{background:var(--pc-c)}\n${R}[data-variant=segmented] .pc-tg-ind{background:var(--background);box-shadow:0 1px 2px rgb(0 0 0/.08),0 0 0 1px color-mix(in oklab,var(--foreground) 6%,transparent)}\n.dark ${R}[data-variant=segmented] .pc-tg-ind{background:color-mix(in oklab,var(--foreground) 12%,var(--background))}\n${R}[data-variant=segmented] ${I}${ON}{color:var(--foreground)}\n\n${R}[data-variant=neo][data-attached]{background:var(--background);border:2px solid var(--foreground);box-shadow:3px 3px 0 var(--foreground)}\n${R}[data-variant=neo][data-attached] ${I}+${I}{border-inline-start:2px solid var(--foreground)}\n${R}[data-variant=neo][data-attached][data-vertical] ${I}+${I}{border-inline-start:0;border-top:2px solid var(--foreground)}\n${R}[data-variant=neo]:not([data-attached]) ${I}{background:var(--background);border:2px solid var(--foreground);box-shadow:2px 2px 0 var(--foreground)}\n${R}[data-variant=neo]:not([data-attached]) ${I}:active:not(:disabled){transform:translate(2px,2px);box-shadow:0 0 0 var(--foreground)}\n${R}[data-variant=neo] ${I}{color:var(--foreground)}\n${R}[data-variant=neo] ${I}${ON}{color:var(--pc-fg)}\n${R}[data-variant=neo] .pc-tg-ind{background:var(--pc-c)}\n\n${R}[data-variant=glass]{background:color-mix(in oklab,var(--background) 40%,transparent);border:1px solid color-mix(in oklab,var(--foreground) 14%,transparent);-webkit-backdrop-filter:blur(14px) saturate(1.6);backdrop-filter:blur(14px) saturate(1.6)}\n${R}[data-variant=glass] ${I}{color:color-mix(in oklab,var(--foreground) 75%,transparent)}\n${R}[data-variant=glass] ${I}${ON}{color:var(--foreground)}\n${R}[data-variant=glass] .pc-tg-ind{background:color-mix(in oklab,var(--background) 78%,transparent);box-shadow:0 2px 8px -2px rgb(0 0 0/.18),inset 0 1px 0 color-mix(in oklab,white 50%,transparent)}\n\n${R}[data-shadow=sm]{box-shadow:0 1px 3px rgb(0 0 0/.1)}\n${R}[data-shadow=md]{box-shadow:0 6px 18px -6px rgb(0 0 0/.22)}\n${R}[data-shadow=glow] .pc-tg-ind{box-shadow:0 0 0 1px color-mix(in oklab,var(--pc-c) 30%,transparent),0 6px 20px -4px color-mix(in oklab,var(--pc-c) 65%,transparent)}\n\n${R}:not([data-anim=none]) ${I}{transition:color .2s,background-color .2s,transform .12s,box-shadow .12s}\n${R}:not([data-anim=none]):not([data-variant=neo]) ${I}:active:not(:disabled){transform:scale(.96)}\n.pc-tg-ind[data-anim=fade]{animation:pc-tg-fade .2s ease-out}\n.pc-tg-ind[data-anim=pop]{animation:pc-tg-pop .34s var(--pc-ease-spring)}\n@keyframes pc-tg-fade{from{opacity:0;transform:scale(.94)}}\n@keyframes pc-tg-pop{0%{opacity:0;transform:scale(.6)}100%{opacity:1;transform:scale(1)}}\n@media (prefers-reduced-motion:reduce){\n  ${R} ${I},.pc-tg-ind{transition:none!important;animation:none!important}\n  ${R} ${I}:active{transform:none!important}\n}\n`\n\nconst SIZES: Record<ToggleGroupSize, string> = {\n  sm: \"h-8 gap-1.5 px-2.5 text-[13px] [&_svg]:size-3.5\",\n  md: \"h-9 gap-2 px-3 text-sm [&_svg]:size-4\",\n  lg: \"h-11 gap-2 px-4 text-[15px] [&_svg]:size-[18px]\",\n}\nconst ICON_ONLY: Record<ToggleGroupSize, string> = { sm: \"w-8 px-0\", md: \"w-9 px-0\", lg: \"w-11 px-0\" }\nconst TRACK = (v: ToggleGroupVariant) => v === \"solid\" || v === \"segmented\" || v === \"glass\"\n\nfunction radius(variant: ToggleGroupVariant, shape: ToggleGroupShape, attached: boolean, part: \"root\" | \"item\") {\n  if (shape === \"square\") return \"rounded-none\"\n  if (shape === \"pill\") return \"rounded-full\"\n  const joined = attached && (variant === \"outline\" || variant === \"neo\")\n  if (part === \"root\") return TRACK(variant) ? \"rounded-xl\" : joined ? (variant === \"neo\" ? \"rounded-md\" : \"rounded-lg\") : \"rounded-lg\"\n  return joined ? \"rounded-none\" : TRACK(variant) ? \"rounded-lg\" : variant === \"neo\" ? \"rounded-md\" : \"rounded-lg\"\n}\n\nfunction indRadius(ctx: Ctx) {\n  if (ctx.shape === \"pill\") return 9999\n  if (ctx.shape === \"square\" || (ctx.attached && (ctx.variant === \"outline\" || ctx.variant === \"neo\"))) return 0\n  return ctx.variant === \"neo\" ? 6 : 8\n}\n\nexport function ToggleGroup(props: ToggleGroupProps) {\n  const {\n    type,\n    items,\n    variant = \"outline\",\n    tone = \"primary\",\n    size = \"md\",\n    shape = \"rounded\",\n    shadow = \"none\",\n    animation = \"slide\",\n    orientation = \"horizontal\",\n    attached = true,\n    fullWidth = false,\n    disabled = false,\n    name,\n    value: valueProp,\n    defaultValue,\n    onValueChange,\n    deselectable,\n    className,\n    children,\n    onKeyDown,\n    ...rest\n  } = props\n  const multiple = type === \"multiple\"\n  const uid = React.useId()\n  const prefersReduced = useReducedMotion()\n  // Gate reduced motion behind mount so server and first client render match.\n  const [mounted, setMounted] = React.useState(false)\n  React.useEffect(() => setMounted(true), [])\n  const reduce = mounted && !!prefersReduced\n  const [touched, setTouched] = React.useState(false)\n\n  const [inner, setInner] = React.useState<string | string[] | null | undefined>(defaultValue)\n  const current = valueProp !== undefined ? valueProp : inner\n  const selected: string[] = Array.isArray(current) ? current : current ? [current] : []\n  const commit = (next: string[]) => {\n    const v = multiple ? next : (next[0] ?? null)\n    setTouched(true)\n    if (valueProp === undefined) setInner(v)\n    ;(onValueChange as ((v: string | string[] | null) => void) | undefined)?.(v)\n  }\n  const toggle = (v: string) => {\n    if (multiple) commit(selected.includes(v) ? selected.filter((x) => x !== v) : [...selected, v])\n    else if (selected[0] === v) {\n      if (deselectable) commit([])\n    } else commit([v])\n  }\n\n  // Roving tabindex: the selected item (single) or the last focused one (multiple) holds the only tab stop.\n  const root = React.useRef<HTMLDivElement>(null)\n  const lastFocus = React.useRef<string | null>(null)\n  const [stop, setStop] = React.useState<string | null>(null)\n  const enabled = () => Array.from(root.current?.querySelectorAll<HTMLButtonElement>(\"[data-slot=toggle-group-item]:not(:disabled)\") ?? [])\n  React.useLayoutEffect(() => {\n    const els = enabled()\n    const pick = multiple\n      ? (els.find((b) => b.dataset.value === lastFocus.current) ?? els[0])\n      : (els.find((b) => b.dataset.state === \"on\") ?? els.find((b) => b.dataset.value === lastFocus.current) ?? els[0])\n    const v = pick?.dataset.value ?? null\n    if (v !== stop) setStop(v)\n  })\n\n  const onKey = (e: React.KeyboardEvent<HTMLDivElement>) => {\n    onKeyDown?.(e)\n    if (e.defaultPrevented) return\n    const els = enabled()\n    if (!els.length) return\n    const i = els.findIndex((b) => b === document.activeElement)\n    if (i < 0) return\n    const rtl = !!root.current && getComputedStyle(root.current).direction === \"rtl\"\n    const next = [\"ArrowDown\", rtl ? \"ArrowLeft\" : \"ArrowRight\"]\n    const prev = [\"ArrowUp\", rtl ? \"ArrowRight\" : \"ArrowLeft\"]\n    let to = -1\n    if (next.includes(e.key)) to = (i + 1) % els.length\n    else if (prev.includes(e.key)) to = (i - 1 + els.length) % els.length\n    else if (e.key === \"Home\") to = 0\n    else if (e.key === \"End\") to = els.length - 1\n    if (to < 0) return\n    e.preventDefault()\n    const el = els[to]!\n    el.focus()\n    const v = el.dataset.value\n    if (!multiple && v && selected[0] !== v) commit([v])\n  }\n\n  const vertical = orientation === \"vertical\"\n  const ctx: Ctx = {\n    multiple,\n    selected,\n    toggle,\n    stop,\n    onItemFocus: (v) => {\n      lastFocus.current = v\n    },\n    uid,\n    variant,\n    size,\n    shape,\n    shadow,\n    animation,\n    attached,\n    fullWidth,\n    vertical,\n    disabled,\n    slide: animation === \"slide\" && !multiple && !reduce,\n    touched,\n  }\n  const joined = attached && (variant === \"outline\" || variant === \"neo\")\n\n  return (\n    <ToggleGroupContext.Provider value={ctx}>\n      <GooduiTokens />\n      <style href=\"pc-toggle-group\" precedence=\"default\">\n        {CSS}\n      </style>\n      <div\n        {...rest}\n        ref={root}\n        role={multiple ? \"group\" : \"radiogroup\"}\n        aria-disabled={disabled || undefined}\n        data-slot=\"toggle-group\"\n        data-variant={variant}\n        data-tone={tone}\n        data-shadow={variant === \"neo\" && shadow !== \"glow\" ? \"none\" : shadow}\n        data-anim={animation}\n        data-attached={joined ? \"\" : undefined}\n        data-vertical={vertical ? \"\" : undefined}\n        onKeyDown={onKey}\n        className={cn(\n          \"pc-tg relative max-w-full\",\n          fullWidth ? \"flex w-full\" : \"inline-flex\",\n          vertical ? \"flex-col items-stretch\" : \"items-center\",\n          joined ? \"overflow-hidden\" : TRACK(variant) ? \"gap-1 p-1\" : variant === \"neo\" ? \"gap-2\" : \"gap-1\",\n          radius(variant, shape, attached, \"root\"),\n          className,\n        )}\n      >\n        {items?.map((o) => (\n          <ToggleGroupItem key={o.value} value={o.value} icon={o.icon} disabled={o.disabled} aria-label={o[\"aria-label\"]} title={o.title}>\n            {o.label}\n          </ToggleGroupItem>\n        ))}\n        {children}\n        {name && selected.map((v) => <input key={v} type=\"hidden\" name={name} value={v} />)}\n      </div>\n    </ToggleGroupContext.Provider>\n  )\n}\n\nexport type ToggleGroupItemProps = Omit<React.ComponentProps<\"button\">, \"value\"> & {\n  /** Value reported to the group. */\n  value: string\n  /** Icon before the label; with no label the item becomes square (pass aria-label). */\n  icon?: React.ReactNode\n}\n\nexport function ToggleGroupItem({ value, icon, className, children, disabled, onClick, onFocus, ...props }: ToggleGroupItemProps) {\n  const ctx = React.useContext(ToggleGroupContext)\n  if (!ctx) throw new Error(\"<ToggleGroupItem> must be used inside <ToggleGroup>\")\n  const on = ctx.selected.includes(value)\n  const off = disabled || ctx.disabled\n  const iconOnly = !!icon && (children == null || children === false || children === \"\")\n  const indAnim = ctx.touched && ctx.animation !== \"none\" ? (ctx.animation === \"pop\" ? \"pop\" : \"fade\") : undefined\n\n  return (\n    <button\n      type=\"button\"\n      role={ctx.multiple ? undefined : \"radio\"}\n      aria-checked={ctx.multiple ? undefined : on}\n      aria-pressed={ctx.multiple ? on : undefined}\n      tabIndex={ctx.stop == null || ctx.stop === value ? 0 : -1}\n      data-slot=\"toggle-group-item\"\n      data-value={value}\n      data-state={on ? \"on\" : \"off\"}\n      disabled={off}\n      onClick={(e) => {\n        onClick?.(e)\n        if (!e.defaultPrevented) ctx.toggle(value)\n      }}\n      onFocus={(e) => {\n        onFocus?.(e)\n        ctx.onItemFocus(value)\n      }}\n      className={cn(\n        \"pc-press pc-press-soft pc-tg-item relative inline-flex shrink-0 cursor-pointer items-center justify-center font-medium whitespace-nowrap outline-none select-none\",\n        \"focus-visible:z-[2] focus-visible:ring-3 focus-visible:ring-[color-mix(in_oklab,var(--pc-c)_40%,transparent)] focus-visible:ring-inset disabled:pointer-events-none disabled:opacity-45\",\n        \"[&_svg]:pointer-events-none [&_svg]:shrink-0\",\n        SIZES[ctx.size],\n        iconOnly && !ctx.fullWidth && ICON_ONLY[ctx.size],\n        radius(ctx.variant, ctx.shape, ctx.attached, \"item\"),\n        (ctx.fullWidth || ctx.vertical) && \"flex-1\",\n        ctx.vertical && !iconOnly && \"justify-start\",\n        className,\n      )}\n      {...props}\n    >\n      <GooduiTokens />\n      {on &&\n        (ctx.slide ? (\n          <motion.span\n            layoutId={`${ctx.uid}-ind`}\n            aria-hidden=\"true\"\n            className=\"pc-tg-ind\"\n            // An explicit radius lets motion correct the corners while the marker scales between items of different widths.\n            style={{ borderRadius: indRadius(ctx) }}\n            transition={spring.snap}\n          />\n        ) : (\n          <span aria-hidden=\"true\" className=\"pc-tg-ind\" data-anim={indAnim} />\n        ))}\n      <span className=\"relative z-[1] inline-flex min-w-0 items-center gap-[inherit]\">\n        {icon}\n        {children}\n      </span>\n    </button>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/toggle-group.tsx"
    }
  ],
  "categories": [
    "components",
    "core"
  ],
  "type": "registry:component"
}