{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "breakdown",
  "title": "Breakdown",
  "description": "Primitives for part-to-whole breakdowns: a segmented 100% bar, legend keys and share formatting.",
  "registryDependencies": [
    "https://www.dashboardblocks.com/r/radix/chart-panel.json",
    "https://www.dashboardblocks.com/r/radix/table.json",
    "https://www.dashboardblocks.com/r/radix/use-reveal.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/breakdown.tsx",
      "content": "'use client'\n\nimport { useBlockState } from '@/registry/components/dashboardblocks/block-state'\nimport {\n  ChartPanelKey,\n  ChartPanelLegend,\n} from '@/registry/components/dashboardblocks/chart-panel'\nimport {\n  DataTable,\n  DataTableBody,\n  DataTableCell,\n  DataTableHead,\n  DataTableHeader,\n  DataTableRow,\n  DataTableRowHead,\n} from '@/registry/components/dashboardblocks/table'\nimport { useReveal } from '@/registry/hooks/use-reveal'\nimport { formatNumber, formatPercent } from '@/registry/lib/dashboardblocks/format'\n\nimport { cn } from '@/lib/utils'\n\ninterface BreakdownSegment {\n  color: string\n  label: string\n  value: number\n}\n\nconst BREAKDOWN_OTHER_COLOR =\n  'color-mix(in oklab, var(--color-muted-foreground) 35%, transparent)'\n\nfunction getTotal(segments: BreakdownSegment[]) {\n  return segments.reduce((sum, segment) => sum + Math.max(0, segment.value), 0)\n}\n\nfunction formatShare(value: number, total: number, fractionDigits = 0) {\n  return formatPercent(total > 0 ? value / total : 0, {\n    maximumFractionDigits: fractionDigits,\n    minimumFractionDigits: fractionDigits,\n  })\n}\n\ninterface BreakdownBarProps {\n  animated?: boolean\n  className?: string\n  segments: BreakdownSegment[]\n}\n\nfunction BreakdownBar({ animated = true, className, segments }: BreakdownBarProps) {\n  const { ref, revealed } = useReveal<HTMLDivElement>(animated)\n  const state = useBlockState()\n  const total = getTotal(segments)\n\n  return (\n    <div\n      ref={ref}\n      aria-hidden\n      className={cn(\n        'flex h-3 w-full gap-0.5 overflow-hidden rounded-full',\n        state && 'bg-muted',\n        state === 'loading' && 'animate-pulse motion-reduce:animate-none',\n        className,\n      )}\n    >\n      {!state &&\n        segments.map((segment) => {\n          const share = total > 0 ? Math.max(0, segment.value) / total : 0\n          return (\n            <div\n              key={segment.label}\n              className='h-full min-w-0 transition-[flex-grow] duration-1000 ease-[cubic-bezier(0.33,1,0.68,1)] motion-reduce:transition-none'\n              style={{\n                backgroundColor: segment.color,\n                flexBasis: 0,\n                flexGrow: revealed ? share : 0,\n              }}\n            />\n          )\n        })}\n    </div>\n  )\n}\n\ninterface BreakdownKeyProps {\n  className?: string\n  color: string\n}\n\nfunction BreakdownKey({ className, color }: BreakdownKeyProps) {\n  return <ChartPanelKey className={className} color={color} />\n}\n\ninterface BreakdownLegendProps {\n  className?: string\n  items: Pick<BreakdownSegment, 'color' | 'label'>[]\n}\n\nfunction BreakdownLegend({ className, items }: BreakdownLegendProps) {\n  return <ChartPanelLegend className={className} items={items} />\n}\n\ninterface BreakdownTableProps {\n  caption: string\n  /** @default formatNumber */\n  format?: (value: number) => string\n  highlightIndex?: number | null\n  /** Names the columns for screen readers. @default ['Category', 'Value', 'Share'] */\n  labels?: [string, string, string]\n  segments: BreakdownSegment[]\n}\n\nfunction BreakdownTable({\n  caption,\n  format = (value) => formatNumber(value),\n  highlightIndex = null,\n  labels = ['Category', 'Value', 'Share'],\n  segments,\n}: BreakdownTableProps) {\n  const total = getTotal(segments)\n\n  return (\n    <DataTable density='compact' caption={caption}>\n      <DataTableHeader className='sr-only'>\n        <DataTableRow>\n          <DataTableHead>{labels[0]}</DataTableHead>\n          <DataTableHead align='end'>{labels[1]}</DataTableHead>\n          <DataTableHead align='end'>{labels[2]}</DataTableHead>\n        </DataTableRow>\n      </DataTableHeader>\n      <DataTableBody>\n        {segments.map((segment, index) => (\n          <DataTableRow key={segment.label} highlighted={highlightIndex === index}>\n            <DataTableRowHead>\n              <span className='flex items-center gap-2'>\n                <BreakdownKey color={segment.color} />\n                {segment.label}\n              </span>\n            </DataTableRowHead>\n            <DataTableCell align='end'>{format(segment.value)}</DataTableCell>\n            <DataTableCell align='end' className='text-muted-foreground w-16'>\n              {formatShare(segment.value, total)}\n            </DataTableCell>\n          </DataTableRow>\n        ))}\n      </DataTableBody>\n    </DataTable>\n  )\n}\n\nexport {\n  BREAKDOWN_OTHER_COLOR,\n  BreakdownBar,\n  BreakdownKey,\n  BreakdownLegend,\n  BreakdownTable,\n  formatShare,\n  getTotal,\n}\n\nexport type { BreakdownBarProps, BreakdownSegment, BreakdownTableProps }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/breakdown.tsx"
    }
  ],
  "type": "registry:component"
}
