{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "signal-graph",
  "title": "Signal graph",
  "description": "A compact live graph, as a trace or a spectrum, with a text summary.",
  "registryDependencies": [
    "https://www.cyberspec.dev/r/cyber.json"
  ],
  "files": [
    {
      "path": "registry/cyber/ui/signal-graph.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { fxMoves } from \"@/registry/cyber/lib/fx\"\nimport \"@/registry/cyber/ui/cyber/signal-graph.css\"\n\ntype Tone = \"signal\" | \"accent\" | \"flag\" | \"ok\"\n\n// The drawing's own units. It's stretched to whatever box it's given, so\n// only the proportions matter; strokes are kept a constant width in CSS.\nconst W = 100\nconst H = 40\nconst pad = 2\n\n// A compact graph of a run of samples: a trace (\"line\") or a spectrum of\n// bars, over a baseline grid, with the latest sample on a readout.\n//\n// - `data`: the samples, oldest first.\n// - `min` and `max` fix the scale. Without them it runs from zero (or the\n//   lowest sample, if that's negative) to the highest, which jumps about\n//   when the data is live, so give live graphs a scale.\n// - `live`: the data is a moving window. Pass a new array as samples\n//   arrive and they scroll in from the end rather than jumping; with FX\n//   off they simply appear. The mark on the newest sample breathes.\n//\n// The drawing is hidden from assistive technology, which gets the label\n// and a sentence instead: the latest, lowest and highest values.\nfunction SignalGraph({\n  label,\n  data,\n  variant = \"line\",\n  unit,\n  precision = 0,\n  min,\n  max,\n  live,\n  tone = \"signal\",\n  ...props\n}: Omit<React.ComponentProps<\"div\">, \"children\"> & {\n  label: string\n  data: number[]\n  variant?: \"line\" | \"bars\"\n  unit?: string\n  precision?: number\n  min?: number\n  max?: number\n  live?: boolean\n  tone?: Tone\n}) {\n  const id = React.useId()\n  const plot = React.useRef<SVGGElement>(null)\n  const arrived = React.useRef(0)\n\n  // The sample that just fell off the start is kept for one more window,\n  // drawn a step before the first, so the trace has something to scroll\n  // out as well as in.\n  const [shown, setShown] = React.useState<{ data: number[]; ghost: number | null }>({ data, ghost: null })\n  if (shown.data !== data) setShown({ data, ghost: live && shown.data.length === data.length ? (shown.data[0] ?? null) : null })\n  const ghost = shown.data === data ? shown.ghost : null\n\n  const count = data.length\n  const latest = count ? data[count - 1] : null\n  const lowest = count ? Math.min(...data) : 0\n  const highest = count ? Math.max(...data) : 0\n  const floor = min ?? Math.min(0, lowest)\n  const ceiling = Math.max(max ?? highest, floor + Number.EPSILON)\n  const level = (value: number) => Math.max(0, Math.min(1, (value - floor) / (ceiling - floor)))\n  const step = variant === \"line\" ? W / Math.max(1, count - 1) : W / Math.max(1, count)\n\n  React.useLayoutEffect(() => {\n    const node = plot.current\n    const now = performance.now()\n    const since = now - arrived.current\n    arrived.current = now\n    if (!node || !live || ghost == null || !fxMoves()) return\n    // As long as the last gap between samples, so a steady feed scrolls\n    // without stopping; a first or a late sample just slides in.\n    const duration = since > 80 && since < 2000 ? since : 320\n    const slide = node.animate([{ transform: `translateX(${step}px)` }, { transform: \"translateX(0)\" }], { duration, easing: \"linear\" })\n    return () => slide.cancel()\n  }, [data, ghost, live, step])\n\n  const samples = ghost == null ? data.map((value, index) => ({ value, at: index })) : [{ value: ghost, at: -1 }, ...data.map((value, index) => ({ value, at: index }))]\n  const y = (value: number) => H - pad - level(value) * (H - pad * 2)\n  const points = samples.map((sample) => `${(sample.at * step).toFixed(2)},${y(sample.value).toFixed(2)}`)\n  const first = samples.length ? samples[0].at * step : 0\n\n  const format = (value: number) => value.toFixed(precision)\n  const summary = latest == null ? \"No samples.\" : `Latest ${format(latest)}${unit ? ` ${unit}` : \"\"}. Lowest ${format(lowest)}, highest ${format(highest)}.`\n\n  return (\n    <div data-kit=\"signal-graph\" data-variant={variant} data-tone={tone} data-live={live ? \"\" : undefined} role=\"group\" aria-labelledby={id} {...props}>\n      <div data-kit=\"signal-graph-head\">\n        <span data-kit=\"label\" id={id}>\n          {label}\n        </span>\n        <span data-kit=\"signal-graph-value\" aria-hidden>\n          {latest == null ? \"--\" : format(latest)}\n          {unit && <span data-kit=\"signal-graph-unit\">{unit}</span>}\n        </span>\n      </div>\n      <span data-sr-only>{summary}</span>\n      <div data-kit=\"signal-graph-plot\" aria-hidden>\n        <svg viewBox={`0 0 ${W} ${H}`} preserveAspectRatio=\"none\" focusable=\"false\">\n          <g ref={plot}>\n            {variant === \"line\" && count > 1 && (\n              <>\n                <polygon data-kit=\"signal-graph-area\" points={`${first.toFixed(2)},${H} ${points.join(\" \")} ${W},${H}`} />\n                <polyline data-kit=\"signal-graph-trace\" points={points.join(\" \")} />\n              </>\n            )}\n            {variant === \"bars\" &&\n              samples.map((sample) => (\n                <rect\n                  key={sample.at}\n                  data-kit=\"signal-graph-bar\"\n                  data-latest={sample.at === count - 1 ? \"\" : undefined}\n                  x={(sample.at * step + step * 0.15).toFixed(2)}\n                  width={(step * 0.7).toFixed(2)}\n                  y={(H - Math.max(0.6, level(sample.value) * H)).toFixed(2)}\n                  height={Math.max(0.6, level(sample.value) * H).toFixed(2)}\n                />\n              ))}\n          </g>\n        </svg>\n        {variant === \"line\" && latest != null && <span data-kit=\"signal-graph-head-mark\" style={{ \"--graph-head\": level(latest) } as React.CSSProperties} />}\n        <span data-micro data-kit=\"signal-graph-scale\" data-end=\"max\">\n          {format(ceiling)}\n        </span>\n        <span data-micro data-kit=\"signal-graph-scale\" data-end=\"min\">\n          {format(floor)}\n        </span>\n      </div>\n    </div>\n  )\n}\n\nexport { SignalGraph }\nexport type { Tone as SignalGraphTone }\n",
      "type": "registry:ui",
      "target": "@ui/signal-graph.tsx"
    },
    {
      "path": "registry/cyber/ui/cyber/signal-graph.css",
      "content": "@layer ui-sdk, cyber.tokens, cyber.base, cyber.ui, cyber.blocks, cyber.fx, cyber.a11y;\n\n@layer cyber.ui {\n  [data-skin=\"cyber\"] [data-kit=\"signal-graph\"] {\n    --graph-color: var(--cy-signal);\n    --graph-height: 56px;\n\n    display: grid;\n    gap: 6px;\n    width: 100%;\n    min-width: 0;\n    text-align: start;\n\n    &[data-tone=\"accent\"] {\n      --graph-color: var(--cy-accent);\n    }\n    &[data-tone=\"flag\"] {\n      --graph-color: var(--cy-flag);\n    }\n    &[data-tone=\"ok\"] {\n      --graph-color: var(--cy-ok);\n    }\n  }\n\n  [data-skin=\"cyber\"] [data-kit=\"signal-graph-head\"] {\n    display: flex;\n    align-items: baseline;\n    justify-content: space-between;\n    gap: 12px;\n    min-width: 0;\n\n    & [data-kit=\"label\"] {\n      min-width: 0;\n      overflow: hidden;\n      text-overflow: ellipsis;\n      white-space: nowrap;\n    }\n  }\n\n  /* The readout is a value, so it's the signal color whatever the trace\n     is drawn in. */\n  [data-skin=\"cyber\"] [data-kit=\"signal-graph-value\"] {\n    flex: none;\n    font-family: var(--cy-mono);\n    font-size: var(--cy-text-md);\n    font-weight: 600;\n    font-variant-numeric: tabular-nums;\n    line-height: 1;\n    color: var(--cy-signal);\n  }\n\n  [data-skin=\"cyber\"] [data-kit=\"signal-graph-unit\"] {\n    margin-inline-start: 0.4em;\n    font-family: var(--cy-font);\n    font-size: var(--cy-text-xs);\n    letter-spacing: var(--cy-track);\n    text-transform: uppercase;\n  }\n\n  /* The grid: a solid baseline, three dashed rules above it, and a tick\n     rule down the leading edge. */\n  [data-skin=\"cyber\"] [data-kit=\"signal-graph-plot\"] {\n    position: relative;\n    height: var(--graph-height);\n    background:\n      linear-gradient(var(--cy-line-ctl), var(--cy-line-ctl)) left bottom / 100% 1px no-repeat,\n      repeating-linear-gradient(to bottom, var(--cy-line-ctl) 0 1px, transparent 1px 25%) left top / 4px 100% no-repeat,\n      repeating-linear-gradient(to right, var(--cy-line-deco) 0 2px, transparent 2px 6px) left 0 top 25% / 100% 1px no-repeat,\n      repeating-linear-gradient(to right, var(--cy-line-deco) 0 2px, transparent 2px 6px) left 0 top 50% / 100% 1px no-repeat,\n      repeating-linear-gradient(to right, var(--cy-line-deco) 0 2px, transparent 2px 6px) left 0 top 75% / 100% 1px no-repeat;\n\n    & > svg {\n      position: absolute;\n      inset: 0;\n      display: block;\n      width: 100%;\n      height: 100%;\n      overflow: hidden;\n    }\n\n    &:dir(rtl) > svg {\n      scale: -1 1;\n    }\n  }\n\n  [data-skin=\"cyber\"] [data-kit=\"signal-graph-trace\"] {\n    fill: none;\n    stroke: var(--graph-color);\n    stroke-width: 1.5px;\n    stroke-linejoin: miter;\n    stroke-linecap: square;\n    vector-effect: non-scaling-stroke;\n  }\n\n  [data-skin=\"cyber\"] [data-kit=\"signal-graph-area\"] {\n    fill: color-mix(in srgb, var(--graph-color) 13%, transparent);\n    stroke: none;\n  }\n\n  /* A spectrum is lit segments: one mask over the whole drawing cuts every\n     bar into rows. Only the mask's alpha is used, so its color is any\n     opaque token. Older bars sit back; the latest is at full strength. */\n  [data-skin=\"cyber\"] [data-kit=\"signal-graph\"][data-variant=\"bars\"] [data-kit=\"signal-graph-plot\"] > svg {\n    mask-image: repeating-linear-gradient(to top, var(--cy-void) 0 3px, transparent 3px 4px);\n  }\n\n  [data-skin=\"cyber\"] [data-kit=\"signal-graph-bar\"] {\n    fill: color-mix(in srgb, var(--graph-color) 62%, var(--cy-panel));\n\n    &[data-latest] {\n      fill: var(--graph-color);\n    }\n  }\n\n  /* The newest sample: a small square on the end of the trace. */\n  [data-skin=\"cyber\"] [data-kit=\"signal-graph-head-mark\"] {\n    position: absolute;\n    inset-inline-end: -2px;\n    inset-block-end: calc((0.05 + var(--graph-head) * 0.9) * 100% - 2.5px);\n    width: 5px;\n    height: 5px;\n    background: var(--cy-ink-hi);\n    box-shadow: 0 0 calc(6px * var(--cy-fx)) var(--graph-color);\n\n    [data-live] & {\n      animation: var(--cy-fx-pulse) 1.6s ease-in-out infinite;\n    }\n  }\n\n  [data-skin=\"cyber\"] [data-kit=\"signal-graph-scale\"] {\n    position: absolute;\n    inset-inline-start: 7px;\n\n    &[data-end=\"max\"] {\n      inset-block-start: 2px;\n    }\n    &[data-end=\"min\"] {\n      inset-block-end: 3px;\n    }\n  }\n\n  @media (forced-colors: active) {\n    [data-skin=\"cyber\"] [data-kit=\"signal-graph-trace\"] {\n      stroke: CanvasText;\n    }\n    [data-skin=\"cyber\"] [data-kit=\"signal-graph-bar\"] {\n      fill: CanvasText;\n    }\n  }\n}\n",
      "type": "registry:file",
      "target": "@ui/cyber/signal-graph.css"
    }
  ],
  "type": "registry:ui"
}
