From 6a0a6613c1c556cf396dfd49aeb63022d181d6dd Mon Sep 17 00:00:00 2001 From: ngqile Date: Wed, 2 Dec 2026 17:58:15 +0800 Subject: [PATCH] feat(pages): render Custom HTML blocks as sized sealed frames and honour hide header Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs --- .../changes/add-custom-html-block/tasks.md | 2 +- .../components/pages/CmsBlocks.html.test.tsx | 43 +++++++++++++++++++ .../app/components/pages/CmsBlocks.tsx | 30 +++++++++---- .../app/components/pages/HtmlFrame.tsx | 19 ++++++++ solvway-admin-portal/app/lib/cmsPages.ts | 3 +- .../app/p/[company]/[slug]/page.tsx | 2 +- .../app/pages/[id]/preview/page.tsx | 3 +- 7 files changed, 89 insertions(+), 13 deletions(-) create mode 100644 solvway-admin-portal/app/components/pages/CmsBlocks.html.test.tsx create mode 100644 solvway-admin-portal/app/components/pages/HtmlFrame.tsx diff --git a/openspec/changes/add-custom-html-block/tasks.md b/openspec/changes/add-custom-html-block/tasks.md index a43fdf8..271fc3e 100644 --- a/openspec/changes/add-custom-html-block/tasks.md +++ b/openspec/changes/add-custom-html-block/tasks.md @@ -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: diff --git a/solvway-admin-portal/app/components/pages/CmsBlocks.html.test.tsx b/solvway-admin-portal/app/components/pages/CmsBlocks.html.test.tsx new file mode 100644 index 0000000..6711a1e --- /dev/null +++ b/solvway-admin-portal/app/components/pages/CmsBlocks.html.test.tsx @@ -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: "

Designed

" }; + +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(); + 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(); + expect(frame()).toHaveAttribute("src", "/pages/5/preview/frame/0"); + }); + + it("grows to its frame's reported height and ignores other windows", () => { + render(); + 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(); + 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"); + }); +}); diff --git a/solvway-admin-portal/app/components/pages/CmsBlocks.tsx b/solvway-admin-portal/app/components/pages/CmsBlocks.tsx index 66e4cf1..42de083 100644 --- a/solvway-admin-portal/app/components/pages/CmsBlocks.tsx +++ b/solvway-admin-portal/app/components/pages/CmsBlocks.tsx @@ -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 (
{banner} -
+ {page.hideHeader ? null :
{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 }: { {company.name} )}
-
-
-

{page.title}

- {page.blocks.map((block, i) => )} +
} +
+

{page.title}

+ {page.blocks.map((block, i) => block.type === "HTML" + ? + :
)}
); diff --git a/solvway-admin-portal/app/components/pages/HtmlFrame.tsx b/solvway-admin-portal/app/components/pages/HtmlFrame.tsx new file mode 100644 index 0000000..e64922a --- /dev/null +++ b/solvway-admin-portal/app/components/pages/HtmlFrame.tsx @@ -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(null); + const [size, setSize] = useState({ 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