feat(pages): render Custom HTML blocks as sized sealed frames and honour hide header
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
This commit is contained in:
1 parent
1ceefd7231
commit
6a0a6613c1
7 files changed
+89
-13
No files matched your search
@@ -17,7 +17,7 @@ Design: `design.md` and `docs/superpowers/specs/2026-12-02-custom-html-block-des
|
||||
- [x] 2.1 Frame routes (public and preview) with the sandbox headers, the 404 rules and the height script.
|
||||
- The middleware skips them; `routeGuard` admits the public frame path.
|
||||
- Security code: read the whole diff.
|
||||
- [ ] 2.2 `CmsBlocks`:
|
||||
- [x] 2.2 `CmsBlocks`:
|
||||
- the sandboxed iframe with height messages from its own frame only;
|
||||
- `hideHeader` hides the header and the title.
|
||||
- [ ] 2.3 Editor:
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
import { act, render, screen } from "@testing-library/react";
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { LandingPage, type PublicBlock } from "./CmsBlocks";
|
||||
import { FRAME_SANDBOX } from "@/app/lib/frameHtml";
|
||||
|
||||
// add-custom-html-block: an HTML block is a sealed frame sized by its own reports; "hide header" drops the header.
|
||||
const company = { code: "INS", name: "INS Sdn Bhd", logoImageId: 4, accentColor: null };
|
||||
const page = (blocks: PublicBlock[], hideHeader = false) => ({ slug: "hotel", title: "Hotel day", hideHeader, blocks });
|
||||
const HTML: PublicBlock = { type: "HTML", html: "<h1>Designed</h1><script>x()</script>" };
|
||||
|
||||
const frame = () => document.querySelector("iframe") as HTMLIFrameElement;
|
||||
|
||||
describe("LandingPage: Custom HTML", () => {
|
||||
it("shows an HTML block as a sealed frame from its own address, never inline", () => {
|
||||
render(<LandingPage company={company} page={page([{ type: "FAQ", items: [{ question: "Q", answer: "A" }] }, HTML])} />);
|
||||
expect(frame()).toHaveAttribute("src", "/p/INS/hotel/frame/1");
|
||||
expect(frame()).toHaveAttribute("sandbox", FRAME_SANDBOX);
|
||||
expect(frame()).toHaveAttribute("title", "Hotel day");
|
||||
expect(frame().style.height).toBe("600px");
|
||||
expect(screen.queryByText("Designed")).not.toBeInTheDocument();
|
||||
expect(document.querySelector("script")).toBeNull();
|
||||
});
|
||||
|
||||
it("uses the preview's frame address when given", () => {
|
||||
render(<LandingPage company={company} page={page([HTML])} frameBase="/pages/5/preview/frame" preview />);
|
||||
expect(frame()).toHaveAttribute("src", "/pages/5/preview/frame/0");
|
||||
});
|
||||
|
||||
it("grows to its frame's reported height and ignores other windows", () => {
|
||||
render(<LandingPage company={company} page={page([HTML])} />);
|
||||
act(() => { window.dispatchEvent(new MessageEvent("message", { data: { type: "colite-frame-height", height: 2400 }, source: window })); });
|
||||
expect(frame().style.height).toBe("600px");
|
||||
act(() => { window.dispatchEvent(new MessageEvent("message", { data: { type: "colite-frame-height", height: 2400 }, source: frame().contentWindow })); });
|
||||
expect(frame().style.height).toBe("2400px");
|
||||
});
|
||||
|
||||
it("hides the company header and shows the title to screen readers only when the page says so", () => {
|
||||
render(<LandingPage company={company} page={page([HTML], true)} />);
|
||||
expect(screen.queryByRole("img", { name: "INS Sdn Bhd" })).not.toBeInTheDocument();
|
||||
expect(document.querySelector("header")).toBeNull();
|
||||
expect(screen.getByRole("heading", { level: 1, name: "Hotel day" })).toHaveClass("sr-only");
|
||||
});
|
||||
});
|
||||
@@ -3,9 +3,11 @@ import { contrastText, imageSrc, isSafeLink, type CmsBlock } from "@/app/lib/cms
|
||||
import type { RegistrationInfo } from "@/app/lib/services/publicRegistration";
|
||||
import { SafeHtml } from "./SafeHtml";
|
||||
import { LandingForm } from "./LandingForm";
|
||||
import { HtmlFrame } from "./HtmlFrame";
|
||||
|
||||
// Landing-page blocks, ported from babyboo's CmsBlocks minus the product blocks. HTML only goes through SafeHtml;
|
||||
// images are this company's uploads; buttons render only for a safe link, checked again here.
|
||||
// images are this company's uploads; buttons render only for a safe link, checked again here. A Custom HTML block is
|
||||
// never rendered here: it is a sealed frame loaded from its own address (add-custom-html-block).
|
||||
|
||||
export type PublicBlock = CmsBlock & { form?: RegistrationInfo; closed?: boolean };
|
||||
|
||||
@@ -86,20 +88,28 @@ function Block({ block, index, company, slug, preview }: { block: PublicBlock; i
|
||||
}
|
||||
}
|
||||
|
||||
/** A whole landing page: the company header (logo, accent), the title as the only h1, then the blocks. */
|
||||
export function LandingPage({ company, page, banner, preview }: {
|
||||
const WIDTH = "mx-auto w-full max-w-5xl px-4";
|
||||
|
||||
/**
|
||||
* A whole landing page: the company header (logo, accent), the title as the only h1, then the blocks. Custom HTML
|
||||
* blocks run full width; with hideHeader the header goes and the title is for screen readers only.
|
||||
*/
|
||||
export function LandingPage({ company, page, banner, preview, frameBase }: {
|
||||
company: Branding;
|
||||
page: { slug: string; title: string; blocks: PublicBlock[] };
|
||||
page: { slug: string; title: string; hideHeader?: boolean; blocks: PublicBlock[] };
|
||||
banner?: ReactNode;
|
||||
/** The editor's draft preview: forms show a note instead of taking sign-ups. */
|
||||
preview?: boolean;
|
||||
/** Where block frames load from; the public page's frame address by default. */
|
||||
frameBase?: string;
|
||||
}) {
|
||||
const frames = frameBase ?? `/p/${encodeURIComponent(company.code)}/${encodeURIComponent(page.slug)}/frame`;
|
||||
const accent = company.accentColor && /^#[0-9A-Fa-f]{6}$/.test(company.accentColor) ? company.accentColor : null;
|
||||
const style = accent ? ({ "--primary": accent, "--primary-foreground": contrastText(accent), "--ring": accent } as CSSProperties) : undefined;
|
||||
return (
|
||||
<div style={style} className="min-h-svh bg-background">
|
||||
{banner}
|
||||
<header className="border-b bg-card">
|
||||
{page.hideHeader ? null : <header className="border-b bg-card">
|
||||
<div className="mx-auto flex max-w-5xl items-center gap-3 px-4 py-3">
|
||||
{company.logoImageId ? (
|
||||
// eslint-disable-next-line @next/next/no-img-element -- the company's uploaded logo
|
||||
@@ -108,10 +118,12 @@ export function LandingPage({ company, page, banner, preview }: {
|
||||
<span className="text-lg font-semibold">{company.name}</span>
|
||||
)}
|
||||
</div>
|
||||
</header>
|
||||
<main className="mx-auto max-w-5xl space-y-10 px-4 py-8">
|
||||
<h1 className="text-3xl font-semibold sm:text-4xl">{page.title}</h1>
|
||||
{page.blocks.map((block, i) => <Block key={i} block={block} index={i} company={company.code} slug={page.slug} preview={preview} />)}
|
||||
</header>}
|
||||
<main className={page.hideHeader ? "space-y-10 pb-8" : "space-y-10 py-8"}>
|
||||
<h1 className={page.hideHeader ? "sr-only" : `${WIDTH} text-3xl font-semibold sm:text-4xl`}>{page.title}</h1>
|
||||
{page.blocks.map((block, i) => block.type === "HTML"
|
||||
? <HtmlFrame key={i} src={`${frames}/${i}`} title={page.title} />
|
||||
: <div key={i} className={WIDTH}><Block block={block} index={i} company={company.code} slug={page.slug} preview={preview} /></div>)}
|
||||
</main>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { FRAME_SANDBOX, nextFrameHeight, START_HEIGHT, type FrameHeight } from "@/app/lib/frameHtml";
|
||||
|
||||
/** A Custom HTML block: the sealed frame, sized by height reports from that frame's own window only. */
|
||||
export function HtmlFrame({ src, title }: { src: string; title: string }) {
|
||||
const ref = useRef<HTMLIFrameElement>(null);
|
||||
const [size, setSize] = useState<FrameHeight>({ height: START_HEIGHT, steps: [] });
|
||||
useEffect(() => {
|
||||
const onMessage = (e: MessageEvent) => {
|
||||
if (!ref.current || e.source !== ref.current.contentWindow) return;
|
||||
setSize((s) => nextFrameHeight(s, e.data));
|
||||
};
|
||||
window.addEventListener("message", onMessage);
|
||||
return () => window.removeEventListener("message", onMessage);
|
||||
}, []);
|
||||
return <iframe ref={ref} src={src} title={title} sandbox={FRAME_SANDBOX} loading="lazy" className="block w-full border-0" style={{ height: `${size.height}px` }} />;
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
// Landing-page blocks and the editor's pure helpers (add-landing-pages), ported from babyboo's cmsPages.ts
|
||||
// minus the product blocks. Images are page_image ids, served at /api/public/page-images/<id>.
|
||||
|
||||
export type BlockType = "HERO" | "RICH_TEXT" | "IMAGE_TEXT" | "FAQ" | "FORM";
|
||||
export type BlockType = "HERO" | "RICH_TEXT" | "IMAGE_TEXT" | "FAQ" | "FORM" | "HTML";
|
||||
|
||||
export const BLOCK_TYPES: BlockType[] = ["HERO", "RICH_TEXT", "IMAGE_TEXT", "FAQ", "FORM"];
|
||||
|
||||
@@ -11,6 +11,7 @@ export const BLOCK_LABELS: Record<BlockType, string> = {
|
||||
IMAGE_TEXT: "PagesBlockImageText",
|
||||
FAQ: "PagesBlockFaq",
|
||||
FORM: "PagesBlockForm",
|
||||
HTML: "PagesBlockHtml",
|
||||
};
|
||||
|
||||
export interface CmsFaqItem {
|
||||
|
||||
@@ -10,7 +10,7 @@ type Props = { params: Promise<{ company: string; slug: string }> };
|
||||
|
||||
interface PublicPage {
|
||||
company: Branding;
|
||||
page: { slug: string; title: string; metaDescription?: string | null; blocks: PublicBlock[] };
|
||||
page: { slug: string; title: string; metaDescription?: string | null; hideHeader?: boolean; blocks: PublicBlock[] };
|
||||
}
|
||||
|
||||
const load = cache(async (company: string, slug: string): Promise<PublicPage> => {
|
||||
|
||||
@@ -12,6 +12,7 @@ type Props = { params: Promise<{ id: string }> };
|
||||
interface AdminPage {
|
||||
slug: string;
|
||||
title: string;
|
||||
hideHeader?: boolean;
|
||||
blocks: PublicBlock[];
|
||||
company?: Branding;
|
||||
}
|
||||
@@ -27,7 +28,7 @@ export default async function PagePreview({ params }: Props) {
|
||||
const banner = <p role="status" className="bg-warning/15 px-4 py-2 text-center text-sm font-medium">{t("PagesPreviewBanner")}</p>;
|
||||
return (
|
||||
<RequireModule pathname="/pages">
|
||||
<LandingPage company={page.company ?? { code: "", name: "" }} page={page} banner={banner} preview />
|
||||
<LandingPage company={page.company ?? { code: "", name: "" }} page={page} banner={banner} preview frameBase={`/pages/${id}/preview/frame`} />
|
||||
</RequireModule>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user