{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "revenue-bridge-chart",
  "title": "Revenue Bridge Chart",
  "description": "A revenue waterfall that derives closing MRR from an opening balance and signed movements, with period comparisons, connectors and accessible balance inspection.",
  "dependencies": [],
  "files": [
    {
      "path": "registry/new-york/revenue-bridge-chart/revenue-bridge-chart.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nconst PALETTES = {\n  dark: {\n    bg: \"#08090A\",\n    surface: \"#0F1011\",\n    text: \"#F7F8F8\",\n    soft: \"#D0D6E0\",\n    sub: \"#8A8F98\",\n    faint: \"#62666D\",\n    border: \"rgba(255,255,255,0.08)\",\n    grid: \"rgba(255,255,255,0.05)\",\n    chip: \"rgba(255,255,255,0.05)\",\n    chipOn: \"rgba(255,255,255,0.1)\",\n    accent: \"#6B76DE\",\n    positive: \"#79AEA0\",\n    negative: \"#C18686\",\n  },\n  light: {\n    bg: \"#FFFFFF\",\n    surface: \"#F7F8F8\",\n    text: \"#08090A\",\n    soft: \"#3C3F44\",\n    sub: \"#62666D\",\n    faint: \"#8A8F98\",\n    border: \"rgba(8,9,10,0.08)\",\n    grid: \"rgba(8,9,10,0.06)\",\n    chip: \"rgba(8,9,10,0.04)\",\n    chipOn: \"rgba(8,9,10,0.08)\",\n    accent: \"#5864C6\",\n    positive: \"#377D69\",\n    negative: \"#A35454\",\n  },\n};\ntype Palette = typeof PALETTES.dark;\nconst FONT =\n  '\"Inter Variable\", Inter, -apple-system, BlinkMacSystemFont, sans-serif';\nfunction Stat({\n  value,\n  label,\n  pal,\n}: {\n  value: string;\n  label: string;\n  pal: Palette;\n}) {\n  return (\n    <div>\n      <p className=\"m-0 text-[18px] leading-tight font-[510] tracking-[-0.018em] tabular-nums\">\n        {value}\n      </p>\n      <p className=\"m-0 mt-1 text-[11px]\" style={{ color: pal.sub }}>\n        {label}\n      </p>\n    </div>\n  );\n}\nfunction Pills({\n  label,\n  options,\n  value,\n  onChange,\n  pal,\n}: {\n  label: string;\n  options: string[];\n  value: number;\n  onChange: (value: number) => void;\n  pal: Palette;\n}) {\n  return (\n    <div\n      role=\"radiogroup\"\n      aria-label={label}\n      className=\"flex w-fit max-w-full flex-wrap rounded-full p-0.5\"\n      style={{ background: pal.chip }}\n    >\n      {options.map((option, i) => (\n        <button\n          key={`${option}-${i}`}\n          type=\"button\"\n          role=\"radio\"\n          aria-checked={i === value}\n          tabIndex={i === value ? 0 : -1}\n          onClick={() => onChange(i)}\n          onKeyDown={(event) => {\n            if (![\"ArrowLeft\", \"ArrowRight\", \"Home\", \"End\"].includes(event.key))\n              return;\n            event.preventDefault();\n            const next =\n              event.key === \"Home\"\n                ? 0\n                : event.key === \"End\"\n                  ? options.length - 1\n                  : (i +\n                      (event.key === \"ArrowRight\" ? 1 : -1) +\n                      options.length) %\n                    options.length;\n            onChange(next);\n            event.currentTarget.parentElement\n              ?.querySelectorAll<HTMLButtonElement>(\"button\")\n              [next]?.focus();\n          }}\n          className=\"cursor-pointer rounded-full px-3 py-1.5 text-[12px] font-[510] tracking-[-0.011em] outline-offset-2 focus-visible:outline-2\"\n          style={{\n            background: i === value ? pal.chipOn : \"transparent\",\n            color: i === value ? pal.text : pal.sub,\n          }}\n        >\n          {option}\n        </button>\n      ))}\n    </div>\n  );\n}\n\nexport interface RevenueBridgeStep {\n  label: string;\n  value: number;\n}\nexport interface RevenueBridgePeriod {\n  label: string;\n  start: number;\n  steps: RevenueBridgeStep[];\n}\nexport interface RevenueBridgeChartProps extends Omit<\n  React.ComponentPropsWithoutRef<\"section\">,\n  \"title\"\n> {\n  theme?: \"dark\" | \"light\";\n  title?: string;\n  subtitle?: string;\n  /** Each period provides an opening balance and signed movements. Closing is calculated. */\n  periods?: RevenueBridgePeriod[];\n  defaultPeriod?: number;\n  height?: number;\n  formatValue?: (value: number) => string;\n}\nconst PERIODS: RevenueBridgePeriod[] = [\n  {\n    label: \"October\",\n    start: 120000,\n    steps: [\n      { label: \"New\", value: 28000 },\n      { label: \"Expansion\", value: 14500 },\n      { label: \"Reactivation\", value: 6500 },\n      { label: \"Contraction\", value: -8000 },\n      { label: \"Churn\", value: -12000 },\n    ],\n  },\n  {\n    label: \"November\",\n    start: 149000,\n    steps: [\n      { label: \"New\", value: 34000 },\n      { label: \"Expansion\", value: 18000 },\n      { label: \"Reactivation\", value: 9000 },\n      { label: \"Contraction\", value: -11000 },\n      { label: \"Churn\", value: -15000 },\n    ],\n  },\n];\nconst FORMAT = (value: number) =>\n  new Intl.NumberFormat(\"en-US\", {\n    style: \"currency\",\n    currency: \"USD\",\n    notation: \"compact\",\n    maximumFractionDigits: 1,\n  }).format(value);\ninterface BridgeBar {\n  label: string;\n  from: number;\n  to: number;\n  value: number;\n  total: boolean;\n}\nfunction buildBridge(period: RevenueBridgePeriod): BridgeBar[] {\n  let balance = period.start;\n  const steps = period.steps.map((step) => {\n    const from = balance;\n    balance += step.value;\n    return {\n      label: step.label,\n      from,\n      to: balance,\n      value: step.value,\n      total: false,\n    };\n  });\n  return [\n    {\n      label: \"Opening\",\n      from: 0,\n      to: period.start,\n      value: period.start,\n      total: true,\n    },\n    ...steps,\n    { label: \"Closing\", from: 0, to: balance, value: balance, total: true },\n  ];\n}\nexport function RevenueBridgeChart({\n  theme = \"dark\",\n  title = \"What moved revenue\",\n  subtitle = \"From opening to closing MRR. Every change accounted for.\",\n  periods: suppliedPeriods = PERIODS,\n  defaultPeriod = 1,\n  height = 280,\n  formatValue = FORMAT,\n  className,\n  style,\n  ...props\n}: RevenueBridgeChartProps) {\n  const pal = PALETTES[theme];\n  const [periodIndex, setPeriodIndex] = React.useState(defaultPeriod);\n  const [active, setActive] = React.useState<number | null>(null);\n  const refs = React.useRef<(SVGGElement | null)[]>([]);\n  const periods = React.useMemo(\n    () =>\n      suppliedPeriods.filter(\n        (period) =>\n          Number.isFinite(period.start) &&\n          period.steps.every((step) => Number.isFinite(step.value)) &&\n          Number.isFinite(\n            period.steps.reduce(\n              (total, step) => total + step.value,\n              period.start,\n            ),\n          ),\n      ),\n    [suppliedPeriods],\n  );\n  const selectedIndex = Number.isInteger(periodIndex)\n    ? Math.max(0, Math.min(periods.length - 1, periodIndex))\n    : 0;\n  const period = periods[selectedIndex];\n  const bars = period ? buildBridge(period) : [];\n  const closing = bars.at(-1)?.value ?? 0;\n  const net = period ? closing - period.start : 0;\n  const delta = period?.start ? (net / Math.abs(period.start)) * 100 : null;\n  const plotHeight = Number.isFinite(height)\n    ? Math.max(220, Math.min(500, height))\n    : 280;\n  const bottom = plotHeight - 36;\n  const low = Math.min(0, ...bars.flatMap((b) => [b.from, b.to])),\n    high = Math.max(0, ...bars.flatMap((b) => [b.from, b.to]));\n  const span = high - low || 1,\n    lower = low - span * (low < 0 ? 0.12 : 0),\n    upper = high + span * 0.18;\n  const y = (value: number) =>\n    bottom - ((value - lower) / (upper - lower)) * (bottom - 18);\n  const width = Math.max(760, bars.length * 90);\n  const stepWidth = (width - 84) / Math.max(1, bars.length),\n    barWidth = Math.min(66, stepWidth * 0.62);\n  const x = (index: number) => 64 + (index + 0.5) * stepWidth;\n  const signed = (value: number) =>\n    `${value > 0 ? \"+\" : \"\"}${formatValue(value)}`;\n  const inspected =\n    active !== null ? bars[Math.min(active, bars.length - 1)] : null;\n  return (\n    <section\n      className={`w-full rounded-2xl p-5 sm:p-6 ${className ?? \"\"}`}\n      style={{\n        background: pal.bg,\n        boxShadow: `inset 0 0 0 1px ${pal.border}`,\n        color: pal.text,\n        fontFamily: FONT,\n        ...style,\n      }}\n      {...props}\n    >\n      <div className=\"flex flex-wrap items-start justify-between gap-x-8 gap-y-4\">\n        <div>\n          <h3 className=\"m-0 text-[17px] font-[590] tracking-[-0.012em]\">\n            {title}\n          </h3>\n          <p className=\"m-0 mt-1 text-[13px]\" style={{ color: pal.sub }}>\n            {subtitle}\n          </p>\n        </div>\n        <div className=\"flex flex-wrap gap-x-7 gap-y-3\">\n          <Stat\n            value={period ? formatValue(period.start) : \"—\"}\n            label=\"Opening MRR\"\n            pal={pal}\n          />\n          <Stat\n            value={period ? formatValue(closing) : \"—\"}\n            label=\"Closing MRR\"\n            pal={pal}\n          />\n          <Stat\n            value={period ? signed(net) : \"—\"}\n            label={\n              delta === null\n                ? \"Net change\"\n                : `${delta > 0 ? \"+\" : \"\"}${delta.toFixed(1)}% vs opening`\n            }\n            pal={pal}\n          />\n        </div>\n      </div>\n      {!period ? (\n        <p className=\"py-16 text-center text-sm\" style={{ color: pal.sub }}>\n          No valid revenue data available.\n        </p>\n      ) : (\n        <div className=\"mt-5 overflow-x-auto\">\n          <svg\n            viewBox={`0 0 ${width} ${plotHeight}`}\n            className=\"block w-full min-w-[660px]\"\n            role=\"group\"\n            aria-label={`${period.label} revenue bridge: opening ${formatValue(period.start)}, closing ${formatValue(closing)}, net change ${signed(net)}. Use arrow keys to inspect movements.`}\n            onPointerLeave={() => setActive(null)}\n          >\n            {Array.from({ length: 5 }, (_, i) => {\n              const value = lower + ((upper - lower) * i) / 4;\n              return (\n                <g key={i}>\n                  <line\n                    x1=\"64\"\n                    x2={width - 20}\n                    y1={y(value)}\n                    y2={y(value)}\n                    stroke={pal.grid}\n                    strokeDasharray=\"2 5\"\n                  />\n                  <text\n                    x=\"54\"\n                    y={y(value) + 4}\n                    textAnchor=\"end\"\n                    fill={pal.sub}\n                    fontSize=\"10\"\n                  >\n                    {formatValue(value)}\n                  </text>\n                </g>\n              );\n            })}\n            <line\n              x1=\"64\"\n              x2={width - 20}\n              y1={y(0)}\n              y2={y(0)}\n              stroke={pal.border}\n            />\n            {bars.map((bar, i) => {\n              const color = bar.total\n                ? pal.accent\n                : bar.value >= 0\n                  ? pal.positive\n                  : pal.negative;\n              const top = Math.min(y(bar.from), y(bar.to)),\n                h = Math.max(1, Math.abs(y(bar.from) - y(bar.to)));\n              return (\n                <g\n                  key={`${bar.label}-${i}`}\n                  ref={(element) => {\n                    refs.current[i] = element;\n                  }}\n                  role=\"button\"\n                  tabIndex={0}\n                  aria-label={`${bar.label}: ${bar.total ? formatValue(bar.value) : signed(bar.value)}${bar.total ? \"\" : `, from ${formatValue(bar.from)} to ${formatValue(bar.to)}`}`}\n                  className=\"cursor-pointer outline-offset-2 focus-visible:outline-2\"\n                  onFocus={() => setActive(i)}\n                  onBlur={() => setActive(null)}\n                  onPointerEnter={() => setActive(i)}\n                  onClick={() => setActive(i)}\n                  onKeyDown={(event) => {\n                    if (event.key === \"Escape\") {\n                      setActive(null);\n                      return;\n                    }\n                    if (\n                      ![\n                        \"ArrowLeft\",\n                        \"ArrowRight\",\n                        \"Home\",\n                        \"End\",\n                        \"Enter\",\n                        \" \",\n                      ].includes(event.key)\n                    )\n                      return;\n                    event.preventDefault();\n                    if (event.key === \"Enter\" || event.key === \" \") {\n                      setActive(i);\n                      return;\n                    }\n                    const next =\n                      event.key === \"Home\"\n                        ? 0\n                        : event.key === \"End\"\n                          ? bars.length - 1\n                          : Math.max(\n                              0,\n                              Math.min(\n                                bars.length - 1,\n                                i + (event.key === \"ArrowRight\" ? 1 : -1),\n                              ),\n                            );\n                    refs.current[next]?.focus();\n                  }}\n                >\n                  {i < bars.length - 1 && (\n                    <line\n                      x1={x(i) + barWidth / 2}\n                      x2={x(i + 1) - barWidth / 2}\n                      y1={y(bar.to)}\n                      y2={y(bar.to)}\n                      stroke={pal.sub}\n                      strokeDasharray=\"3 4\"\n                      strokeOpacity=\"0.4\"\n                    />\n                  )}\n                  <rect\n                    x={x(i) - barWidth / 2}\n                    y={top}\n                    width={barWidth}\n                    height={h}\n                    rx=\"3\"\n                    fill={color}\n                    fillOpacity={\n                      active === null || active === i\n                        ? bar.total\n                          ? 0.78\n                          : 0.55\n                        : 0.24\n                    }\n                    stroke={active === i ? pal.text : color}\n                    strokeOpacity={active === i ? 0.8 : 0.4}\n                  />\n                  <text\n                    x={x(i)}\n                    y={top - 8}\n                    textAnchor=\"middle\"\n                    fill={\n                      active === null || active === i ? pal.soft : pal.faint\n                    }\n                    fontSize=\"10\"\n                    fontFamily=\"monospace\"\n                  >\n                    {bar.total ? formatValue(bar.value) : signed(bar.value)}\n                  </text>\n                  <text\n                    x={x(i)}\n                    y={plotHeight - 10}\n                    textAnchor=\"middle\"\n                    fill={active === i ? pal.text : pal.sub}\n                    fontSize=\"10\"\n                  >\n                    {bar.label}\n                  </text>\n                </g>\n              );\n            })}\n          </svg>\n        </div>\n      )}\n      <div\n        className=\"mt-3 flex min-h-10 flex-wrap items-center justify-between gap-3 border-t pt-3 text-[11px]\"\n        style={{ borderColor: pal.border, color: pal.sub }}\n      >\n        <p className=\"m-0\" aria-live=\"polite\">\n          {inspected ? (\n            <>\n              <span style={{ color: pal.text }}>{inspected.label}</span> ·{\" \"}\n              {inspected.total\n                ? formatValue(inspected.value)\n                : `${signed(inspected.value)} · ${formatValue(inspected.from)} → ${formatValue(inspected.to)}`}\n            </>\n          ) : (\n            \"Hover or focus a bar to trace the balance\"\n          )}\n        </p>\n        <span>Closing = opening + all movements</span>\n      </div>\n      <div className=\"mt-3 flex flex-wrap items-center justify-between gap-3\">\n        <Pills\n          label=\"Period\"\n          options={periods.map((p) => p.label)}\n          value={selectedIndex}\n          onChange={(value) => {\n            setPeriodIndex(value);\n            setActive(null);\n          }}\n          pal={pal}\n        />\n        <div\n          className=\"flex flex-wrap gap-3 text-[10px]\"\n          style={{ color: pal.sub }}\n        >\n          {[\n            [\"Balance\", pal.accent],\n            [\"Increase\", pal.positive],\n            [\"Decrease\", pal.negative],\n          ].map(([label, color]) => (\n            <span key={label} className=\"flex items-center gap-1.5\">\n              <span\n                className=\"size-2 rounded-sm\"\n                style={{ background: color }}\n              />\n              {label}\n            </span>\n          ))}\n        </div>\n      </div>\n    </section>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}