# Add the "Funnel Chart" block to this P1 project

Add a Puck component from P1 Blocks (https://p1-blocks.prolificdigital.io/blocks/funnel-chart) to this Pantheon Content Publisher (P1) Next.js project. A conversion funnel with counts, share of the first stage, step conversion and the biggest drop-off called out.

## Steps

1. Create `components/puck/funnel-chart-block.tsx` with the exact file contents below. If `components/puck/` doesn't exist, find the folder where this project keeps its Puck components (look at the imports in `puck.config.tsx`) and put it there.
2. In `puck.config.tsx`, import it: `import { funnelChartBlock } from "./components/puck/funnel-chart-block";`
3. Register it under `components` as `FunnelChartBlock: funnelChartBlock`.
4. Add `"FunnelChartBlock"` to the best-fitting entry in `categories` (this block's library category is "Dashboards"). Create a category only if none fits.
5. Dashboard widgets are designed to sit inside the Dashboard Grid block (https://p1-blocks.prolificdigital.io/r/dashboard-grid.md). If the project doesn't have it yet, offer to add it too.
6. Run the project's type check (usually `pnpm typecheck`) and resolve anything project-specific.
7. If `package.json` has a `sync:registry` script, tell the user it must run (or that CI will run it) before the new component appears in the P1 editor.

## Rules

- Do not add dependencies. The block is self-contained by design: one type-only import from `@puckeditor/core`, Tailwind v4 classes, no client JavaScript.
- Keep the files as-is unless the user asks for changes. The accent color is `indigo-*`; if the project has brand colors, offer to swap them with a find-and-replace on `indigo-`.
- Data values are text fields on purpose: P1 data-source tokens (`{{ source.field }}`) only bind in text fields. Don't convert them to number fields.
- Don't change prop names in `defaultProps`. Content editors' saved pages depend on them.

## File: `components/puck/funnel-chart-block.tsx`

```tsx
import type { ComponentConfig } from "@puckeditor/core";

/**
 * Funnel Chart — a conversion funnel. Each stage shows its count, its share of the first
 * stage and how many continued from the stage before; the biggest drop-off is called out
 * in text. Values are text fields, so stages can be bound to a P1 data source.
 * Register: `FunnelChartBlock: funnelChartBlock` in puck.config.tsx.
 */
// spec: status-colors

type Heading = "h2" | "h3" | "h4" | "p";

export type FunnelChartProps = {
  title: string;
  headingLevel: Heading;
  subtitle: string;
  stages: { label: string; value: string }[];
  highlightDropoff: "yes" | "no";
  footnote: string;
};

/** "1,234.5", "$12k" or "-3.2%" → number; anything unparseable → null. */
function toNumber(raw: string | undefined): number | null {
  if (raw == null) return null;
  const cleaned = String(raw).replace(/[^0-9.\-]/g, "");
  if (cleaned === "" || cleaned === "-" || cleaned === ".") return null;
  const n = Number(cleaned);
  return Number.isFinite(n) ? n : null;
}

const fmt = (n: number) => n.toLocaleString("en-US", { maximumFractionDigits: 1 });

function pctText(ratio: number | null) {
  if (ratio === null) return "—";
  const p = ratio * 100;
  if (p > 0 && p < 1) return "<1%";
  return `${p < 10 && p % 1 !== 0 ? p.toFixed(1) : Math.round(p)}%`;
}

const SHADES = ["bg-indigo-600", "bg-indigo-500", "bg-indigo-400", "bg-indigo-300", "bg-indigo-200"];

export const funnelChartBlock: ComponentConfig<FunnelChartProps> = {
  label: "Funnel Chart",
  fields: {
    title: { type: "text", label: "Title" },
    headingLevel: {
      type: "select",
      label: "Heading level",
      options: [
        { label: "H2", value: "h2" },
        { label: "H3 (recommended inside a dashboard)", value: "h3" },
        { label: "H4", value: "h4" },
        { label: "Not a heading", value: "p" },
      ],
      metadata: { help: "Sets the page outline for screen readers and search. The look doesn't change." },
    },
    subtitle: { type: "text", label: "Period or subtitle", metadata: { help: "e.g. 2026–27 application cycle. Leave blank to hide." } },
    stages: {
      type: "array",
      label: "Stages",
      min: 0,
      max: 10,
      getItemSummary: (item) => item?.label || "Stage",
      defaultItemProps: { label: "New stage", value: "0" },
      arrayFields: {
        label: { type: "text", label: "Stage name" },
        value: { type: "text", label: "Count", metadata: { help: "People or items that reached this stage, e.g. 4,960. Bind with {{ item.field }}." } },
      },
    },
    highlightDropoff: {
      type: "radio",
      label: "Call out biggest drop-off",
      options: [
        { label: "Yes", value: "yes" },
        { label: "No", value: "no" },
      ],
    },
    footnote: { type: "text", label: "Footnote", metadata: { help: "Where the numbers come from. Leave blank to hide." } },
  },
  defaultProps: {
    title: "Scholarship application funnel",
    headingLevel: "h3",
    subtitle: "2026–27 cycle, as of Oct 1",
    stages: [
      { label: "Viewed eligibility page", value: "12,480" },
      { label: "Started an application", value: "4,960" },
      { label: "Submitted", value: "1,720" },
      { label: "Documents complete", value: "1,390" },
      { label: "Awarded", value: "640" },
    ],
    highlightDropoff: "yes",
    footnote: "Sample data · Source: Scholarship portal analytics, Oct 1, 2026",
  },
  render: ({ id, title, headingLevel, subtitle, stages, highlightDropoff, footnote }) => {
    const H = headingLevel || "h3";
    const titleId = `${id}-title`;
    const rows = (stages ?? []).filter((s) => s?.label?.trim()).map((s) => ({ label: s.label.trim(), n: toNumber(s.value) }));
    const first = rows[0]?.n ?? null;
    const max = Math.max(0, ...rows.map((r) => r.n ?? 0));
    const steps = rows.map((r, i) => {
      const prev = i > 0 ? rows[i - 1].n : null;
      return prev !== null && prev > 0 && r.n !== null ? r.n / prev : null;
    });
    let worst = -1;
    steps.forEach((s, i) => {
      if (s !== null && s < 1 && (worst < 0 || s < (steps[worst] as number))) worst = i;
    });
    const callout = highlightDropoff !== "no" && worst > 0 && rows.length > 2 ? worst : -1;

    return (
      <div className="flex h-full flex-col rounded-xl bg-white p-5 shadow-xs ring-1 ring-neutral-950/5 sm:p-6 not-in-data-[dashboard-grid]:mx-4 not-in-data-[dashboard-grid]:my-8 sm:not-in-data-[dashboard-grid]:mx-auto sm:not-in-data-[dashboard-grid]:max-w-2xl">
        {title || subtitle ? (
          <div>
            {title ? (
              <H id={titleId} className="text-sm font-semibold text-neutral-950">
                {title}
              </H>
            ) : null}
            {subtitle ? <p className="mt-1 text-sm text-neutral-500">{subtitle}</p> : null}
          </div>
        ) : null}

        {rows.length === 0 ? (
          <div className="mt-4 flex flex-1 flex-col items-center justify-center rounded-lg border border-dashed border-neutral-300 px-4 py-10 text-center">
            <p className="text-sm font-medium text-neutral-900">No stages yet</p>
            <p className="mt-1 text-sm text-neutral-500">Add stages from first to last with a count for each, or bind a data source.</p>
          </div>
        ) : (
          <ol role="list" aria-labelledby={title ? titleId : undefined} className="mt-5">
            {rows.map((r, i) => {
              const width = r.n !== null && max > 0 ? Math.max(1.5, (r.n / max) * 100) : 0;
              const ofFirst = first !== null && first > 0 && r.n !== null ? r.n / first : null;
              const step = steps[i];
              const isWorst = i === callout;
              return (
                <li key={`${r.label}-${i}`}>
                  {i > 0 ? (
                    <p
                      className={`flex flex-wrap items-center justify-center gap-1.5 py-1.5 text-xs tabular-nums ${
                        isWorst ? "font-semibold text-amber-800" : "text-neutral-500"
                      }`}
                    >
                      <svg aria-hidden="true" viewBox="0 0 12 12" fill="none" stroke="currentColor" strokeWidth="1.5" className="size-3">
                        <path d="M6 2v8m0 0L3 7m3 3 3-3" strokeLinecap="round" strokeLinejoin="round" />
                      </svg>
                      {step === null ? "—" : `${pctText(step)} continue`}
                      {isWorst ? (
                        <span className="rounded bg-amber-50 px-1.5 py-px font-semibold text-amber-800 ring-1 ring-amber-600/25 ring-inset">Biggest drop-off</span>
                      ) : null}
                    </p>
                  ) : null}
                  <div className="flex items-baseline justify-between gap-3">
                    <p className="min-w-0 text-sm font-medium text-neutral-950">{r.label}</p>
                    <p className="shrink-0 text-sm text-neutral-600 tabular-nums">
                      <span className="font-semibold text-neutral-950">{r.n === null ? "—" : fmt(r.n)}</span>
                      {i > 0 ? <span className="ml-1.5 text-xs text-neutral-500">{pctText(ofFirst)} of first</span> : null}
                    </p>
                  </div>
                  <div aria-hidden="true" className="mt-1.5 flex h-7 justify-center rounded-md bg-neutral-50">
                    <div
                      className={`h-full rounded-md ${SHADES[Math.min(i, SHADES.length - 1)]} ${isWorst ? "ring-2 ring-amber-400 ring-offset-2 ring-offset-white" : ""}`}
                      style={{ width: `${width}%` }}
                    />
                  </div>
                </li>
              );
            })}
          </ol>
        )}

        {callout > 0 ? (
          <p className="mt-5 flex gap-2 rounded-lg bg-amber-50 px-3 py-2.5 text-sm text-amber-900 ring-1 ring-amber-600/20 ring-inset">
            <svg aria-hidden="true" viewBox="0 0 20 20" fill="currentColor" className="mt-0.5 size-4 shrink-0 text-amber-600">
              <path
                fillRule="evenodd"
                d="M8.485 2.495c.673-1.167 2.357-1.167 3.03 0l6.28 10.875c.673 1.167-.17 2.625-1.516 2.625H3.72c-1.347 0-2.189-1.458-1.515-2.625L8.485 2.495ZM10 5a.75.75 0 0 1 .75.75v3.5a.75.75 0 0 1-1.5 0v-3.5A.75.75 0 0 1 10 5Zm0 9a1 1 0 1 0 0-2 1 1 0 0 0 0 2Z"
                clipRule="evenodd"
              />
            </svg>
            <span>
              <span className="font-semibold">Biggest drop-off:</span> {rows[callout - 1].label} → {rows[callout].label}.{" "}
              <span className="tabular-nums">{pctText(1 - (steps[callout] as number))}</span> didn&rsquo;t continue.
            </span>
          </p>
        ) : null}

        {footnote ? <p className="mt-auto pt-4 text-xs text-neutral-500">{footnote}</p> : null}
      </div>
    );
  },
};
```
