{
  "$schema": "https://zoblocks.design/schema/registry-item.json",
  "name": "chart-accordion",
  "type": "zoblocks:component",
  "title": "Chart Accordion",
  "description": "Accordion with a clinical summary vocabulary: a status paired with its severity, a count, and a timestamp at the precision the record holds. Adds an expand-all control that never opens a section the reader may not have.",
  "categories": [
    "disclosure",
    "clinical"
  ],
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "registryDependencies": [
    "utils",
    "tokens",
    "accordion-core",
    "accordion"
  ],
  "files": [
    {
      "path": "registry/zoblocks/chart-accordion/chart-accordion.tsx",
      "type": "zoblocks:component",
      "target": "components/zoblocks/chart-accordion.tsx",
      "content": "\"use client\";\n\n/**\n * ChartAccordion — the record's sections, readable while closed.\n *\n * `Accordion` will let you build a header that says nothing. This one will not.\n * A section's summary is composed here from a small, closed vocabulary — a\n * status with a severity, a count, a time — so the two rules that matter cannot\n * be forgotten at the call site:\n *\n *   1. **Severity never travels alone.** A rail with no words is colour\n *      carrying meaning, which forced-colors mode discards, monochrome printing\n *      discards, and roughly one in twelve men cannot resolve. Passing\n *      `severity` without `status` is a type error.\n *   2. **A time is a time.** `updatedAt` is rendered from the string the record\n *      holds, at the precision the record holds it, beside its zone. A relative\n *      time alone (\"3 hours ago\") is not a clinical timestamp.\n *\n * Everything else is `Accordion`, including the access model. The toolbar is\n * here rather than there because expanding a whole chart is a chart-shaped\n * action: it is what makes the record printable and searchable in one press,\n * and it must never expand a section the reader may not have.\n */\n\nimport * as React from \"react\";\nimport { cn } from \"@/lib/utils\";\nimport {\n  Accordion,\n  type AccordionDensity,\n  type AccordionProps,\n} from \"@/components/zoblocks/accordion\";\nimport {\n  DEFAULT_ACCORDION_LOCALE,\n  isWithheld,\n  type AccessDescriptor,\n  type AccordionHeadingLevel,\n  type AccordionItem,\n  type AccordionSeverity,\n  type DisclosureEvent,\n} from \"@/lib/zoblocks-accordion\";\n\ninterface ChartSectionBase {\n  key: React.Key;\n  /** The section's name, as the reader's vocabulary spells it. */\n  label: React.ReactNode;\n  /** Content. Omitted for a withheld section, which has none. */\n  children?: React.ReactNode;\n  /**\n   * The one fact that would change what the reader does next.\n   *\n   * Required alongside `severity`, because a coloured rail with nothing beside\n   * it is a signal only some readers receive.\n   */\n  status?: React.ReactNode;\n  /** How many things are in here. Rendered beside the status, never instead of it. */\n  count?: React.ReactNode;\n  /**\n   * When this section last changed, as the record spells it.\n   *\n   * A string rather than a Date: a FHIR `2026-08` means August, and widening it\n   * to the first of the month invents a precision nobody recorded.\n   */\n  updatedAt?: string;\n  access?: AccordionItem[\"access\"];\n  pinned?: boolean;\n}\n\n/**\n * Severity and status travel together or not at all.\n *\n * Enforced in the type rather than at runtime, so the omission is a build\n * failure at the call site instead of a missing signal in a rendered chart.\n */\nexport type ChartSection =\n  | (ChartSectionBase & { severity: AccordionSeverity; status: React.ReactNode })\n  | (ChartSectionBase & { severity?: undefined });\n\nexport interface ChartAccordionProps extends Omit<\n  AccordionProps,\n  \"items\" | \"activeKey\" | \"defaultActiveKey\" | \"accordion\" | \"onChange\"\n> {\n  /**\n   * The record's sections, in the order the house reads them. Each carries its own summary,\n   * severity and access rules.\n   */\n  sections: readonly ChartSection[];\n  /** Open on first render. */\n  defaultOpenKeys?: readonly React.Key[];\n  /** Fired with the open sections whenever they change. */\n  onChange?: (keys: React.Key[]) => void;\n  /** Show \"Expand all\" and \"Collapse all\". On by default. */\n  toolbar?: boolean;\n  /** Leading content in the toolbar — usually who the record belongs to. */\n  toolbarLabel?: React.ReactNode;\n  headingLevel?: AccordionHeadingLevel;\n  density?: AccordionDensity;\n  onDisclose?: (event: DisclosureEvent) => boolean | Promise<boolean>;\n}\n\nconst SEVERITY_CLASS: Record<AccordionSeverity, string> = {\n  critical: \"zb-chip--critical\",\n  high: \"zb-chip--high\",\n  low: \"zb-chip--low\",\n  normal: \"zb-chip--normal\",\n  unknown: \"zb-chip--unknown\",\n};\n\n/**\n * A severity chip.\n *\n * A literal lookup rather than an interpolated class name: Tailwind resolves\n * classes by scanning source text, so a class assembled at runtime produces no\n * CSS — which on a severity chip silently deletes the signal.\n *\n * This should route through StatusBadge once that component lands, so that\n * critical reads identically here, on a lab result, and on a medication. Until\n * then it uses the same `--zb-badge-*` tokens, which is what makes that a\n * refactor rather than a re-design.\n */\nfunction Chip({ severity, children }: { severity?: AccordionSeverity; children: React.ReactNode }) {\n  return (\n    <span className={cn(\"zb-chip\", severity ? SEVERITY_CLASS[severity] : undefined)}>\n      {children}\n    </span>\n  );\n}\n\nfunction summaryFor(section: ChartSection): React.ReactNode {\n  const parts: React.ReactNode[] = [];\n\n  if (section.status !== undefined && section.status !== null) {\n    parts.push(\n      <Chip key=\"status\" {...(section.severity ? { severity: section.severity } : {})}>\n        {section.status}\n      </Chip>,\n    );\n  }\n  if (section.count !== undefined && section.count !== null) {\n    parts.push(\n      <span key=\"count\" className=\"zb-chart-accordion__count\">\n        {section.count}\n      </span>,\n    );\n  }\n  if (section.updatedAt) {\n    parts.push(\n      <time key=\"time\" className=\"zb-chart-accordion__time\" dateTime={section.updatedAt}>\n        {section.updatedAt}\n      </time>,\n    );\n  }\n\n  return parts.length ? parts : undefined;\n}\n\nexport function ChartAccordion({\n  sections,\n  defaultOpenKeys,\n  onChange,\n  toolbar = true,\n  toolbarLabel,\n  headingLevel = 3,\n  density = \"clinical\",\n  locale: localeOverride,\n  className,\n  ...rest\n}: ChartAccordionProps) {\n  const locale = React.useMemo(\n    () => ({ ...DEFAULT_ACCORDION_LOCALE, ...localeOverride }),\n    [localeOverride],\n  );\n\n  const [openKeys, setOpenKeys] = React.useState<React.Key[]>(() => [...(defaultOpenKeys ?? [])]);\n\n  const commit = React.useCallback(\n    (keys: React.Key[]) => {\n      setOpenKeys(keys);\n      onChange?.(keys);\n    },\n    [onChange],\n  );\n\n  const items = React.useMemo<AccordionItem[]>(\n    () =>\n      sections.map((section) => {\n        const summary = summaryFor(section);\n        const base = {\n          /*\n           * Typed from the target rather than inferred from the source.\n           *\n           * Both sides declare `React.Key`, but React 19 leaves that alias open\n           * for a host to extend, and Next does extend it. Compiled under this\n           * repository's config the two agree; compiled under the docs app's,\n           * the inferred literal picked up the widened alias and the declared\n           * one did not, so building the items failed to typecheck in the only\n           * project that renders them. Reading the type off `AccordionItem`\n           * makes them the same type by construction, under any host.\n           */\n          key: section.key as AccordionItem[\"key\"],\n          label: section.label,\n          ...(summary === undefined ? {} : { summary }),\n          ...(section.severity ? { severity: section.severity } : {}),\n          ...(section.pinned ? { pinned: section.pinned } : {}),\n        };\n\n        // The withheld arm is a separate object literal rather than a spread,\n        // so the discriminated union still discriminates: TypeScript cannot see\n        // through a conditional spread to know `children` was never set.\n        const access = section.access as AccessDescriptor | undefined;\n        if (access?.kind === \"withheld\") {\n          return { ...base, access };\n        }\n        return {\n          ...base,\n          ...(access ? { access } : {}),\n          ...(section.children === undefined ? {} : { children: section.children }),\n        };\n      }),\n    [sections],\n  );\n\n  const expandable = React.useMemo(\n    () => items.filter((item) => !isWithheld(item)).map((item) => item.key),\n    [items],\n  );\n\n  return (\n    <div className={cn(\"zb-chart-accordion\", className)}>\n      {toolbar ? (\n        <div className=\"zb-chart-accordion__toolbar\">\n          {toolbarLabel ? (\n            <span className=\"zb-chart-accordion__toolbar-label\">{toolbarLabel}</span>\n          ) : null}\n          <span className=\"zb-chart-accordion__toolbar-actions\">\n            <button\n              type=\"button\"\n              className=\"zb-chart-accordion__action\"\n              onClick={() => commit([...expandable])}\n            >\n              {locale.expandAll}\n            </button>\n            <button type=\"button\" className=\"zb-chart-accordion__action\" onClick={() => commit([])}>\n              {locale.collapseAll}\n            </button>\n          </span>\n        </div>\n      ) : null}\n\n      <Accordion\n        {...rest}\n        items={items}\n        activeKey={openKeys}\n        onChange={commit}\n        headingLevel={headingLevel}\n        density={density}\n        locale={locale}\n      />\n    </div>\n  );\n}\n"
    }
  ]
}
