{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "checkbox-group",
  "title": "Checkbox group",
  "description": "Several independent choices to one question in one frame (Base UI CheckboxGroup): an optional parent row that checks or clears all with a count, rows with description and meta, Form participant.",
  "dependencies": [
    "@base-ui/react@1.9.0"
  ],
  "registryDependencies": [
    "@keyline-ui/keyline",
    "utils",
    "@keyline-ui/checkbox",
    "@keyline-ui/field",
    "@keyline-ui/keyline-render"
  ],
  "files": [
    {
      "path": "src/registry/keyline/ui/checkbox-group.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { CheckboxGroup as CheckboxGroupPrimitive } from \"@base-ui/react/checkbox-group\";\nimport { Field as FieldPrimitive } from \"@base-ui/react/field\";\nimport { Fieldset as FieldsetPrimitive } from \"@base-ui/react/fieldset\";\nimport { mergeProps } from \"@base-ui/react/merge-props\";\nimport { useRender } from \"@base-ui/react/use-render\";\n\nimport { cn } from \"@/lib/utils\";\nimport { type PartProps, resolveClassName, resolveStyle } from \"../lib/keyline-render\";\nimport { Checkbox, choiceStyles } from \"./checkbox\";\nimport { FieldMessages, type FieldParticipantProps } from \"./field\";\n\n/*\n * Checkbox group, as parts (CheckboxGroupRoot, Base UI's CheckboxGroup as a fieldset in its own Field; its\n * CheckboxGroupLegend; the CheckboxGroupFrame; CheckboxGroupItem rows) and CheckboxGroup, the field built from them.\n *\n *   Notify me about\n *   ┌───────────────────────────────┬──────┐\n *   │ ▬  All events                 │  2/4 │   parent row (selectAll): mixed, checked or clear; 2px rule under it\n *   ┝━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┿━━━━━━┥\n *   │ ■  Delayed                    │   12 │   rows: box cell · label + description · meta cell\n *   ├───────────────────────────────┼──────┤\n *   │ □  Delivered                  │   48 │\n *   └───────────────────────────────┴──────┘\n */\n\ntype Layout = \"control\" | \"cell\";\ntype Orientation = NonNullable<FieldParticipantProps[\"orientation\"]>;\n\nconst LayoutContext = React.createContext<Layout>(\"control\");\n\ntype CheckboxGroupRootProps = CheckboxGroupPrimitive.Props &\n  Pick<FieldPrimitive.Root.Props, \"name\" | \"invalid\" | \"validate\" | \"validationMode\"> & {\n    /** control (default): its own 2px frame, legend above. cell: rows of an enclosing frame; the legend is announced\n     * but not shown. */\n    layout?: Layout;\n    orientation?: Orientation;\n  };\n\n/**\n * Base UI's CheckboxGroup, rendered as a fieldset inside its own Base UI Field.Root (the two in one part): `name`,\n * `invalid`, `validate` and `validationMode` go to the field, so Form values, FormData and Form `errors` reach the\n * group; the rest (`value`, `defaultValue`, `onValueChange` with Base UI's event details last, `allValues`,\n * `disabled`) to the group. Lay it out with CheckboxGroupLegend, CheckboxGroupFrame and CheckboxGroupItem rows.\n */\nfunction CheckboxGroupRoot({\n  name,\n  invalid,\n  validate,\n  validationMode,\n  disabled,\n  layout = \"control\",\n  orientation,\n  className,\n  ...props\n}: CheckboxGroupRootProps): React.ReactElement {\n  return (\n    <LayoutContext.Provider value={layout}>\n      <FieldPrimitive.Root\n        name={name}\n        invalid={invalid}\n        validate={validate}\n        validationMode={validationMode}\n        disabled={disabled}\n        className=\"contents\"\n      >\n        <FieldsetPrimitive.Root\n          render={\n            <CheckboxGroupPrimitive\n              data-kl-layout={layout}\n              data-kl-orientation={orientation}\n              data-invalid={invalid ? \"\" : undefined}\n              disabled={disabled}\n              className={resolveClassName(\"kl-field\", className)}\n              {...props}\n              data-slot=\"checkbox-group\"\n            />\n          }\n        />\n      </FieldPrimitive.Root>\n    </LayoutContext.Provider>\n  );\n}\n\n/** Names the group: Base UI's Fieldset.Legend, in the field label's voice. Visually hidden in the `cell` layout. */\nfunction CheckboxGroupLegend({ className, ...props }: FieldsetPrimitive.Legend.Props): React.ReactElement {\n  const layout = React.useContext(LayoutContext);\n  return (\n    <FieldsetPrimitive.Legend\n      className={resolveClassName(\n        cn(\n          \"kl-field-label [[data-disabled]>[data-slot=checkbox-group]>&]:text-kl-ink-2 [[data-slot=checkbox-group][data-disabled]>&]:text-kl-ink-2\",\n          layout === \"cell\" && \"kl-sr\",\n        ),\n        className,\n      )}\n      {...props}\n      data-slot=\"checkbox-group-legend\"\n    />\n  );\n}\n\ntype FrameState = { layout: Layout };\n\nconst frame = cn(\n  \"kl-ctl flex min-w-0 flex-col bg-kl-cell data-[kl-layout=cell]:bg-transparent\",\n  // no option has meta: the frame says so to its meta cells (a :has() on the frame itself; one in the cells' selector\n  // restyles the whole document on any DOM change). An empty cell hides; the parent's count sits undivided.\n  \"[--kl-cbg-empty:initial] [--kl-cbg-pbs:initial] [--kl-cbg-pmin:initial]\",\n  \"not-has-[[data-slot=checkbox-group-item]:not([data-kl-parent])_[data-slot=checkbox-group-item-meta]:not([data-empty])]:[--kl-cbg-empty:none]\",\n  \"not-has-[[data-slot=checkbox-group-item]:not([data-kl-parent])_[data-slot=checkbox-group-item-meta]:not([data-empty])]:[--kl-cbg-pmin:0px]\",\n  \"not-has-[[data-slot=checkbox-group-item]:not([data-kl-parent])_[data-slot=checkbox-group-item-meta]:not([data-empty])]:[--kl-cbg-pbs:0px]\",\n  // attention: the frame turns red (a Base UI check, Form errors, or `invalid`); the message is below\n  \"[[data-slot=checkbox-group][data-invalid]:not([data-disabled])>&]:border-kl-red\",\n  \"[[data-slot=checkbox-group][data-invalid]:not([data-disabled])>&]:[--kl-cut-line:var(--kl-red)]\",\n  \"[[data-invalid]:not([data-disabled])>[data-slot=checkbox-group]>&]:border-kl-red\",\n  \"[[data-invalid]:not([data-disabled])>[data-slot=checkbox-group]>&]:[--kl-cut-line:var(--kl-red)]\",\n  \"[[data-slot=checkbox-group][data-disabled]>&]:border-kl-dis-line\",\n  \"[[data-slot=checkbox-group][data-disabled]>&]:[--kl-cut-line:var(--kl-dis-line)]\",\n  \"[[data-disabled]>[data-slot=checkbox-group]>&]:border-kl-dis-line\",\n  \"[[data-disabled]>[data-slot=checkbox-group]>&]:[--kl-cut-line:var(--kl-dis-line)]\",\n);\n\n/** The frame of rows: they meet on 1px keylines, and the parent row meets its options on a 2px rule. */\nfunction CheckboxGroupFrame({ render, className, style, ...props }: PartProps<\"div\", FrameState>): React.ReactElement {\n  const layout = React.useContext(LayoutContext);\n  const state = React.useMemo(() => ({ layout }), [layout]);\n  const own = {\n    \"data-slot\": \"checkbox-group-frame\",\n    \"data-kl-layout\": layout,\n    className: resolveClassName(frame, className)(state),\n    style: resolveStyle(undefined, style)(state),\n  };\n  return useRender({ defaultTagName: \"div\", render, state, props: mergeProps<\"div\">(props, own) });\n}\n\nconst item = cn(\n  \"group/cbg-row min-w-0\",\n  \"[[data-slot=checkbox-group-item]+&]:border-t-(length:--kl-k1) [[data-slot=checkbox-group-item]+&]:border-t-kl-line\",\n  \"[[data-slot=checkbox-group-item][data-kl-parent]+&]:border-t-(length:--kl-k2)\",\n  \"forced-colors:[[data-slot=checkbox-group-item][data-kl-parent]+&]:border-t-[CanvasText]\",\n);\n\nconst row = cn(\n  choiceStyles.row,\n  \"text-kl-ink [[data-slot=checkbox-group-item]:not([data-disabled])>&]:hover:bg-kl-tint\",\n  \"[[data-slot=checkbox-group-item][data-disabled]>&]:cursor-not-allowed\",\n  \"[[data-slot=checkbox-group-item][data-disabled]>&]:bg-(image:--kl-hatch)\",\n  \"[[data-slot=checkbox-group-item][data-disabled]>&]:text-kl-dis\",\n);\n\n// the column exists only when an OPTION has meta; the parent's count alone sits undivided at the row's end\nconst meta = cn(\n  \"flex min-w-(--kl-cbg-meta,64px) flex-none items-center justify-end px-(--kl-inset)\",\n  \"border-s-(length:--kl-k1) border-s-kl-seam font-(--kl-weight-strong) text-kl-ink-2 tabular-nums\",\n  \"group-data-disabled/cbg-row:text-kl-dis [&_.kl-code]:text-(length:--kl-text-meta) [&_.kl-code]:text-inherit\",\n  \"data-empty:[display:var(--kl-cbg-empty,flex)]\",\n  \"[[data-slot=checkbox-group-frame]>[data-kl-parent]_&]:min-w-[var(--kl-cbg-pmin,var(--kl-cbg-meta,64px))]\",\n  \"[[data-slot=checkbox-group-frame]>[data-kl-parent]_&]:border-s-[length:var(--kl-cbg-pbs,var(--kl-k1))]\",\n);\n\ntype CheckboxGroupItemProps = Omit<React.ComponentProps<typeof Checkbox>, \"children\" | \"className\"> & {\n  label: React.ReactNode;\n  description?: React.ReactNode;\n  meta?: React.ReactNode;\n  /** Goes to the row (Base UI's Field.Item); a function receives its state. */\n  className?: FieldPrimitive.Item.Props[\"className\"];\n};\n\n/**\n * One row: box cell · label (+ description) · optional meta cell. The row is Base UI's Field.Item, its label is the\n * whole row (the hit target), and every Checkbox prop (`value`, `parent`, `disabled`) goes to the box.\n */\nfunction CheckboxGroupItem({\n  label,\n  description,\n  meta: metaContent,\n  disabled,\n  className,\n  ...props\n}: CheckboxGroupItemProps): React.ReactElement {\n  return (\n    <FieldPrimitive.Item\n      data-kl-parent={props.parent ? \"\" : undefined}\n      disabled={disabled}\n      className={resolveClassName(item, className)}\n      data-slot=\"checkbox-group-item\"\n    >\n      <FieldPrimitive.Label data-slot=\"checkbox-group-item-row\" className={row}>\n        <span data-slot=\"checkbox-group-item-box\" className={choiceStyles.box}>\n          <Checkbox disabled={disabled} {...props} />\n        </span>\n        <span data-slot=\"checkbox-group-item-text\" className={choiceStyles.text}>\n          <span\n            data-slot=\"checkbox-group-item-label\"\n            className={cn(choiceStyles.label, \"group-data-disabled/cbg-row:text-kl-dis\")}\n          >\n            {label}\n          </span>\n          {description ? (\n            <FieldPrimitive.Description\n              data-slot=\"checkbox-group-item-description\"\n              className={cn(choiceStyles.description, \"group-data-disabled/cbg-row:text-kl-dis\")}\n            >\n              {description}\n            </FieldPrimitive.Description>\n          ) : null}\n        </span>\n        {metaContent != null ? (\n          <span data-slot=\"checkbox-group-item-meta\" className={meta}>\n            {metaContent}\n          </span>\n        ) : (\n          <span data-slot=\"checkbox-group-item-meta\" className={meta} data-empty=\"\" aria-hidden />\n        )}\n      </FieldPrimitive.Label>\n    </FieldPrimitive.Item>\n  );\n}\n\ntype CheckboxGroupOption = {\n  value: string;\n  label: React.ReactNode;\n  description?: React.ReactNode;\n  /** Trailing cell: a count, a price, a code. One row with meta keeps the column on every row. */\n  meta?: React.ReactNode;\n  disabled?: boolean;\n};\n\ntype CheckboxGroupProps = Omit<\n  CheckboxGroupPrimitive.Props,\n  \"value\" | \"defaultValue\" | \"onValueChange\" | \"render\" | \"children\" | \"className\"\n> &\n  FieldParticipantProps & {\n    /** Names the group (a fieldset legend). With layout=\"cell\" it is announced but not shown: the enclosing frame's header names it. */\n    legend: React.ReactNode;\n    options?: CheckboxGroupOption[];\n    value?: string[];\n    defaultValue?: string[];\n    /** Called with the checked values and Base UI's event details (the reason, the event). */\n    onValueChange?: (value: string[], eventDetails: CheckboxGroupPrimitive.ChangeEventDetails) => void;\n    /**\n     * A parent row that checks or clears every option (Base UI's parent checkbox: mixed while some are\n     * checked). true labels it \"All\"; pass a node to name it (\"All carriers\"). Its meta cell counts \"3/5\".\n     */\n    selectAll?: React.ReactNode;\n    /** Form name: Base UI Form values and FormData get the checked values under it. */\n    name?: string;\n    /** control (default): its own 2px frame, legend above. cell: rows of an enclosing frame (a panel body, a popover). */\n    layout?: Layout;\n    /** Extra rows (CheckboxGroupItem) after the options, for rows the options shape can't describe. */\n    children?: React.ReactNode;\n    className?: CheckboxGroupPrimitive.Props[\"className\"];\n  };\n\n/**\n * Several independent choices that belong to one question, in one frame, built from the parts above.\n *\n * - Behaviour is Base UI CheckboxGroup in a Fieldset in a Field: Tab visits each box, Space toggles, the\n *   parent row checks or clears every enabled option; disabled options keep their state.\n * - Field participant: `name` puts the checked values in Form values and FormData, `validate` and Form\n *   `errors` reach it, and `invalid` + `error` show the attention state (red frame, the message below).\n * - The parent row is the group's summary, so it is divided from its options by a 2px rule (the same\n *   weight a toolbar group's edge has): one unit, then its parts.\n * - Each row's whole width is the hit target (the label wraps the box); long labels wrap inside the row.\n */\nfunction CheckboxGroup({\n  legend,\n  options = [],\n  value,\n  defaultValue,\n  onValueChange,\n  selectAll,\n  layout = \"control\",\n  children,\n  allValues: allValuesProp,\n  error,\n  description,\n  invalid,\n  ...props\n}: CheckboxGroupProps): React.ReactElement {\n  const [inner, setInner] = React.useState<string[]>(defaultValue ?? []);\n  const current = value ?? inner;\n  const allValues = allValuesProp ?? options.map((o) => o.value);\n  const checked = current.filter((v) => allValues.includes(v)).length;\n  return (\n    <CheckboxGroupRoot\n      layout={layout}\n      invalid={invalid}\n      value={current}\n      onValueChange={(v, eventDetails) => {\n        setInner(v);\n        onValueChange?.(v, eventDetails);\n      }}\n      allValues={allValues}\n      {...props}\n    >\n      <CheckboxGroupLegend>{legend}</CheckboxGroupLegend>\n      <CheckboxGroupFrame>\n        {selectAll != null && selectAll !== false ? (\n          <CheckboxGroupItem\n            parent\n            label={selectAll === true ? \"All\" : selectAll}\n            meta={\n              <span className=\"kl-code\">\n                {checked}/{allValues.length}\n              </span>\n            }\n          />\n        ) : null}\n        {options.map((o) => (\n          <CheckboxGroupItem\n            key={o.value}\n            value={o.value}\n            label={o.label}\n            description={o.description}\n            meta={o.meta}\n            disabled={o.disabled}\n          />\n        ))}\n        {children}\n      </CheckboxGroupFrame>\n      <FieldMessages description={description} error={error} invalid={invalid} layout={layout} />\n    </CheckboxGroupRoot>\n  );\n}\n\nexport {\n  CheckboxGroup,\n  CheckboxGroupRoot,\n  CheckboxGroupLegend,\n  CheckboxGroupFrame,\n  CheckboxGroupItem,\n  type CheckboxGroupOption,\n  type CheckboxGroupProps,\n};\n",
      "type": "registry:ui",
      "target": "components/keyline/ui/checkbox-group.tsx"
    }
  ],
  "type": "registry:ui"
}