Clinical Note
ExperimentalA clinical note editor that records where every character came from, and refuses to let anyone sign what they have not read.
Why it exists
A rich text editor is a solved problem and competes with a hundred free ones.
The parts that are hard are knowing which passages were copied forward from a note about a different admission, proving a clinician actually read the text a model drafted before signing it, and producing bytes that hash the same way twice so a signature over them means something. A 2022 analysis of over 100 million notes found 50.1% of note text duplicated from prior documentation on the same patient; copy-and-paste has been implicated in roughly a third of errors in ambulatory patient-safety analyses; and CMS's July 2025 signature guidance treats an AI scribe exactly as it treats a human one, which makes the review gate the only thing standing between a clinician and words they never read. Note bloat, copy-paste error and AI attribution are one missing data structure seen three times. This component stores it.
Playground
Change a prop and watch the component and its code change with it.
- A model-drafted passage has not been reviewedHistory of present illness, second sentence. Blocks
- 32% of this note is copied forwardFrom the admission note, 14 Aug. Warns
- Every required section has contentPass
import { SignGate } from "@/components/zoblocks/clinical-note";export default function Example() { return ( <SignGate findings={findings} author={{ display: "R. Menon, MD", role: "Resident", requiresCosign: true }} attestation="I have reviewed this note in its entirety and attest that it accurately reflects the care I provided." canSign /> );}Examples
The cases worth copying. Open one in StackBlitz to run it with its dependencies installed.
Empty Progress Draft
Shows the default Progress Note and required-section findings. Content is fictional and illustrative.
import { ClinicalNote } from "@/components/zoblocks/clinical-note";import { AUTHOR, ExampleNotice, NOW, SUBJECT } from "./_shared/demo-note";export default function EmptyProgressDraft() { return ( <div> <ExampleNotice /> <ClinicalNote subject={SUBJECT} author={AUTHOR} noteType="progress" now={NOW} /> </div> );}Origin and Copy-Forward Visibility
Shows fictional typed, dictated, pulled, and copied ranges with the composition summary; provenance does not validate content.
import { ClinicalNote, mixed, para } from "@/components/zoblocks/clinical-note";import { SECTIONS } from "@zoblocks/clinical-note-core";import { AUTHOR, ExampleNotice, NOW, SUBJECT, demoNote } from "./_shared/demo-note";const PULLED_AT = "2026-08-16T06:12:00+05:30";const PROVENANCE_NOTE = demoNote("progress", { [SECTIONS.hpi.code]: [ mixed( ["Fictional placeholder text ", "typed"], ["with a dictated sample range ", "dictated", { confidence: 0.96 }], ["and a pulled sample range ", "pulled", { source: "Observation/DEMO-001", at: PULLED_AT }], ["plus copied sample text", "copied", { source: "Composition/DEMO-PRIOR" }], ), ], [SECTIONS.assessmentAndPlan.code]: [ para("Fictional placeholder text for interface demonstration only."), ],});export default function OriginVisibility() { return ( <div> <ExampleNotice> The origins describe sample text sources; they do not establish whether a passage is true. </ExampleNotice> <ClinicalNote subject={SUBJECT} author={AUTHOR} noteType="progress" value={PROVENANCE_NOTE} now={NOW} /> </div> );}What Blocks Signing
Compares empty required sections with a schema-complete fictional note and displays actual onCommit arguments; nothing is persisted or cryptographically signed.
import * as React from "react";import { ClinicalNote, para } from "@/components/zoblocks/clinical-note";import { SECTIONS } from "@zoblocks/clinical-note-core";import { AUTHOR, ExampleNotice, NOW, SUBJECT, demoNote } from "./_shared/demo-note";const COMPLETE_PROGRESS = demoNote("progress", { [SECTIONS.hpi.code]: [para("Fictional placeholder text for interface demonstration only.")], [SECTIONS.assessmentAndPlan.code]: [ para("Fictional placeholder text for interface demonstration only."), ],});export default function WhatBlocksSigning() { const [complete, setComplete] = React.useState(false); const [callbackArgs, setCallbackArgs] = React.useState<unknown>(); return ( <div> <ExampleNotice> No note is persisted or cryptographically signed. The panel shows callback arguments only. </ExampleNotice> <button type="button" className="mb-3 rounded-md border border-[var(--zb-border)] px-3 py-1.5 text-sm text-[var(--zb-text)]" onClick={() => setComplete((current) => !current)} > {complete ? "Show empty draft" : "Show schema-complete draft"} </button> <ClinicalNote key={complete ? "complete" : "empty"} subject={SUBJECT} author={AUTHOR} noteType="progress" value={complete ? COMPLETE_PROGRESS : undefined} now={NOW} attestation="I reviewed this fictional example for interface demonstration." onCommit={(kind, doc, acknowledged) => setCallbackArgs({ kind, doc: doc.toJSON(), acknowledged }) } /> <pre className="mt-3 overflow-auto rounded-md border border-[var(--zb-border)] bg-[var(--zb-surface)] p-3 text-xs text-[var(--zb-text)]" aria-live="polite" > {callbackArgs === undefined ? "No callback received yet." : JSON.stringify(callbackArgs, null, 2)} </pre> </div> );}Template Blanks and F2 Navigation
Uses core wildcard expansion and the component’s F2 and next-blank navigation; no phrase picker is implied.
import { ClinicalNote, para } from "@/components/zoblocks/clinical-note";import { expand, SECTIONS } from "@zoblocks/clinical-note-core";import type { Node as PMNode } from "prosemirror-model";import { AUTHOR, ExampleNotice, NOW, SUBJECT, demoNote } from "./_shared/demo-note";// Core wildcard expansion: each ***…*** becomes a blank the editor can jump to.const expansion = expand("Fictional placeholder ***sample***", { provenance: { origin: "template", source: "Phrase/DEMO" },});const blankParagraphs: PMNode[] = [];expansion.content.forEach((node) => blankParagraphs.push(node));const TEMPLATE_NOTE = demoNote("progress", { [SECTIONS.hpi.code]: blankParagraphs, [SECTIONS.assessmentAndPlan.code]: [ para("Fictional placeholder text for interface demonstration only."), ],});export default function TemplateBlanks() { return ( <div> <ExampleNotice> F2 and the visible next-blank control navigate between wildcard blanks; they do not fill them. The phrases prop is not a phrase picker. </ExampleNotice> <ClinicalNote subject={SUBJECT} author={AUTHOR} noteType="progress" value={TEMPLATE_NOTE} now={NOW} /> </div> );}Signed Reader with Attestations and Addenda
Displays a fictional note with supplied attestation and addendum rows; the reader does not verify signatures or create legal supersession.
import { ClinicalNote, para } from "@/components/zoblocks/clinical-note";import { SECTIONS } from "@zoblocks/clinical-note-core";import { ExampleNotice, SUBJECT, demoNote } from "./_shared/demo-note";const READER_NOTE = demoNote("progress", { [SECTIONS.hpi.code]: [para("Fictional placeholder text for interface demonstration only.")], [SECTIONS.assessmentAndPlan.code]: [ para("Fictional placeholder text for interface demonstration only."), ],});const ATTESTATIONS = [ { who: "Example Author", role: "Demonstration only", when: "16 Aug 2026, 14:38 IST (UTC+05:30)", statement: "Illustrative supplied attestation text.", },];const ADDENDA = [ { author: "Example Author", when: "16 Aug 2026, 14:42 IST (UTC+05:30)", text: "Fictional addendum text for interface demonstration only.", },];export default function SignedReader() { return ( <div> <ExampleNotice> Attestations and addenda are supplied display data; the reader does not verify signatures or create legal supersession. </ExampleNotice> <ClinicalNote.Reader subject={SUBJECT} title="Illustrative progress note" doc={READER_NOTE} attestations={ATTESTATIONS} addenda={ADDENDA} /> </div> );}Installation
npx @zoblocks/cli add clinical-noteFirst install? Run zoblocks init once to say where your @/ alias points. The source is copied into your repository, along with anything it depends on.
clsxtailwind-merge@zoblocks/clinical-note-coreprosemirror-viewprosemirror-stateprosemirror-modelprosemirror-keymapprosemirror-historyprosemirror-commandsUsage
Props are the FHIR resource.
import { ClinicalNote } from "@/components/zoblocks/clinical-note";// The minimum. `now` has no default on purpose — the host owns the clock.<ClinicalNote noteType="progress" subject={{ reference: "Patient/4471902", display: "RANDOL, Joshua", identifier: "4471902", birthDate: "12 Mar 1996", detail: "30y M · Bed 4E-12", }} author={{ display: "R. Menon, MD", role: "Resident", requiresCosign: true }} now={await serverTime()} onCommit={(kind, doc, acknowledgedWarnings) => save(kind, doc, acknowledgedWarnings)}/>// Reading a signed note loads no editor at all.<ClinicalNote.Reader subject={subject} title="Progress note" doc={signedDoc} attestations={[{ who: "R. Menon, MD", when: "16 Aug 2026, 14:41 IST (UTC+05:30)" }]} addenda={[{ author: "A. Iyer, MD", when: "19 Aug 2026, 09:14 IST", text: "…" }]}/>Use it when
- Clinician-facing documentation where the note is the legal record — progress notes, H&Ps, consultations, discharge summaries. The gate and the addendum model are what make it a record rather than a document.
- Anywhere an ambient scribe or a drafting model writes into the note. Generated text arrives marked unreviewed and cannot be signed in that state, which is the only mechanical protection the signer has.
- Teaching settings with a resident-and-attending countersignature, where two signatures with two times and two authors have to survive into the export.
- Deployments that need to measure copy-forward rather than estimate it. The ratio is computed from the marks and can be recorded on the signature.
Avoid it when
- Patient-facing note composition. The gate's language, the do-not-use lint and the section model all assume a clinical author; a patient-authored narrative is a different surface with different rules.
- Anything that must round-trip provenance through a third-party FHIR server. Per-range authorship is a custom extension and a conforming server may drop it — verify with the actual endpoint before promising the feature.
- Free-form documents that are not clinical notes. The schema refuses prose outside a coded section, which is the point here and an obstruction anywhere else.
- Signing workflows where the browser clock is the only clock. `now` is required precisely so that a deployment cannot accidentally attest to a time nobody can defend.
API
Every prop is typed. Props not listed are forwarded to the root element.
SignGate
7 props · the front doorauthorrequiredNoteAuthor- Default
— - Who is signing, and in what capacity.
canSignrequiredboolean- Default
— - Whether policy permits this author to sign at all. Separate from whether the findings currently allow it.
findingsrequiredreadonly Finding[]- Default
— - What is standing between this note and a signature, each with a severity. Rendered in full rather than summarised: "3 issues" tells an author to hunt, and the point of the gate is that it already knows.
attestationstring- Default
— - The sentence being signed. Shown before the control, never after the decision.
onCancel(() => void)- Default
— - Fired when the author backs out of signing. The draft is untouched.
onNavigate((finding: Finding) => void)- Default
— - Fired when the author jumps to the passage a finding is about. Without it a finding names a problem and offers no way to reach it.
onSign((acknowledged: string[]) => void)- Default
— - Fired with the ids of every finding the author acknowledged on the way through. Acknowledgement is part of the record, not a dismissal.
ClinicalNoteReader
5 propsdocrequiredPMNode- Default
— - The signed document. Read-only by construction — this component has no editing path at all.
subjectrequiredNoteSubject- Default
— - Who the note is about. Rendered so a reader can check the chart before believing the note.
titlerequiredstring- Default
— - The note's heading, and the article's accessible name.
addendareadonly Addendum[]- Default
[] - Addenda appended after signing. Never merged into the body: an addendum is a separate authored act, and folding it into the original text would rewrite what somebody signed.
attestationsreadonly Attestation[]- Default
[] - Signatures on the note, in order. A countersignature is another attestation rather than a second copy of the first, so each carries its own signer, capacity and time.
ClinicalNote
15 props15 props
authorrequiredNoteAuthor- Default
— - Who is writing. Distinct from `recordedBy` on the signature: the person composing and the person attesting are not always the same.
noteTyperequiredNoteTypeDef | 'progress' | 'historyAndPhysical' | 'consultation' | 'dischargeSummary'- Default
— - Decides which sections exist and which of them block a signature.
nowrequiredDate- Default
— - The instant the gate is evaluated against, from the host's clock. Required, and there is no default. A browser clock on a ward workstation is not evidence, and 42 CFR 482.24(c)(1) wants entries dated, timed and authenticated. Passing `new Date()` is a decision the host makes, not one this component makes silently on their behalf.
subjectrequiredNoteSubject- Default
— - Who the note is about. Never optional — it is the wrong-patient mitigation.
attestationstring- Default
— - The attestation sentence. Wording is a legal and organisational decision.
editorLabelstring- Default
"Note body" - Accessible name for the editable region. Defaults to "Note body". User-visible text, so it is a prop: a deployment in another language needs to be able to change it.
gateOptionsPartial<Omit<GateContext, 'now' | 'noteType' | 'subject'>>- Default
— - Overrides for the default gate context — thresholds, mostly.
onChange((doc: PMNode) => void)- Default
— - Fired as the note is edited, with the origin of the edit preserved.
onCommit((kind: CommitKind, doc: PMNode, acknowledged: string[]) => void)- Default
— - Fired for each of the three commits. Three verbs with three legal meanings, never one blue Save: `draft` is reversible, `sign` is not, and `addend` is the only legal operation on a note that has already been signed.
phrasesreadonly Phrase[]- Default
— - Dot phrases. None ship in the package; a phrase library is yours.
Page 1 of 2
Accessibility
Screen readers and semantics
- The toolbar is one tab stop, not fifteen. role=toolbar with a roving tabindex: one Tab in, arrow keys within, one Tab out. Without it there are fifteen Tab presses between the document and the sign button, every time, for anyone who does not use a mouse.
- Provenance is never colour alone. Each of the six origins carries a hue and a distinct underline style — wavy for dictated, dashed for template, dotted for pulled, solid for copied, double for AI. Under forced-colors the tints are dropped and the underline styles carry the whole distinction, which is also what survives a monochrome print.
- The blocked sign button says why. aria-describedby points at a live count of what is blocking. A disabled control with no stated reason is the most common way a gate gets routed around.
- Save state reaches assistive technology. The status strip is a live region: polite for ordinary transitions, assertive for a failed save. A silent spinner is a status change nobody hears, and an unheard save failure is lost work.
- Sections are real landmarks. The rail is a nav with aria-current on the section holding the caret, and each section is a schema node rather than a bold paragraph — which is what makes both heading navigation and the required-section check possible at all.
- Formatting is semantic. Bold and italic render as strong and em, not styled spans, so a screen reader can convey emphasis. Underline and strikethrough are deliberately absent: underline reads as a link, and strikethrough is silently dropped by some renderers, which applied to a retraction is a safety defect.
Source
Exactly what lands in your repository, read from the published registry so it can never drift from what the CLI installs.
Show sourceHide source1034 lines
"use client";/** * ClinicalNote — a note editor that knows who wrote every character. * * A rich text editor is a solved problem; a clinical note is not. Every * character in one has an origin: the clinician typed it, dictated it, a * template fired it, it was pulled from a lab result, it was copied forward * from a note about a different admission, or a model wrote it. In every EHR * shipping today all of that collapses into identical black text the moment it * lands on screen — and the clinician signs, attesting to all of it equally. * * Three well-documented problems fall out of that one missing structure. A 2022 * analysis of over 100 million notes found 50.1% of note text duplicated from * prior documentation on the same patient. Copy-and-paste has been implicated * in roughly a third of errors in ambulatory patient-safety analyses. And CMS's * July 2025 signature guidance treats an AI scribe exactly as it treats a human * one: the clinician signs, and owns every word, whether or not the tool is * named. * * Storing origin per range does not solve any of those. It makes all three * *visible*, which is the prerequisite — and it is what turns "I reviewed it" * from an unfalsifiable claim into something the sign button can check. * * The engine is `@zoblocks/clinical-note-core`: schema, provenance, * gate rules and serializers, with no DOM and no React. This file is the skin. */import * as React from "react";import { cn } from "@/lib/utils";import { DEFAULT_RULES, ORIGINS, NODES, emptyNote, formatDuration, sectionAttrs, sections, toText, type Finding, type GateContext, type GateRule, type NoteTypeDef, type NoteTypeName, type Origin, type Phrase,} from "@zoblocks/clinical-note-core";import type { Node as PMNode } from "prosemirror-model";import { ORIGIN_LABEL, useClinicalNote, type ClinicalNoteApi } from "@/lib/zoblocks-clinical-note";export { ORIGIN_LABEL, useClinicalNote, mixed, noteDoc, noteSection, para,} from "@/lib/zoblocks-clinical-note";/* ------------------------------------------------------------------ *//* Glyphs *//* ------------------------------------------------------------------ *//** * Inline, on a 24px grid with a 2px stroke — Lucide's geometry. * * Inline rather than a dependency because a registry component is copied into * a customer's repository, and adding an icon package to their bundle for six * glyphs is a decision that should be theirs. */function Icon({ path, className }: { path: React.ReactNode; className?: string }) { return ( <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2} strokeLinecap="round" strokeLinejoin="round" className={cn("size-4 shrink-0", className)} aria-hidden="true" focusable="false" > {path} </svg> );}const UndoIcon = () => ( <Icon path={ <> <path d="M9 14 4 9l5-5" /> <path d="M4 9h10.5a5.5 5.5 0 0 1 0 11H11" /> </> } />);const RedoIcon = () => ( <Icon path={ <> <path d="m15 14 5-5-5-5" /> <path d="M20 9H9.5a5.5 5.5 0 0 0 0 11H13" /> </> } />);const BoldIcon = () => ( <Icon path={<path d="M6 12h9a4 4 0 0 1 0 8H7a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h7a4 4 0 0 1 0 8" />} />);const ItalicIcon = () => ( <Icon path={ <> <path d="M19 4h-9" /> <path d="M14 20H5" /> <path d="m15 4-6 16" /> </> } />);const OriginsIcon = () => ( <Icon path={ <> <circle cx="12" cy="12" r="10" /> <path d="M12 18a6 6 0 0 0 0-12z" fill="currentColor" stroke="none" /> </> } />);const BlockIcon = () => ( <Icon path={ <> <circle cx="12" cy="12" r="10" /> <path d="m15 9-6 6" /> <path d="m9 9 6 6" /> </> } />);const WarnIcon = () => ( <Icon path={ <> <path d="m21.7 18-8-14a2 2 0 0 0-3.4 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.7-3" /> <path d="M12 9v4" /> <path d="M12 17h.01" /> </> } />);const PassIcon = () => ( <Icon path={ <> <circle cx="12" cy="12" r="10" /> <path d="m9 12 2 2 4-4" /> </> } />);const ShieldIcon = () => ( <Icon path={ <> <path d="M20 13c0 5-3.5 7.5-7.7 9a1 1 0 0 1-.7 0C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.2-2.7a1.2 1.2 0 0 1 1.5 0C14.5 3.8 17 5 19 5a1 1 0 0 1 1 1z" /> <path d="m9 12 2 2 4-4" /> </> } />);/* ------------------------------------------------------------------ *//* Props *//* ------------------------------------------------------------------ *//** Who the note is about. Two independent identifiers, minimum. */export interface NoteSubject { /** FHIR reference, e.g. `Patient/4471902`. */ reference: string; /** Family-name-first, as a chart banner shows it. */ display: string; /** Medical record number, or whatever your deployment's second identifier is. */ identifier?: string; /** Rendered verbatim. Ambiguity in a date of birth is a wrong-patient event. */ birthDate?: string; /** Free text: "30y M", "Bed 4E-12". */ detail?: string;}/** Who is writing, and whether someone else has to countersign. */export interface NoteAuthor { display: string; /** "Resident", "Attending", "Scribe" — shown beside the name. */ role?: string; requiresCosign?: boolean;}/** What the host is doing with the draft, said honestly. */export type SaveState = | { kind: "saved"; at: string } | { kind: "saving" } | { kind: "offline"; pending: number } | { kind: "failed"; reason?: string };export type CommitKind = "draft" | "sign" | "addend";export interface ClinicalNoteProps extends Omit<React.HTMLAttributes<HTMLDivElement>, "onChange"> { /** Who the note is about. Never optional — it is the wrong-patient mitigation. */ subject: NoteSubject; /** * Who is writing. Distinct from `recordedBy` on the signature: the person composing and the * person attesting are not always the same. */ author: NoteAuthor; /** Decides which sections exist and which of them block a signature. */ noteType: NoteTypeName | NoteTypeDef; /** The document. Defaults to an empty note of `noteType`. */ value?: PMNode; /** Fired as the note is edited, with the origin of the edit preserved. */ onChange?: (doc: PMNode) => void; /** * The instant the gate is evaluated against, from the host's clock. * * Required, and there is no default. A browser clock on a ward workstation * is not evidence, and 42 CFR 482.24(c)(1) wants entries dated, timed and * authenticated. Passing `new Date()` is a decision the host makes, not one * this component makes silently on their behalf. */ now: Date; /** Extra gate rules, appended to the defaults. */ rules?: readonly GateRule[]; /** Overrides for the default gate context — thresholds, mostly. */ gateOptions?: Partial<Omit<GateContext, "noteType" | "now" | "subject">>; /** Dot phrases. None ship in the package; a phrase library is yours. */ phrases?: readonly Phrase[]; /** Draft state, shown in the status strip. Never a silent spinner. */ saveState?: SaveState; /** * Fired for each of the three commits. * * Three verbs with three legal meanings, never one blue Save: `draft` is * reversible, `sign` is not, and `addend` is the only legal operation on a * note that has already been signed. */ onCommit?: (kind: CommitKind, doc: PMNode, acknowledged: string[]) => void; /** Renders the signed reader instead of the editor. */ readOnly?: boolean; /** The attestation sentence. Wording is a legal and organisational decision. */ attestation?: string; /** Absolute, with offset — the created/edited line under the document. */ timestampLine?: string; /** * Accessible name for the editable region. Defaults to "Note body". * * User-visible text, so it is a prop: a deployment in another language needs * to be able to change it. */ editorLabel?: string;}/* ------------------------------------------------------------------ *//* Pieces *//* ------------------------------------------------------------------ */function Banner({ subject, status }: { subject: NoteSubject; status: React.ReactNode }) { return ( <div className="flex items-start justify-between gap-3 border-b border-[var(--zb-border)] px-5 py-3"> <div className="min-w-0"> <div className="flex flex-wrap items-center gap-2"> <span className="truncate text-sm font-semibold text-[var(--zb-text)]"> {subject.display} </span> {status} </div> <div className="mt-0.5 flex flex-wrap gap-x-2 text-xs tabular-nums text-[var(--zb-text-muted)]"> {subject.identifier ? <span>MRN {subject.identifier}</span> : null} {subject.birthDate ? <span>· DOB {subject.birthDate}</span> : null} {subject.detail ? <span>· {subject.detail}</span> : null} </div> </div> </div> );}function ToolButton({ label, onClick, disabled, pressed, children,}: { label: string; onClick?: () => void; disabled?: boolean; pressed?: boolean; children: React.ReactNode;}) { return ( <button type="button" // A toolbar is one tab stop, not fifteen. Arrow keys move within it; the // roving tabindex is managed by the toolbar below. tabIndex={-1} aria-label={label} aria-pressed={pressed} disabled={disabled} onClick={onClick} className={cn( "inline-flex h-7 min-w-7 items-center justify-center gap-1.5 rounded-md px-1.5", "text-[var(--zb-text-muted)] transition-colors", "hover:bg-[var(--zb-surface-raised)] hover:text-[var(--zb-text)]", "focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--zb-focus-ring)]", "disabled:pointer-events-none disabled:opacity-40", pressed && "bg-[var(--zb-accent-subtle)] text-[var(--zb-accent-hover)]", )} > {children} </button> );}/** * The toolbar, as one tab stop with arrow-key movement. * * The WAI-ARIA toolbar pattern, and it is not decoration: fifteen individually * focusable buttons means fifteen Tab presses between the document and the * sign button, every single time, for anyone who does not use a mouse. */function Toolbar({ children, label }: { children: React.ReactNode; label: string }) { const ref = React.useRef<HTMLDivElement>(null); const [active, setActive] = React.useState(0); const buttons = React.useCallback( () => Array.from(ref.current?.querySelectorAll<HTMLButtonElement>("button:not([disabled])") ?? []), [], ); React.useEffect(() => { const all = buttons(); all.forEach((button, i) => (button.tabIndex = i === active ? 0 : -1)); }); function onKeyDown(event: React.KeyboardEvent) { const all = buttons(); if (all.length === 0) return; // One decision, one place. The previous shape computed the target twice — // once to decide whether the key was ours, once to act on it — which is // how the two got to disagree about Home and End. const target = ((): number | null => { switch (event.key) { case "ArrowRight": return (active + 1) % all.length; case "ArrowLeft": return (active - 1 + all.length) % all.length; case "Home": return 0; case "End": return all.length - 1; default: // Anything else belongs to the host, or to type-ahead. return null; } })(); if (target === null) return; event.preventDefault(); setActive(target); all[target]?.focus(); } return ( <div ref={ref} role="toolbar" aria-label={label} aria-orientation="horizontal" onKeyDown={onKeyDown} className="flex flex-wrap items-center gap-1 border-b border-[var(--zb-border)] px-4 py-1.5" > {children} </div> );}function Cluster({ children }: { children: React.ReactNode }) { return ( <span className="inline-flex items-center gap-0.5 rounded-lg bg-[var(--zb-bg-subtle)] p-0.5"> {children} </span> );}/** The 100%-stacked bar. Five swatch rows is a legend; this is the data. */function OriginBar({ ratio }: { ratio: Record<Origin, number> }) { const present = ORIGINS.filter((origin) => ratio[origin] > 0); return ( <div className="flex h-2 overflow-hidden rounded-full ring-1 ring-inset ring-[var(--zb-border)]" role="img" aria-label={present .map((origin) => `${ORIGIN_LABEL[origin]} ${Math.round(ratio[origin] * 100)}%`) .join(", ")} > {present.map((origin) => ( <span key={origin} data-origin={origin} className="zb-note-bar block h-full" style={{ width: `${ratio[origin] * 100}%` }} /> ))} </div> );}function SeverityIcon({ severity }: { severity: Finding["severity"] }) { if (severity === "block") return <BlockIcon />; if (severity === "warn") return <WarnIcon />; return <PassIcon />;}/** * The sign gate. * * Three severities and only one of them stops you. A gate that blocks on * everything is routed around within a week; one that blocks on nothing is * decoration. Warnings are shown, counted, and handed to `onCommit` so the * host can record them — a signature over a note with five acknowledged * warnings is a different artifact from one with none. */export function SignGate({ findings, canSign, attestation, author, onSign, onCancel, onNavigate,}: { /** * What is standing between this note and a signature, each with a severity. * * Rendered in full rather than summarised: "3 issues" tells an author to * hunt, and the point of the gate is that it already knows. */ findings: readonly Finding[]; /** Whether policy permits this author to sign at all. Separate from whether the findings currently allow it. */ canSign: boolean; /** The sentence being signed. Shown before the control, never after the decision. */ attestation?: string; /** Who is signing, and in what capacity. */ author: NoteAuthor; /** Fired with the ids of every finding the author acknowledged on the way through. Acknowledgement is part of the record, not a dismissal. */ onSign?: (acknowledged: string[]) => void; /** Fired when the author backs out of signing. The draft is untouched. */ onCancel?: () => void; /** Fired when the author jumps to the passage a finding is about. Without it a finding names a problem and offers no way to reach it. */ onNavigate?: (finding: Finding) => void;}) { const [attested, setAttested] = React.useState(false); const blocking = findings.filter((f) => f.severity === "block"); const warnings = findings.filter((f) => f.severity === "warn"); const passed = findings.filter((f) => f.severity === "pass"); return ( <div className="flex flex-col gap-4" data-testid="sign-gate"> <div role="status" className={cn( "flex items-center gap-2 rounded-md px-3 py-2 text-sm", blocking.length > 0 ? "bg-[var(--zb-status-critical-bg)] text-[var(--zb-status-critical)]" : "bg-[var(--zb-status-normal-bg)] text-[var(--zb-status-normal)]", )} > <SeverityIcon severity={blocking.length > 0 ? "block" : "pass"} /> <span> <strong className="font-semibold">{blocking.length} blocking</strong> · {warnings.length}{" "} {warnings.length === 1 ? "warning" : "warnings"} · {passed.length} passed </span> </div> <ul className="divide-y divide-[var(--zb-border)] overflow-hidden rounded-lg border border-[var(--zb-border)]"> {findings.map((finding) => ( <li key={finding.id} className={cn( "grid grid-cols-[auto_1fr_auto] items-start gap-3 px-3 py-2.5 text-sm", finding.severity === "block" && "bg-[color-mix(in_oklab,var(--zb-status-critical-bg)_30%,transparent)]", )} > <span className={cn( "mt-0.5", finding.severity === "block" && "text-[var(--zb-status-critical)]", finding.severity === "warn" && "text-[var(--zb-status-high)]", finding.severity === "pass" && "text-[var(--zb-status-normal)]", )} > <SeverityIcon severity={finding.severity} /> </span> <span className="min-w-0"> <span className="text-[var(--zb-text)]">{finding.title}</span> {finding.detail ? ( <span className="mt-0.5 block text-xs text-[var(--zb-text-muted)]"> {finding.detail}{" "} {finding.at && onNavigate ? ( <button type="button" onClick={() => onNavigate(finding)} className="text-[var(--zb-accent)] underline underline-offset-2 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--zb-focus-ring)]" > Go to it </button> ) : null} </span> ) : null} </span> <span className={cn( "rounded-full px-2 py-0.5 font-mono text-[10px] uppercase tracking-wider", finding.severity === "block" && "bg-[var(--zb-status-critical-bg)] text-[var(--zb-status-critical)]", finding.severity === "warn" && "bg-[var(--zb-status-high-bg)] text-[var(--zb-status-high)]", finding.severity === "pass" && "bg-[var(--zb-status-normal-bg)] text-[var(--zb-status-normal)]", )} > {finding.severity === "block" ? "Blocks" : finding.severity === "warn" ? "Warns" : "Pass"} </span> </li> ))} </ul> {attestation ? ( <label className="flex items-start gap-2 border-t border-[var(--zb-border)] pt-4 text-sm"> <input type="checkbox" checked={attested} onChange={(event) => setAttested(event.target.checked)} className="mt-0.5 size-4 accent-[var(--zb-accent)]" /> <span> <span className="text-[var(--zb-text)]">{attestation}</span> <span className="mt-1 block text-xs text-[var(--zb-text-muted)]"> Signing as <strong className="font-medium">{author.display}</strong> {author.role ? ` · ${author.role}` : null} {author.requiresCosign ? " · a countersignature will be requested" : null} </span> </span> </label> ) : null} <div className="flex justify-end gap-2"> <button type="button" onClick={onCancel} className="h-9 rounded-md px-3 text-sm text-[var(--zb-text-muted)] hover:bg-[var(--zb-surface-raised)] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--zb-focus-ring)]" > Back to note </button> <button type="button" disabled={!canSign || (Boolean(attestation) && !attested)} onClick={() => onSign?.(warnings.map((warning) => warning.id))} className={cn( "inline-flex h-9 items-center gap-2 rounded-md px-4 text-sm font-medium", "bg-[var(--zb-accent)] text-[var(--zb-text-on-accent)]", "hover:bg-[var(--zb-accent-hover)]", "focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--zb-focus-ring)]", "disabled:pointer-events-none disabled:opacity-45", )} > <ShieldIcon /> Sign </button> </div> </div> );}/* ------------------------------------------------------------------ *//* Reader *//* ------------------------------------------------------------------ */export interface Attestation { who: string; role?: string; /** Absolute, with offset. "Today, 14:38" is not a record. */ when: string; statement?: string;}export interface Addendum { author: string; when: string; text: string;}/** * A signed note, read-only, with its addenda. * * Separate from the editor on purpose: most people who open a note never edit * one, and this path loads no ProseMirror view at all. * * The original text is never shown as corrected — it is shown as superseded. * Both statements stand, in order, with their own authors and times. That is * the difference between a record and a document, and it is what makes the * chart admissible. */export function ClinicalNoteReader({ subject, title, doc, attestations = [], addenda = [], className, ...rest}: { /** Who the note is about. Rendered so a reader can check the chart before believing the note. */ subject: NoteSubject; /** The note's heading, and the article's accessible name. */ title: string; /** The signed document. Read-only by construction — this component has no editing path at all. */ doc: PMNode; /** * Signatures on the note, in order. * * A countersignature is another attestation rather than a second copy of the * first, so each carries its own signer, capacity and time. */ attestations?: readonly Attestation[]; /** * Addenda appended after signing. * * Never merged into the body: an addendum is a separate authored act, and * folding it into the original text would rewrite what somebody signed. */ addenda?: readonly Addendum[];} & React.HTMLAttributes<HTMLElement>) { return ( <article className={cn( "overflow-hidden rounded-xl border border-[var(--zb-border)] bg-[var(--zb-surface)]", className, )} aria-label={`${title}, signed`} // A signed note is read in the same chart, at the same density, as the // editor that produced it. data-zb-density="clinical" {...rest} > <Banner subject={subject} status={ <span className="inline-flex items-center gap-1 rounded-full bg-[var(--zb-status-normal-bg)] px-2 py-0.5 text-xs font-medium text-[var(--zb-status-normal)]"> <ShieldIcon /> Signed </span> } /> <div className="space-y-5 px-5 py-4"> <dl className="overflow-hidden rounded-lg border border-[var(--zb-border)] text-sm"> {attestations.map((attestation) => ( <div key={`${attestation.who}-${attestation.when}`} className="grid grid-cols-[minmax(7rem,10rem)_1fr] border-b border-[var(--zb-border)] last:border-b-0" > <dt className="bg-[var(--zb-bg-subtle)] px-3 py-2 text-[var(--zb-text-muted)]"> {attestation.statement ? "Attestation" : "Signed by"} </dt> <dd className="px-3 py-2"> <span className="text-[var(--zb-text)]"> {attestation.who} {attestation.role ? ` · ${attestation.role}` : null} </span> <span className="mt-0.5 block text-xs tabular-nums text-[var(--zb-text-muted)]"> {attestation.when} </span> {attestation.statement ? ( <span className="mt-1 block text-[var(--zb-text-muted)]"> “{attestation.statement}” </span> ) : null} </dd> </div> ))} </dl> <div className="zb-note-doc zb-note-read max-w-[74ch]"> {sections(doc).map(({ node }) => { const { code, title } = sectionAttrs(node); // One section at a time through the same serializer the export // uses, so the reader can never drift from the transmitted text. const lines = toText(NODES.doc.create(null, [node])) .trim() .split("\n") .slice(1); if (lines.length === 0) return null; return ( <section key={code}> <h3>{title}</h3> {lines.map((line, i) => ( <p key={i}>{line}</p> ))} </section> ); })} </div> {addenda.map((addendum, i) => ( <div key={i} className="overflow-hidden rounded-lg border border-[var(--zb-border)]"> <div className="flex flex-wrap items-center justify-between gap-2 bg-[var(--zb-bg-subtle)] px-3 py-2 text-xs tabular-nums text-[var(--zb-text-muted)]"> <span> <strong className="font-semibold text-[var(--zb-text)]">Addendum {i + 1}</strong> ·{" "} {addendum.author} · {addendum.when} </span> </div> <p className="px-3 py-3 text-sm text-[var(--zb-text)]">{addendum.text}</p> </div> ))} <p className="rounded-md bg-[var(--zb-status-low-bg)] px-3 py-2 text-sm text-[var(--zb-text-muted)]"> The signed note above cannot be edited. Corrections are made by adding a further addendum. </p> </div> </article> );}/* ------------------------------------------------------------------ *//* The editor *//* ------------------------------------------------------------------ */function SaveStateLine({ state }: { state?: SaveState }) { if (!state) return null; const tone = state.kind === "failed" ? "danger" : state.kind === "offline" ? "warning" : "success"; const text = state.kind === "saved" ? `Saved ${state.at}` : state.kind === "saving" ? "Saving…" : state.kind === "offline" ? `Offline — ${state.pending} change${state.pending === 1 ? "" : "s"} held locally` : `Not saved${state.reason ? ` — ${state.reason}` : ""}`; return ( // Polite for ordinary transitions; a failed save is the one a clinician has // to hear about, because the alternative is losing twenty minutes of work. <span className="inline-flex items-center gap-1.5" role="status" aria-live={state.kind === "failed" ? "assertive" : "polite"} > <span className={cn( "size-1.5 rounded-full", tone === "success" && "bg-[var(--zb-status-normal)]", tone === "warning" && "bg-[var(--zb-status-high)]", tone === "danger" && "bg-[var(--zb-status-critical)]", )} /> <span className="tabular-nums">{text}</span> </span> );}export function ClinicalNote({ subject, author, noteType, value, onChange, now, rules, gateOptions, phrases, saveState, onCommit, readOnly = false, attestation, timestampLine, editorLabel = "Note body", className, ...rest}: ClinicalNoteProps) { const initial = React.useMemo(() => value ?? emptyNote(noteType), [value, noteType]); const allRules = React.useMemo<readonly GateRule[]>( () => (rules ? [...DEFAULT_RULES, ...rules] : DEFAULT_RULES), [rules], ); const gateContext = React.useMemo<GateContext>( () => ({ ...gateOptions, noteType, now, subject: subject.reference }), [gateOptions, noteType, now, subject.reference], ); const api: ClinicalNoteApi = useClinicalNote({ value: initial, onChange, rules: allRules, gateContext, phrases, readOnly, label: editorLabel, }); const [gateOpen, setGateOpen] = React.useState(false); // Scoped per instance: a fixed id cross-wires two notes on one page. const blockedId = `${React.useId()}-blocked`; const noteSections = React.useMemo(() => sections(api.doc), [api.doc]); const copied = Math.round(api.composition.ratio.copied * 100); const unreviewed = api.gate.findings.find( (f: Finding) => f.id === "unreviewed-ai" && f.severity === "block", ); return ( <div className={cn( "zb-note flex flex-col overflow-hidden rounded-xl border border-[var(--zb-border)] bg-[var(--zb-surface)]", className, )} data-zb-density="clinical" {...rest} > <Banner subject={subject} status={ <span className="rounded-full bg-[var(--zb-status-low-bg)] px-2 py-0.5 text-xs font-medium text-[var(--zb-status-low)]"> Draft </span> } /> <Toolbar label="Note formatting and insertion"> <Cluster> <ToolButton label="Undo" onClick={api.cmd.undo} disabled={!api.canUndo}> <UndoIcon /> </ToolButton> <ToolButton label="Redo" onClick={api.cmd.redo} disabled={!api.canRedo}> <RedoIcon /> </ToolButton> </Cluster> <Cluster> <ToolButton label="Bold" onClick={api.cmd.bold}> <BoldIcon /> </ToolButton> <ToolButton label="Italic" onClick={api.cmd.italic}> <ItalicIcon /> </ToolButton> </Cluster> {api.blankCount > 0 ? ( <ToolButton label={`Go to next blank, ${api.blankCount} remaining`} onClick={api.cmd.nextBlank} > <span className="font-mono text-[11px]">✳ {api.blankCount}</span> <kbd className="rounded border border-[var(--zb-border)] px-1 font-mono text-[10px] text-[var(--zb-text-muted)]"> F2 </kbd> </ToolButton> ) : null} <span className="flex-1" /> <ToolButton label="Show where each passage came from" pressed={api.showOrigins} onClick={() => api.setShowOrigins(!api.showOrigins)} > <OriginsIcon /> <span className="text-xs">Origins</span> </ToolButton> </Toolbar> <div className="flex flex-wrap items-center gap-x-4 gap-y-1 border-b border-[var(--zb-border)] bg-[var(--zb-bg-subtle)] px-4 py-1.5 text-xs text-[var(--zb-text-muted)]"> <SaveStateLine state={saveState} /> <span> <strong className="font-semibold text-[var(--zb-text)]">{author.display}</strong> {author.role ? ` · ${author.role}` : null} {author.requiresCosign ? " · cosign required" : null} </span> <span className="flex-1" /> {copied > 0 ? ( <span className="rounded-full bg-[var(--zb-status-high-bg)] px-2 py-0.5 tabular-nums text-[var(--zb-status-high)]"> {copied}% copied forward </span> ) : null} {unreviewed ? ( <span className="rounded-full bg-[var(--zb-status-normal-bg)] px-2 py-0.5 text-[var(--zb-status-normal)]"> {unreviewed.title} </span> ) : null} </div> <div className="flex min-h-[20rem] flex-col sm:flex-row"> <nav aria-label="Sections" className="shrink-0 border-b border-[var(--zb-border)] px-2 py-3 sm:w-56 sm:border-b-0 sm:border-e" > <p className="px-2 pb-2 text-[10px] font-medium uppercase tracking-wider text-[var(--zb-text-muted)]"> {noteSections.length} sections </p> <ul> {noteSections.map(({ node }) => { const { code, title, required } = sectionAttrs(node); const empty = node.textContent.trim() === ""; return ( <li key={code}> <button type="button" onClick={() => api.cmd.goToSection(code)} aria-current={api.activeSection === code ? "true" : undefined} className={cn( "relative flex w-full items-center justify-between gap-2 rounded-md py-1.5 ps-3 pe-2 text-start text-[13px]", "text-[var(--zb-text-muted)] hover:bg-[var(--zb-surface-raised)]", "focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--zb-focus-ring)]", api.activeSection === code && "bg-[var(--zb-accent-subtle)] font-medium text-[var(--zb-accent-hover)] before:absolute before:inset-y-1.5 before:start-0 before:w-0.5 before:rounded before:bg-[var(--zb-accent)]", )} > <span className="truncate">{title}</span> {required && empty ? ( <span className="shrink-0 rounded-full bg-[var(--zb-status-critical-bg)] px-1.5 text-[10px] text-[var(--zb-status-critical)]"> required </span> ) : null} </button> </li> ); })} </ul> {api.composition.total > 0 ? ( <div className="mt-4 border-t border-[var(--zb-border)] px-2 pt-3"> <p className="pb-2 text-[10px] font-medium uppercase tracking-wider text-[var(--zb-text-muted)]"> Origins </p> <OriginBar ratio={api.composition.ratio} /> <ul className="mt-2 space-y-1 text-[11px] tabular-nums text-[var(--zb-text-muted)]"> {ORIGINS.filter((origin) => api.composition.ratio[origin] > 0).map((origin) => ( <li key={origin} className="flex items-center gap-2"> <span data-origin={origin} className="zb-note-bar size-2 rounded-sm" /> <span className="flex-1">{ORIGIN_LABEL[origin]}</span> <span>{Math.round(api.composition.ratio[origin] * 100)}%</span> </li> ))} </ul> </div> ) : null} </nav> <div className="min-w-0 flex-1 px-6 py-5"> <div ref={api.ref} className="max-w-[74ch]" /> </div> </div> <div className="flex flex-wrap items-center justify-between gap-3 border-t border-[var(--zb-border)] px-5 py-3"> <p className="text-xs tabular-nums text-[var(--zb-text-muted)]">{timestampLine}</p> <div className="flex gap-2"> <button type="button" onClick={() => onCommit?.("draft", api.doc, [])} className="h-9 rounded-md border border-[var(--zb-border)] px-3 text-sm text-[var(--zb-text)] hover:bg-[var(--zb-surface-raised)] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--zb-focus-ring)]" > Save draft </button> <button type="button" onClick={() => setGateOpen(true)} aria-describedby={api.gate.canSign ? undefined : blockedId} className={cn( "inline-flex h-9 items-center gap-2 rounded-md px-4 text-sm font-medium", "focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--zb-focus-ring)]", // Outlined rather than faded while blocked: the button still opens the gate, so // its label has to keep full contrast — opacity took it below 3:1. api.gate.canSign ? "bg-[var(--zb-accent)] text-[var(--zb-text-on-accent)] hover:bg-[var(--zb-accent-hover)]" : "border border-[var(--zb-border-strong)] bg-[var(--zb-surface)] text-[var(--zb-text)] hover:bg-[var(--zb-bg-subtle)]", )} > <ShieldIcon /> Sign & file </button> {!api.gate.canSign ? ( <span id={blockedId} className="sr-only"> {api.gate.blocking.length} item {api.gate.blocking.length === 1 ? "" : "s"} must be resolved before this note can be signed. </span> ) : null} </div> </div> {gateOpen ? ( <div className="border-t border-[var(--zb-border)] bg-[var(--zb-bg-subtle)] px-5 py-4"> <SignGate findings={api.gate.findings} canSign={api.gate.canSign} attestation={attestation} author={author} onCancel={() => setGateOpen(false)} onNavigate={(finding) => { setGateOpen(false); if (finding.id === "unreviewed-ai") api.cmd.acceptAllAi(); }} onSign={(acknowledged) => { setGateOpen(false); onCommit?.("sign", api.doc, acknowledged); }} /> </div> ) : null} </div> );}ClinicalNote.Reader = ClinicalNoteReader;ClinicalNote.SignGate = SignGate;export { formatDuration };