{
  "schemaVersion": 1,
  "target": "react",
  "name": "toggle-group",
  "version": "0.1.0",
  "category": "forms",
  "title": "Toggle Group",
  "description": "A segmented control for one or several choices, with a raised pill that springs to the pressed item.",
  "tags": [
    "toggle",
    "segmented-control",
    "group",
    "toolbar",
    "base-ui"
  ],
  "requires": {
    "projectTypes": [
      "next",
      "vite-react"
    ],
    "styling": "tailwind-v4"
  },
  "npmDependencies": {
    "@base-ui/react": "^1.9.0"
  },
  "registryDependencies": [],
  "docsPath": "/components/toggle-group",
  "props": [
    {
      "name": "value / defaultValue",
      "type": "string[]",
      "description": "The pressed items' values."
    },
    {
      "name": "onValueChange",
      "type": "(value: string[]) => void",
      "description": "Called when the pressed items change."
    },
    {
      "name": "multiple",
      "type": "boolean",
      "default": "false",
      "description": "Independent toggles instead of one choice."
    },
    {
      "name": "variant",
      "type": "\"segmented\" | \"ghost\"",
      "default": "\"segmented\"",
      "description": "A hairline track with a sliding pill, or spaced toolbar buttons."
    },
    {
      "name": "size",
      "type": "\"sm\" | \"md\"",
      "default": "\"md\"",
      "description": "24px or 28px items."
    },
    {
      "name": "allowEmpty",
      "type": "boolean",
      "default": "false for single segmented",
      "description": "Whether the only pressed item can be released."
    },
    {
      "name": "orientation",
      "type": "\"horizontal\" | \"vertical\"",
      "default": "\"horizontal\"",
      "description": "Layout and arrow-key direction."
    },
    {
      "name": "iconOnly",
      "type": "boolean",
      "default": "false",
      "description": "ToggleGroupItem: a square item for one icon; give it an aria-label."
    }
  ],
  "accessibility": [
    "Renders role=\"group\" of native buttons with aria-pressed; name the group with aria-label.",
    "Arrow keys move focus between items with roving tabindex; Space or Enter presses the focused item.",
    "The pill springs over 350ms on click and jumps without moving when the choice changes from the keyboard.",
    "Under reduced motion the pill moves without a transition."
  ],
  "states": [
    "single",
    "multiple",
    "hover",
    "focus-visible",
    "disabled"
  ],
  "files": [
    {
      "path": "ui/toggle-group.tsx",
      "content": "\"use client\";\n\nimport { Toggle as BaseToggle } from \"@base-ui/react/toggle\";\nimport { ToggleGroup as BaseToggleGroup } from \"@base-ui/react/toggle-group\";\nimport {\n  createContext,\n  type KeyboardEvent,\n  type PointerEvent,\n  useContext,\n  useLayoutEffect,\n  useRef,\n  useState,\n} from \"react\";\n\ntype ClassName<State> = string | ((state: State) => string | undefined) | undefined;\n\nfunction cx(...classes: (string | false | null | undefined)[]) {\n  return classes.filter(Boolean).join(\" \");\n}\n\n/** Prepends base classes while keeping Base UI's state-function form of className working. */\nfunction withBase<State>(base: string, className: ClassName<State>) {\n  return typeof className === \"function\" ? (state: State) => cx(base, className(state)) : cx(base, className);\n}\n\n// A critically damped spring (bounce 0) settling in about 350ms, sampled for CSS linear().\nconst spring =\n  \"ease-[linear(0,0.044,0.144,0.263,0.384,0.495,0.593,0.675,0.744,0.8,0.844,0.88,0.908,0.929,0.946,0.959,0.969,0.977,0.982,0.987,1)]\";\n\nexport type ToggleGroupVariant = \"segmented\" | \"ghost\";\n\nconst sizes = {\n  sm: { text: \"h-6 px-2 text-xs\", icon: \"size-6\" },\n  md: { text: \"h-7 px-2.5 text-sm\", icon: \"size-7\" },\n} as const;\n\nexport type ToggleGroupSize = keyof typeof sizes;\n\ninterface GroupContextValue {\n  variant: ToggleGroupVariant;\n  size: ToggleGroupSize;\n  pill: boolean;\n}\n\nconst GroupContext = createContext<GroupContextValue>({ variant: \"segmented\", size: \"md\", pill: false });\n\nexport interface ToggleGroupProps<Value extends string = string> extends BaseToggleGroup.Props<Value> {\n  /**\n   * `segmented` sits the items in a hairline track; with one choice, a raised pill slides to the\n   * pressed item. `ghost` spaces the items apart, like toolbar buttons.\n   */\n  variant?: ToggleGroupVariant;\n  size?: ToggleGroupSize;\n  /**\n   * Whether pressing the only pressed item can clear the selection. Off for single-choice\n   * segmented groups, which always keep one item pressed; on otherwise.\n   */\n  allowEmpty?: boolean;\n}\n\n/**\n * A row of toggle buttons sharing one value: an array of the pressed items' `value`s. Single\n * choice by default; add `multiple` for independent toggles. Arrow keys move focus between\n * items and Space or Enter presses one. The sliding pill springs over 350ms on click and\n * jumps without moving when the choice changes from the keyboard.\n */\nexport function ToggleGroup<Value extends string = string>({\n  variant = \"segmented\",\n  size = \"md\",\n  multiple = false,\n  allowEmpty,\n  value,\n  defaultValue,\n  onValueChange,\n  orientation = \"horizontal\",\n  className,\n  children,\n  onPointerDown,\n  onKeyDown,\n  ...props\n}: ToggleGroupProps<Value>) {\n  const [uncontrolled, setUncontrolled] = useState<readonly Value[]>(defaultValue ?? []);\n  const current = value ?? uncontrolled;\n  const keepOne = !multiple && !(allowEmpty ?? variant === \"ghost\");\n  const pill = variant === \"segmented\" && !multiple;\n\n  const rootRef = useRef<HTMLDivElement>(null);\n  const pillRef = useRef<HTMLSpanElement>(null);\n  const [input, setInput] = useState<\"pointer\" | \"keyboard\">(\"pointer\");\n  const [placed, setPlaced] = useState(false);\n\n  // Measures the pressed item and moves the pill onto it with transforms (no layout animation).\n  useLayoutEffect(() => {\n    const root = rootRef.current;\n    const indicator = pillRef.current;\n    if (!pill || !root || !indicator) return;\n    const place = () => {\n      const pressed = root.querySelector<HTMLElement>(\":scope > [data-pressed]\");\n      if (!pressed) {\n        indicator.style.opacity = \"0\";\n        return;\n      }\n      const vertical = orientation === \"vertical\";\n      indicator.style.opacity = \"1\";\n      indicator.style.width = `${pressed.offsetWidth}px`;\n      indicator.style.height = `${pressed.offsetHeight}px`;\n      indicator.style.translate = vertical ? `0 ${pressed.offsetTop}px` : `${pressed.offsetLeft}px 0`;\n      setPlaced(true);\n    };\n    place();\n    const observer = new ResizeObserver(place);\n    observer.observe(root);\n    return () => observer.disconnect();\n  }, [pill, current, orientation]);\n\n  return (\n    <GroupContext.Provider value={{ variant, size, pill: pill && placed }}>\n      <BaseToggleGroup<Value>\n        ref={rootRef}\n        multiple={multiple}\n        value={current}\n        orientation={orientation}\n        onValueChange={(next, details) => {\n          if (keepOne && next.length === 0) return;\n          if (value === undefined) setUncontrolled(next);\n          onValueChange?.(next, details);\n        }}\n        data-variant={variant}\n        data-input={input}\n        onPointerDown={(event: PointerEvent<HTMLDivElement>) => {\n          setInput(\"pointer\");\n          onPointerDown?.(event as never);\n        }}\n        onKeyDown={(event: KeyboardEvent<HTMLDivElement>) => {\n          setInput(\"keyboard\");\n          onKeyDown?.(event as never);\n        }}\n        className={withBase(\n          cx(\n            \"group/toggles relative isolate inline-flex w-fit data-[orientation=vertical]:flex-col\",\n            variant === \"segmented\"\n              ? \"gap-0.5 rounded-lg border border-zinc-950/10 bg-zinc-950/[0.03] p-0.5 dark:border-white/10 dark:bg-transparent\"\n              : \"gap-1\",\n            \"data-[disabled]:opacity-45\",\n          ),\n          className,\n        )}\n        {...props}\n      >\n        {pill ? (\n          <span\n            ref={pillRef}\n            aria-hidden=\"true\"\n            className={cx(\n              \"pointer-events-none absolute top-0 left-0 -z-10 rounded-md opacity-0\",\n              \"bg-white shadow-[0_1px_2px_rgb(0_0_0/0.08),0_0_0_1px_rgb(0_0_0/0.06)]\",\n              \"dark:bg-white/[0.12] dark:shadow-[inset_0_1px_0_rgb(255_255_255/0.08),0_0_0_1px_rgb(255_255_255/0.06)]\",\n              placed &&\n                cx(\n                  \"group-data-[input=pointer]/toggles:transition-[translate,width,height] group-data-[input=pointer]/toggles:duration-350\",\n                  spring,\n                  \"motion-reduce:transition-none group-data-[input=pointer]/toggles:motion-reduce:transition-none\",\n                ),\n            )}\n          />\n        ) : null}\n        {children}\n      </BaseToggleGroup>\n    </GroupContext.Provider>\n  );\n}\n\nexport interface ToggleGroupItemProps<Value extends string = string> extends BaseToggle.Props<Value> {\n  /** Identifies the item in the group's value. */\n  value: Value;\n  /** A square item for a single icon. Give it an `aria-label`. */\n  iconOnly?: boolean;\n}\n\n/** One item in a ToggleGroup. It sets `aria-pressed`, so its state is announced. */\nexport function ToggleGroupItem<Value extends string = string>({ iconOnly = false, className, ...props }: ToggleGroupItemProps<Value>) {\n  const { variant, size, pill } = useContext(GroupContext);\n  return (\n    <BaseToggle<Value>\n      className={withBase(\n        cx(\n          \"relative inline-flex shrink-0 cursor-pointer items-center justify-center gap-1.5 rounded-md font-medium whitespace-nowrap select-none\",\n          iconOnly ? sizes[size].icon : sizes[size].text,\n          \"text-zinc-500 hover:text-zinc-950 data-[pressed]:text-zinc-950\",\n          \"dark:text-zinc-400 dark:hover:text-white dark:data-[pressed]:text-white\",\n          variant === \"ghost\"\n            ? \"hover:bg-zinc-950/[0.04] data-[pressed]:bg-zinc-950/[0.07] dark:hover:bg-white/[0.06] dark:data-[pressed]:bg-white/[0.1]\"\n            : pill\n              ? \"\"\n              : cx(\n                  // Multiple choice, or before the pill is measured: each pressed item carries its own raised fill.\n                  \"data-[pressed]:bg-white data-[pressed]:shadow-[0_1px_2px_rgb(0_0_0/0.08),0_0_0_1px_rgb(0_0_0/0.06)]\",\n                  \"dark:data-[pressed]:bg-white/[0.12] dark:data-[pressed]:shadow-[inset_0_1px_0_rgb(255_255_255/0.08),0_0_0_1px_rgb(255_255_255/0.06)]\",\n                ),\n          \"transition-[background-color,color,box-shadow,scale] duration-150 ease-out active:duration-100\",\n          \"motion-safe:active:scale-[0.97] motion-reduce:transition-none\",\n          \"focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-sky-500\",\n          \"data-[disabled]:pointer-events-none data-[disabled]:opacity-45\",\n          \"[&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0\",\n        ),\n        className,\n      )}\n      {...props}\n    />\n  );\n}\n"
    }
  ],
  "example": {
    "content": "import { ToggleGroup, ToggleGroupItem } from \"@/components/shaken/ui/toggle-group\";\n\nfunction ListIcon() {\n  return (\n    <svg viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\">\n      <path d=\"M5.5 4h8M5.5 8h8M5.5 12h8M2.5 4h.01M2.5 8h.01M2.5 12h.01\" />\n    </svg>\n  );\n}\n\nfunction GridIcon() {\n  return (\n    <svg viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\">\n      <rect x=\"2.5\" y=\"2.5\" width=\"4.5\" height=\"4.5\" rx=\"1\" />\n      <rect x=\"9\" y=\"2.5\" width=\"4.5\" height=\"4.5\" rx=\"1\" />\n      <rect x=\"2.5\" y=\"9\" width=\"4.5\" height=\"4.5\" rx=\"1\" />\n      <rect x=\"9\" y=\"9\" width=\"4.5\" height=\"4.5\" rx=\"1\" />\n    </svg>\n  );\n}\n\nfunction GraphIcon() {\n  return (\n    <svg viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\">\n      <circle cx=\"3.75\" cy=\"8\" r=\"1.75\" />\n      <circle cx=\"12.25\" cy=\"3.75\" r=\"1.75\" />\n      <circle cx=\"12.25\" cy=\"12.25\" r=\"1.75\" />\n      <path d=\"M5.4 7.2l5.2-2.6M5.4 8.8l5.2 2.6\" />\n    </svg>\n  );\n}\n\nexport default function ToggleGroupExample() {\n  return (\n    <div className=\"grid justify-items-start gap-6\">\n      <ToggleGroup aria-label=\"Time range\" defaultValue={[\"24h\"]}>\n        <ToggleGroupItem value=\"1h\">1h</ToggleGroupItem>\n        <ToggleGroupItem value=\"24h\">24h</ToggleGroupItem>\n        <ToggleGroupItem value=\"7d\">7 days</ToggleGroupItem>\n        <ToggleGroupItem value=\"30d\">30 days</ToggleGroupItem>\n      </ToggleGroup>\n\n      <ToggleGroup aria-label=\"Layout\" defaultValue={[\"list\"]}>\n        <ToggleGroupItem value=\"list\" iconOnly aria-label=\"List\">\n          <ListIcon />\n        </ToggleGroupItem>\n        <ToggleGroupItem value=\"grid\" iconOnly aria-label=\"Grid\">\n          <GridIcon />\n        </ToggleGroupItem>\n        <ToggleGroupItem value=\"graph\" iconOnly aria-label=\"Graph\">\n          <GraphIcon />\n        </ToggleGroupItem>\n      </ToggleGroup>\n\n      <ToggleGroup aria-label=\"Show steps\" multiple defaultValue={[\"running\", \"failed\"]}>\n        <ToggleGroupItem value=\"running\">Running</ToggleGroupItem>\n        <ToggleGroupItem value=\"failed\">Failed</ToggleGroupItem>\n        <ToggleGroupItem value=\"waiting\">Waiting</ToggleGroupItem>\n        <ToggleGroupItem value=\"done\">Done</ToggleGroupItem>\n      </ToggleGroup>\n\n      <ToggleGroup variant=\"ghost\" aria-label=\"Text style\" multiple defaultValue={[\"bold\"]}>\n        <ToggleGroupItem value=\"bold\" aria-label=\"Bold\" iconOnly className=\"font-bold\">\n          B\n        </ToggleGroupItem>\n        <ToggleGroupItem value=\"italic\" aria-label=\"Italic\" iconOnly className=\"font-serif italic\">\n          I\n        </ToggleGroupItem>\n        <ToggleGroupItem value=\"code\" aria-label=\"Code\" iconOnly className=\"font-mono text-xs\">\n          {\"</>\"}\n        </ToggleGroupItem>\n      </ToggleGroup>\n    </div>\n  );\n}\n"
  }
}
