{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "diverging-sentiment-chart",
  "title": "Diverging Sentiment Chart",
  "description": "Five-point survey results around a zero line: negative opens left, positive opens right, neutral sits across the middle, and topics sort themselves by net score.",
  "dependencies": [],
  "files": [
    {
      "path": "registry/new-york/diverging-sentiment-chart/diverging-sentiment-chart.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\n/** Shares in percent: very negative, negative, neutral, positive, very positive. */\nexport type SentimentSplit = [number, number, number, number, number];\n\nexport interface SentimentTopic {\n  label: string;\n  /** One split per period, in the same order as periods. */\n  splits: SentimentSplit[];\n}\n\nexport interface DivergingSentimentChartProps extends Omit<\n  React.ComponentPropsWithoutRef<\"section\">,\n  \"title\"\n> {\n  theme?: \"dark\" | \"light\";\n  title?: string;\n  subtitle?: string;\n  topics?: SentimentTopic[];\n  periods?: string[];\n  /** Index of the period shown first. Defaults to the latest. */\n  defaultPeriod?: number;\n  /** Names of the five levels, from most negative to most positive. */\n  levels?: SentimentLevels;\n  /** Colour of the positive side. */\n  positive?: string;\n  /** Colour of the negative side. */\n  negative?: string;\n  /** Height of each row, in px. */\n  rowHeight?: number;\n  onPeriodChange?: (period: string) => void;\n}\n\nexport type SentimentLevels = [string, string, string, string, string];\n\nconst PALETTES = {\n  dark: {\n    card: \"#08090A\",\n    surface: \"#0F1011\",\n    border: \"rgba(255,255,255,0.08)\",\n    grid: \"rgba(255,255,255,0.05)\",\n    text: \"#F7F8F8\",\n    soft: \"#D0D6E0\",\n    sub: \"#8A8F98\",\n    faint: \"#62666D\",\n    tip: \"#161718\",\n    tipShadow: \"0 4px 24px rgba(0,0,0,0.4)\",\n    chip: \"rgba(255,255,255,0.05)\",\n    chipOn: \"rgba(255,255,255,0.1)\",\n    accent: \"#5E6AD2\",\n    warm: \"#FC7840\",\n  },\n  light: {\n    card: \"#FFFFFF\",\n    surface: \"#F7F8F8\",\n    border: \"rgba(8,9,10,0.08)\",\n    grid: \"rgba(8,9,10,0.06)\",\n    text: \"#08090A\",\n    soft: \"#3C3F44\",\n    sub: \"#62666D\",\n    faint: \"#8A8F98\",\n    tip: \"#FFFFFF\",\n    tipShadow: \"0 3px 12px rgba(8,9,10,0.09)\",\n    chip: \"rgba(8,9,10,0.04)\",\n    chipOn: \"#FFFFFF\",\n    accent: \"#5E6AD2\",\n    warm: \"#FC7840\",\n  },\n};\n\ntype Palette = (typeof PALETTES)[\"dark\"];\n\nconst CARD_FONT =\n  '\"Inter Variable\", Inter, \"SF Pro Display\", -apple-system, BlinkMacSystemFont, sans-serif';\n\n/** The chart is drawn at its real width, so its text stays the same size on a phone. */\nfunction useWidth(ref: React.RefObject<HTMLElement | null>) {\n  const [width, setWidth] = React.useState(700);\n  React.useEffect(() => {\n    const el = ref.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  }, [ref]);\n  return width;\n}\n\nfunction Stat({\n  value,\n  label,\n  pal,\n}: {\n  value: string;\n  label: string;\n  pal: Palette;\n}) {\n  return (\n    <div className=\"text-left sm:text-right\">\n      <p className=\"m-0 text-[18px] leading-[1.2] font-[510] tracking-[-0.018em] tabular-nums\">\n        {value}\n      </p>\n      <p\n        className=\"m-0 mt-0.5 text-[12px] tracking-[-0.011em]\"\n        style={{ color: pal.sub }}\n      >\n        {label}\n      </p>\n    </div>\n  );\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: (index: number) => void;\n  pal: Palette;\n}) {\n  return (\n    <div\n      className=\"flex rounded-full p-0.5\"\n      style={{ background: pal.chip }}\n      role=\"radiogroup\"\n      aria-label={label}\n    >\n      {options.map((option, i) => (\n        <button\n          key={option}\n          type=\"button\"\n          role=\"radio\"\n          aria-checked={i === value}\n          onClick={() => onChange(i)}\n          className=\"h-7 cursor-pointer rounded-full px-3 text-[13px] font-[510] tracking-[-0.011em] transition-colors outline-none focus-visible:ring-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\nfunction Tip({\n  x,\n  y,\n  width,\n  pal,\n  children,\n}: {\n  x: number;\n  y: number;\n  width: number;\n  pal: Palette;\n  children: React.ReactNode;\n}) {\n  return (\n    <div\n      aria-hidden=\"true\"\n      className=\"pointer-events-none absolute z-10 -translate-x-1/2 rounded-lg px-2.5 py-1.5 text-[12px] tracking-[-0.011em] whitespace-nowrap\"\n      style={{\n        left: Math.max(110, Math.min(width - 110, x)),\n        top: Math.max(0, y - 42),\n        background: pal.tip,\n        color: pal.sub,\n        boxShadow: `inset 0 0 0 1px ${pal.border}, ${pal.tipShadow}`,\n      }}\n    >\n      {children}\n    </div>\n  );\n}\n\nconst PERIODS = [\"Last survey\", \"This survey\"];\n\nconst TOPICS: SentimentTopic[] = [\n  {\n    label: \"Onboarding\",\n    splits: [\n      [6, 14, 22, 38, 20],\n      [3, 9, 18, 41, 29],\n    ],\n  },\n  {\n    label: \"Support\",\n    splits: [\n      [4, 10, 18, 40, 28],\n      [4, 11, 20, 39, 26],\n    ],\n  },\n  {\n    label: \"Reliability\",\n    splits: [\n      [12, 22, 24, 30, 12],\n      [5, 13, 22, 38, 22],\n    ],\n  },\n  {\n    label: \"Docs\",\n    splits: [\n      [8, 18, 30, 32, 12],\n      [8, 16, 28, 34, 14],\n    ],\n  },\n  {\n    label: \"Mobile app\",\n    splits: [\n      [10, 20, 28, 30, 12],\n      [18, 28, 24, 22, 8],\n    ],\n  },\n  {\n    label: \"Pricing\",\n    splits: [\n      [20, 30, 24, 20, 6],\n      [16, 26, 28, 22, 8],\n    ],\n  },\n];\n\nconst LEVELS: SentimentLevels = [\n  \"Very negative\",\n  \"Negative\",\n  \"Neutral\",\n  \"Positive\",\n  \"Very positive\",\n];\n\nconst EASE = \"cubic-bezier(0.22, 1, 0.36, 1)\";\nconst net = (s: SentimentSplit) => s[3] + s[4] - s[0] - s[1];\nconst signed = (n: number) =>\n  `${n > 0 ? \"+\" : n < 0 ? \"−\" : \"\"}${Math.abs(Math.round(n))}`;\n\nexport function DivergingSentimentChart({\n  theme = \"dark\",\n  title = \"Customer sentiment\",\n  subtitle = \"How people feel about each part of the product.\",\n  topics = TOPICS,\n  periods = PERIODS,\n  defaultPeriod,\n  levels = LEVELS,\n  positive,\n  negative,\n  rowHeight = 38,\n  onPeriodChange,\n  className,\n  style,\n  ...props\n}: DivergingSentimentChartProps) {\n  const pal = PALETTES[theme];\n  const good = positive ?? pal.accent;\n  const bad = negative ?? pal.warm;\n  const boxRef = React.useRef<HTMLDivElement>(null);\n  const width = useWidth(boxRef);\n  const [period, setPeriod] = React.useState(\n    defaultPeriod ?? Math.max(0, periods.length - 1),\n  );\n  const [active, setActive] = React.useState<{\n    topic: number;\n    level: number;\n  } | null>(null);\n\n  const split = (t: SentimentTopic) =>\n    t.splits[period] ?? ([0, 0, 100, 0, 0] as SentimentSplit);\n  const order = topics\n    .map((t, i) => ({ i, score: net(split(t)) }))\n    .sort((a, b) => b.score - a.score);\n  const ranks = new Array<number>(topics.length);\n  order.forEach((o, rank) => {\n    ranks[o.i] = rank;\n  });\n\n  // One symmetric scale across every period, on round numbers, so rows only move when their numbers do.\n  const reach = Math.max(\n    10,\n    ...topics.flatMap((t) =>\n      t.splits.flatMap((s) => [s[0] + s[1] + s[2] / 2, s[3] + s[4] + s[2] / 2]),\n    ),\n  );\n  const scale = Math.ceil(reach / 20) * 20;\n  const at = (share: number) => 50 + (share / scale) * 50;\n  const fills = [\n    { color: bad, opacity: 1 },\n    { color: bad, opacity: 0.5 },\n    { color: pal.sub, opacity: 0.32 },\n    { color: good, opacity: 0.55 },\n    { color: good, opacity: 1 },\n  ];\n  const overall =\n    topics.reduce((n, t) => n + net(split(t)), 0) / Math.max(1, topics.length);\n  const best = topics[order[0]?.i];\n  const worst = topics[order[order.length - 1]?.i];\n  const picked = active && topics[active.topic];\n  const pickedShare = active && picked ? split(picked)[active.level] : 0;\n  const labelWidth = width < 460 ? 76 : 92;\n  const plotWidth = width - labelWidth - 44 - 16;\n\n  return (\n    <section\n      className={`w-full rounded-2xl p-5 sm:p-6 ${className ?? \"\"}`}\n      style={{\n        background: pal.card,\n        boxShadow: `inset 0 0 0 1px ${pal.border}`,\n        color: pal.text,\n        fontFamily: CARD_FONT,\n        ...style,\n      }}\n      {...props}\n    >\n      <div className=\"flex flex-wrap items-start justify-between gap-x-8 gap-y-3\">\n        <div>\n          <h3 className=\"m-0 text-[17px] leading-[1.33] font-[590] tracking-[-0.012em]\">\n            {title}\n          </h3>\n          <p\n            className=\"m-0 mt-0.5 text-[13px] tracking-[-0.011em]\"\n            style={{ color: pal.sub }}\n          >\n            {subtitle}\n          </p>\n        </div>\n        <div className=\"flex gap-7\">\n          <Stat value={signed(overall)} label=\"Net score\" pal={pal} />\n          <Stat value={best?.label ?? \"—\"} label=\"Most loved\" pal={pal} />\n          <Stat value={worst?.label ?? \"—\"} label=\"Needs work\" pal={pal} />\n        </div>\n      </div>\n\n      {!topics.length ? (\n        <p className=\"py-16 text-center text-sm\" style={{ color: pal.sub }}>\n          No data available.\n        </p>\n      ) : (\n        <div ref={boxRef} className=\"relative mt-5\">\n          <div\n            className=\"relative\"\n            style={{ height: topics.length * rowHeight }}\n          >\n            {/* The zero line sits under the rows, in the middle of the plot. */}\n            <span\n              aria-hidden=\"true\"\n              className=\"absolute inset-y-0 w-px\"\n              style={{\n                left: labelWidth + 8 + plotWidth / 2,\n                background: pal.border,\n              }}\n            />\n            {topics.map((t, i) => {\n              const s = split(t);\n              let left = -(s[0] + s[1] + s[2] / 2);\n              const score = net(s);\n              return (\n                <div\n                  key={t.label}\n                  className=\"absolute inset-x-0 top-0 flex items-center gap-2 motion-safe:transition-transform motion-safe:duration-700\"\n                  style={{\n                    height: rowHeight,\n                    transform: `translateY(${ranks[i] * rowHeight}px)`,\n                    transitionTimingFunction: EASE,\n                  }}\n                >\n                  <span\n                    className=\"shrink-0 truncate text-[13px] font-[510] tracking-[-0.011em]\"\n                    style={{ width: labelWidth, color: pal.soft }}\n                  >\n                    {t.label}\n                  </span>\n                  <span\n                    className=\"relative h-5 min-w-0 flex-1\"\n                    role=\"img\"\n                    aria-label={`${t.label}: ${levels.map((name, k) => `${name} ${s[k]}%`).join(\", \")}`}\n                  >\n                    {s.map((share, k) => {\n                      const start = left;\n                      left += share;\n                      const on = active?.topic === i && active.level === k;\n                      return (\n                        <span\n                          key={k}\n                          onPointerEnter={() =>\n                            setActive({ topic: i, level: k })\n                          }\n                          onPointerLeave={() => setActive(null)}\n                          className=\"absolute inset-y-0 motion-safe:transition-[left,width,opacity] motion-safe:duration-700\"\n                          style={{\n                            left: `${at(start)}%`,\n                            width: `calc(${(share / scale) * 50}% - 1px)`,\n                            background: fills[k].color,\n                            opacity:\n                              active && !on\n                                ? fills[k].opacity * 0.5\n                                : fills[k].opacity,\n                            borderRadius:\n                              k === 0\n                                ? \"4px 0 0 4px\"\n                                : k === 4\n                                  ? \"0 4px 4px 0\"\n                                  : 0,\n                            transitionTimingFunction: EASE,\n                          }}\n                        />\n                      );\n                    })}\n                  </span>\n                  <span\n                    className=\"w-9 shrink-0 text-right text-[13px] font-[510] tabular-nums\"\n                    style={{ color: score < 0 ? bad : pal.text }}\n                  >\n                    {signed(score)}\n                  </span>\n                </div>\n              );\n            })}\n          </div>\n\n          <div\n            aria-hidden=\"true\"\n            className=\"relative mt-1 h-4 text-[11px] tabular-nums\"\n            style={{\n              color: pal.faint,\n              marginLeft: labelWidth + 8,\n              width: plotWidth,\n            }}\n          >\n            {[-scale, -scale / 2, 0, scale / 2, scale].map((v) => (\n              <span\n                key={v}\n                className=\"absolute top-0 -translate-x-1/2\"\n                style={{ left: `${at(v)}%` }}\n              >\n                {Math.abs(v)}%\n              </span>\n            ))}\n          </div>\n\n          {picked && active && (\n            <Tip\n              x={\n                labelWidth +\n                8 +\n                (plotWidth *\n                  at(\n                    -(\n                      split(picked)[0] +\n                      split(picked)[1] +\n                      split(picked)[2] / 2\n                    ) +\n                      split(picked)\n                        .slice(0, active.level)\n                        .reduce((n, v) => n + v, 0) +\n                      pickedShare / 2,\n                  )) /\n                  100\n              }\n              y={ranks[active.topic] * rowHeight + 4}\n              width={width}\n              pal={pal}\n            >\n              <span className=\"font-[510]\" style={{ color: pal.text }}>\n                {pickedShare}%\n              </span>{\" \"}\n              {levels[active.level].toLowerCase()} on {picked.label}\n            </Tip>\n          )}\n        </div>\n      )}\n\n      <div className=\"mt-3 flex flex-wrap items-center justify-between gap-2\">\n        <Pills\n          label=\"Survey\"\n          options={periods}\n          value={period}\n          onChange={(i) => {\n            setPeriod(i);\n            onPeriodChange?.(periods[i]);\n          }}\n          pal={pal}\n        />\n        <div\n          className=\"flex items-center gap-1 text-[12px]\"\n          style={{ color: pal.sub }}\n        >\n          <span className=\"mr-1\">{levels[1]}</span>\n          {fills.map((f, k) => (\n            <span\n              key={k}\n              className=\"h-2.5 w-3.5 rounded-[3px]\"\n              style={{ background: f.color, opacity: f.opacity }}\n            />\n          ))}\n          <span className=\"ml-1\">{levels[3]}</span>\n        </div>\n      </div>\n    </section>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}