# Add the "Video Embed" block to this P1 project

Add a Puck component from P1 Blocks (https://p1-blocks.prolificdigital.io/blocks/video-embed) to this Pantheon Content Publisher (P1) Next.js project. Paste a YouTube or Vimeo link for a lazy-loaded, privacy-friendly player with caption and transcript link.

## Steps

1. Create `components/puck/video-embed-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 { videoEmbedBlock } from "./components/puck/video-embed-block";`
3. Register it under `components` as `VideoEmbedBlock: videoEmbedBlock`.
4. Add `"VideoEmbedBlock"` to the best-fitting entry in `categories` (this block's library category is "Content"). Create a category only if none fits.
5. Run the project's type check (usually `pnpm typecheck`) and resolve anything project-specific.
6. 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-`.
- Images are plain `imageUrl` + `imageAlt` text fields. If this project defines a P1 media-library field (search for `"p1-media"`), offer to switch to it.
- Don't change prop names in `defaultProps`. Content editors' saved pages depend on them.

## File: `components/puck/video-embed-block.tsx`

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

/**
 * Video Embed — paste a YouTube or Vimeo link and get a privacy-friendly player
 * (youtube-nocookie.com, Vimeo with do-not-track) that lazy-loads, with a caption and transcript link.
 * Register: `VideoEmbedBlock: videoEmbedBlock` in puck.config.tsx.
 */

type Heading = "h1" | "h2" | "h3" | "h4";
type Aspect = "16:9" | "4:3" | "1:1" | "9:16";

export type VideoEmbedProps = {
  heading: string;
  headingLevel: Heading;
  intro: string;
  videoUrl: string;
  videoTitle: string;
  aspect: Aspect;
  startAt: string;
  caption: string;
  transcriptLabel: string;
  transcriptHref: string;
};

const aspectClass: Record<Aspect, string> = {
  "16:9": "aspect-video",
  "4:3": "aspect-4/3",
  "1:1": "aspect-square",
  "9:16": "aspect-9/16",
};

/** "90", "1:30" or "1m30s" → seconds; anything else → 0. */
function toSeconds(raw: string | undefined): number {
  const v = (raw || "").trim();
  if (!v) return 0;
  if (/^\d+$/.test(v)) return Number(v);
  const colon = v.match(/^(?:(\d+):)?(\d{1,2}):(\d{2})$/);
  if (colon) return Number(colon[1] || 0) * 3600 + Number(colon[2]) * 60 + Number(colon[3]);
  const units = v.match(/^(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?$/i);
  if (units && (units[1] || units[2] || units[3])) return Number(units[1] || 0) * 3600 + Number(units[2] || 0) * 60 + Number(units[3] || 0);
  return 0;
}

/** Turns a watch/share/embed URL into a privacy-friendly embed URL, or null if it isn't YouTube or Vimeo. */
function toEmbed(raw: string | undefined, start: number): string | null {
  const input = (raw || "").trim();
  if (!input) return null;
  let url: URL;
  try {
    url = new URL(/^https?:\/\//i.test(input) ? input : `https://${input}`);
  } catch {
    return null;
  }
  const host = url.hostname.replace(/^(www|m)\./, "");
  const path = url.pathname.split("/").filter(Boolean);

  let youtubeId: string | null = null;
  if (host === "youtu.be") youtubeId = path[0] ?? null;
  else if (host === "youtube.com" || host === "youtube-nocookie.com" || host === "music.youtube.com") {
    if (path[0] === "watch") youtubeId = url.searchParams.get("v");
    else if (["embed", "shorts", "live", "v"].includes(path[0] ?? "")) youtubeId = path[1] ?? null;
  }
  if (youtubeId && /^[\w-]{11}$/.test(youtubeId)) {
    const t = start || toSeconds(url.searchParams.get("t") ?? url.searchParams.get("start") ?? "");
    const params = new URLSearchParams({ rel: "0" });
    if (t) params.set("start", String(t));
    return `https://www.youtube-nocookie.com/embed/${youtubeId}?${params.toString()}`;
  }

  if (host === "vimeo.com" || host === "player.vimeo.com") {
    const segments = path[0] === "video" ? path.slice(1) : path;
    const idIndex = segments.findIndex((s) => /^\d+$/.test(s));
    if (idIndex === -1) return null;
    const vimeoId = segments[idIndex];
    const params = new URLSearchParams({ dnt: "1" });
    const hash = url.searchParams.get("h") || (segments[idIndex + 1] && /^[a-f0-9]+$/i.test(segments[idIndex + 1]) ? segments[idIndex + 1] : "");
    if (hash) params.set("h", hash);
    const fragment = start ? `#t=${start}s` : "";
    return `https://player.vimeo.com/video/${vimeoId}?${params.toString()}${fragment}`;
  }
  return null;
}

export const videoEmbedBlock: ComponentConfig<VideoEmbedProps> = {
  label: "Video Embed",
  fields: {
    heading: { type: "text", label: "Heading", metadata: { help: "Leave blank to show only the video." } },
    headingLevel: {
      type: "select",
      label: "Heading level",
      options: [
        { label: "H1", value: "h1" },
        { label: "H2", value: "h2" },
        { label: "H3", value: "h3" },
        { label: "H4", value: "h4" },
      ],
      metadata: { help: "Sets the page outline for screen readers and search. The look doesn't change." },
    },
    intro: { type: "textarea", label: "Intro" },
    videoUrl: {
      type: "text",
      label: "YouTube or Vimeo link",
      metadata: { help: "Paste the link from your browser or the Share button. YouTube plays from youtube-nocookie.com and Vimeo with do-not-track on." },
    },
    videoTitle: {
      type: "text",
      label: "Video title (required)",
      metadata: { help: "Screen readers announce this before the player, so describe the video, like “Riverside Greenway construction time-lapse”." },
    },
    aspect: {
      type: "select",
      label: "Shape",
      options: [
        { label: "Widescreen 16:9", value: "16:9" },
        { label: "Classic 4:3", value: "4:3" },
        { label: "Square 1:1", value: "1:1" },
        { label: "Vertical 9:16", value: "9:16" },
      ],
      metadata: { help: "Match the video's own shape to avoid black bars. Vertical suits Shorts and phone video." },
    },
    startAt: { type: "text", label: "Start at", metadata: { help: "Optional, like 1:30 or 90. Leave blank to start at the beginning." } },
    caption: { type: "textarea", label: "Caption", metadata: { help: "Shown under the video. Leave blank to hide it." } },
    transcriptLabel: { type: "text", label: "Transcript link text", metadata: { help: "A transcript helps people who can't play audio. Leave blank to hide the link." } },
    transcriptHref: { type: "text", label: "Transcript link", metadata: { help: "A page or file with the full transcript and audio description." } },
  },
  defaultProps: {
    heading: "Three years of the greenway in three minutes",
    headingLevel: "h2",
    intro: "Watch the Riverside Greenway go from a fenced-off rail bed to two miles of lit, accessible trail, told by the neighbors who pushed for it.",
    videoUrl: "https://www.youtube.com/watch?v=M7lc1UVf-VE",
    videoTitle: "Riverside Greenway: three years in three minutes",
    aspect: "16:9",
    startAt: "",
    caption: "Filmed by the Parks Department media team, 2023 – 2026. Captions are available in English and Spanish.",
    transcriptLabel: "Read the full transcript",
    transcriptHref: "/greenway/video-transcript",
  },
  render: ({ heading, headingLevel, intro, videoUrl, videoTitle, aspect, startAt, caption, transcriptLabel, transcriptHref }) => {
    const H = headingLevel || "h2";
    const src = toEmbed(videoUrl, toSeconds(startAt));
    const ratio = aspectClass[aspect] ?? aspectClass["16:9"];
    const vertical = aspect === "9:16";
    const title = videoTitle?.trim() || heading?.trim() || "Embedded video";
    const hasFooter = Boolean(caption || transcriptLabel);

    return (
      <section className="bg-white px-4 py-16 sm:px-6 sm:py-20 lg:px-8 lg:py-24">
        <div className="mx-auto max-w-5xl">
          {heading || intro ? (
            <div className="max-w-2xl">
              {heading ? <H className="text-3xl font-semibold tracking-tight text-balance text-neutral-950 sm:text-4xl">{heading}</H> : null}
              {intro ? <p className="mt-4 text-lg leading-8 text-pretty text-neutral-600">{intro}</p> : null}
            </div>
          ) : null}

          <figure className={`${heading || intro ? "mt-10" : ""} ${vertical ? "mx-auto max-w-sm" : ""}`}>
            <div className={`relative w-full overflow-hidden rounded-2xl bg-neutral-950 shadow-xl ring-1 ring-neutral-950/10 ${ratio}`}>
              {src ? (
                <iframe
                  src={src}
                  title={title}
                  loading="lazy"
                  allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; fullscreen"
                  allowFullScreen
                  referrerPolicy="strict-origin-when-cross-origin"
                  className="absolute inset-0 size-full border-0"
                />
              ) : (
                <div className="absolute inset-0 grid place-items-center p-6 text-center">
                  <div>
                    <svg aria-hidden="true" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={1.5} className="mx-auto size-10 text-neutral-500">
                      <path strokeLinecap="round" strokeLinejoin="round" d="m15.75 10.5 4.72-4.72a.75.75 0 0 1 1.28.53v11.38a.75.75 0 0 1-1.28.53l-4.72-4.72M4.5 18.75h9a2.25 2.25 0 0 0 2.25-2.25v-9a2.25 2.25 0 0 0-2.25-2.25h-9A2.25 2.25 0 0 0 2.25 7.5v9a2.25 2.25 0 0 0 2.25 2.25Z" />
                    </svg>
                    <p className="mt-3 text-sm text-neutral-300">Add a YouTube or Vimeo link to show the video.</p>
                  </div>
                </div>
              )}
            </div>

            {hasFooter ? (
              <figcaption className="mt-4 flex flex-col gap-3 sm:flex-row sm:items-start sm:justify-between sm:gap-8">
                {caption ? (
                  <p className="flex max-w-2xl items-start gap-3 text-sm leading-6 text-pretty text-neutral-600">
                    <span aria-hidden="true" className="mt-3 h-px w-6 shrink-0 bg-indigo-600" />
                    <span>{caption}</span>
                  </p>
                ) : null}
                {transcriptLabel ? (
                  <a
                    href={transcriptHref || "#"}
                    className="inline-flex min-h-11 shrink-0 items-center gap-2 self-start rounded-lg px-3 text-sm font-semibold text-indigo-700 ring-1 ring-neutral-200 ring-inset hover:bg-indigo-50 hover:text-indigo-900 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 sm:-mt-2"
                  >
                    <svg aria-hidden="true" viewBox="0 0 20 20" fill="currentColor" className="size-4">
                      <path fillRule="evenodd" d="M4.5 2A1.5 1.5 0 0 0 3 3.5v13A1.5 1.5 0 0 0 4.5 18h11a1.5 1.5 0 0 0 1.5-1.5V7.621a1.5 1.5 0 0 0-.44-1.06l-4.12-4.122A1.5 1.5 0 0 0 11.378 2H4.5Zm2.25 8.5a.75.75 0 0 0 0 1.5h6.5a.75.75 0 0 0 0-1.5h-6.5Zm0 3a.75.75 0 0 0 0 1.5h6.5a.75.75 0 0 0 0-1.5h-6.5Z" clipRule="evenodd" />
                    </svg>
                    {transcriptLabel}
                  </a>
                ) : null}
              </figcaption>
            ) : null}
          </figure>
        </div>
      </section>
    );
  },
};
```
