# Add the "Progress Goals" block to this P1 project

Add a Puck component from P1 Blocks (https://p1-blocks.prolificdigital.io/blocks/progress-goals) to this Pantheon Content Publisher (P1) Next.js project. Labelled progress bars for several goals showing current of target and percent, with exceeded goals marked.

## Steps

1. Create `components/puck/progress-goals-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 { progressGoalsBlock } from "./components/puck/progress-goals-block";`
3. Register it under `components` as `ProgressGoalsBlock: progressGoalsBlock`.
4. Add `"ProgressGoalsBlock"` 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/progress-goals-block.tsx`

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

/**
 * Progress Goals — several goals, each a labelled progress bar with "current of target"
 * and a percentage. Handles goals that are exceeded or have no target. Every figure is a
 * text field, so it can be bound to a P1 data source. Place inside a Dashboard Grid.
 * Register: `ProgressGoalsBlock: progressGoalsBlock` in puck.config.tsx.
 */

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

export type ProgressGoalsProps = {
  title: string;
  headingLevel: Heading;
  subtitle: string;
  items: { label: string; current: string; target: string; unit: string }[];
  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 });

/** Currency symbols lead ($182,500); everything else trails (3,200 hrs). */
function withUnit(n: number | null, unit: string | undefined) {
  if (n === null) return "—";
  const u = unit?.trim() ?? "";
  if (!u) return fmt(n);
  if (/^[$€£¥]$/.test(u)) return `${u}${fmt(n)}`;
  return /^[%°]/.test(u) ? `${fmt(n)}${u}` : `${fmt(n)} ${u}`;
}

export const progressGoalsBlock: ComponentConfig<ProgressGoalsProps> = {
  label: "Progress Goals",
  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. Fiscal year 2026. Leave blank to hide." } },
    items: {
      type: "array",
      label: "Goals",
      min: 0,
      max: 12,
      getItemSummary: (item) => item?.label || "Goal",
      defaultItemProps: { label: "New goal", current: "0", target: "100", unit: "" },
      arrayFields: {
        label: { type: "text", label: "Goal name" },
        current: { type: "text", label: "Current", metadata: { help: "Progress so far, e.g. 3,200. Bind with {{ item.field }}." } },
        target: { type: "text", label: "Target", metadata: { help: "e.g. 5,000. Leave blank or 0 to show the total without a bar." } },
        unit: { type: "text", label: "Unit", metadata: { help: "e.g. hrs, meals or $. A currency symbol goes before the number." } },
      },
    },
    footnote: { type: "text", label: "Footnote", metadata: { help: "Where the numbers come from. Leave blank to hide." } },
  },
  defaultProps: {
    title: "Annual service goals",
    headingLevel: "h3",
    subtitle: "Fiscal year 2026 · through Sep 30",
    items: [
      { label: "Volunteer hours", current: "3,200", target: "5,000", unit: "hrs" },
      { label: "Meals delivered", current: "41,800", target: "40,000", unit: "meals" },
      { label: "Trees planted", current: "1,240", target: "2,000", unit: "trees" },
      { label: "Scholarship fund", current: "182,500", target: "250,000", unit: "$" },
    ],
    footnote: "Sample data · Source: Program records, Sep 30, 2026",
  },
  render: ({ id, title, headingLevel, subtitle, items, footnote }) => {
    const H = headingLevel || "h3";
    const titleId = `${id}-title`;
    const goals = (items ?? []).filter((it) => it?.label?.trim());

    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}

        {goals.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 goals yet</p>
            <p className="mt-1 text-sm text-neutral-500">Add a goal with a current value and a target, or bind a data source.</p>
          </div>
        ) : (
          <ul role="list" aria-labelledby={title ? titleId : undefined} className="mt-5 space-y-5">
            {goals.map((g, i) => {
              const cur = toNumber(g.current);
              const tgt = toNumber(g.target);
              const hasTarget = tgt !== null && tgt > 0;
              const ratio = hasTarget && cur !== null ? Math.max(0, cur / tgt) : null;
              const pct = ratio === null ? null : Math.round(ratio * 100);
              const pctText = pct === null ? "" : ratio! > 0 && pct < 1 ? "<1%" : `${pct}%`;
              const exceeded = ratio !== null && ratio > 1;
              // Over goal, the bar fills and the goal line sits where the target falls on the larger total.
              const goalAt = exceeded ? (1 / ratio!) * 100 : null;
              const fill = ratio === null ? 0 : Math.min(100, ratio * 100);
              const curText = withUnit(cur, g.unit);
              const tgtText = withUnit(tgt, g.unit);
              const detail = hasTarget ? `${withUnit(cur, /^[$€£¥]$/.test(g.unit?.trim() ?? "") ? g.unit : "")} of ${tgtText}` : curText;
              const labelId = `${id}-goal-${i}`;

              return (
                <li key={`${g.label}-${i}`}>
                  <div className="flex flex-wrap items-baseline justify-between gap-x-3 gap-y-1">
                    <p id={labelId} className="min-w-0 text-sm font-medium text-neutral-950">
                      {g.label}
                    </p>
                    {exceeded ? (
                      <span className="inline-flex items-center gap-1 rounded-md bg-indigo-50 px-1.5 py-0.5 text-xs font-semibold text-indigo-700 ring-1 ring-indigo-600/20 ring-inset">
                        <svg aria-hidden="true" viewBox="0 0 12 12" fill="none" stroke="currentColor" strokeWidth="1.75" className="size-3">
                          <path d="m2.5 6.25 2.25 2.25 4.75-5" strokeLinecap="round" strokeLinejoin="round" />
                        </svg>
                        Goal exceeded
                      </span>
                    ) : pctText ? (
                      <span className="text-sm font-semibold text-neutral-950 tabular-nums">{pctText}</span>
                    ) : null}
                  </div>

                  {hasTarget ? (
                    <div
                      role="progressbar"
                      aria-labelledby={labelId}
                      aria-valuemin={0}
                      aria-valuemax={tgt!}
                      aria-valuenow={cur === null ? undefined : Math.min(Math.max(cur, 0), tgt!)}
                      aria-valuetext={cur === null ? "No data" : `${curText} of ${tgtText}, ${pctText}${exceeded ? ", goal exceeded" : ""}`}
                      className="relative mt-2 h-2 rounded-full bg-neutral-100"
                    >
                      <div className="h-full overflow-hidden rounded-full" style={{ width: `${fill}%` }}>
                        <div className="h-full w-full bg-indigo-600" />
                      </div>
                      {goalAt !== null ? (
                        <>
                          <div
                            aria-hidden="true"
                            className="absolute inset-y-0 right-0 rounded-r-full bg-indigo-900"
                            style={{ left: `${goalAt}%` }}
                          />
                          <div
                            aria-hidden="true"
                            className="absolute -inset-y-1 w-0.5 -translate-x-1/2 rounded-full bg-neutral-950 ring-2 ring-white"
                            style={{ left: `${goalAt}%` }}
                          />
                        </>
                      ) : null}
                    </div>
                  ) : null}

                  <p className="mt-1.5 text-xs text-neutral-600 tabular-nums">
                    {detail}
                    {hasTarget && pctText ? <span aria-hidden="true"> · </span> : null}
                    {hasTarget && pctText ? <span className={exceeded ? "font-semibold text-indigo-700" : undefined}>{pctText}</span> : null}
                    {!hasTarget ? <span className="text-neutral-500"> · No target set</span> : null}
                  </p>
                </li>
              );
            })}
          </ul>
        )}

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