{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "field",
  "title": "Field",
  "description": "Field, input, affix cells, field group and validation with an inserted attention cell. Form (Base UI Form) and vertical, horizontal or responsive field orientation.",
  "dependencies": [
    "@base-ui/react@1.9.0"
  ],
  "registryDependencies": [
    "@keyline-ui/keyline",
    "utils",
    "@keyline-ui/keyline-render"
  ],
  "files": [
    {
      "path": "src/registry/keyline/ui/field.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { Field as FieldPrimitive } from \"@base-ui/react/field\";\nimport { Fieldset as FieldsetPrimitive } from \"@base-ui/react/fieldset\";\nimport { Form as FormPrimitive } from \"@base-ui/react/form\";\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\";\n\n/*\n * Field, as parts named as Base UI names them (FieldRoot, FieldLabel, FieldControl, FieldDescription, FieldError,\n * FieldValidity, Fieldset, FieldsetLegend, Form), and Keyline's own parts for the frame of cells: FieldFrame,\n * FieldAffix, FieldBar, FormActions and the field group.\n *\n *   Gross weight\n *   ┌────┬────────────────────┬────┐\n *   │ !  │            12,004.0│ kg │   the frame: the ! cell (inserted when invalid) · the control · an affix\n *   └────┴────────────────────┴────┘\n *   ▲ Over 24,000 kg needs two trucks.\n *\n * The construction every field component shares (the field's grid, its label, the frame of cells, the input and affix\n * cells, the ! cell, the frame's invalid and disabled states, and the orientation a Form gives its fields) is the\n * theme's: `kl-field`, `kl-field-label`, `kl-field-frame`, `kl-field-input`, `kl-field-affix` and `kl-field-bang` in\n * theme/foundation.css. Select, Combobox, NumberField and the other field components render the same classes.\n */\n\ntype Orientation = \"vertical\" | \"horizontal\" | \"responsive\";\ntype Layout = \"control\" | \"cell\";\n\n/* ------------------------------------------------------------------ the field */\n\n/**\n * Base UI's Field.Root: label, frame, description and error, with Base UI's validation (invalid, validate, match).\n * `orientation`: vertical (label above, the default), horizontal (label in a start column of --kl-form-label,\n * everything else in the control column), responsive (horizontal while the enclosing Form, or any element with\n * `container: kl-form / inline-size`, is at least 560px wide). Without it the field follows its Form's orientation.\n * A field with layout=\"cell\" never goes horizontal.\n */\nfunction FieldRoot({\n  className,\n  orientation,\n  ...props\n}: FieldPrimitive.Root.Props & { orientation?: Orientation }): React.ReactElement {\n  return (\n    <FieldPrimitive.Root\n      data-kl-orientation={orientation}\n      className={resolveClassName(\"kl-field\", className)}\n      {...props}\n      data-slot=\"field\"\n    />\n  );\n}\n\n/** FieldRoot, by its short name. */\nconst Field = FieldRoot;\n\n/** Names the control; Base UI ties it to the control (`for`, or `aria-labelledby` for a non-native control). */\nfunction FieldLabel({ className, ...props }: FieldPrimitive.Label.Props): React.ReactElement {\n  return (\n    <FieldPrimitive.Label\n      className={resolveClassName(\"kl-field-label\", className)}\n      {...props}\n      data-slot=\"field-label\"\n    />\n  );\n}\n\ntype FieldFrameState = { layout: Layout };\n\nconst frame = cn(\n  \"kl-ctl kl-cells kl-field-frame kl-edge\",\n  // a bar spans the frame: the frame wraps, the bar takes a whole line above or below the input row\n  \"has-[>[data-slot=field-bar]]:flex-wrap\",\n  // INPUT GROUP: a Button or Toggle with layout=\"cell\" is a key cell of the frame. It takes the frame's height (not a\n  // cell button's 48px), a seam before it and its own inside ring; the frame's edge stays the input's. These rules\n  // outrank the cell's own utilities by one attribute (the child's slot).\n  \"*:data-[slot=toggle]:min-h-0 *:data-[slot=toggle]:flex-none *:data-[slot=toggle]:self-stretch\",\n  \"*:data-[slot=toggle]:data-kl-icon:w-[calc(var(--kl-ctl)-2*var(--kl-bw,2px))] *:data-[slot=toggle]:data-kl-icon:min-w-0\",\n  \"data-[kl-layout=cell]:*:data-[slot=toggle]:data-kl-icon:w-(--kl-row)\",\n  \"*:data-[slot=button]:data-[kl-layout=cell]:min-h-0 *:data-[slot=button]:data-[kl-layout=cell]:flex-none\",\n  \"*:data-[slot=button]:data-[kl-layout=cell]:cursor-pointer *:data-[slot=button]:data-[kl-layout=cell]:self-stretch\",\n  \"*:data-[slot=button]:data-[kl-layout=cell]:data-[size=icon]:w-[calc(var(--kl-ctl)-2*var(--kl-bw,2px))]\",\n  \"*:data-[slot=button]:data-[kl-layout=cell]:data-[size=icon]:min-w-0\",\n  \"data-[kl-layout=cell]:*:data-[slot=button]:data-[kl-layout=cell]:data-[size=icon]:w-(--kl-row)\",\n);\n\n/**\n * The control frame: a row of cells (affixes, the control, buttons and toggles with `layout=\"cell\"`, bars). The\n * attention cell is inserted at its start and opens while the field is invalid. Its edge is the focus indicator.\n * `layout=\"cell\"`: the frame is a cell of an enclosing frame (no border or cut of its own).\n */\nfunction FieldFrame({\n  layout = \"control\",\n  render,\n  className,\n  style,\n  children,\n  ...props\n}: PartProps<\"div\", FieldFrameState> & { layout?: Layout }): React.ReactElement {\n  const state = React.useMemo(() => ({ layout }), [layout]);\n  const own = {\n    \"data-slot\": \"field-frame\",\n    \"data-kl-layout\": layout,\n    className: resolveClassName(frame, className)(state),\n    style: resolveStyle(undefined, style)(state),\n    children: (\n      <>\n        <span className=\"kl-slot kl-field-bang\" aria-hidden>\n          <span>\n            <span>!</span>\n          </span>\n        </span>\n        {children}\n      </>\n    ),\n  };\n  return useRender({ defaultTagName: \"div\", render, state, props: mergeProps<\"div\">(props, own) });\n}\n\n/** The text input cell: Base UI's Field.Control. `align=\"end\"` right-aligns a number with tabular figures. */\nfunction FieldControl({\n  className,\n  align,\n  ...props\n}: FieldPrimitive.Control.Props & { align?: \"start\" | \"end\" }): React.ReactElement {\n  return (\n    <FieldPrimitive.Control\n      data-align={align}\n      className={resolveClassName(\"kl-field-input\", className)}\n      {...props}\n      data-slot=\"field-control\"\n    />\n  );\n}\n\n/** A multi-line control cell: Base UI's Field.Control rendering a <textarea>, resizable in height. */\nfunction FieldTextarea({\n  className,\n  render,\n  ...props\n}: Omit<React.ComponentProps<\"textarea\">, \"className\" | \"style\"> &\n  Pick<FieldPrimitive.Control.Props, \"className\" | \"style\" | \"render\">): React.ReactElement {\n  return (\n    <FieldPrimitive.Control\n      render={render ?? <textarea />}\n      className={resolveClassName(\"kl-field-input min-h-[72px] w-auto resize-y py-[9px] leading-[1.4]\", className)}\n      {...(props as FieldPrimitive.Control.Props)}\n      data-slot=\"field-textarea\"\n    />\n  );\n}\n\ntype FieldBarState = { side: \"start\" | \"end\" };\n\nconst bar = cn(\n  \"kl-cells order-1 min-h-[calc(var(--kl-row)-4px)] min-w-0 flex-[1_0_100%] cursor-default bg-kl-cell\",\n  \"border-s-0 border-t-(length:--kl-k1) border-t-kl-seam text-(length:--kl-text-label) text-kl-ink-2\",\n  \"data-[side=start]:-order-1 data-[side=start]:border-t-0 data-[side=start]:border-b-(length:--kl-k1)\",\n  \"data-[side=start]:border-b-kl-seam data-[side=start]:ps-[calc(var(--kl-c,0px)*0.35)]\",\n  \"data-[side=start]:[&+*]:border-s-0\",\n  \"*:data-kl-spacer:flex-1 *:data-kl-spacer:border-s-0 *:data-[slot=field-affix]:min-w-0\",\n  // a bar's cells never drop under the 24px target (WCAG 2.5.8; compact measured 24x23 with no spacing exception)\n  \"*:data-[slot=toggle]:min-h-6 *:data-[slot=toggle]:data-kl-icon:w-[max(24px,calc(var(--kl-row)-4px))]\",\n  \"*:data-[slot=button]:data-[kl-layout=cell]:min-h-6 *:data-[slot=toggle]:data-kl-icon:min-w-0\",\n  \"*:data-[slot=button]:data-[kl-layout=cell]:data-[size=icon]:w-[max(24px,calc(var(--kl-row)-4px))]\",\n  \"*:data-[slot=button]:data-[kl-layout=cell]:data-[size=icon]:min-w-0\",\n);\n\n/**\n * A row across the frame's whole width, above (`side=\"start\"`) or below (`side=\"end\"`) the input row: a textarea's\n * formatting toggles, its character count, a hint. Its children are cells. Buttons inside a frame are Button\n * `layout=\"cell\"` (size \"icon\" with an aria-label for icon-only); toggles are Toggle `layout=\"cell\"`. They sit beside\n * the input as cells and keep their own focus ring. A child with `data-kl-spacer` takes the room between.\n */\nfunction FieldBar({\n  side = \"end\",\n  render,\n  className,\n  style,\n  ...props\n}: PartProps<\"div\", FieldBarState> & { side?: \"start\" | \"end\" }): React.ReactElement {\n  const state = React.useMemo(() => ({ side }), [side]);\n  const own = {\n    \"data-slot\": \"field-bar\",\n    \"data-side\": side,\n    className: resolveClassName(bar, className)(state),\n    style: resolveStyle(undefined, style)(state),\n  };\n  return useRender({ defaultTagName: \"div\", render, state, props: mergeProps<\"div\">(props, own) });\n}\n\ntype EmptyState = Record<string, never>;\nconst EMPTY: EmptyState = {};\n\n/** A unit, currency or icon cell inside the frame. */\nfunction FieldAffix({ render, className, style, ...props }: PartProps<\"span\", EmptyState>): React.ReactElement {\n  const own = {\n    \"data-slot\": \"field-affix\",\n    className: resolveClassName(\"kl-field-affix\", className)(EMPTY),\n    style: resolveStyle(undefined, style)(EMPTY),\n  };\n  return useRender({ defaultTagName: \"span\", render, state: EMPTY, props: mergeProps<\"span\">(props, own) });\n}\n\n/** The look of a field's description and error, for a component that renders its own (Questionnaire's step). */\nconst fieldStyles = {\n  description: \"m-0 text-(length:--kl-text-label) text-kl-ink-2\",\n  error: cn(\n    \"m-0 flex items-baseline gap-2 text-(length:--kl-text-label) font-(--kl-weight-strong) text-kl-red-text\",\n    \"[&>.kl-glyph]:[transform:translateY(1px)]\",\n  ),\n};\n\n/** A line under the frame, read out with the control (Base UI ties it through aria-describedby). */\nfunction FieldDescription({ className, ...props }: FieldPrimitive.Description.Props): React.ReactElement {\n  return (\n    <FieldPrimitive.Description\n      className={resolveClassName(fieldStyles.description, className)}\n      {...props}\n      data-slot=\"field-description\"\n    />\n  );\n}\n\n/**\n * The field's error. With children it shows them (pair with `match` for a specific check). Without children it shows\n * Base UI's own message: a `validate` result, a native constraint message, or a server error from Form `errors` for\n * this field's name. Either way the attention glyph leads.\n */\nfunction FieldError({ className, children, ...props }: FieldPrimitive.Error.Props): React.ReactElement {\n  return (\n    <FieldPrimitive.Error\n      className={resolveClassName(fieldStyles.error, className)}\n      render={(rp) => (\n        <div {...rp}>\n          <span className=\"kl-glyph\" data-kind=\"attention\" aria-hidden />\n          <span>{rp.children}</span>\n        </div>\n      )}\n      {...props}\n      {...(children != null ? { children } : null)}\n      data-slot=\"field-error\"\n    />\n  );\n}\n\n/** Base UI's Field.Validity: renders its function child with the field's validity state. No element of its own. */\nconst FieldValidity = FieldPrimitive.Validity;\n\n/* ------------------------------------------------------------------ fieldset */\n\n/** Base UI's Fieldset.Root: a group of fields under one legend, laid out as a field. */\nfunction Fieldset({\n  className,\n  orientation,\n  ...props\n}: FieldsetPrimitive.Root.Props & { orientation?: Orientation }): React.ReactElement {\n  return (\n    <FieldsetPrimitive.Root\n      data-kl-orientation={orientation}\n      className={resolveClassName(\"kl-field\", className)}\n      {...props}\n      data-slot=\"fieldset\"\n    />\n  );\n}\n\n/** Base UI's Fieldset.Legend, in the field label's voice. */\nfunction FieldsetLegend({ className, ...props }: FieldsetPrimitive.Legend.Props): React.ReactElement {\n  return (\n    <FieldsetPrimitive.Legend\n      className={resolveClassName(\"kl-field-label\", className)}\n      {...props}\n      data-slot=\"fieldset-legend\"\n    />\n  );\n}\n\n/* ------------------------------------------------------------------ form */\n\nconst form = cn(\n  \"@container/kl-form grid min-w-0 [align-content:start] gap-5 [--kl-form-gap:16px] [--kl-form-label:11rem]\",\n  \"[[data-kl-density=compact]_&]:gap-3.5 [[data-kl-density=compact]_&]:[--kl-form-gap:12px]\",\n  \"data-[kl-density=compact]:gap-3.5 [[data-kl-density=compact]_&]:[--kl-form-label:10rem]\",\n  \"data-[kl-density=compact]:[--kl-form-gap:12px] data-[kl-density=compact]:[--kl-form-label:10rem]\",\n);\n\n/**\n * Base UI's Form (submit-time validation, server `errors` keyed by field name, `actionsRef`), laid out as a column of\n * fields. It is the container the responsive orientation measures. While its fields are horizontal, FormActions sits\n * in the control column, under the inputs; mark any other element (a row of checkboxes, a notice about one field)\n * data-kl-span=\"control\" to put it there too.\n */\nfunction Form({\n  className,\n  orientation = \"vertical\",\n  ...props\n}: FormPrimitive.Props & { orientation?: Orientation }): React.ReactElement {\n  return (\n    <FormPrimitive\n      data-kl-form-orientation={orientation}\n      className={resolveClassName(form, className)}\n      {...props}\n      data-slot=\"form\"\n    />\n  );\n}\n\n/** The form's commit row: buttons in reading order, the commit action first. */\nfunction FormActions({ render, className, style, ...props }: PartProps<\"div\", EmptyState>): React.ReactElement {\n  const own = {\n    \"data-slot\": \"form-actions\",\n    className: resolveClassName(\"flex flex-wrap items-center gap-2\", className)(EMPTY),\n    style: resolveStyle(undefined, style)(EMPTY),\n  };\n  return useRender({ defaultTagName: \"div\", render, state: EMPTY, props: mergeProps<\"div\">(props, own) });\n}\n\n/* ------------------------------------------------------------------ for field components */\n\n/**\n * Base UI describes only a participant's hidden control. A participant whose focusable parts are its own (spin\n * buttons, OTP slots, a picker trigger) mirrors that aria-describedby (description, error) onto them, so they are\n * announced where focus lands. Base UI sets and changes the attribute after render, so it is observed.\n */\nfunction useDescribedByMirror(\n  root: React.RefObject<HTMLElement | null>,\n  targets: string,\n  source = 'input[type=\"hidden\"]',\n) {\n  React.useEffect(() => {\n    const el = root.current;\n    const hidden = el?.querySelector<HTMLElement>(source);\n    if (!el || !hidden) return;\n    const sync = () => {\n      const ids = hidden.getAttribute(\"aria-describedby\");\n      el.querySelectorAll<HTMLElement>(targets).forEach((t) =>\n        ids ? t.setAttribute(\"aria-describedby\", ids) : t.removeAttribute(\"aria-describedby\"),\n      );\n    };\n    sync();\n    const mo = new MutationObserver(sync);\n    mo.observe(hidden, { attributes: true, attributeFilter: [\"aria-describedby\"] });\n    return () => mo.disconnect();\n  }, [root, targets, source]);\n}\n\n/**\n * The description and error a single-control component (Select, Combobox, Slider, NumberField) prints under its\n * frame. `error` with `invalid` always shows; `error` alone replaces Base UI's message whenever the field fails a\n * check; no `error` shows Base UI's own (a `validate` result, a native constraint, or the Form's `errors` entry for\n * the field's name). Must sit inside the component's Field.Root. With layout=\"cell\" there is no room under the frame:\n * the messages are visually hidden but still describe the control (the frame's red edge and ! cell are the visible\n * signal).\n */\nfunction FieldMessages({\n  description,\n  error,\n  invalid,\n  layout,\n}: {\n  description?: React.ReactNode;\n  error?: React.ReactNode;\n  invalid?: boolean;\n  layout?: Layout;\n}) {\n  const sr = layout === \"cell\" ? \"kl-sr\" : undefined;\n  return (\n    <>\n      {description ? <FieldDescription className={sr}>{description}</FieldDescription> : null}\n      <FieldError className={sr} match={invalid && error != null ? true : undefined}>\n        {error ?? undefined}\n      </FieldError>\n    </>\n  );\n}\n\n/** The Field.Root props a single-control component passes through. */\ntype FieldParticipantProps = {\n  /** Shows the attention state; pair it with `error`. */\n  invalid?: boolean;\n  /** Message under the frame. Without it, Base UI's message for the failed check (or the Form's server error) shows. */\n  error?: React.ReactNode;\n  description?: React.ReactNode;\n  /** Base UI Field validation: return a message (or messages) to fail, null to pass. */\n  validate?: FieldPrimitive.Root.Props[\"validate\"];\n  validationMode?: FieldPrimitive.Root.Props[\"validationMode\"];\n  /** Field orientation (see Field). */\n  orientation?: Orientation;\n};\n\n/* ------------------------------------------------------------------ field group */\n\n/** Several labelled inputs in ONE frame (e.g. L × W × H). Each part is a cell with its own label. */\nfunction FieldGroup({\n  className,\n  legend,\n  children,\n  invalid,\n  orientation,\n  ...props\n}: FieldsetPrimitive.Root.Props & {\n  legend: React.ReactNode;\n  /** Marks the whole group invalid (e.g. a cross-field rule). Parts mark themselves with `invalid`. */ invalid?: boolean;\n  orientation?: Orientation;\n}): React.ReactElement {\n  return (\n    <FieldsetPrimitive.Root\n      data-invalid={invalid ? \"\" : undefined}\n      data-kl-orientation={orientation}\n      className={resolveClassName(\"kl-field\", className)}\n      {...props}\n      data-slot=\"field-group\"\n    >\n      <FieldsetPrimitive.Legend className=\"kl-field-label\" data-slot=\"field-group-legend\">\n        {legend}\n      </FieldsetPrimitive.Legend>\n      <div data-slot=\"field-group-frame\" className=\"kl-ctl kl-cells kl-field-frame\">\n        {children}\n      </div>\n    </FieldsetPrimitive.Root>\n  );\n}\n\n/**\n * One labelled cell of a FieldGroup: a Base UI Field.Root with its label and control. It takes the focus edge (a\n * flush band on the group frame's keylines); the frame does not.\n */\nfunction FieldGroupPart({\n  label,\n  className,\n  inputProps,\n  ...props\n}: FieldPrimitive.Root.Props & {\n  label: string;\n  inputProps?: FieldPrimitive.Control.Props;\n}): React.ReactElement {\n  return (\n    <FieldPrimitive.Root\n      className={resolveClassName(\n        cn(\n          \"kl-field-part kl-edge group/field-part grid min-w-0 flex-1 grid-rows-[auto_1fr] px-(--kl-inset) pt-[5px] pb-0\",\n          \"data-invalid:bg-[color-mix(in_oklab,var(--kl-red)_10%,var(--kl-cell))]\",\n        ),\n        className,\n      )}\n      {...props}\n      data-slot=\"field-group-part\"\n    >\n      <FieldPrimitive.Label\n        data-slot=\"field-group-part-label\"\n        className={cn(\n          \"font-(family-name:--kl-font-mono) text-(length:--kl-text-code)/[1.2] font-medium text-kl-ink-2 not-italic normal-nums\",\n          \"group-data-invalid/field-part:font-(--kl-weight-strong) group-data-invalid/field-part:text-kl-red-text\",\n        )}\n      >\n        {label}\n      </FieldPrimitive.Label>\n      <FieldPrimitive.Control\n        data-slot=\"field-group-part-control\"\n        className=\"kl-field-input w-full px-0 pt-0 pb-1 text-[16px] font-(--kl-weight-strong)\"\n        {...inputProps}\n      />\n    </FieldPrimitive.Root>\n  );\n}\n\n/** Error for a whole group (cross-field rules such as \"height exceeds the trailer\"). Announced politely. */\nfunction FieldGroupError({\n  render,\n  className,\n  style,\n  children,\n  ...props\n}: PartProps<\"p\", EmptyState>): React.ReactElement | null {\n  const own = {\n    \"data-slot\": \"field-group-error\",\n    role: \"alert\",\n    className: resolveClassName(fieldStyles.error, className)(EMPTY),\n    style: resolveStyle(undefined, style)(EMPTY),\n    children: (\n      <>\n        <span className=\"kl-glyph\" data-kind=\"attention\" aria-hidden />\n        <span>{children}</span>\n      </>\n    ),\n  };\n  const element = useRender({ defaultTagName: \"p\", render, state: EMPTY, props: mergeProps<\"p\">(props, own) });\n  return children ? element : null;\n}\n\n/** The sign between two parts of a group (× by default). Hidden from assistive technology. */\nfunction FieldGroupJoin({\n  render,\n  className,\n  style,\n  children = \"×\",\n  ...props\n}: PartProps<\"span\", EmptyState>): React.ReactElement {\n  const own = {\n    \"data-slot\": \"field-group-join\",\n    \"aria-hidden\": true,\n    className: resolveClassName(\n      \"grid w-[22px] flex-none place-items-center text-(length:--kl-text-meta) text-kl-ink-2\",\n      className,\n    )(EMPTY),\n    style: resolveStyle(undefined, style)(EMPTY),\n    children,\n  };\n  return useRender({ defaultTagName: \"span\", render, state: EMPTY, props: mergeProps<\"span\">(props, own) });\n}\n\nexport {\n  Form,\n  FormActions,\n  Field,\n  FieldRoot,\n  FieldLabel,\n  FieldFrame,\n  FieldControl,\n  FieldTextarea,\n  FieldAffix,\n  FieldBar,\n  FieldDescription,\n  FieldError,\n  FieldValidity,\n  Fieldset,\n  FieldsetLegend,\n  FieldGroup,\n  FieldGroupPart,\n  FieldGroupJoin,\n  FieldGroupError,\n  FieldMessages,\n  fieldStyles,\n  useDescribedByMirror,\n  type FieldParticipantProps,\n};\n",
      "type": "registry:ui",
      "target": "components/keyline/ui/field.tsx"
    }
  ],
  "type": "registry:ui"
}