{
  "name": "checkbox-group",
  "type": "registry:ui",
  "framework": "react",
  "title": "Checkbox Group",
  "description": "Several answers out of a visible list, each with an optional description.",
  "dependencies": [
    "radix-ui"
  ],
  "files": [
    {
      "path": "ui/checkbox-group.tsx",
      "content": "\"use client\"\n\nimport { Checkbox as CheckboxPrimitive } from \"radix-ui\"\nimport * as React from \"react\"\n\nimport { cn } from \"@/lib/utils\"\n\n/*\n * Several answers out of a visible list, where a radio group takes one. The\n * group owns the set of chosen values; each row is a Radix Checkbox, which\n * owns the role, the space bar and the hidden input a form reads.\n */\ninterface CheckboxGroupContextValue {\n  readonly values: readonly string[]\n  readonly toggle: (value: string) => void\n  readonly name: string | undefined\n  readonly disabled: boolean\n}\n\nconst CheckboxGroupContext =\n  React.createContext<CheckboxGroupContextValue | null>(null)\n\nfunction useCheckboxGroup(component: string): CheckboxGroupContextValue {\n  const context = React.useContext(CheckboxGroupContext)\n  if (context === null) {\n    throw new Error(`<${component}> must be used inside a <CheckboxGroup>.`)\n  }\n  return context\n}\n\nexport interface CheckboxGroupProps extends Omit<\n  React.HTMLAttributes<HTMLDivElement>,\n  \"onChange\" | \"defaultValue\"\n> {\n  /** Controlled selection. Leave undefined to let the group own it. */\n  value?: readonly string[]\n  defaultValue?: readonly string[]\n  onValueChange?: (value: readonly string[]) => void\n  /** Accessible name for the group. */\n  label?: string\n  /** Shared `name` of the rows. A form then receives one entry per checked row. */\n  name?: string\n  /** Disables every row. */\n  disabled?: boolean\n}\n\nconst CheckboxGroup = React.forwardRef<HTMLDivElement, CheckboxGroupProps>(\n  (\n    {\n      className,\n      value,\n      defaultValue = [],\n      onValueChange,\n      label,\n      name,\n      disabled = false,\n      children,\n      ...props\n    },\n    ref\n  ) => {\n    const [internal, setInternal] =\n      React.useState<readonly string[]>(defaultValue)\n    const isControlled = value !== undefined\n    const current = isControlled ? value : internal\n\n    const toggle = React.useCallback(\n      (option: string) => {\n        const next = current.includes(option)\n          ? current.filter((each) => each !== option)\n          : [...current, option]\n        if (!isControlled) setInternal(next)\n        onValueChange?.(next)\n      },\n      [current, isControlled, onValueChange]\n    )\n\n    const context = React.useMemo<CheckboxGroupContextValue>(\n      () => ({ values: current, toggle, name, disabled }),\n      [current, toggle, name, disabled]\n    )\n\n    return (\n      <CheckboxGroupContext.Provider value={context}>\n        <div\n          ref={ref}\n          role=\"group\"\n          aria-label={label}\n          className={cn(\"flex flex-col gap-2\", className)}\n          {...props}\n        >\n          {children}\n        </div>\n      </CheckboxGroupContext.Provider>\n    )\n  }\n)\nCheckboxGroup.displayName = \"CheckboxGroup\"\n\nexport interface CheckboxOptionProps extends Omit<\n  React.ComponentPropsWithoutRef<typeof CheckboxPrimitive.Root>,\n  \"checked\" | \"defaultChecked\" | \"onCheckedChange\" | \"value\" | \"name\"\n> {\n  /** What this row stands for in the group's value. */\n  value: string\n  /** The quieter line under the label. */\n  description?: React.ReactNode\n}\n\nconst CheckboxOption = React.forwardRef<\n  React.ComponentRef<typeof CheckboxPrimitive.Root>,\n  CheckboxOptionProps\n>(({ className, value, description, disabled, children, ...props }, ref) => {\n  const group = useCheckboxGroup(\"CheckboxOption\")\n  const checked = group.values.includes(value)\n  const isDisabled = group.disabled || disabled === true\n\n  return (\n    /*\n     * The label wraps the box, so a press on the text reaches it and the text\n     * is its accessible name.\n     */\n    <label\n      className={cn(\n        \"group flex items-start gap-3 text-[15px] leading-5 text-newt-text-secondary\",\n        isDisabled ? \"cursor-not-allowed opacity-50\" : \"cursor-pointer\",\n        checked && !isDisabled && \"text-newt-text-primary\",\n        className\n      )}\n    >\n      <CheckboxPrimitive.Root\n        ref={ref}\n        name={group.name}\n        value={value}\n        checked={checked}\n        disabled={isDisabled}\n        onCheckedChange={() => group.toggle(value)}\n        className={cn(\n          \"mt-px flex h-5 w-5 shrink-0 cursor-pointer items-center justify-center rounded-[var(--newt-radius-sm)] border-2 border-newt-text-muted bg-transparent p-0 transition-colors duration-fast ease-newt disabled:cursor-not-allowed\",\n          \"data-[state=checked]:border-newt-brand data-[state=checked]:bg-newt-brand\",\n          \"focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-newt-text-link\",\n          !isDisabled && \"group-hover:border-newt-text-secondary\"\n        )}\n        {...props}\n      >\n        {/* Mounted in both states, so the check can scale in and out. */}\n        <CheckboxPrimitive.Indicator\n          forceMount\n          className=\"flex scale-0 text-newt-on-accent transition-transform duration-fast ease-newt data-[state=checked]:scale-100\"\n        >\n          <svg aria-hidden=\"true\" viewBox=\"0 0 16 16\" className=\"h-3.5 w-3.5\">\n            <path\n              fill=\"currentColor\"\n              d=\"M6.2 11.6 3.1 8.5a1 1 0 0 1 1.4-1.4l1.7 1.7 4.3-4.3a1 1 0 1 1 1.4 1.4l-5 5a1 1 0 0 1-1.4 0Z\"\n            />\n          </svg>\n        </CheckboxPrimitive.Indicator>\n      </CheckboxPrimitive.Root>\n      <span className=\"flex min-w-0 flex-col gap-0.5\">\n        <span>{children}</span>\n        {description === undefined ? null : (\n          <span className=\"text-[13px] leading-[17px] text-newt-text-muted\">\n            {description}\n          </span>\n        )}\n      </span>\n    </label>\n  )\n})\nCheckboxOption.displayName = \"CheckboxOption\"\n\nexport { CheckboxGroup, CheckboxOption }\n",
      "type": "registry:ui"
    }
  ]
}