{
  "$schema": "https://zoblocks.design/schema/registry-item.json",
  "name": "trend-indicator",
  "type": "zoblocks:component",
  "title": "Trend Indicator",
  "description": "Breaks the line wherever comparability breaks and says why in words. Renders no trend below three comparable points. Takes the valence as a required prop, so a falling PHQ-9 reads as improvement and a falling eGFR does not, and carries the reliable-change threshold so a two-point move renders as noise.",
  "categories": [
    "clinical",
    "data-display"
  ],
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "registryDependencies": [
    "utils",
    "tokens",
    "trend-core"
  ],
  "files": [
    {
      "path": "registry/zoblocks/trend-indicator/trend-indicator.tsx",
      "type": "zoblocks:component",
      "target": "components/zoblocks/trend-indicator.tsx",
      "content": "/**\n * TrendIndicator — a sparkline that refuses to draw a trend it cannot justify.\n *\n *     <TrendIndicator series={{ id, label, points, valence: \"higher-is-worse\" }} />\n *\n * Below three comparable points it draws no line and says why. Where an assay,\n * a method or a unit changed it breaks the series and states the reason —\n * a dashed gap is a convention a reader has to already know, and two lines\n * that do not join are unambiguous.\n *\n * `valence` is required. Half of clinical measures improve by falling and half\n * by rising: a falling PHQ-9 is improvement, a falling eGFR is not. A library\n * that guesses gets one half wrong silently, and in colour.\n *\n * One SVG path per segment, memoised on series identity. No chart library,\n * no per-point DOM, and it renders the same on a server.\n *\n * Styling lives in `styles/zoblocks-trend.css`, installed alongside.\n */\n\nimport * as React from \"react\";\nimport { cn } from \"@/lib/utils\";\nimport {\n  describeTrend,\n  geometry,\n  readTrend,\n  segment,\n  whyNoTrend,\n  type TrendSeries,\n} from \"@/lib/zoblocks-trend\";\n\nexport {\n  DEFAULT_MIN_POINTS,\n  comparableRun,\n  describeTrend,\n  geometry,\n  readTrend,\n  segment,\n  whyNoTrend,\n  type Direction,\n  type Judgement,\n  type PathGeometry,\n  type Segment,\n  type TrendPoint,\n  type TrendReading,\n  type TrendSeries,\n  type Valence,\n} from \"@/lib/zoblocks-trend\";\n\nexport interface TrendIndicatorProps extends Omit<\n  React.HTMLAttributes<HTMLDivElement>,\n  \"children\"\n> {\n  /**\n   * The points, oldest first. A direction is drawn only when the units and the method match\n   * across them.\n   */\n  series: TrendSeries;\n  /** Caller's choice. Below 40px the line is dropped for the glyph and delta. */\n  width?: number;\n  /**\n   * Drawing height in pixels. The sparkline scales to it; the labels do not, so very small\n   * heights lose the axis rather than the numbers.\n   */\n  height?: number;\n  /**\n   * An element id holding the series as text.\n   *\n   * When absent the component emits its own visually-hidden table. A sparkline\n   * with no text alternative is unreadable and unsearchable, and \"decorative\"\n   * is not true of a line somebody is about to act on.\n   */\n  describedBy?: string;\n  /**\n   * Fired when a point is chosen. Supplying it makes the points interactive; without it the\n   * trend is a picture.\n   */\n  onSelectPoint?: (index: number, series: TrendSeries) => void;\n}\n\n/** Below this the line is unreadable and the glyph is more honest. */\nconst MIN_LINE_WIDTH = 40;\n\nfunction TrendIndicatorImpl({\n  series,\n  width = 64,\n  height = 20,\n  describedBy,\n  onSelectPoint,\n  className,\n  ...rest\n}: TrendIndicatorProps) {\n  const reading = readTrend(series);\n  const breaks = React.useMemo(() => segment(series).filter((s) => s.breakReason), [series]);\n  const path = React.useMemo(() => geometry(series, width, height), [series, width, height]);\n\n  const tableId = React.useId();\n  const label = describeTrend(series);\n  const drawLine = reading !== null && width >= MIN_LINE_WIDTH;\n\n  return (\n    <div\n      {...rest}\n      className={cn(\"zb-trend\", className)}\n      data-zb-trend=\"\"\n      data-zb-direction={reading?.direction ?? \"none\"}\n      data-zb-judgement={reading?.judgement ?? \"unknown\"}\n      data-zb-noise={reading?.withinNoise ? \"\" : undefined}\n      role=\"figure\"\n      aria-label={label}\n      aria-describedby={describedBy ?? tableId}\n    >\n      {drawLine ? (\n        <svg\n          className=\"zb-trend__chart\"\n          viewBox={`0 0 ${width} ${height}`}\n          width={width}\n          height={height}\n          // The figure carries the name and the table carries the data. The\n          // svg itself is neither, so it stays out of the tree entirely.\n          aria-hidden=\"true\"\n          focusable=\"false\"\n          preserveAspectRatio=\"none\"\n        >\n          {path.band ? (\n            <rect\n              className=\"zb-trend__band\"\n              x={0}\n              y={path.band.y}\n              width={width}\n              height={path.band.height}\n            />\n          ) : null}\n          {/*\n            One path per comparable segment. Two lines that do not join say\n            \"these are not the same series\" without a legend; a dashed gap\n            says it only to someone who already knows the convention.\n          */}\n          {path.paths.map((d, index) => (\n            <path key={index} className=\"zb-trend__line\" d={d} fill=\"none\" />\n          ))}\n          {path.last ? (\n            <circle className=\"zb-trend__terminus\" cx={path.last.x} cy={path.last.y} r={1.75} />\n          ) : null}\n        </svg>\n      ) : null}\n\n      {/*\n        The glyph carries direction independently of the colour, and it is the\n        only thing left below 40px — never an unreadable line.\n      */}\n      {reading ? (\n        <span className=\"zb-trend__readout\" aria-hidden=\"true\">\n          <span className=\"zb-trend__glyph\" data-zb-direction={reading.direction} />\n          <span className=\"zb-trend__delta\">\n            {reading.change > 0 ? \"+\" : \"\"}\n            {reading.change}\n            {series.unit ? <span className=\"zb-trend__unit\">{series.unit}</span> : null}\n          </span>\n        </span>\n      ) : (\n        // Not a flat line. An absent trend and a trend that did not move are\n        // different facts, and drawing the second for the first is the lie\n        // this component exists to refuse.\n        <span className=\"zb-trend__none\" aria-hidden=\"true\">\n          {whyNoTrend(series)}\n        </span>\n      )}\n\n      {breaks.length ? (\n        <span className=\"zb-trend__break\" aria-hidden=\"true\">\n          {breaks[0]?.breakReason}\n        </span>\n      ) : null}\n\n      {/*\n        The text alternative, emitted unless the host supplied one.\n        A sparkline with no series behind it is unreadable to a screen reader\n        and invisible to a page search, and neither is acceptable for a line\n        somebody is about to act on.\n      */}\n      {describedBy ? null : (\n        <table id={tableId} className=\"zb-trend__table\">\n          <caption>{series.label}</caption>\n          <thead>\n            <tr>\n              <th scope=\"col\">Date</th>\n              <th scope=\"col\">Value{series.unit ? ` (${series.unit})` : \"\"}</th>\n              <th scope=\"col\">Note</th>\n            </tr>\n          </thead>\n          <tbody>\n            {series.points.map((point, index) => (\n              <tr key={`${point.at}-${index}`}>\n                <th scope=\"row\">{point.at}</th>\n                <td>\n                  {onSelectPoint ? (\n                    <button type=\"button\" onClick={() => onSelectPoint(index, series)}>\n                      {point.value}\n                    </button>\n                  ) : (\n                    point.value\n                  )}\n                </td>\n                <td>{point.breaksComparability ?? \"\"}</td>\n              </tr>\n            ))}\n          </tbody>\n        </table>\n      )}\n    </div>\n  );\n}\n\n/**\n * Memoised on the series identity.\n *\n * A flowsheet renders one of these per row and re-renders on every filter\n * keystroke. Comparing the whole object would defeat that: hosts rebuild\n * series from a query response, so the reference changes even when nothing\n * did.\n */\nexport const TrendIndicator = React.memo(TrendIndicatorImpl, (a, b) => {\n  return (\n    a.series.id === b.series.id &&\n    a.series.points === b.series.points &&\n    a.series.valence === b.series.valence &&\n    a.width === b.width &&\n    a.height === b.height &&\n    a.describedBy === b.describedBy &&\n    a.onSelectPoint === b.onSelectPoint &&\n    a.className === b.className\n  );\n});\n\nTrendIndicator.displayName = \"TrendIndicator\";\n"
    }
  ]
}
