{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "race-replay-line-chart",
  "title": "Race Replay Line Chart",
  "description": "A replayable multi-series line chart: press play and the lines race through the year, the leader drawn stronger, with live rankings, a scrubber and four speeds.",
  "dependencies": [],
  "files": [
    {
      "path": "registry/new-york/race-replay-line-chart/race-replay-line-chart.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface ReplaySeries {\n  label: string;\n  values: number[];\n  /** Leave it out to use the chart's own palette. */\n  color?: string;\n}\n\nexport interface RaceReplayLineChartProps extends Omit<\n  React.ComponentPropsWithoutRef<\"section\">,\n  \"title\"\n> {\n  title?: string;\n  subtitle?: string;\n  theme?: \"dark\" | \"light\";\n  labels?: string[];\n  series?: ReplaySeries[];\n  /** Length of one replay at 1× speed, in seconds. */\n  duration?: number;\n  /** Height of the plot, in px. */\n  height?: number;\n  formatValue?: (value: number) => string;\n}\n\nconst PALETTES = {\n  dark: {\n    bg: \"#08090A\",\n    border: \"rgba(255,255,255,0.08)\",\n    grid: \"rgba(255,255,255,0.05)\",\n    text: \"#F7F8F8\",\n    sub: \"#8A8F98\",\n    faint: \"#62666D\",\n    surface: \"#0F1011\",\n    chip: \"rgba(255,255,255,0.05)\",\n    chipOn: \"rgba(255,255,255,0.1)\",\n    track: \"rgba(255,255,255,0.08)\",\n    thumb: \"#F7F8F8\",\n    btn: \"#E5E5E6\",\n    btnText: \"#08090A\",\n    accent: \"#5E6AD2\",\n    series: [\"#5E6AD2\", \"#D0D6E0\", \"#4EA7FC\"],\n  },\n  light: {\n    bg: \"#FFFFFF\",\n    border: \"rgba(8,9,10,0.08)\",\n    grid: \"rgba(8,9,10,0.06)\",\n    text: \"#08090A\",\n    sub: \"#62666D\",\n    faint: \"#8A8F98\",\n    surface: \"#F7F8F8\",\n    chip: \"rgba(8,9,10,0.04)\",\n    chipOn: \"#FFFFFF\",\n    track: \"rgba(8,9,10,0.1)\",\n    thumb: \"#08090A\",\n    btn: \"#08090A\",\n    btnText: \"#F7F8F8\",\n    accent: \"#5E6AD2\",\n    series: [\"#5E6AD2\", \"#62666D\", \"#4EA7FC\"],\n  },\n};\n\nconst LABELS = [\n  \"Jan\",\n  \"Feb\",\n  \"Mar\",\n  \"Apr\",\n  \"May\",\n  \"Jun\",\n  \"Jul\",\n  \"Aug\",\n  \"Sep\",\n  \"Oct\",\n  \"Nov\",\n  \"Dec\",\n];\n\nconst SERIES: ReplaySeries[] = [\n  {\n    label: \"Atlas\",\n    values: [12, 18, 26, 30, 36, 51, 64, 72, 85, 104, 128, 154],\n  },\n  {\n    label: \"Nova\",\n    values: [18, 27, 33, 41, 52, 63, 68, 79, 89, 98, 114, 132],\n  },\n  {\n    label: \"Orbit\",\n    values: [9, 16, 24, 35, 48, 58, 74, 83, 94, 108, 121, 143],\n  },\n];\n\nconst FORMAT = (n: number) => `$${Math.round(n)}k`;\nconst SPEEDS = [0.5, 1, 2, 4];\n\nconst LEFT = 44;\nconst RIGHT = 12;\nconst TOP = 12;\nconst BOTTOM = 26;\n\nexport function RaceReplayLineChart({\n  title = \"Revenue race\",\n  subtitle = \"Replay the year and watch the lead change hands.\",\n  theme = \"dark\",\n  labels = LABELS,\n  series = SERIES,\n  duration = 12,\n  height = 260,\n  formatValue = FORMAT,\n  className,\n  style,\n  ...props\n}: RaceReplayLineChartProps) {\n  const pal = PALETTES[theme];\n  const [progress, setProgress] = React.useState(1);\n  const [playing, setPlaying] = React.useState(false);\n  const progressRef = React.useRef(1);\n  const [speed, setSpeed] = React.useState(1);\n  const root = React.useRef<HTMLElement>(null);\n  const plotRef = React.useRef<HTMLDivElement>(null);\n  const [width, setWidth] = React.useState(700);\n  const [onScreen, setOnScreen] = React.useState(false);\n  const [tabVisible, setTabVisible] = React.useState(true);\n  const sliderId = React.useId();\n\n  React.useEffect(() => {\n    progressRef.current = progress;\n  }, [progress]);\n\n  React.useEffect(() => {\n    const node = root.current;\n    if (!node) return;\n    const observer = new IntersectionObserver(([entry]) =>\n      setOnScreen(entry.isIntersecting),\n    );\n    observer.observe(node);\n    const visibility = () => setTabVisible(!document.hidden);\n    document.addEventListener(\"visibilitychange\", visibility);\n    return () => {\n      observer.disconnect();\n      document.removeEventListener(\"visibilitychange\", visibility);\n    };\n  }, []);\n\n  // Drawn at the real width, so the axis text stays the same size on a phone.\n  React.useEffect(() => {\n    const el = plotRef.current;\n    if (!el) return;\n    const ro = new ResizeObserver(([entry]) =>\n      setWidth(Math.max(240, Math.round(entry.contentRect.width))),\n    );\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, []);\n\n  React.useEffect(() => {\n    if (!playing || !onScreen || !tabVisible) return;\n    let frame = 0;\n    let last: number | null = null;\n    const tick = (now: number) => {\n      const delta = last === null ? 0 : Math.min(now - last, 100);\n      last = now;\n      const next = Math.min(\n        1,\n        progressRef.current +\n          (delta / (Math.max(0.1, duration) * 1000)) * speed,\n      );\n      progressRef.current = next;\n      setProgress(next);\n      if (next < 1) frame = requestAnimationFrame(tick);\n      else setPlaying(false);\n    };\n    frame = requestAnimationFrame(tick);\n    return () => cancelAnimationFrame(frame);\n  }, [playing, speed, duration, onScreen, tabVisible]);\n\n  const valid = series.filter(\n    (s) => s.values.length === labels.length && s.values.every(Number.isFinite),\n  );\n  const colorOf = (s: ReplaySeries) =>\n    s.color ?? pal.series[valid.indexOf(s) % pal.series.length];\n  const position = progress * Math.max(0, labels.length - 1);\n  const index = Math.floor(position);\n  const fraction = position - index;\n  const value = (s: ReplaySeries) =>\n    (s.values[index] ?? 0) +\n    ((s.values[Math.min(index + 1, labels.length - 1)] ?? 0) -\n      (s.values[index] ?? 0)) *\n      fraction;\n  const ranking = [...valid].sort((a, b) => value(b) - value(a));\n  const leader = ranking[0];\n  const min = Math.min(0, ...valid.flatMap((s) => s.values));\n  const max = Math.max(min + 1, ...valid.flatMap((s) => s.values));\n  const span = width - LEFT - RIGHT;\n  const base = height - BOTTOM;\n  const x = (p: number) => LEFT + (p / Math.max(1, labels.length - 1)) * span;\n  const y = (v: number) => base - ((v - min) / (max - min)) * (base - TOP);\n  const hasData = valid.length > 0 && labels.length > 0;\n  const running = playing && progress < 1;\n  const every = Math.max(1, Math.ceil(labels.length / Math.floor(width / 72)));\n\n  return (\n    <section\n      ref={root}\n      className={`w-full rounded-2xl p-5 sm:p-7 ${className ?? \"\"}`}\n      style={{\n        background: pal.bg,\n        boxShadow: `inset 0 0 0 1px ${pal.border}`,\n        color: pal.text,\n        fontFamily:\n          '\"Inter Variable\", Inter, \"SF Pro Display\", -apple-system, BlinkMacSystemFont, sans-serif',\n        ...style,\n      }}\n      {...props}\n    >\n      <div className=\"flex flex-wrap items-start justify-between gap-4\">\n        <div>\n          <h3 className=\"m-0 text-[20px] leading-[1.33] font-[590] tracking-[-0.012em]\">\n            {title}\n          </h3>\n          <p\n            className=\"m-0 mt-1 text-[13px] tracking-[-0.011em]\"\n            style={{ color: pal.sub }}\n          >\n            {subtitle}\n          </p>\n        </div>\n        <div className=\"text-left sm:text-right\">\n          <p className=\"m-0 text-[32px] leading-none font-[510] tracking-[-0.022em] tabular-nums\">\n            {labels[index] ?? \"—\"}\n          </p>\n          <p className=\"m-0 mt-1.5 text-[13px]\" style={{ color: pal.sub }}>\n            {leader\n              ? `${leader.label} leads, ${formatValue(value(leader))}`\n              : \"No data\"}\n          </p>\n        </div>\n      </div>\n\n      {!hasData ? (\n        <p className=\"py-16 text-center text-sm\" style={{ color: pal.sub }}>\n          No replay data available.\n        </p>\n      ) : (\n        <>\n          <div ref={plotRef} className=\"mt-5\">\n            <svg\n              viewBox={`0 0 ${width} ${height}`}\n              className=\"block w-full\"\n              style={{ height }}\n              role=\"img\"\n              aria-label={`${labels[index]}: ${ranking.map((s) => `${s.label} ${formatValue(value(s))}`).join(\", \")}`}\n            >\n              {[0, 1, 2, 3, 4].map((i) => {\n                const v = min + ((max - min) * i) / 4;\n                return (\n                  <g key={i}>\n                    <line\n                      x1={LEFT}\n                      x2={width - RIGHT}\n                      y1={y(v)}\n                      y2={y(v)}\n                      stroke={pal.grid}\n                    />\n                    <text\n                      x={LEFT - 10}\n                      y={y(v) + 4}\n                      textAnchor=\"end\"\n                      fill={pal.faint}\n                      fontSize=\"11\"\n                    >\n                      {formatValue(v)}\n                    </text>\n                  </g>\n                );\n              })}\n              {labels.map(\n                (label, i) =>\n                  i % every === 0 && (\n                    <text\n                      key={label}\n                      x={x(i)}\n                      y={height - 6}\n                      textAnchor={i === 0 ? \"start\" : \"middle\"}\n                      fill={pal.faint}\n                      fontSize=\"11\"\n                    >\n                      {label}\n                    </text>\n                  ),\n              )}\n              <line\n                x1={x(position)}\n                x2={x(position)}\n                y1={TOP}\n                y2={base}\n                stroke={pal.border}\n              />\n              {valid.map((s) => {\n                const points = s.values\n                  .slice(0, index + 1)\n                  .map((v, i) => `${x(i)},${y(v)}`);\n                points.push(`${x(position)},${y(value(s))}`);\n                const lead = s === leader;\n                return (\n                  <g key={s.label}>\n                    <polyline\n                      points={s.values\n                        .map((v, i) => `${x(i)},${y(v)}`)\n                        .join(\" \")}\n                      fill=\"none\"\n                      stroke={colorOf(s)}\n                      strokeWidth=\"1\"\n                      opacity=\"0.14\"\n                    />\n                    {/* The line in front is drawn stronger, so the lead reads at a glance. */}\n                    <polyline\n                      points={points.join(\" \")}\n                      fill=\"none\"\n                      stroke={colorOf(s)}\n                      strokeWidth={lead ? 2 : 1.5}\n                      strokeLinejoin=\"round\"\n                      strokeLinecap=\"round\"\n                      opacity={lead ? 1 : 0.62}\n                    />\n                    <circle\n                      cx={x(position)}\n                      cy={y(value(s))}\n                      r=\"3.5\"\n                      fill={colorOf(s)}\n                      stroke={pal.bg}\n                      strokeWidth=\"2\"\n                    />\n                  </g>\n                );\n              })}\n            </svg>\n          </div>\n\n          <ol\n            className=\"m-0 mt-3 grid list-none gap-1.5 p-0 sm:grid-cols-3\"\n            aria-label=\"Current ranking\"\n          >\n            {ranking.map((s, i) => (\n              <li\n                key={s.label}\n                className=\"flex h-9 items-center gap-2 rounded-lg px-3 text-[13px] tracking-[-0.011em]\"\n                style={{\n                  background: pal.surface,\n                  boxShadow: `inset 0 0 0 1px ${pal.border}`,\n                }}\n              >\n                <span className=\"tabular-nums\" style={{ color: pal.faint }}>\n                  {i + 1}\n                </span>\n                <span\n                  className=\"size-2 rounded-full\"\n                  style={{ background: colorOf(s) }}\n                />\n                <span className=\"font-[510]\">{s.label}</span>\n                <span\n                  className=\"ml-auto tabular-nums\"\n                  style={{ color: pal.sub }}\n                >\n                  {formatValue(value(s))}\n                </span>\n              </li>\n            ))}\n          </ol>\n\n          <div className=\"mt-4 flex flex-wrap items-center gap-3\">\n            <button\n              type=\"button\"\n              onClick={() => {\n                if (progress >= 1) {\n                  progressRef.current = 0;\n                  setProgress(0);\n                  setPlaying(true);\n                } else setPlaying((p) => !p);\n              }}\n              className=\"flex h-8 min-w-[92px] cursor-pointer items-center justify-center gap-1.5 rounded-full px-3.5 text-[13px] font-[510] tracking-[-0.011em] transition-opacity hover:opacity-90\"\n              style={{ background: pal.btn, color: pal.btnText }}\n            >\n              <svg\n                width=\"10\"\n                height=\"10\"\n                viewBox=\"0 0 10 10\"\n                fill=\"currentColor\"\n                aria-hidden=\"true\"\n              >\n                {running ? (\n                  <path d=\"M1.5 1h2.4v8H1.5zM6.1 1h2.4v8H6.1z\" />\n                ) : (\n                  <path d=\"M2 1.1v7.8c0 .4.4.6.7.4l6-3.9c.3-.2.3-.6 0-.8l-6-3.9c-.3-.2-.7 0-.7.4Z\" />\n                )}\n              </svg>\n              {running ? \"Pause\" : progress >= 1 ? \"Replay\" : \"Play\"}\n            </button>\n\n            <div className=\"relative h-5 min-w-32 flex-1\">\n              <label htmlFor={sliderId} className=\"sr-only\">\n                Replay timeline\n              </label>\n              <input\n                id={sliderId}\n                type=\"range\"\n                min=\"0\"\n                max=\"1000\"\n                step=\"1\"\n                value={Math.round(progress * 1000)}\n                onChange={(e) => {\n                  setPlaying(false);\n                  setProgress(Number(e.target.value) / 1000);\n                }}\n                aria-valuetext={`${labels[index] ?? \"\"}, ${Math.round(progress * 100)} percent`}\n                className=\"peer absolute inset-0 z-10 m-0 size-full cursor-pointer opacity-0\"\n              />\n              <span\n                aria-hidden=\"true\"\n                className=\"absolute inset-x-0 top-1/2 h-[3px] -translate-y-1/2 rounded-full\"\n                style={{ background: pal.track }}\n              />\n              <span\n                aria-hidden=\"true\"\n                className=\"absolute top-1/2 left-0 h-[3px] -translate-y-1/2 rounded-full\"\n                style={{ width: `${progress * 100}%`, background: pal.accent }}\n              />\n              <span\n                aria-hidden=\"true\"\n                className=\"absolute top-1/2 size-3 -translate-x-1/2 -translate-y-1/2 rounded-full peer-focus-visible:ring-2\"\n                style={{\n                  left: `${progress * 100}%`,\n                  background: pal.thumb,\n                }}\n              />\n            </div>\n\n            <div\n              className=\"flex rounded-full p-0.5\"\n              style={{ background: pal.chip }}\n              role=\"radiogroup\"\n              aria-label=\"Playback speed\"\n            >\n              {SPEEDS.map((s) => (\n                <button\n                  key={s}\n                  type=\"button\"\n                  role=\"radio\"\n                  aria-checked={speed === s}\n                  onClick={() => setSpeed(s)}\n                  className=\"h-7 cursor-pointer rounded-full px-2.5 text-[12px] font-[510] tabular-nums transition-colors outline-none focus-visible:ring-2\"\n                  style={{\n                    background: speed === s ? pal.chipOn : \"transparent\",\n                    color: speed === s ? pal.text : pal.sub,\n                  }}\n                >\n                  {s}×\n                </button>\n              ))}\n            </div>\n          </div>\n        </>\n      )}\n    </section>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}