{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "share-of-total-bar",
  "title": "Share of Total Bar",
  "description": "One bar split into the parts that make up a total, with last period's split underneath and a list of each part's share, amount and change in points.",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/share-of-total-bar/share-of-total-bar.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface ShareSegment {\n  label: string;\n  /** One value per period, in the same order as periods. */\n  values: number[];\n  color?: string;\n}\n\nexport interface ShareOfTotalBarProps extends Omit<\n  React.ComponentPropsWithoutRef<\"section\">,\n  \"title\"\n> {\n  theme?: \"dark\" | \"light\";\n  title?: string;\n  subtitle?: string;\n  segments?: ShareSegment[];\n  /** Oldest first. Each period is compared with the one before it. */\n  periods?: string[];\n  /** Index of the period shown first. Defaults to the latest. */\n  defaultPeriod?: number;\n  formatValue?: (value: number) => string;\n  onPeriodChange?: (period: string) => void;\n}\n\nconst PALETTES = {\n  dark: {\n    card: \"#08090A\",\n    border: \"rgba(255,255,255,0.08)\",\n    text: \"#F7F8F8\",\n    soft: \"#D0D6E0\",\n    sub: \"#8A8F98\",\n    faint: \"#62666D\",\n    chip: \"rgba(255,255,255,0.05)\",\n    chipOn: \"rgba(255,255,255,0.1)\",\n    rowOn: \"rgba(255,255,255,0.04)\",\n    up: \"#27A644\",\n    down: \"#EB5757\",\n    colors: [\"#5E6AD2\", \"#4EA7FC\", \"#A2A9F4\", \"#62666D\", \"#3C3F44\"],\n  },\n  light: {\n    card: \"#FFFFFF\",\n    border: \"rgba(8,9,10,0.08)\",\n    text: \"#08090A\",\n    soft: \"#3C3F44\",\n    sub: \"#62666D\",\n    faint: \"#8A8F98\",\n    chip: \"rgba(8,9,10,0.04)\",\n    chipOn: \"#FFFFFF\",\n    rowOn: \"rgba(8,9,10,0.03)\",\n    up: \"#27A644\",\n    down: \"#EB5757\",\n    colors: [\"#5E6AD2\", \"#4EA7FC\", \"#A2A9F4\", \"#8A8F98\", \"#D0D6E0\"],\n  },\n};\n\ntype Palette = (typeof PALETTES)[\"dark\"];\n\nconst CARD_FONT =\n  '\"Inter Variable\", Inter, \"SF Pro Display\", -apple-system, BlinkMacSystemFont, sans-serif';\nconst EASE = \"cubic-bezier(0.22, 1, 0.36, 1)\";\nconst RING = \"outline-none focus-visible:ring-2 focus-visible:ring-[#5E6AD2]\";\n\nconst PERIODS = [\"Jul\", \"Aug\", \"Sep\", \"Oct\"];\n\nconst SEGMENTS: ShareSegment[] = [\n  { label: \"Business\", values: [82400, 88900, 94200, 103600] },\n  { label: \"Pro\", values: [71200, 72800, 74100, 76300] },\n  { label: \"Enterprise\", values: [38600, 41200, 47800, 52900] },\n  { label: \"Starter\", values: [22100, 21400, 20600, 19800] },\n  { label: \"Add-ons\", values: [9800, 10600, 11900, 12400] },\n];\n\nconst FORMAT = (n: number) =>\n  n >= 1e6\n    ? `$${(n / 1e6).toFixed(2)}M`\n    : n >= 1e3\n      ? `$${(n / 1e3).toFixed(1)}k`\n      : `$${Math.round(n)}`;\n\n/** Drawn as SVG, because a tiny border-radius circle turns lumpy on 1x screens. */\nfunction Dot({ size, color }: { size: number; color: string }) {\n  const r = size / 2;\n  return (\n    <svg\n      aria-hidden=\"true\"\n      width={size}\n      height={size}\n      viewBox={`0 0 ${size} ${size}`}\n      className=\"block shrink-0\"\n    >\n      <circle cx={r} cy={r} r={r} style={{ fill: color }} />\n    </svg>\n  );\n}\n\nfunction Stat({\n  value,\n  label,\n  pal,\n  color,\n}: {\n  value: string;\n  label: string;\n  pal: Palette;\n  color?: string;\n}) {\n  return (\n    <div className=\"min-w-0 text-left sm:text-right\">\n      <p\n        className=\"m-0 truncate text-[18px] leading-[1.2] font-[510] tracking-[-0.018em] tabular-nums\"\n        style={{ color: color ?? pal.text }}\n      >\n        {value}\n      </p>\n      <p\n        className=\"m-0 mt-0.5 text-[12px] tracking-[-0.011em] whitespace-nowrap\"\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  data,\n}: {\n  label: string;\n  options: string[];\n  value: number;\n  onChange: (index: number) => void;\n  pal: Palette;\n  data: string;\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          {...{ [`data-${data}`]: option }}\n          onClick={() => onChange(i)}\n          className={`h-7 cursor-pointer rounded-full border-0 px-3 text-[13px] font-[510] tracking-[-0.011em] whitespace-nowrap transition-colors ${RING}`}\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\nconst sharesOf = (segments: ShareSegment[], period: number) => {\n  const values = segments.map((s) => Math.max(0, s.values[period] ?? 0));\n  const total = values.reduce((a, b) => a + b, 0);\n  return {\n    total,\n    values,\n    shares: values.map((v) => (total ? (v / total) * 100 : 0)),\n  };\n};\n\nexport function ShareOfTotalBar({\n  theme = \"dark\",\n  title = \"Revenue by plan\",\n  subtitle = \"How each plan adds up to the month.\",\n  segments = SEGMENTS,\n  periods = PERIODS,\n  defaultPeriod,\n  formatValue = FORMAT,\n  onPeriodChange,\n  className,\n  style,\n  ...props\n}: ShareOfTotalBarProps) {\n  const pal = PALETTES[theme];\n  const boxRef = React.useRef<HTMLDivElement>(null);\n  const [period, setPeriod] = React.useState(\n    defaultPeriod ?? Math.max(0, periods.length - 1),\n  );\n  const [active, setActive] = React.useState<number | null>(null);\n  const [mode, setMode] = React.useState(0);\n  const [seen, setSeen] = React.useState(false);\n\n  React.useEffect(() => {\n    const el = boxRef.current;\n    if (!el) return;\n    const io = new IntersectionObserver(\n      ([entry]) => {\n        if (!entry.isIntersecting) return;\n        io.disconnect();\n        setSeen(true);\n      },\n      { threshold: 0.2 },\n    );\n    io.observe(el);\n    return () => io.disconnect();\n  }, []);\n\n  const colorOf = (i: number) =>\n    segments[i]?.color ?? pal.colors[i % pal.colors.length];\n  const now = sharesOf(segments, period);\n  const before = period > 0 ? sharesOf(segments, period - 1) : null;\n  const lead = now.shares.indexOf(Math.max(...now.shares));\n  const growth =\n    before && before.total\n      ? ((now.total - before.total) / before.total) * 100\n      : null;\n  const pts = (i: number) =>\n    before ? Math.round((now.shares[i] - before.shares[i]) * 10) / 10 : 0;\n  const pct = (v: number) => `${v.toFixed(v >= 10 ? 0 : 1)}%`;\n\n  const focus = active ?? null;\n  const readout =\n    focus === null\n      ? `${segments.length} parts make up ${formatValue(now.total)} in ${periods[period]}.`\n      : `${segments[focus].label}, ${formatValue(now.values[focus])}, ${pct(now.shares[focus])} of the total${before ? `, ${pts(focus) >= 0 ? \"+\" : \"\"}${pts(focus)} pts vs ${periods[period - 1]}` : \"\"}`;\n\n  const bar = (\n    data: ReturnType<typeof sharesOf>,\n    height: number,\n    interactive: boolean,\n  ) => (\n    <div\n      className=\"flex w-full overflow-hidden\"\n      style={{\n        height,\n        gap: interactive ? 2 : 1,\n        borderRadius: height > 10 ? 8 : 3,\n      }}\n    >\n      {segments.map((s, i) => {\n        const dim = focus !== null && focus !== i;\n        const width = seen\n          ? data.shares[i]\n          : interactive\n            ? 100 / segments.length\n            : 0;\n        const common = {\n          className: `block h-full shrink-0 transition-[flex-basis,opacity] duration-700 ${interactive ? `cursor-pointer border-0 p-0 ${RING}` : \"\"}`,\n          style: {\n            flexBasis: `calc(${width}% - ${interactive ? 2 : 1}px)`,\n            minWidth: data.shares[i] > 0 ? 2 : 0,\n            background: colorOf(i),\n            opacity:\n              !seen && interactive ? 0.15 : dim ? 0.3 : interactive ? 1 : 0.55,\n            transitionTimingFunction: EASE,\n          } as React.CSSProperties,\n        };\n        return interactive ? (\n          <button\n            key={s.label}\n            type=\"button\"\n            data-segment={s.label}\n            aria-label={`${s.label}, ${formatValue(data.values[i])}, ${pct(data.shares[i])}`}\n            onPointerEnter={() => setActive(i)}\n            onPointerLeave={() => setActive(null)}\n            onFocus={() => setActive(i)}\n            onBlur={() => setActive(null)}\n            {...common}\n          />\n        ) : (\n          <span key={s.label} aria-hidden=\"true\" {...common} />\n        );\n      })}\n    </div>\n  );\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=\"grid grid-cols-3 gap-7\">\n          <Stat value={formatValue(now.total)} label=\"Total\" pal={pal} />\n          <Stat\n            value={segments[lead] ? pct(now.shares[lead]) : \"–\"}\n            label={\n              segments[lead] ? `Largest, ${segments[lead].label}` : \"Largest\"\n            }\n            pal={pal}\n          />\n          <Stat\n            value={\n              growth === null\n                ? \"–\"\n                : `${growth >= 0 ? \"+\" : \"\"}${growth.toFixed(1)}%`\n            }\n            label={before ? `vs ${periods[period - 1]}` : \"First period\"}\n            pal={pal}\n            color={\n              growth === null ? undefined : growth >= 0 ? pal.up : pal.down\n            }\n          />\n        </div>\n      </div>\n\n      <div\n        ref={boxRef}\n        className=\"mt-6\"\n        role=\"group\"\n        aria-label={`${title}, ${periods[period]}`}\n      >\n        {bar(now, 40, true)}\n        <div className=\"mt-2 flex items-center gap-2\">\n          <span\n            className=\"block h-4 w-7 shrink-0 text-[11px] leading-[16px]\"\n            style={{ color: pal.faint }}\n          >\n            {before ? periods[period - 1] : \"\"}\n          </span>\n          <div className=\"min-w-0 flex-1\" style={{ opacity: before ? 1 : 0 }}>\n            {bar(before ?? now, 6, false)}\n          </div>\n        </div>\n        <p\n          aria-live=\"polite\"\n          className=\"m-0 mt-3 h-5 truncate text-[12.5px] leading-[20px] tracking-[-0.008em]\"\n          style={{ color: focus === null ? pal.sub : pal.soft }}\n        >\n          {readout}\n        </p>\n      </div>\n\n      <ul className=\"m-0 mt-3 list-none p-0\" aria-label=\"Parts\">\n        {segments.map((s, i) => {\n          const on = focus === i;\n          const delta = pts(i);\n          return (\n            <li\n              key={s.label}\n              data-row={s.label}\n              tabIndex={0}\n              onPointerEnter={() => setActive(i)}\n              onPointerLeave={() => setActive(null)}\n              onFocus={() => setActive(i)}\n              onBlur={() => setActive(null)}\n              className={`grid h-9 grid-cols-[minmax(0,1fr)_64px_52px] items-center gap-3 rounded-md px-2 text-[13px] leading-[20px] tracking-[-0.011em] transition-opacity duration-200 sm:grid-cols-[minmax(0,1fr)_88px_64px_72px] ${RING}`}\n              style={{\n                background: on ? pal.rowOn : \"transparent\",\n                borderTop: i ? `1px solid ${pal.border}` : \"none\",\n                opacity: focus !== null && !on ? 0.5 : 1,\n              }}\n            >\n              <span className=\"flex min-w-0 items-center gap-2\">\n                <Dot size={8} color={colorOf(i)} />\n                <span\n                  className=\"truncate font-[510]\"\n                  style={{ color: pal.text }}\n                >\n                  {s.label}\n                </span>\n              </span>\n              <span\n                className=\"text-right tabular-nums\"\n                style={{ color: pal.soft }}\n              >\n                {mode === 0 ? pct(now.shares[i]) : formatValue(now.values[i])}\n              </span>\n              <span\n                className=\"hidden text-right tabular-nums sm:block\"\n                style={{ color: pal.sub }}\n              >\n                {mode === 0 ? formatValue(now.values[i]) : pct(now.shares[i])}\n              </span>\n              <span\n                className=\"text-right text-[12px] tabular-nums\"\n                style={{\n                  color:\n                    !before || delta === 0\n                      ? pal.faint\n                      : delta > 0\n                        ? pal.up\n                        : pal.down,\n                }}\n              >\n                {!before ? \"–\" : `${delta > 0 ? \"+\" : \"\"}${delta} pts`}\n              </span>\n            </li>\n          );\n        })}\n      </ul>\n\n      <div className=\"mt-4 flex flex-wrap items-center justify-between gap-2\">\n        <Pills\n          label=\"Month\"\n          options={periods}\n          value={period}\n          data=\"period\"\n          onChange={(i) => {\n            setPeriod(i);\n            onPeriodChange?.(periods[i]);\n          }}\n          pal={pal}\n        />\n        <Pills\n          label=\"Show first\"\n          options={[\"Share\", \"Amount\"]}\n          value={mode}\n          data=\"mode\"\n          onChange={setMode}\n          pal={pal}\n        />\n      </div>\n    </section>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}