{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "distribution-card",
  "title": "Distribution Card",
  "description": "A total with delta, a stacked share bar, and a ranked list of the contributors with their values.",
  "dependencies": [
    "lucide-react"
  ],
  "registryDependencies": [
    "card",
    "button",
    "dropdown-menu",
    "tooltip"
  ],
  "files": [
    {
      "path": "registry/dashboardcn/blocks/distribution-card.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { ChevronDown } from \"lucide-react\"\n\nimport { cn } from \"@/lib/utils\"\nimport { type NumberFormat } from \"@/registry/dashboardcn/lib/format\"\nimport { Button } from \"@/components/ui/button\"\nimport { Card, CardContent, CardHeader, CardTitle } from \"@/components/ui/card\"\nimport {\n  DropdownMenu,\n  DropdownMenuContent,\n  DropdownMenuRadioGroup,\n  DropdownMenuRadioItem,\n  DropdownMenuTrigger,\n} from \"@/components/ui/dropdown-menu\"\nimport { DeltaBadge } from \"@/registry/dashboardcn/ui/delta-badge\"\nimport { DistributionBar } from \"@/registry/dashboardcn/ui/distribution-bar\"\nimport { MetricValue } from \"@/registry/dashboardcn/ui/metric-value\"\n\nexport interface DistributionCardItem {\n  name: string\n  value: number\n  icon?: React.ReactNode\n  /** Any CSS color. Defaults to chart-1 through chart-5 in order. */\n  color?: string\n}\n\nexport interface DistributionCardOption {\n  value: string\n  label: string\n}\n\nexport interface DistributionCardProps extends React.ComponentProps<typeof Card> {\n  title: string\n  items: DistributionCardItem[]\n  /** Headline total. Defaults to the sum of the items. */\n  total?: number\n  /** Absolute change in the total, in the same unit, e.g. 390000 for +$390k. */\n  delta?: number\n  format?: NumberFormat\n  currency?: string\n  /** Small label above each item's value, e.g. \"Capital in\". */\n  valueLabel?: string\n  /** Options for the header dropdown. Omit to hide it. */\n  options?: DistributionCardOption[]\n  value?: string\n  defaultValue?: string\n  onValueChange?: (value: string) => void\n}\n\nconst defaultColors = [\n  \"var(--chart-1)\",\n  \"var(--chart-2)\",\n  \"var(--chart-3)\",\n  \"var(--chart-4)\",\n  \"var(--chart-5)\",\n]\n\nconst compactFormatters = new Map<string, Intl.NumberFormat>()\n\n/** \"$12.4M\", or \"+$390K\" when signed. */\nfunction formatCompact(\n  value: number,\n  format: NumberFormat,\n  currency: string,\n  signed = false\n) {\n  const key = `${format}:${currency}:${signed}`\n  let formatter = compactFormatters.get(key)\n  if (!formatter) {\n    formatter = new Intl.NumberFormat(\"en-US\", {\n      ...(format === \"currency\" ? { style: \"currency\", currency } : {}),\n      notation: \"compact\",\n      signDisplay: signed ? \"exceptZero\" : \"auto\",\n      // Explicit minimum keeps Node and browsers in agreement (\"$390K\", not \"$390.0K\").\n      minimumFractionDigits: 0,\n      maximumFractionDigits: 1,\n    })\n    compactFormatters.set(key, formatter)\n  }\n  return formatter.format(value)\n}\n\n/** A total with delta, a stacked share bar, and a ranked list of the contributors. */\nfunction DistributionCard({\n  title,\n  items,\n  total,\n  delta,\n  format = \"currency\",\n  currency = \"USD\",\n  valueLabel,\n  options,\n  value,\n  defaultValue,\n  onValueChange,\n  className,\n  ...props\n}: DistributionCardProps) {\n  const [internal, setInternal] = React.useState(defaultValue ?? options?.[0]?.value ?? \"\")\n  const selected = value ?? internal\n  const selectedLabel = options?.find((option) => option.value === selected)?.label\n  const sum = total ?? items.reduce((acc, item) => acc + item.value, 0)\n  const segments = items.map((item, index) => ({\n    ...item,\n    color: item.color ?? defaultColors[index % defaultColors.length],\n  }))\n\n  return (\n    <Card data-slot=\"distribution-card\" className={cn(\"gap-0 py-0\", className)} {...props}>\n      <CardHeader className=\"flex items-center justify-between gap-3 border-b py-3 [.border-b]:pb-3\">\n        <CardTitle className=\"text-sm\">{title}</CardTitle>\n        {options?.length ? (\n          <DropdownMenu>\n              <DropdownMenuTrigger asChild>\n                <Button variant=\"outline\" size=\"sm\">\n                  {selectedLabel}\n                  <ChevronDown className=\"text-muted-foreground\" />\n                </Button>\n              </DropdownMenuTrigger>\n              <DropdownMenuContent align=\"end\">\n                <DropdownMenuRadioGroup\n                  value={selected}\n                  onValueChange={(next) => {\n                    setInternal(next)\n                    onValueChange?.(next)\n                  }}\n                >\n                  {options.map((option) => (\n                    <DropdownMenuRadioItem key={option.value} value={option.value}>\n                      {option.label}\n                    </DropdownMenuRadioItem>\n                  ))}\n                </DropdownMenuRadioGroup>\n              </DropdownMenuContent>\n            </DropdownMenu>\n        ) : null}\n      </CardHeader>\n      <CardContent className=\"flex flex-col gap-4 py-5\">\n        <div className=\"flex flex-wrap items-center gap-2\">\n          <span className=\"text-3xl font-semibold tabular-nums tracking-tight\">\n            {formatCompact(sum, format, currency)}\n          </span>\n          {delta !== undefined ? (\n            <DeltaBadge delta={delta} variant=\"soft\">\n              {formatCompact(delta, format, currency, true)}\n            </DeltaBadge>\n          ) : null}\n        </div>\n        <DistributionBar segments={segments} showLegend={false} />\n        <ul className=\"flex flex-col divide-y\">\n          {segments.map((item) => (\n            <li\n              key={item.name}\n              data-slot=\"distribution-card-item\"\n              className=\"flex items-center gap-3 py-3 last:pb-0\"\n            >\n              {item.icon ? (\n                <span className=\"bg-muted flex size-9 shrink-0 items-center justify-center rounded-lg [&>svg]:size-5\">\n                  {item.icon}\n                </span>\n              ) : (\n                <span\n                  aria-hidden=\"true\"\n                  className=\"size-2.5 shrink-0 rounded-[2px]\"\n                  style={{ backgroundColor: item.color }}\n                />\n              )}\n              <span className=\"min-w-0 flex-1 truncate text-sm font-medium\">{item.name}</span>\n              <div className=\"flex flex-col items-end\">\n                {valueLabel ? (\n                  <span className=\"text-muted-foreground text-xs\">{valueLabel}</span>\n                ) : null}\n                <MetricValue\n                  value={item.value}\n                  format={format}\n                  currency={currency}\n                  maximumFractionDigits={0}\n                  className=\"text-sm font-semibold\"\n                />\n              </div>\n            </li>\n          ))}\n        </ul>\n      </CardContent>\n    </Card>\n  )\n}\n\nexport { DistributionCard }\n",
      "type": "registry:component"
    },
    {
      "path": "registry/dashboardcn/ui/delta-badge.tsx",
      "content": "import * as React from \"react\"\nimport { Minus, TrendingDown, TrendingUp } from \"lucide-react\"\n\nimport { cn } from \"@/lib/utils\"\nimport { formatDelta } from \"@/registry/dashboardcn/lib/format\"\n\nexport type DeltaDirection = \"up\" | \"down\" | \"flat\"\n\nexport function getDeltaDirection(delta: number | undefined): DeltaDirection {\n  if (delta === undefined || delta === 0 || !Number.isFinite(delta)) return \"flat\"\n  return delta > 0 ? \"up\" : \"down\"\n}\n\nconst directionIcon: Record<DeltaDirection, React.ElementType> = {\n  up: TrendingUp,\n  down: TrendingDown,\n  flat: Minus,\n}\n\nexport interface DeltaBadgeProps extends React.ComponentProps<\"span\"> {\n  /** Fractional change, e.g. 0.124 for +12.4%. */\n  delta: number\n  /** Treat a decrease as good and an increase as bad (churn, latency, errors). */\n  invert?: boolean\n  variant?: \"outline\" | \"soft\" | \"text\"\n  showIcon?: boolean\n}\n\nfunction DeltaBadge({\n  delta,\n  invert = false,\n  variant = \"outline\",\n  showIcon = true,\n  className,\n  children,\n  ...props\n}: DeltaBadgeProps) {\n  const direction = getDeltaDirection(delta)\n  const positive = direction === \"flat\" ? null : (direction === \"up\") !== invert\n  const Icon = directionIcon[direction]\n\n  return (\n    <span\n      data-slot=\"delta-badge\"\n      data-direction={direction}\n      data-positive={positive}\n      className={cn(\n        \"inline-flex items-center gap-1 text-xs font-medium tabular-nums\",\n        variant === \"outline\" && \"rounded-md border px-1.5 py-0.5\",\n        variant === \"soft\" && \"rounded-md px-1.5 py-0.5\",\n        variant === \"soft\" && positive === true && \"bg-emerald-500/10\",\n        variant === \"soft\" && positive === false && \"bg-red-500/10\",\n        variant === \"soft\" && positive === null && \"bg-muted\",\n        positive === true && \"text-emerald-600 dark:text-emerald-400\",\n        positive === false && \"text-red-600 dark:text-red-400\",\n        positive === null && \"text-muted-foreground\",\n        className\n      )}\n      {...props}\n    >\n      {showIcon ? <Icon className=\"size-3\" aria-hidden=\"true\" /> : null}\n      {children ?? formatDelta(delta)}\n    </span>\n  )\n}\n\nexport { DeltaBadge }\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/dashboardcn/ui/distribution-bar.tsx",
      "content": "import * as React from \"react\"\n\nimport { cn } from \"@/lib/utils\"\nimport { formatNumber } from \"@/registry/dashboardcn/lib/format\"\n\nexport interface DistributionSegment {\n  name: string\n  value: number\n  /** Any CSS color. Defaults to chart-1 through chart-5 in order. */\n  color?: string\n}\n\nexport interface DistributionBarProps extends React.ComponentProps<\"div\"> {\n  segments: DistributionSegment[]\n  valueFormatter?: (value: number) => string\n  showLegend?: boolean\n  /** Show absolute values in the legend in addition to the share. */\n  showValues?: boolean\n}\n\nconst defaultColors = [\n  \"var(--chart-1)\",\n  \"var(--chart-2)\",\n  \"var(--chart-3)\",\n  \"var(--chart-4)\",\n  \"var(--chart-5)\",\n]\n\nfunction DistributionBar({\n  segments,\n  valueFormatter = (value) => formatNumber(value, { format: \"compact\" }),\n  showLegend = true,\n  showValues = false,\n  className,\n  ...props\n}: DistributionBarProps) {\n  const total = segments.reduce((sum, segment) => sum + segment.value, 0)\n  const resolved = segments.map((segment, index) => ({\n    ...segment,\n    color: segment.color ?? defaultColors[index % defaultColors.length],\n    share: total > 0 ? segment.value / total : 0,\n  }))\n\n  return (\n    <div\n      data-slot=\"distribution-bar\"\n      className={cn(\"flex flex-col gap-3\", className)}\n      {...props}\n    >\n      <div\n        role=\"img\"\n        aria-label={resolved\n          .map(\n            (segment) =>\n              `${segment.name} ${formatNumber(segment.share, { format: \"percent\" })}`\n          )\n          .join(\", \")}\n        className=\"flex h-2.5 w-full gap-0.5 overflow-hidden rounded-full\"\n      >\n        {resolved.map((segment) =>\n          segment.share > 0 ? (\n            <div\n              key={segment.name}\n              className=\"h-full transition-[width] duration-300 first:rounded-l-full last:rounded-r-full\"\n              style={{\n                width: `${segment.share * 100}%`,\n                backgroundColor: segment.color,\n              }}\n            />\n          ) : null\n        )}\n      </div>\n      {showLegend ? (\n        <ul className=\"flex flex-wrap gap-x-4 gap-y-1.5 text-sm\">\n          {resolved.map((segment) => (\n            <li key={segment.name} className=\"flex items-center gap-2\">\n              <span\n                aria-hidden=\"true\"\n                className=\"size-2.5 shrink-0 rounded-[2px]\"\n                style={{ backgroundColor: segment.color }}\n              />\n              <span className=\"text-muted-foreground\">{segment.name}</span>\n              <span className=\"font-medium tabular-nums\">\n                {formatNumber(segment.share, { format: \"percent\" })}\n              </span>\n              {showValues ? (\n                <span className=\"text-muted-foreground tabular-nums\">\n                  ({valueFormatter(segment.value)})\n                </span>\n              ) : null}\n            </li>\n          ))}\n        </ul>\n      ) : null}\n    </div>\n  )\n}\n\nexport { DistributionBar }\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/dashboardcn/ui/metric-value.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport { cn } from \"@/lib/utils\"\nimport { formatNumber, type NumberFormat } from \"@/registry/dashboardcn/lib/format\"\nimport {\n  Tooltip,\n  TooltipContent,\n  TooltipProvider,\n  TooltipTrigger,\n} from \"@/components/ui/tooltip\"\n\n/** Values at or above this are abbreviated by default. */\nexport const DEFAULT_COMPACT_FROM = 100_000\n\nexport interface MetricValueProps extends Omit<React.ComponentProps<\"span\">, \"children\"> {\n  /** Strings render as-is. */\n  value: number | string\n  format?: NumberFormat\n  currency?: string\n  maximumFractionDigits?: number\n  /** Values at or above this are abbreviated (e.g. $158K) with the full value in a tooltip. Set to Infinity to always show the full value. */\n  compactFrom?: number\n  /** Text after the number, e.g. a unit. */\n  suffix?: React.ReactNode\n}\n\n/** A formatted number. Large values are abbreviated and reveal the full value on hover. */\nfunction MetricValue({\n  value,\n  format,\n  currency,\n  maximumFractionDigits,\n  compactFrom = DEFAULT_COMPACT_FROM,\n  suffix,\n  className,\n  ...props\n}: MetricValueProps) {\n  const classes = cn(\"tabular-nums\", className)\n\n  if (typeof value !== \"number\") {\n    return (\n      <span data-slot=\"metric-value\" className={classes} {...props}>\n        {value}\n        {suffix}\n      </span>\n    )\n  }\n\n  const full = formatNumber(value, { format, currency, maximumFractionDigits })\n  const abbreviate = format !== \"percent\" && Math.abs(value) >= compactFrom\n  if (!abbreviate) {\n    return (\n      <span data-slot=\"metric-value\" className={classes} {...props}>\n        {full}\n        {suffix}\n      </span>\n    )\n  }\n\n  return (\n    <TooltipProvider>\n      <Tooltip>\n        <TooltipTrigger asChild>\n          <span\n            data-slot=\"metric-value\"\n            data-compact=\"\"\n            className={cn(classes, \"cursor-default\")}\n            {...props}\n          >\n            {formatNumber(value, { format, currency, compact: true })}\n            {suffix}\n          </span>\n        </TooltipTrigger>\n        <TooltipContent className=\"tabular-nums\">\n          {full}\n          {suffix}\n        </TooltipContent>\n      </Tooltip>\n    </TooltipProvider>\n  )\n}\n\nexport { MetricValue }\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/dashboardcn/lib/format.ts",
      "content": "export type NumberFormat = \"number\" | \"compact\" | \"currency\" | \"percent\"\n\nexport interface FormatNumberOptions {\n  format?: NumberFormat\n  /** ISO 4217 code, used when format is \"currency\". Defaults to USD. */\n  currency?: string\n  locale?: string\n  maximumFractionDigits?: number\n  /** Abbreviate large values, e.g. \"$158K\" instead of \"$158,143\". */\n  compact?: boolean\n}\n\nconst formatterCache = new Map<string, Intl.NumberFormat>()\n\nfunction getFormatter(locale: string, options: Intl.NumberFormatOptions) {\n  const key = locale + JSON.stringify(options)\n  let formatter = formatterCache.get(key)\n  if (!formatter) {\n    formatter = new Intl.NumberFormat(locale, options)\n    formatterCache.set(key, formatter)\n  }\n  return formatter\n}\n\n/**\n * Format a metric value for display.\n *\n * formatNumber(1234567)                          -> \"1,234,567\"\n * formatNumber(1234567, { format: \"compact\" })   -> \"1.2M\"\n * formatNumber(48.2, { format: \"currency\" })     -> \"$48.20\"\n * formatNumber(158143, { format: \"currency\", compact: true }) -> \"$158K\"\n * formatNumber(0.124, { format: \"percent\" })     -> \"12.4%\"\n */\nexport function formatNumber(\n  value: number,\n  {\n    format = \"number\",\n    currency = \"USD\",\n    locale = \"en-US\",\n    maximumFractionDigits,\n    compact = false,\n  }: FormatNumberOptions = {}\n): string {\n  if (!Number.isFinite(value)) return \"—\"\n\n  // Compact values keep three significant digits: $158K, $1.23M, 41.2K.\n  const compactOptions: Intl.NumberFormatOptions = compact\n    ? maximumFractionDigits === undefined\n      ? { notation: \"compact\", maximumSignificantDigits: 3 }\n      : { notation: \"compact\", maximumFractionDigits }\n    : {}\n  switch (format) {\n    case \"compact\":\n      return getFormatter(locale, {\n        notation: \"compact\",\n        maximumFractionDigits: maximumFractionDigits ?? 1,\n      }).format(value)\n    case \"currency\":\n      return getFormatter(locale, {\n        style: \"currency\",\n        currency,\n        maximumFractionDigits: maximumFractionDigits ?? 2,\n        ...compactOptions,\n      }).format(value)\n    case \"percent\":\n      return getFormatter(locale, {\n        style: \"percent\",\n        maximumFractionDigits: maximumFractionDigits ?? 1,\n      }).format(value)\n    default:\n      return getFormatter(locale, {\n        maximumFractionDigits: maximumFractionDigits ?? 0,\n        ...compactOptions,\n      }).format(value)\n  }\n}\n\n/**\n * Format a fractional change as a signed percentage.\n *\n * formatDelta(0.124)  -> \"+12.4%\"\n * formatDelta(-0.03)  -> \"-3.0%\"\n * formatDelta(0)      -> \"0.0%\"\n */\nexport function formatDelta(delta: number, locale = \"en-US\"): string {\n  if (!Number.isFinite(delta)) return \"—\"\n  return getFormatter(locale, {\n    style: \"percent\",\n    signDisplay: \"exceptZero\",\n    maximumFractionDigits: 1,\n    minimumFractionDigits: 1,\n  }).format(delta)\n}\n\n/**\n * Fractional change between two values. Returns 0 when there is no baseline.\n */\nexport function computeDelta(current: number, previous: number): number {\n  if (!previous) return 0\n  return (current - previous) / Math.abs(previous)\n}\n",
      "type": "registry:lib"
    }
  ],
  "categories": [
    "dashboard",
    "charts",
    "finance"
  ],
  "type": "registry:block"
}