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

Add a Puck component from P1 Blocks (https://p1-blocks.prolificdigital.io/blocks/bullet-chart) to this Pantheon Content Publisher (P1) Next.js project. Compact gauges showing each measure's actual value against a target and poor, fair and good bands.

## Steps

1. Create `components/puck/bullet-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 { bulletChartBlock } from "./components/puck/bullet-chart-block";`
3. Register it under `components` as `BulletChartBlock: bulletChartBlock`.
4. Add `"BulletChartBlock"` 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/bullet-chart-block.tsx`

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

/**
 * Bullet Chart — Stephen Few's compact gauge: for each measure, the actual value as a bar, a
 * target tick, and grey bands for poor, fair and good ranges. Every value is a text field,
 * bindable to a P1 data source. Place inside a Dashboard Grid.
 * Register: `BulletChartBlock: bulletChartBlock` in puck.config.tsx.
 */

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

type Measure = {
  label: string;
  actual: string;
  target: string;
  poor: string;
  fair: string;
  max: string;
  prefix: string;
  suffix: string;
};

export type BulletChartProps = {
  title: string;
  headingLevel: Heading;
  subtitle: string;
  measures: Measure[];
  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;
}

/** Round, human-friendly axis ticks (1, 2, 2.5, 5 × 10ⁿ) covering lo..hi. */
function niceTicks(lo: number, hi: number, count = 4): number[] {
  if (lo === hi) {
    if (lo === 0) hi = 1;
    else if (lo > 0) lo = 0;
    else hi = 0;
  }
  const rough = (hi - lo) / count;
  const pow = Math.pow(10, Math.floor(Math.log10(rough)));
  const f = rough / pow;
  const step = (f <= 1 ? 1 : f <= 2 ? 2 : f <= 2.5 ? 2.5 : f <= 5 ? 5 : 10) * pow;
  const start = Math.floor(lo / step) * step;
  const end = Math.ceil(hi / step) * step;
  const ticks: number[] = [];
  for (let v = start; v <= end + step / 2 && ticks.length < 12; v += step) ticks.push(Number(v.toPrecision(12)));
  return ticks;
}

const compact = new Intl.NumberFormat("en-US", { notation: "compact", maximumFractionDigits: 1 });
const full = new Intl.NumberFormat("en-US", { maximumFractionDigits: 2 });

function format(n: number | null, prefix = "", suffix = "", short = false) {
  if (n === null) return "—";
  const unit = suffix && /^[a-z]/i.test(suffix) ? ` ${suffix}` : suffix;
  return `${n < 0 ? "−" : ""}${prefix}${(short ? compact : full).format(Math.abs(n))}${unit}`;
}

export const bulletChartBlock: ComponentConfig<BulletChartProps> = {
  label: "Bullet 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: "Subtitle or period", metadata: { help: "e.g. Q3 FY2026. Leave blank to hide." } },
    measures: {
      type: "array",
      label: "Measures",
      min: 1,
      max: 8,
      getItemSummary: (item) => (item?.label ? `${item.label}${item.actual ? ` · ${item.actual}` : ""}` : "Measure"),
      defaultItemProps: { label: "New measure", actual: "", target: "", poor: "", fair: "", max: "", prefix: "", suffix: "" },
      arrayFields: {
        label: { type: "text", label: "Measure" },
        actual: { type: "text", label: "Actual", metadata: { help: "The current value. Or bind it, e.g. {{ scorecard.retention }}." } },
        target: { type: "text", label: "Target", metadata: { help: "Drawn as a black tick. Leave blank to hide." } },
        poor: { type: "text", label: "Poor below", metadata: { help: "Values under this are in the poor (darkest) band. Bands assume higher is better." } },
        fair: { type: "text", label: "Fair below", metadata: { help: "Values from Poor up to this are fair; above it is good." } },
        max: { type: "text", label: "Scale maximum", metadata: { help: "Right end of the scale. Leave blank to fit the data." } },
        prefix: { type: "text", label: "Prefix", metadata: { help: "e.g. $." } },
        suffix: { type: "text", label: "Suffix", metadata: { help: "e.g. % or hrs." } },
      },
    },
    footnote: { type: "text", label: "Footnote", metadata: { help: "Where the data comes from. Leave blank to hide." } },
  },
  defaultProps: {
    title: "Quarterly scorecard",
    headingLevel: "h3",
    subtitle: "Q3 FY2026 against board-approved targets",
    measures: [
      { label: "Donor retention", actual: "58", target: "60", poor: "40", fair: "55", max: "80", prefix: "", suffix: "%" },
      { label: "Clients served", actual: "1,180", target: "1,000", poor: "700", fair: "950", max: "1,400", prefix: "", suffix: "" },
      { label: "Volunteer hours", actual: "4,210", target: "5,000", poor: "3,000", fair: "4,500", max: "6,000", prefix: "", suffix: "hrs" },
      { label: "Grant renewal rate", actual: "71", target: "85", poor: "60", fair: "75", max: "100", prefix: "", suffix: "%" },
      { label: "Operating reserve", actual: "4.6", target: "6", poor: "3", fair: "5", max: "9", prefix: "", suffix: "mo" },
    ],
    footnote: "Sample data · Source: finance and development quarterly reports",
  },
  render: ({ title, headingLevel, subtitle, measures, footnote }) => {
    const H = headingLevel || "h3";
    const rows = (Array.isArray(measures) ? measures : []).map((m, i) => {
      const actual = toNumber(m?.actual);
      const target = toNumber(m?.target);
      const poor = toNumber(m?.poor);
      const fair = toNumber(m?.fair);
      const explicit = toNumber(m?.max);
      const ticks = niceTicks(0, Math.max(actual ?? 0, target ?? 0, poor ?? 0, fair ?? 0, 0), 4);
      const max = explicit && explicit > 0 ? explicit : ticks[ticks.length - 1] || 1;
      const pct = (v: number | null) => (v === null ? 0 : Math.max(0, Math.min(100, (v / max) * 100)));
      const rating =
        actual === null || (poor === null && fair === null)
          ? null
          : poor !== null && actual < poor
            ? "Poor"
            : fair !== null && actual < fair
              ? "Fair"
              : "Good";
      return {
        key: `m${i}`,
        label: m?.label?.trim() || `Measure ${i + 1}`,
        prefix: m?.prefix?.trim() || "",
        suffix: m?.suffix?.trim() || "",
        actual,
        target,
        poor,
        fair,
        max,
        pct,
        rating,
      };
    });
    const hasData = rows.some((r) => r.actual !== null);

    const header = (
      <div>
        {title ? <H className="text-sm font-semibold text-neutral-950">{title}</H> : null}
        {subtitle ? <p className="mt-0.5 text-sm text-neutral-500">{subtitle}</p> : null}
      </div>
    );
    const foot = footnote ? <p className="mt-auto pt-4 text-xs text-neutral-500">{footnote}</p> : null;
    const card =
      "@container 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";

    if (!hasData) {
      return (
        <div className={card}>
          {header}
          <div className="mt-4 flex flex-1 flex-col items-center justify-center gap-1 rounded-lg border border-dashed border-neutral-200 px-4 py-10 text-center">
            <svg aria-hidden="true" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" className="size-6 text-neutral-400">
              <rect x="3" y="8" width="18" height="8" rx="1" />
              <path d="M3 12h10M16 6v12" strokeLinecap="round" />
            </svg>
            <p className="text-sm font-medium text-neutral-900">No data yet</p>
            <p className="text-xs text-neutral-500">Measures appear once actual values are entered or the data source returns them.</p>
          </div>
          {foot}
        </div>
      );
    }

    return (
      <div className={card}>
        {header}

        <ul aria-label="Key" className="mt-4 flex flex-wrap items-center gap-x-4 gap-y-2 text-xs text-neutral-600">
          <li className="flex items-center gap-1.5">
            <span aria-hidden="true" className="h-1.5 w-4 rounded-full bg-indigo-600" />
            Actual
          </li>
          <li className="flex items-center gap-1.5">
            <span aria-hidden="true" className="h-3 w-0.5 rounded-full bg-neutral-950" />
            Target
          </li>
          <li className="flex items-center gap-1.5">
            <span aria-hidden="true" className="flex h-2.5 overflow-hidden rounded-sm">
              <span className="w-2.5 bg-neutral-300" />
              <span className="w-2.5 bg-neutral-200" />
              <span className="w-2.5 bg-neutral-100" />
            </span>
            Poor · Fair · Good
          </li>
        </ul>

        <ul className="mt-5 space-y-5">
          {rows.map((r) => {
            const gap = r.actual !== null && r.target !== null ? r.target - r.actual : null;
            const bandsText = [
              r.poor !== null ? `poor below ${format(r.poor, r.prefix, r.suffix)}` : "",
              r.fair !== null ? `fair below ${format(r.fair, r.prefix, r.suffix)}` : "",
              `scale to ${format(r.max, r.prefix, r.suffix)}`,
            ]
              .filter(Boolean)
              .join(", ");
            return (
              <li key={r.key} className="flex flex-col gap-2 @md:flex-row @md:items-center @md:gap-4">
                <div className="flex items-baseline justify-between gap-3 @md:w-40 @md:shrink-0 @md:flex-col @md:items-start @md:gap-0.5">
                  <span className="min-w-0 text-sm text-pretty text-neutral-700">{r.label}</span>
                  <span className="shrink-0 text-sm text-neutral-500 tabular-nums">
                    <span className="text-base font-semibold text-neutral-950">{format(r.actual, r.prefix, r.suffix)}</span>
                    {r.target !== null ? <span className="ml-1.5 text-xs">of {format(r.target, r.prefix, r.suffix)}</span> : null}
                  </span>
                </div>

                <div className="min-w-0 flex-1">
                  <div
                    role="img"
                    aria-label={`${r.label}: actual ${format(r.actual, r.prefix, r.suffix)}${r.target !== null ? `, target ${format(r.target, r.prefix, r.suffix)}` : ""}${
                      r.rating ? `, in the ${r.rating.toLowerCase()} range` : ""
                    }. Bands: ${bandsText}.`}
                    className="relative h-6 overflow-hidden rounded-sm bg-neutral-100"
                  >
                    {r.fair !== null ? <span className="absolute inset-y-0 left-0 bg-neutral-200" style={{ width: `${r.pct(r.fair)}%` }} /> : null}
                    {r.poor !== null ? <span className="absolute inset-y-0 left-0 bg-neutral-300" style={{ width: `${r.pct(r.poor)}%` }} /> : null}
                    {r.actual !== null ? <span className="absolute inset-y-0 left-0 my-auto h-2 rounded-r-sm bg-indigo-600" style={{ width: `${r.pct(r.actual)}%` }} /> : null}
                    {r.target !== null ? (
                      <span className="absolute inset-y-1 w-0.5 -translate-x-1/2 rounded-full bg-neutral-950" style={{ left: `${Math.min(99.5, r.pct(r.target))}%` }} />
                    ) : null}
                  </div>
                  <div className="mt-1 flex justify-between gap-3 text-xs text-neutral-500 tabular-nums">
                    <span>
                      {r.rating ? <span className="font-medium text-neutral-800">{r.rating}</span> : null}
                      {gap !== null ? (
                        <span>
                          {r.rating ? " · " : ""}
                          {gap <= 0 ? "Target met" : `${format(gap, r.prefix, r.suffix)} to target`}
                        </span>
                      ) : null}
                    </span>
                    <span aria-hidden="true">{format(r.max, r.prefix, r.suffix, true)}</span>
                  </div>
                </div>
              </li>
            );
          })}
        </ul>

        <details className="group mt-4">
          <summary className="inline-flex min-h-11 cursor-pointer list-none items-center gap-1.5 rounded-md text-sm font-medium text-indigo-700 hover:text-indigo-900 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 [&::-webkit-details-marker]:hidden">
            <svg aria-hidden="true" viewBox="0 0 16 16" fill="currentColor" className="size-4 transition-transform group-open:rotate-90 motion-reduce:transition-none">
              <path d="M6.22 4.22a.75.75 0 0 1 1.06 0l3.25 3.25a.75.75 0 0 1 0 1.06l-3.25 3.25a.75.75 0 0 1-1.06-1.06L8.94 8 6.22 5.28a.75.75 0 0 1 0-1.06Z" />
            </svg>
            View data table
          </summary>
          <div className="mt-2 relative overflow-x-auto rounded-lg ring-1 ring-neutral-950/5">
            <table className="w-full text-left text-sm">
              {title ? <caption className="sr-only">{title}</caption> : null}
              <thead className="bg-neutral-50 text-xs text-neutral-600">
                <tr>
                  <th scope="col" className="px-3 py-2 font-medium">
                    Measure
                  </th>
                  <th scope="col" className="px-3 py-2 text-right font-medium">
                    Actual
                  </th>
                  <th scope="col" className="px-3 py-2 text-right font-medium">
                    Target
                  </th>
                  <th scope="col" className="px-3 py-2 text-right font-medium">
                    Poor below
                  </th>
                  <th scope="col" className="px-3 py-2 text-right font-medium">
                    Fair below
                  </th>
                  <th scope="col" className="px-3 py-2 font-medium">
                    Rating
                  </th>
                </tr>
              </thead>
              <tbody className="divide-y divide-neutral-100">
                {rows.map((r) => (
                  <tr key={r.key}>
                    <th scope="row" className="px-3 py-2 font-normal text-neutral-700">
                      {r.label}
                    </th>
                    <td className="px-3 py-2 text-right whitespace-nowrap text-neutral-950 tabular-nums">{format(r.actual, r.prefix, r.suffix)}</td>
                    <td className="px-3 py-2 text-right whitespace-nowrap text-neutral-950 tabular-nums">{format(r.target, r.prefix, r.suffix)}</td>
                    <td className="px-3 py-2 text-right whitespace-nowrap text-neutral-950 tabular-nums">{format(r.poor, r.prefix, r.suffix)}</td>
                    <td className="px-3 py-2 text-right whitespace-nowrap text-neutral-950 tabular-nums">{format(r.fair, r.prefix, r.suffix)}</td>
                    <td className="px-3 py-2 whitespace-nowrap text-neutral-700">{r.rating ?? "—"}</td>
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        </details>

        {foot}
      </div>
    );
  },
};
```
