{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"toggle-group","title":"Toggle Group","author":"brijr","description":"A group of toggles that share state, built on UI SDK.","dependencies":["@ui-sdk/react","@ui-sdk/core","class-variance-authority","cn"],"registryDependencies":["https://ui.bridger.to/r/toggle.json"],"files":[{"path":"components/ui/toggle-group.tsx","content":"\"use client\"\n\nimport * as React from \"react\"\nimport {\n  Toggle as TogglePrimitive,\n  ToggleGroup as ToggleGroupPrimitive,\n  createToggleGroupController,\n} from \"@ui-sdk/react/toggle\"\nimport { type VariantProps } from \"class-variance-authority\"\nimport { cn } from \"cn\"\nimport { toggleVariants } from \"@/components/ui/toggle\"\n\nconst ToggleGroupContext = React.createContext<\n  VariantProps<typeof toggleVariants>\n>({\n  size: \"default\",\n  variant: \"default\",\n})\n\nfunction ToggleGroup<T extends string = string>({\n  className,\n  variant = \"default\",\n  size = \"default\",\n  children,\n  name,\n  form,\n  value,\n  defaultValue,\n  onValueChange,\n  controller,\n  ...props\n}: Omit<\n  React.ComponentProps<typeof ToggleGroupPrimitive<T>>,\n  \"className\" | \"children\"\n> &\n  VariantProps<typeof toggleVariants> & {\n    className?: string\n    children?: React.ReactNode\n    /**\n     * Submits the pressed values under this name, one form entry per value.\n     * A form reset returns the group to `defaultValue`.\n     */\n    name?: string\n    /** The id of a form to belong to, when the group isn't inside that form. */\n    form?: string\n  }) {\n  // The library's toggles submit nothing. With a name, the group is controlled\n  // from here so hidden inputs can mirror what is pressed.\n  const submits = name !== undefined\n  const [ownController] = React.useState(() => createToggleGroupController<T>())\n  const groupController = controller ?? ownController\n  const [pressed, setPressed] = React.useState<T[]>(defaultValue ?? [])\n  const current = value ?? pressed\n  const anchor = React.useRef<HTMLInputElement>(null)\n\n  const onReset = React.useEffectEvent((event: Event) => {\n    // Like the library's own controls: wait to see whether the reset was\n    // canceled, and leave a controlled value with no default as it is.\n    setTimeout(() => {\n      if (event.defaultPrevented) return\n      if (value !== undefined && defaultValue === undefined) return\n      groupController.setValue(defaultValue ?? [])\n    })\n  })\n\n  React.useEffect(() => {\n    const owner = submits ? anchor.current?.form : null\n    if (!owner) return\n    owner.addEventListener(\"reset\", onReset)\n    return () => owner.removeEventListener(\"reset\", onReset)\n  }, [submits, form])\n\n  return (\n    <ToggleGroupPrimitive<T>\n      data-variant={variant}\n      data-size={size}\n      className={cn(\n        \"group/toggle-group flex w-fit items-center rounded-lg data-[orientation=vertical]:flex-col data-[orientation=vertical]:items-stretch data-[variant=outline]:shadow-(--elevation-control)\",\n        className\n      )}\n      value={submits ? current : value}\n      defaultValue={submits ? undefined : defaultValue}\n      onValueChange={\n        submits\n          ? (next, details) => {\n              onValueChange?.(next, details)\n              if (!details.canceled) setPressed(next)\n            }\n          : onValueChange\n      }\n      controller={submits ? groupController : controller}\n      {...props}\n    >\n      <ToggleGroupContext.Provider value={{ variant, size }}>\n        {children}\n      </ToggleGroupContext.Provider>\n      {submits ? (\n        <>\n          {/* Unnamed, so it submits nothing: it only finds the owning form. */}\n          <input ref={anchor} type=\"hidden\" form={form} />\n          {current.map((pressedValue) => (\n            <input\n              key={pressedValue}\n              type=\"hidden\"\n              name={name}\n              value={pressedValue}\n              form={form}\n              disabled={props.disabled}\n            />\n          ))}\n        </>\n      ) : null}\n    </ToggleGroupPrimitive>\n  )\n}\n\nfunction ToggleGroupItem({\n  className,\n  variant,\n  size,\n  ...props\n}: Omit<React.ComponentProps<typeof TogglePrimitive>, \"className\" | \"name\"> &\n  VariantProps<typeof toggleVariants> & { className?: string }) {\n  const context = React.useContext(ToggleGroupContext)\n  const itemVariant = variant ?? context.variant\n  const itemSize = size ?? context.size\n\n  return (\n    <TogglePrimitive\n      data-variant={itemVariant}\n      data-size={itemSize}\n      className={cn(\n        toggleVariants({ variant: itemVariant, size: itemSize }),\n        // `last-of-type`, not `last`: a named group ends with hidden inputs.\n        \"flex-auto shrink-0 rounded-none shadow-none group-data-[orientation=horizontal]/toggle-group:first:rounded-l-lg group-data-[orientation=vertical]/toggle-group:first:rounded-t-lg group-data-[orientation=horizontal]/toggle-group:last-of-type:rounded-r-lg group-data-[orientation=vertical]/toggle-group:last-of-type:rounded-b-lg focus:z-10 focus-visible:z-10 group-data-[orientation=horizontal]/toggle-group:data-[variant=outline]:border-l-0 group-data-[orientation=vertical]/toggle-group:data-[variant=outline]:border-t-0 group-data-[orientation=horizontal]/toggle-group:data-[variant=outline]:first:border-l group-data-[orientation=vertical]/toggle-group:data-[variant=outline]:first:border-t group-data-[orientation=horizontal]/toggle-group:pointer-coarse:after:inset-x-0 group-data-[orientation=vertical]/toggle-group:pointer-coarse:after:inset-y-0\",\n        className\n      )}\n      {...props}\n    />\n  )\n}\n\nexport { ToggleGroup, ToggleGroupItem }\n","type":"registry:ui"}],"type":"registry:ui"}