{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "funnel-04",
  "description": "Two segments as lines through every stage, each as a share of its first step, with the gap between them shaded.",
  "dependencies": [
    "recharts"
  ],
  "registryDependencies": [
    "card",
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/chart-panel.json",
    "https://www.dashboardblocks.com/r/radix/funnel.json",
    "https://www.dashboardblocks.com/r/radix/kpi.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/funnel/funnel-04.tsx",
      "content": "'use client'\n\nimport {\n  BlockReady,\n  BlockStateProvider,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport {\n  chartActiveDot,\n  chartAxisProps,\n  chartGridProps,\n  chartLineCursor,\n  ChartTooltipContent,\n  chartValueAxisWidth,\n} from '@/registry/components/dashboardblocks/chart'\nimport {\n  ChartPanelFigure,\n  ChartPanelLegend,\n  ChartPanelTable,\n} from '@/registry/components/dashboardblocks/chart-panel'\nimport { formatRate } from '@/registry/components/dashboardblocks/funnel'\nimport { KPIValue } from '@/registry/components/dashboardblocks/kpi'\nimport { formatNumber } from '@/registry/lib/dashboardblocks/format'\nimport {\n  Area,\n  CartesianGrid,\n  ComposedChart,\n  LabelList,\n  Line,\n  ResponsiveContainer,\n  Tooltip,\n  XAxis,\n  YAxis,\n} from 'recharts'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\ninterface FunnelSegment {\n  color: string\n  key: string\n  label: string\n}\n\ninterface ComparedStage {\n  label: string\n  values: Record<string, number>\n}\n\ninterface Funnel4Props extends BlockStateProps {\n  description: string\n  segments: FunnelSegment[]\n  /** Leave out, or pass none, to show the empty state. */\n  stages?: ComparedStage[]\n  title: string\n}\n\nconst exampleProps: Funnel4Props = {\n  description: 'Share of visitors reaching each step, by device.',\n  segments: [\n    { color: 'var(--color-chart-2)', key: 'desktop', label: 'Desktop' },\n    { color: 'var(--color-chart-3)', key: 'mobile', label: 'Mobile' },\n  ],\n  stages: [\n    { label: 'Viewed product', values: { desktop: 18_400, mobile: 26_900 } },\n    { label: 'Added to cart', values: { desktop: 5_890, mobile: 6_190 } },\n    { label: 'Started checkout', values: { desktop: 3_310, mobile: 2_690 } },\n    { label: 'Purchased', values: { desktop: 2_020, mobile: 1_210 } },\n  ],\n  title: 'Conversion by device',\n}\n\nconst GAP_COLOR = 'color-mix(in oklab, var(--color-chart-2) 12%, transparent)'\n\nconst Funnel4 = (props: Funnel4Props) => {\n  const { description, segments, stages = [], errorMessage, status, title } = props\n  const state = getBlockState(status, stages)\n  const first = stages[0]\n  const rate = (stage: ComparedStage, key: string) => {\n    const start = first?.values[key] ?? 0\n    return start > 0 ? (stage.values[key] ?? 0) / start : 0\n  }\n  const rows = stages.map((stage) => {\n    const rates = segments.map((segment) => rate(stage, segment.key))\n    return {\n      gap: rates.length > 1 ? [Math.min(...rates), Math.max(...rates)] : null,\n      label: stage.label,\n      ...Object.fromEntries(\n        segments.map((segment, index) => [segment.key, rates[index]]),\n      ),\n    }\n  })\n  const lastStage = stages.at(-1)\n  const lastIndex = stages.length - 1\n  const topKey = lastStage\n    ? segments.reduce<string | undefined>(\n        (top, segment) =>\n          top === undefined || rate(lastStage, segment.key) > rate(lastStage, top)\n            ? segment.key\n            : top,\n        undefined,\n      )\n    : undefined\n\n  return (\n    <BlockStateProvider errorMessage={errorMessage} state={state}>\n      <Card>\n        <CardContent>\n          <section className='@container flex flex-col gap-4'>\n            <header className='flex flex-col gap-0.5'>\n              <h2 className='font-semibold tracking-tight'>{title}</h2>\n              <p className='text-muted-foreground text-sm'>{description}</p>\n            </header>\n            {state === 'empty' || state === 'error' ? (\n              <div className='border-t pt-4'>\n                <KPIValue />\n              </div>\n            ) : (\n              <dl className='flex flex-wrap items-start gap-x-10 gap-y-3 border-t pt-4'>\n                {segments.map((segment) => (\n                  <div key={segment.key} className='flex flex-col gap-1'>\n                    <dt className='text-muted-foreground text-sm'>{segment.label}</dt>\n                    <dd>\n                      <KPIValue\n                        value={lastStage ? rate(lastStage, segment.key) : undefined}\n                        format={(value) => formatRate(value)}\n                      />\n                    </dd>\n                    <BlockReady className='text-xs'>\n                      <dd className='text-muted-foreground text-xs tabular-nums'>\n                        {formatNumber(lastStage?.values[segment.key] ?? 0)} of{' '}\n                        {formatNumber(first?.values[segment.key] ?? 0)}\n                      </dd>\n                    </BlockReady>\n                  </div>\n                ))}\n              </dl>\n            )}\n            <ChartPanelLegend\n              items={[\n                ...segments.map((segment) => ({\n                  color: segment.color,\n                  label: segment.label,\n                  shape: 'line' as const,\n                })),\n                ...(segments.length > 1 ? [{ color: GAP_COLOR, label: 'Gap' }] : []),\n              ]}\n            />\n            <ChartPanelFigure className='h-56'>\n              <ResponsiveContainer width='100%' height='100%'>\n                <ComposedChart\n                  aria-label={`${title} chart`}\n                  data={rows}\n                  margin={{ top: 8, right: 96, bottom: 8, left: 0 }}\n                >\n                  <CartesianGrid {...chartGridProps} />\n                  <XAxis\n                    {...chartAxisProps}\n                    dataKey='label'\n                    interval='preserveStartEnd'\n                    minTickGap={12}\n                  />\n                  <YAxis\n                    {...chartAxisProps}\n                    domain={[0, 1]}\n                    tickFormatter={(value: number) => formatRate(value, 0)}\n                    ticks={[0, 0.25, 0.5, 0.75, 1]}\n                    width={chartValueAxisWidth}\n                  />\n                  <Tooltip\n                    content={(tooltipProps) => (\n                      <ChartTooltipContent\n                        {...tooltipProps}\n                        payload={tooltipProps.payload?.filter(\n                          (item) => item.dataKey !== 'gap',\n                        )}\n                        valueFormatter={(value) => formatRate(value)}\n                      />\n                    )}\n                    cursor={chartLineCursor}\n                  />\n                  {segments.length > 1 && (\n                    <Area\n                      activeDot={false}\n                      dataKey='gap'\n                      fill={GAP_COLOR}\n                      fillOpacity={1}\n                      name='Gap'\n                      stroke='none'\n                      type='linear'\n                    />\n                  )}\n                  {segments.map((segment) => (\n                    <Line\n                      key={segment.key}\n                      activeDot={chartActiveDot}\n                      dataKey={segment.key}\n                      dot={{ fill: segment.color, r: 3, strokeWidth: 0 }}\n                      name={segment.label}\n                      stroke={segment.color}\n                      strokeWidth={2}\n                      type='linear'\n                    >\n                      <LabelList\n                        dataKey={segment.key}\n                        content={({ index, value, x, y }) =>\n                          index === lastIndex ? (\n                            <text\n                              x={Number(x) + 8}\n                              y={Number(y) + (segment.key === topKey ? -7 : 7)}\n                              dominantBaseline='middle'\n                              fill='var(--color-foreground)'\n                              fontSize={11}\n                              fontWeight={500}\n                            >\n                              {segment.label} {formatRate(Number(value))}\n                            </text>\n                          ) : null\n                        }\n                      />\n                    </Line>\n                  ))}\n                </ComposedChart>\n              </ResponsiveContainer>\n            </ChartPanelFigure>\n            <ChartPanelTable\n              caption={`${title}: ${description}`}\n              columns={[\n                { key: 'label', label: 'Stage' },\n                ...segments.map((segment) => ({\n                  format: (value: number) => formatRate(value),\n                  key: segment.key,\n                  label: segment.label,\n                })),\n              ]}\n              rows={rows}\n            />\n          </section>\n        </CardContent>\n      </Card>\n    </BlockStateProvider>\n  )\n}\n\nexport { Funnel4, exampleProps as funnel4ExampleProps, type Funnel4Props }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/funnel/funnel-04.tsx"
    }
  ],
  "meta": {
    "shows": "Two segments as lines through every stage, each as a share of its first step, with the gap between them shaded.",
    "size": "full",
    "usedWith": [
      "funnel-01",
      "data-table-03"
    ],
    "component": "Funnel4",
    "props": "{ description: string; segments: { color: string; key: string; label: string }[]; stages?: { label: string; values: Record<string, number> }[]; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
