{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "chart-streamgraph",
  "type": "registry:component",
  "title": "Streamgraph",
  "description": "Stacked series flow around a centre line as smooth organic streams that grow out of the axis on mount. A crosshair reads every period, hovering a stream isolates it, and legend chips hide series while the remaining streams reflow.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "charts",
    "chart",
    "streamgraph",
    "stacked",
    "svg",
    "legend",
    "interactive"
  ],
  "docs": "https://hyperblur.dev/effects/chart-streamgraph",
  "files": [
    {
      "path": "registry/hyperblur/chart-streamgraph.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/chart-streamgraph.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface StreamSeries {\n  name: string;\n  color: string;\n  values: number[];\n}\n\nexport interface ChartStreamgraphProps extends Omit<React.HTMLAttributes<HTMLDivElement>, \"children\" | \"title\"> {\n  series?: StreamSeries[];\n  labels?: string[];\n  baseline?: \"center\" | \"zero\";\n  height?: number;\n  title?: string;\n  summary?: string;\n  unit?: string;\n  format?: (value: number) => string;\n  showLegend?: boolean;\n  defaultHidden?: string[];\n  onSeriesToggle?: (name: string, visible: boolean) => void;\n}\n\nconst defaultLabels = [\"Aug 4\", \"Aug 11\", \"Aug 18\", \"Aug 25\", \"Sep 1\", \"Sep 8\", \"Sep 15\", \"Sep 22\", \"Sep 29\", \"Oct 6\", \"Oct 13\", \"Oct 20\", \"Oct 27\", \"Nov 3\"];\n\nconst defaultSeries: StreamSeries[] = [\n  { name: \"Organic\", color: \"#2f7bff\", values: [42, 44, 47, 45, 51, 56, 58, 61, 59, 64, 69, 72, 70, 76] },\n  { name: \"Direct\", color: \"#7fb3ff\", values: [28, 27, 30, 32, 31, 33, 36, 35, 38, 37, 39, 41, 44, 43] },\n  { name: \"Referral\", color: \"#22c3a6\", values: [12, 15, 14, 18, 24, 31, 27, 22, 19, 18, 21, 25, 29, 33] },\n  { name: \"Social\", color: \"#a78bfa\", values: [9, 11, 18, 26, 21, 15, 13, 12, 16, 22, 30, 24, 19, 17] },\n  { name: \"Email\", color: \"#f5b544\", values: [6, 14, 7, 6, 15, 7, 6, 16, 8, 7, 17, 8, 7, 19] },\n  { name: \"Paid\", color: \"#f472b6\", values: [18, 17, 16, 14, 13, 13, 12, 14, 18, 21, 19, 16, 15, 14] },\n];\n\nconst W = 600;\n\nfunction smooth(points: [number, number][], move: boolean) {\n  if (!points.length) return \"\";\n  const f = (n: number) => n.toFixed(2);\n  let d = `${move ? \"M\" : \"L\"}${f(points[0][0])} ${f(points[0][1])}`;\n  for (let i = 0; i < points.length - 1; i++) {\n    const p0 = points[i - 1] ?? points[i];\n    const p1 = points[i];\n    const p2 = points[i + 1];\n    const p3 = points[i + 2] ?? p2;\n    const c1x = p1[0] + (p2[0] - p0[0]) / 6;\n    const c1y = p1[1] + (p2[1] - p0[1]) / 6;\n    const c2x = p2[0] - (p3[0] - p1[0]) / 6;\n    const c2y = p2[1] - (p3[1] - p1[1]) / 6;\n    d += ` C${f(c1x)} ${f(c1y)} ${f(c2x)} ${f(c2y)} ${f(p2[0])} ${f(p2[1])}`;\n  }\n  return d;\n}\n\nconst css = `\n.hb-wi1 {\n  position: relative;\n  width: 100%;\n  box-sizing: border-box;\n  color: #ededed;\n  font-family: inherit;\n  font-size: 12px;\n}\n.hb-wi1 *, .hb-wi1 *::before, .hb-wi1 *::after { box-sizing: border-box; }\n.hb-wi1-plot {\n  position: relative;\n  width: 100%;\n  height: var(--hb-wi1-h, 220px);\n  outline: none;\n  cursor: crosshair;\n  touch-action: pan-y;\n  border-radius: 10px;\n}\n.hb-wi1-plot:focus-visible { box-shadow: 0 0 0 2px rgba(127, 179, 255, 0.7); }\n.hb-wi1-svg { position: absolute; inset: 0; width: 100%; height: 100%; display: block; overflow: visible; }\n.hb-wi1-guide { stroke: rgba(255, 255, 255, 0.05); stroke-width: 1; vector-effect: non-scaling-stroke; }\n.hb-wi1-axis { stroke: rgba(255, 255, 255, 0.14); stroke-width: 1; stroke-dasharray: 2 4; vector-effect: non-scaling-stroke; }\n.hb-wi1-layer {\n  transform-box: view-box;\n  transform-origin: 50% var(--hb-wi1-o, 50%);\n  transform: scaleY(0);\n  opacity: 0;\n  stroke: rgba(10, 10, 10, 0.55);\n  stroke-width: 1;\n  vector-effect: non-scaling-stroke;\n  transition: d 0.7s cubic-bezier(0.65, 0, 0.25, 1), opacity 0.25s ease, filter 0.25s ease;\n}\n.hb-wi1[data-in=\"true\"] .hb-wi1-layer {\n  transform: scaleY(1);\n  opacity: 1;\n  transition: transform 1.1s cubic-bezier(0.16, 1, 0.3, 1) var(--hb-wi1-d, 0ms), opacity 0.5s ease var(--hb-wi1-d, 0ms), d 0.7s cubic-bezier(0.65, 0, 0.25, 1), filter 0.25s ease;\n}\n.hb-wi1[data-focus=\"true\"] .hb-wi1-layer { opacity: 0.22; }\n.hb-wi1[data-focus=\"true\"] .hb-wi1-layer[data-on=\"true\"] { opacity: 1; filter: brightness(1.12) saturate(1.1); }\n.hb-wi1-cursor {\n  position: absolute;\n  top: 6px;\n  bottom: 6px;\n  width: 1px;\n  margin-left: -0.5px;\n  background: linear-gradient(to bottom, transparent, rgba(255, 255, 255, 0.55) 18%, rgba(255, 255, 255, 0.55) 82%, transparent);\n  pointer-events: none;\n  transition: left 0.16s cubic-bezier(0.2, 0.8, 0.2, 1), opacity 0.2s ease;\n}\n.hb-wi1-tip {\n  position: absolute;\n  top: 8px;\n  z-index: 2;\n  min-width: 148px;\n  padding: 9px 10px 8px;\n  border-radius: 10px;\n  background: rgba(17, 17, 17, 0.94);\n  border: 1px solid #262626;\n  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.5), inset 0 1px 0 rgba(255, 255, 255, 0.05);\n  backdrop-filter: blur(8px);\n  pointer-events: none;\n  transition: left 0.16s cubic-bezier(0.2, 0.8, 0.2, 1), opacity 0.2s ease, transform 0.2s ease;\n}\n.hb-wi1-tip-h { display: flex; justify-content: space-between; gap: 12px; margin-bottom: 6px; font-size: 11px; color: #8f8f8f; }\n.hb-wi1-tip-h b { color: #ededed; font-weight: 600; font-variant-numeric: tabular-nums; }\n.hb-wi1-row { display: flex; align-items: center; gap: 7px; padding: 1.5px 0; font-size: 11.5px; color: #a1a1a1; }\n.hb-wi1-row[data-on=\"true\"] { color: #ededed; }\n.hb-wi1-row i { width: 7px; height: 7px; border-radius: 2px; flex: none; }\n.hb-wi1-row span { flex: 1; }\n.hb-wi1-row b { font-weight: 500; font-variant-numeric: tabular-nums; }\n.hb-wi1-x { position: relative; height: 22px; margin-top: 6px; }\n.hb-wi1-xl {\n  position: absolute;\n  top: 4px;\n  transform: translateX(-50%);\n  font-size: 10.5px;\n  color: #6f6f6f;\n  white-space: nowrap;\n  font-variant-numeric: tabular-nums;\n  transition: color 0.2s ease;\n}\n.hb-wi1-xl[data-on=\"true\"] { color: #ededed; }\n.hb-wi1-legend { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }\n.hb-wi1-chip {\n  display: inline-flex;\n  align-items: center;\n  gap: 6px;\n  height: 24px;\n  padding: 0 9px 0 7px;\n  border-radius: 999px;\n  border: 1px solid #262626;\n  background: #111;\n  color: #ededed;\n  font: inherit;\n  font-size: 11px;\n  cursor: pointer;\n  transition: background 0.2s ease, border-color 0.2s ease, color 0.2s ease, opacity 0.2s ease;\n}\n.hb-wi1-chip:hover { background: #171717; border-color: #333; }\n.hb-wi1-chip:focus-visible { outline: none; box-shadow: 0 0 0 2px rgba(127, 179, 255, 0.7); }\n.hb-wi1-chip i { width: 8px; height: 8px; border-radius: 50%; transition: transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1), background 0.2s ease; }\n.hb-wi1-chip[aria-pressed=\"false\"] { color: #6f6f6f; }\n.hb-wi1-chip[aria-pressed=\"false\"] i { background: transparent !important; box-shadow: inset 0 0 0 1.5px #4a4a4a; transform: scale(0.85); }\n.hb-wi1-chip em { font-style: normal; color: #6f6f6f; font-variant-numeric: tabular-nums; }\n.hb-wi1-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }\n@media (prefers-reduced-motion: reduce) {\n  .hb-wi1-layer, .hb-wi1[data-in=\"true\"] .hb-wi1-layer { transform: none; opacity: 1; transition: none; }\n  .hb-wi1-cursor, .hb-wi1-tip, .hb-wi1-chip, .hb-wi1-chip i { transition: none; }\n}\n`;\n\nexport function ChartStreamgraph({\n  series = defaultSeries,\n  labels = defaultLabels,\n  baseline = \"center\",\n  height = 220,\n  title = \"Sessions by channel\",\n  summary,\n  unit = \"k\",\n  format,\n  showLegend = true,\n  defaultHidden = [],\n  onSeriesToggle,\n  className,\n  style,\n  ...rest\n}: ChartStreamgraphProps) {\n  const id = React.useId().replace(/:/g, \"\");\n  const [inView, setInView] = React.useState(false);\n  const [hidden, setHidden] = React.useState<string[]>(defaultHidden);\n  const [col, setCol] = React.useState<number | null>(null);\n  const [layer, setLayer] = React.useState<string | null>(null);\n  const plotRef = React.useRef<HTMLDivElement>(null);\n\n  React.useEffect(() => {\n    const t = window.setTimeout(() => setInView(true), 60);\n    return () => window.clearTimeout(t);\n  }, []);\n\n  const n = Math.max(2, labels.length);\n  const fmt = format ?? ((v: number) => `${Math.round(v)}${unit}`);\n  const H = 240;\n  const pad = 12;\n\n  const geometry = React.useMemo(() => {\n    const vis = series.map((s) => (hidden.includes(s.name) ? 0 : 1));\n    const totals = Array.from({ length: n }, (_, j) => series.reduce((acc, s, i) => acc + (s.values[j] ?? 0) * vis[i], 0));\n    const allTotals = Array.from({ length: n }, (_, j) => series.reduce((acc, s) => acc + (s.values[j] ?? 0), 0));\n    const max = Math.max(1, ...allTotals);\n    const scale = (H - pad * 2) / max;\n    const xs = Array.from({ length: n }, (_, j) => (j / (n - 1)) * W);\n    const cum = Array.from({ length: n }, (_, j) => (baseline === \"center\" ? -totals[j] / 2 : 0));\n    const toY = (v: number) => (baseline === \"center\" ? H / 2 - v * scale : H - pad - v * scale);\n    const paths = series.map((s, i) => {\n      const lower: [number, number][] = [];\n      const upper: [number, number][] = [];\n      for (let j = 0; j < n; j++) {\n        const v = (s.values[j] ?? 0) * vis[i];\n        lower.push([xs[j], toY(cum[j])]);\n        cum[j] += v;\n        upper.push([xs[j], toY(cum[j])]);\n      }\n      return `${smooth(upper, true)} ${smooth(lower.reverse(), false)} Z`;\n    });\n    return { paths, xs, totals };\n  }, [series, hidden, n, baseline]);\n\n  const pick = (clientX: number) => {\n    const el = plotRef.current;\n    if (!el) return;\n    const r = el.getBoundingClientRect();\n    const t = Math.min(1, Math.max(0, (clientX - r.left) / r.width));\n    setCol(Math.round(t * (n - 1)));\n  };\n\n  const toggle = (name: string) => {\n    setHidden((h) => {\n      const visibleNow = !h.includes(name);\n      if (visibleNow && series.length - h.length <= 1) return h;\n      onSeriesToggle?.(name, !visibleNow);\n      return visibleNow ? [...h, name] : h.filter((x) => x !== name);\n    });\n  };\n\n  const onKey = (e: React.KeyboardEvent) => {\n    if (e.key === \"ArrowRight\" || e.key === \"ArrowLeft\") {\n      e.preventDefault();\n      const dir = e.key === \"ArrowRight\" ? 1 : -1;\n      setCol((c) => Math.min(n - 1, Math.max(0, (c ?? (dir > 0 ? -1 : n)) + dir)));\n    } else if (e.key === \"Home\") {\n      e.preventDefault();\n      setCol(0);\n    } else if (e.key === \"End\") {\n      e.preventDefault();\n      setCol(n - 1);\n    } else if (e.key === \"Escape\") {\n      setCol(null);\n    }\n  };\n\n  const tipLeft = col === null ? 0 : (col / (n - 1)) * 100;\n  const tipSide = tipLeft > 58 ? \"left\" : \"right\";\n  const step = n > 10 ? 2 : 1;\n  const rows = col === null ? [] : series.map((s) => ({ s, v: hidden.includes(s.name) ? null : s.values[col] ?? 0 })).filter((r) => r.v !== null).sort((a, b) => (b.v ?? 0) - (a.v ?? 0));\n  const desc =\n    summary ??\n    `Stacked stream of ${series.length} series over ${n} periods, from ${labels[0]} to ${labels[n - 1]}. Latest total ${fmt(geometry.totals[n - 1] ?? 0)}.`;\n\n  return (\n    <div\n      {...rest}\n      className={[\"hb-wi1\", className].filter(Boolean).join(\" \")}\n      style={{ ...style, [\"--hb-wi1-h\" as string]: `${height}px`, [\"--hb-wi1-o\" as string]: baseline === \"center\" ? \"50%\" : \"100%\" } as React.CSSProperties}\n      data-in={inView}\n      data-focus={layer !== null}\n    >\n      <style href=\"hb-chart-streamgraph\" precedence=\"default\">{css}</style>\n      <div\n        ref={plotRef}\n        className=\"hb-wi1-plot\"\n        tabIndex={0}\n        role=\"group\"\n        aria-label={`${title}. Use left and right arrows to read each period.`}\n        onPointerMove={(e) => pick(e.clientX)}\n        onPointerLeave={() => {\n          setCol(null);\n          setLayer(null);\n        }}\n        onKeyDown={onKey}\n        onBlur={() => setCol(null)}\n      >\n        <svg className=\"hb-wi1-svg\" viewBox={`0 0 ${W} ${H}`} preserveAspectRatio=\"none\" role=\"img\" aria-labelledby={`${id}-t ${id}-d`}>\n          <title id={`${id}-t`}>{title}</title>\n          <desc id={`${id}-d`}>{desc}</desc>\n          {geometry.xs.map((x, j) => (j % step === 0 ? <line key={j} className=\"hb-wi1-guide\" x1={x.toFixed(2)} x2={x.toFixed(2)} y1={0} y2={H} /> : null))}\n          {baseline === \"center\" ? <line className=\"hb-wi1-axis\" x1={0} x2={W} y1={H / 2} y2={H / 2} /> : <line className=\"hb-wi1-axis\" x1={0} x2={W} y1={H - pad} y2={H - pad} />}\n          {series.map((s, i) => (\n            <path\n              key={s.name}\n              className=\"hb-wi1-layer\"\n              d={geometry.paths[i]}\n              fill={s.color}\n              data-on={layer === s.name}\n              style={{ d: `path(\"${geometry.paths[i]}\")`, [\"--hb-wi1-d\" as string]: `${i * 90}ms` } as React.CSSProperties}\n              onPointerEnter={() => setLayer(s.name)}\n              onPointerLeave={() => setLayer(null)}\n            />\n          ))}\n        </svg>\n        <div className=\"hb-wi1-cursor\" style={{ left: `${tipLeft.toFixed(2)}%`, opacity: col === null ? 0 : 1 }} />\n        <div\n          className=\"hb-wi1-tip\"\n          aria-hidden={col === null}\n          style={{\n            left: `${tipLeft.toFixed(2)}%`,\n            opacity: col === null ? 0 : 1,\n            transform: tipSide === \"right\" ? \"translateX(12px)\" : \"translateX(calc(-100% - 12px))\",\n          }}\n        >\n          {col !== null ? (\n            <>\n              <div className=\"hb-wi1-tip-h\">\n                <span>{labels[col]}</span>\n                <b>{fmt(geometry.totals[col] ?? 0)}</b>\n              </div>\n              {rows.map(({ s, v }) => (\n                <div key={s.name} className=\"hb-wi1-row\" data-on={layer === null || layer === s.name}>\n                  <i style={{ background: s.color }} />\n                  <span>{s.name}</span>\n                  <b>{fmt(v ?? 0)}</b>\n                </div>\n              ))}\n            </>\n          ) : null}\n        </div>\n        <div className=\"hb-wi1-sr\" aria-live=\"polite\">\n          {col !== null ? `${labels[col]}: total ${fmt(geometry.totals[col] ?? 0)}. ${rows.map((r) => `${r.s.name} ${fmt(r.v ?? 0)}`).join(\", \")}` : \"\"}\n        </div>\n      </div>\n      <div className=\"hb-wi1-x\" aria-hidden=\"true\">\n        {labels.map((l, j) =>\n          j % step === 0 ? (\n            <span key={l + j} className=\"hb-wi1-xl\" data-on={col === j} style={{ left: `${((j / (n - 1)) * 100).toFixed(2)}%`, transform: j === 0 ? \"none\" : j === n - 1 ? \"translateX(-100%)\" : undefined }}>\n              {l}\n            </span>\n          ) : null,\n        )}\n      </div>\n      {showLegend ? (\n        <div className=\"hb-wi1-legend\">\n          {series.map((s) => {\n            const on = !hidden.includes(s.name);\n            const last = s.values[n - 1] ?? 0;\n            return (\n              <button key={s.name} type=\"button\" className=\"hb-wi1-chip\" aria-pressed={on} onClick={() => toggle(s.name)} onPointerEnter={() => on && setLayer(s.name)} onPointerLeave={() => setLayer(null)} onFocus={() => on && setLayer(s.name)} onBlur={() => setLayer(null)}>\n                <i style={{ background: s.color }} />\n                {s.name}\n                <em>{fmt(last)}</em>\n              </button>\n            );\n          })}\n        </div>\n      ) : null}\n    </div>\n  );\n}\n"
    }
  ]
}