{
  "$schema": "https://zoblocks.design/schema/registry-item.json",
  "name": "chart-header",
  "type": "zoblocks:component",
  "title": "Chart Header",
  "description": "Sticky chrome over PatientBanner. Collapse moves content behind a disclosure rather than out of the accessibility tree. The encounter is an explicit control that will sit in “none selected” rather than pick one for you. The Sex Parameter for Clinical Use appears only where an order or a result is in view, with the context it applies to.",
  "categories": [
    "clinical",
    "navigation"
  ],
  "dependencies": [
    "clsx",
    "tailwind-merge",
    "@zoblocks/identity"
  ],
  "registryDependencies": [
    "utils",
    "tokens",
    "chart-header-core"
  ],
  "files": [
    {
      "path": "registry/zoblocks/chart-header/chart-header.tsx",
      "type": "zoblocks:component",
      "target": "components/zoblocks/chart-header.tsx",
      "content": "\"use client\";\n\n/**\n * ChartHeader — patient context that collapses to a safety bar, not to a name.\n *\n *     <ChartHeader\n *       patient={patient}\n *       identifiers={[{ kind: \"mrn\" }, { kind: \"nhs\" }]}\n *       surface=\"orders\"\n *       encounters={open}\n *       selectedEncounterId={encounterId}\n *       onSelectEncounter={setEncounterId}\n *       safety={{ allergies: { label: \"Penicillin — anaphylaxis\", tone: \"critical\" } }}\n *     >\n *       <OrderForm />\n *     </ChartHeader>\n *\n * The workspace chrome over `PatientBanner`, which is the safety control: two\n * identifiers before a care action, enforced by the type. This adds the three\n * things a header needs that a banner does not — it is always there, it knows\n * which encounter you are documenting into, and it degrades to the strip you\n * must not act without rather than to a heading.\n *\n * Three rules are load-bearing.\n *\n *   Collapse hides nothing. The expanded content moves behind a disclosure\n *   rather than out of the DOM, so a screen-reader user is never worse off\n *   than a sighted one, and focus never lands behind the sticky element.\n *\n *   The encounter is a control, not a subtitle. \"No encounter selected\" is a\n *   state it will sit in rather than pick for you, because a note filed into\n *   an encounter nobody chose is the most common misfiling in the building.\n *\n *   `Patient.gender` never appears. On an order or a result screen the header\n *   shows the Sex Parameter for Clinical Use with the context it applies to,\n *   and says \"not recorded\" when there is none.\n *\n * Styling lives in `styles/zoblocks-chart-header.css`, installed alongside.\n */\n\nimport * as React from \"react\";\nimport {\n  PatientBanner,\n  type IdentifierSpec,\n  type PatientBannerProps,\n  type TwoOrMore,\n} from \"@zoblocks/identity\";\nimport { cn } from \"@/lib/utils\";\nimport { clockTime } from \"@/lib/zoblocks-clock\";\nimport {\n  SAFETY_ORDER,\n  describeEncounterContext,\n  describeProgram,\n  describeSafety,\n  describeSpcu,\n  hasExpired,\n  resolveEncounterContext,\n  resolveSpcu,\n  safetyStrip,\n  type ChartSurface,\n  type EncounterOption,\n  type Program,\n  type SafetyInput,\n} from \"@/lib/zoblocks-chart-header\";\n\nexport {\n  EXT_SPCU,\n  SAFETY_ORDER,\n  SPCU_SURFACES,\n  describeEncounterContext,\n  describeProgram,\n  describeSafety,\n  describeSpcu,\n  hasExpired,\n  programFromEpisode,\n  resolveEncounterContext,\n  resolveSpcu,\n  safetyStrip,\n  type ChartSurface,\n  type EncounterContext,\n  type EncounterOption,\n  type Program,\n  type SafetyFact,\n  type SafetyInput,\n  type SafetyKind,\n  type SafetyTone,\n  type SpcuReading,\n} from \"@/lib/zoblocks-chart-header\";\n\n/**\n * The FHIR `Patient` the banner accepts, taken from the banner rather than\n * re-declared. A registry component that redeclares a resource shape is one\n * upstream field away from disagreeing with the thing it renders.\n */\ntype BannerPatient = NonNullable<PatientBannerProps[\"patient\"]>;\n\nexport interface ChartHeaderProps {\n  /**\n   * The patient this chart belongs to. `Patient.gender` is deliberately not rendered — it is\n   * administrative gender, and a bare “M” beside a dose is a prescriber reading the wrong\n   * reference range.\n   */\n  patient: BannerPatient;\n  /**\n   * Two, enforced by the type — this is NPSG.01.01.01 in the signature rather\n   * than in a review comment.\n   */\n  identifiers: TwoOrMore<IdentifierSpec>;\n  /**\n   * What the clinician has in view. Decides whether the Sex Parameter for\n   * Clinical Use appears at all.\n   */\n  surface?: ChartSurface;\n  /** ISO 8601, supplied by the host. Decides whether a dated fact has lapsed. */\n  now?: string;\n  /** Every encounter open for this patient. One is selected by the caller. */\n  encounters?: readonly EncounterOption[];\n  /** Which encounter is in view, controlled. */\n  selectedEncounterId?: string;\n  /**\n   * Switching encounter is a deliberate act, and the host re-guards every open\n   * form on the far side of it.\n   */\n  onSelectEncounter?: (id: string | undefined) => void;\n  /**\n   * The banner facts, in the fixed house order: allergies, code status, isolation, legal\n   * status. Same order on every chart in the building, because a reader scans position before\n   * words.\n   */\n  safety?: SafetyInput;\n  /** The care programme and where the patient is in it, e.g. IOP week 3 of 8. */\n  program?: Program;\n  /** Where the patient physically is. Shown because the answer changes who can act. */\n  ward?: string;\n  /** Rendered at the end of the expanded row. Actions belong to the application. */\n  actions?: React.ReactNode;\n  /**\n   * Start collapsed.\n   *\n   * Uncontrolled by design: the host owns the scroll sentinel because only the\n   * host knows what its scroll container is, and an IntersectionObserver wired\n   * to the wrong ancestor is worse than none.\n   */\n  collapsed?: boolean;\n  /**\n   * Fired when the header collapses to its safety strip or expands again. Content moves behind\n   * a disclosure rather than out of the DOM.\n   */\n  onCollapsedChange?: (collapsed: boolean) => void;\n  /** Applied to the header element. */\n  className?: string;\n  /**\n   * The screen, rendered inside the patient context the banner establishes.\n   *\n   * This is what lets `PatientGuard` compare a form against the chart on\n   * screen — the header is not a decoration above the content, it is the\n   * statement the content is made under.\n   */\n  children?: React.ReactNode;\n}\n\n/* ------------------------------------------------------------------ */\n/* The strip                                                           */\n/* ------------------------------------------------------------------ */\n\n/**\n * The 44px bar.\n *\n * Memoised on its own inputs. A chart header re-renders whenever anything on\n * the page changes, and this is the part that must not: it is read\n * continuously and re-rendering it moves nothing but costs a frame on every\n * vitals refresh.\n */\nconst SafetyStrip = React.memo(function SafetyStrip({\n  safety,\n  now,\n  scrollable,\n}: {\n  safety: SafetyInput;\n  now?: string;\n  /**\n   * True when the strip is the collapsed one, which scrolls sideways.\n   *\n   * A scrollable region has to be reachable by keyboard or its content is\n   * unreachable to anyone not using a pointer — WCAG 2.1.1, and axe's\n   * `scrollable-region-focusable` caught it on the first audit after this\n   * component landed. The expanded strip wraps instead of scrolling, so it\n   * needs no tab stop and does not get one: an extra stop on the most-read\n   * eighty pixels of the screen is a cost, and paying it twice buys nothing.\n   */\n  scrollable: boolean;\n}) {\n  const facts = React.useMemo(() => safetyStrip(safety), [safety]);\n\n  return (\n    <ul\n      className=\"zb-chart-header__strip\"\n      aria-label=\"Safety\"\n      {...(scrollable ? { tabIndex: 0 } : {})}\n    >\n      {facts\n        .slice()\n        .sort((a, b) => SAFETY_ORDER.indexOf(a.kind) - SAFETY_ORDER.indexOf(b.kind))\n        .map((fact, index) => {\n          const expired = hasExpired(fact, now);\n          return (\n            <li\n              key={`${fact.kind}-${index}`}\n              className=\"zb-chart-header__fact\"\n              data-zb-kind={fact.kind}\n              data-zb-tone={expired ? \"critical\" : fact.tone}\n              data-zb-expired={expired ? \"\" : undefined}\n            >\n              <span className=\"zb-chart-header__fact-label\">{fact.label}</span>\n              {fact.detail ? (\n                <span className=\"zb-chart-header__fact-detail\">{fact.detail}</span>\n              ) : null}\n              {/*\n                An expired hold is shown as expired rather than removed.\n                Dropping it the moment it lapses is how somebody finds out at\n                02:00 that the legal basis for an admission ended at 20:00.\n              */}\n              {fact.until ? (\n                <span className=\"zb-chart-header__fact-until\">\n                  {expired ? \"expired\" : \"until\"} {clockTime(fact.until, now)}\n                </span>\n              ) : null}\n            </li>\n          );\n        })}\n    </ul>\n  );\n});\n\n/* ------------------------------------------------------------------ */\n/* The header                                                          */\n/* ------------------------------------------------------------------ */\n\nexport function ChartHeader({\n  patient,\n  identifiers,\n  surface = \"overview\",\n  now,\n  encounters = [],\n  selectedEncounterId,\n  onSelectEncounter,\n  safety = {},\n  program,\n  ward,\n  actions,\n  collapsed = false,\n  onCollapsedChange,\n  className,\n  children,\n}: ChartHeaderProps) {\n  const detailId = React.useId();\n  const context = resolveEncounterContext(encounters, selectedEncounterId);\n  const spcu = resolveSpcu(patient, surface, now);\n  const facts = React.useMemo(() => safetyStrip(safety), [safety]);\n\n  return (\n    <div className={cn(\"zb-chart-header\", className)} data-zb-chart-header=\"\">\n      <header\n        className=\"zb-chart-header__bar\"\n        data-zb-collapsed={collapsed ? \"\" : undefined}\n        data-zb-surface={surface}\n        // A banner landmark, so \"skip to the patient\" is a thing a screen\n        // reader can offer without the application inventing it.\n        role=\"banner\"\n        aria-label={describeSafety(facts, now)}\n      >\n        {/*\n          The strip is outside the disclosure on purpose. It is the one part\n          that must be readable in both heights, and moving it in and out of\n          the tree at 44px would be the exact failure the collapse exists to\n          avoid.\n        */}\n        <div className=\"zb-chart-header__persistent\">\n          <button\n            type=\"button\"\n            className=\"zb-chart-header__toggle\"\n            aria-expanded={!collapsed}\n            aria-controls={detailId}\n            onClick={() => onCollapsedChange?.(!collapsed)}\n          >\n            {collapsed ? \"Show patient details\" : \"Collapse\"}\n          </button>\n          <SafetyStrip safety={safety} now={now} scrollable={collapsed} />\n        </div>\n\n        {/*\n          Hidden with `hidden`, not unmounted.\n          Collapse moves content behind a disclosure so a screen-reader user is\n          never worse off than a sighted one — and, just as importantly, focus\n          can never land on something scrolled behind the sticky bar.\n        */}\n        <div id={detailId} className=\"zb-chart-header__detail\" hidden={collapsed}>\n          <PatientBanner\n            context=\"action\"\n            patient={patient}\n            identifiers={identifiers}\n            {...(ward ? { ward } : {})}\n            {...(actions ? { actions } : {})}\n          />\n\n          <div className=\"zb-chart-header__context\">\n            {/*\n              The encounter is a control. A subtitle cannot be wrong on\n              purpose, and this one has to be able to say \"nothing is\n              selected\" and mean it.\n            */}\n            {onSelectEncounter ? (\n              <label className=\"zb-chart-header__encounter\">\n                <span className=\"zb-chart-header__encounter-label\">Documenting into</span>\n                <select\n                  className=\"zb-chart-header__encounter-select\"\n                  data-zb-encounter={context.kind}\n                  value={context.kind === \"selected\" ? context.encounter.id : \"\"}\n                  onChange={(event) => onSelectEncounter(event.target.value || undefined)}\n                >\n                  <option value=\"\">No encounter selected</option>\n                  {encounters.map((encounter) => (\n                    <option key={encounter.id} value={encounter.id}>\n                      {encounter.label}\n                    </option>\n                  ))}\n                </select>\n              </label>\n            ) : (\n              // Read-only rather than a disabled control: a disabled select\n              // leaves the tab order, so a keyboard user cannot reach the one\n              // fact that says where their note is going.\n              <p className=\"zb-chart-header__encounter\" data-zb-encounter={context.kind}>\n                <span className=\"zb-chart-header__encounter-label\">Documenting into</span>\n                <span className=\"zb-chart-header__encounter-value\">\n                  {context.kind === \"selected\" ? context.encounter.label : \"No encounter selected\"}\n                </span>\n              </p>\n            )}\n\n            {/*\n              Announced, because the reason changes as encounters open and\n              close and the difference between \"none open\" and \"three open,\n              choose one\" decides what the clinician does next.\n            */}\n            {context.kind === \"none\" ? (\n              <p className=\"zb-chart-header__encounter-reason\" role=\"status\">\n                {describeEncounterContext(context)}\n              </p>\n            ) : null}\n\n            {program ? (\n              <p className=\"zb-chart-header__program\">{describeProgram(program)}</p>\n            ) : null}\n\n            {/*\n              Only on an order or a result screen, and always with its context.\n              Everywhere else this field is a demographic wearing a clinical\n              name, which is what it was invented to replace.\n            */}\n            {spcu ? (\n              <p\n                className=\"zb-chart-header__spcu\"\n                data-zb-recorded={spcu.recorded ? \"\" : undefined}\n              >\n                {describeSpcu(spcu)}\n              </p>\n            ) : null}\n          </div>\n        </div>\n      </header>\n\n      {children}\n    </div>\n  );\n}\n\nChartHeader.displayName = \"ChartHeader\";\n"
    }
  ]
}
