feat(pages): feature cards, steps, trust strip, CTA band and section looks #60

Merged
ngqile merged 8 commits from add-page-sections into main 2026-12-03 02:53:34 +00:00
6 changed files with 230 additions and 8 deletions
Showing only changes of commit f2f293ea58 - Show all commits

No files matched your search

+1 -1
View File
@@ -20,7 +20,7 @@ Design: `docs/superpowers/specs/2026-12-03-page-builder-design.md`, change 1. Bl
## Risks / Trade-offs
- A Dark or Accent band right after another band of the same look merges visually → acceptable; staff see it in the preview.
- `HTML` blocks with a look: the band surrounds the frame; harmless.
- `HTML` blocks ignore a look: a Custom HTML block is a full design of its own, so the editor does not offer the picker for it and the renderer draws no band.
- Rollback: pages saved with new types must be edited before the previous release saves them; their new sections render as nothing under it.
## Migration Plan
+1 -1
View File
@@ -9,7 +9,7 @@ Design: `design.md` and `docs/superpowers/specs/2026-12-03-page-builder-design.m
## 2. Portal
- [ ] 2.1 Model and renderer: types, `emptyBlock`, `BLOCK_LABELS`, icon map; `CmsBlocks` renders the four new types and the looks (full-width bands, readable text, Accent from the company colour); Default unchanged.
- [x] 2.1 Model and renderer: types, `emptyBlock`, `BLOCK_LABELS`, icon map; `CmsBlocks` renders the four new types and the looks (full-width bands, readable text, Accent from the company colour); Default unchanged.
- [ ] 2.2 Editor: add buttons, fields and card lists (add, remove, move) for the new types, the look select on every block, inline card errors; labels in `defaultMessages.ts` and `zh_CN.seed.json`, `npm run i18n:sql`, `sql/01-section-labels.sql` and `sql/99-rollback.sql`.
- Verify (2.1 to 2.2): `cd solvway-admin-portal && npx vitest run; echo VITEST=$? && npx tsc --noEmit && npm run lint`
@@ -0,0 +1,77 @@
import { render, screen, within } from "@testing-library/react";
import { describe, it, expect } from "vitest";
import { LandingPage, type PublicBlock } from "./CmsBlocks";
// add-page-sections: card sections, the CTA band, section looks and the image-less hero.
const company = { code: "INS", name: "INS Sdn Bhd", logoImageId: null, accentColor: "#FFEE00" };
const page = (blocks: PublicBlock[]) => ({ slug: "hotel", title: "Hotel day", blocks });
describe("LandingPage: sections", () => {
it("shows feature cards with their icons, titles and texts", () => {
render(<LandingPage company={company} page={page([{
type: "FEATURES", heading: "What we do", text: "One partner.",
cards: [{ icon: "wifi", title: "Managed WiFi", text: "Guests connect." }, { icon: "tv", title: "In-room TV", text: "Casting." }],
}])} />);
expect(screen.getByRole("heading", { name: "What we do" })).toBeInTheDocument();
expect(screen.getByText("One partner.")).toBeInTheDocument();
const items = screen.getAllByRole("listitem");
expect(items).toHaveLength(2);
expect(within(items[0]).getByRole("heading", { name: "Managed WiFi" })).toBeInTheDocument();
expect(items[0].querySelector("svg.lucide-wifi")).not.toBeNull();
expect(items[1].querySelector("svg.lucide-tv")).not.toBeNull();
});
it("numbers steps in order", () => {
render(<LandingPage company={company} page={page([{
type: "STEPS", heading: "How it works",
cards: [{ title: "Survey", text: "We visit." }, { title: "Install", text: "We fit it." }, { title: "Support", text: "We stay." }],
}])} />);
const list = screen.getByRole("list");
expect(list.tagName).toBe("OL");
const items = within(list).getAllByRole("listitem");
expect(items.map((li) => li.textContent)).toEqual(["1SurveyWe visit.", "2InstallWe fit it.", "3SupportWe stay."]);
});
it("shows a trust strip of short items", () => {
render(<LandingPage company={company} page={page([{ type: "TRUST", cards: [{ title: "Certified with LG" }, { title: "Local support" }] }])} />);
expect(screen.getAllByRole("listitem").map((li) => li.textContent)).toEqual(["Certified with LG", "Local support"]);
});
it("shows a call-to-action band with its button, and drops an unsafe one", () => {
render(<LandingPage company={company} page={page([
{ type: "CTA", heading: "Ready?", text: "Talk to us.", buttonLabel: "Get a quote", buttonUrl: "#signup" },
{ type: "CTA", heading: "Bad", buttonLabel: "Evil", buttonUrl: "javascript:alert(1)" },
])} />);
expect(screen.getByRole("heading", { name: "Ready?" })).toBeInTheDocument();
expect(screen.getByRole("link", { name: "Get a quote" })).toHaveAttribute("href", "#signup");
expect(screen.queryByRole("link", { name: "Evil" })).not.toBeInTheDocument();
});
it("draws a non-default look as a full-width band and leaves default blocks as before", () => {
render(<LandingPage company={company} page={page([
{ type: "CTA", heading: "Dark", buttonLabel: "Go", buttonUrl: "/x", look: "DARK" },
{ type: "CTA", heading: "Accent", buttonLabel: "Go", buttonUrl: "/x", look: "ACCENT" },
{ type: "CTA", heading: "Muted", buttonLabel: "Go", buttonUrl: "/x", look: "MUTED" },
{ type: "CTA", heading: "Plain", buttonLabel: "Go", buttonUrl: "/x" },
{ type: "CTA", heading: "Junk", buttonLabel: "Go", buttonUrl: "/x", look: "NEON" as never },
])} />);
const band = (name: string) => screen.getByRole("heading", { name }).closest("[data-look]");
expect(band("Dark")).toHaveAttribute("data-look", "DARK");
expect(band("Dark")).toHaveClass("bg-foreground", "text-background");
expect(band("Accent")).toHaveClass("bg-primary", "text-primary-foreground");
expect(band("Muted")).toHaveClass("bg-muted");
expect(band("Plain")).toBeNull();
expect(band("Junk")).toBeNull();
// On an accent band the button flips, so it never matches the band's colour.
expect(within(band("Accent") as HTMLElement).getByRole("link", { name: "Go" })).toHaveClass("bg-background", "text-foreground");
});
it("shows a hero without an image as text on its look's background", () => {
render(<LandingPage company={company} page={page([{ type: "HERO", heading: "No picture", buttonLabel: "Join", buttonUrl: "#signup", look: "DARK" }])} />);
expect(screen.getByRole("heading", { name: "No picture" })).toBeInTheDocument();
expect(document.querySelector("main img")).toBeNull();
expect(screen.getByRole("link", { name: "Join" })).toBeInTheDocument();
// On a band the hero takes the band's colours; its own grey box would put light text on a light background.
expect(screen.getByRole("heading", { name: "No picture" }).closest("section")).not.toHaveClass("bg-muted");
});
});
@@ -1,5 +1,5 @@
import type { CSSProperties, ReactNode } from "react";
import { anchorId, contrastText, imageSrc, isSafeLink, type CmsBlock } from "@/app/lib/cmsPages";
import { ICONS, LOOKS, anchorId, contrastText, imageSrc, isSafeLink, type CmsBlock, type Look } from "@/app/lib/cmsPages";
import type { RegistrationInfo } from "@/app/lib/services/publicRegistration";
import { SafeHtml } from "./SafeHtml";
import { LandingForm } from "./LandingForm";
@@ -20,12 +20,21 @@ export interface Branding {
const PROSE = "space-y-3 text-base leading-relaxed [&_a]:text-primary [&_a]:underline [&_blockquote]:border-l-2 [&_blockquote]:pl-3 [&_h2]:text-2xl [&_h2]:font-semibold [&_h3]:text-xl [&_h3]:font-semibold [&_ol]:list-decimal [&_ol]:pl-5 [&_ul]:list-disc [&_ul]:pl-5";
// add-page-sections: non-default looks are full-width bands. On an accent band the button flips to stay visible.
const BANDS: Record<Exclude<Look, "DEFAULT">, string> = {
MUTED: "bg-muted text-foreground",
DARK: "bg-foreground text-background",
ACCENT: "bg-primary text-primary-foreground",
};
const bandOf = (look: unknown) => (LOOKS.includes(look as Look) && look !== "DEFAULT" ? (look as Exclude<Look, "DEFAULT">) : null);
function ButtonLink({ block }: { block: CmsBlock }) {
if (!block.buttonLabel || !isSafeLink(block.buttonUrl)) return null;
const external = /^https:/i.test(block.buttonUrl!);
const colours = bandOf(block.look) === "ACCENT" ? "bg-background text-foreground" : "bg-primary text-primary-foreground";
return (
<a href={block.buttonUrl} {...(external ? { target: "_blank", rel: "noopener noreferrer" } : {})}
className="inline-flex h-11 items-center rounded-md bg-primary px-5 text-sm font-medium text-primary-foreground hover:opacity-90 focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none">
className={`inline-flex h-11 items-center rounded-md px-5 text-sm font-medium hover:opacity-90 focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none ${colours}`}>
{block.buttonLabel}
</a>
);
@@ -40,7 +49,7 @@ function Block({ block, index, company, slug, preview }: { block: PublicBlock; i
switch (block.type) {
case "HERO":
return (
<section className="relative overflow-hidden rounded-lg bg-muted">
<section className={`relative overflow-hidden rounded-lg ${bandOf(block.look) ? "" : "bg-muted"}`}>
<BlockImage id={block.imageId} className="absolute inset-0 h-full w-full object-cover" />
<div className={block.imageId ? "relative flex min-h-72 flex-col justify-end gap-3 bg-gradient-to-t from-black/80 via-black/50 to-transparent p-6 text-white sm:min-h-96" : "flex flex-col gap-3 p-6"}>
{block.heading ? <h2 className="text-3xl font-semibold sm:text-4xl">{block.heading}</h2> : null}
@@ -83,6 +92,60 @@ function Block({ block, index, company, slug, preview }: { block: PublicBlock; i
<LandingForm companyCode={company} slug={slug} index={index} info={block.closed ? undefined : block.form} preview={preview} />
</section>
);
case "FEATURES":
return (
<section className="space-y-6">
<div className="max-w-2xl space-y-2">
{block.heading ? <h2 className="text-2xl font-semibold sm:text-3xl">{block.heading}</h2> : null}
{block.text ? <p className="text-lg opacity-80">{block.text}</p> : null}
</div>
<ul className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
{(block.cards ?? []).map((card, i) => {
const Icon = card.icon ? ICONS[card.icon] : undefined;
return (
<li key={i} className="space-y-2 rounded-lg border bg-card p-5 text-card-foreground shadow-card">
{Icon ? <Icon aria-hidden="true" className="size-7 text-primary" /> : null}
<h3 className="text-lg font-semibold">{card.title}</h3>
<p className="text-muted-foreground">{card.text}</p>
</li>
);
})}
</ul>
</section>
);
case "STEPS":
return (
<section className="space-y-6">
{block.heading ? <h2 className="text-2xl font-semibold sm:text-3xl">{block.heading}</h2> : null}
<ol className="grid gap-6 sm:grid-cols-2 lg:grid-cols-3">
{(block.cards ?? []).map((card, i) => (
<li key={i} className="flex gap-4">
<span aria-hidden="true" className="flex size-10 shrink-0 items-center justify-center rounded-full border-2 border-current text-lg font-semibold">{i + 1}</span>
<div className="space-y-1">
<h3 className="text-lg font-semibold">{card.title}</h3>
<p className="opacity-80">{card.text}</p>
</div>
</li>
))}
</ol>
</section>
);
case "TRUST":
return (
<section>
<ul className="flex flex-wrap items-center justify-center gap-x-8 gap-y-2 text-center font-medium">
{(block.cards ?? []).map((card, i) => <li key={i}>{card.title}</li>)}
</ul>
</section>
);
case "CTA":
return (
<section className="flex flex-col items-center gap-4 py-4 text-center">
<h2 className="text-2xl font-semibold sm:text-3xl">{block.heading}</h2>
{block.text ? <p className="max-w-2xl text-lg opacity-80">{block.text}</p> : null}
<ButtonLink block={block} />
</section>
);
default:
return null;
}
@@ -90,6 +153,17 @@ function Block({ block, index, company, slug, preview }: { block: PublicBlock; i
const WIDTH = "mx-auto w-full max-w-5xl px-4";
/** A block in the page width; with a non-default look, inside a full-width band of that colour. */
function Section({ block, children }: { block: PublicBlock; children: ReactNode }) {
const band = bandOf(block.look);
if (!band) return <div id={anchorId(block.anchor)} className={WIDTH}>{children}</div>;
return (
<div id={anchorId(block.anchor)} data-look={band} className={`py-12 ${BANDS[band]}`}>
<div className={WIDTH}>{children}</div>
</div>
);
}
/**
* 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.
@@ -124,7 +198,7 @@ export function LandingPage({ company, page, banner, preview, frameBase }: {
<div className="space-y-10">
{page.blocks.map((block, i) => block.type === "HTML"
? <HtmlFrame key={i} id={anchorId(block.anchor)} src={`${frames}/${i}`} title={page.title} />
: <div key={i} id={anchorId(block.anchor)} className={WIDTH}><Block block={block} index={i} company={company.code} slug={page.slug} preview={preview} /></div>)}
: <Section key={i} block={block}><Block block={block} index={i} company={company.code} slug={page.slug} preview={preview} /></Section>)}
</div>
</main>
</div>
@@ -0,0 +1,32 @@
import { describe, it, expect } from "vitest";
import { BLOCK_TYPES, ICONS, LOOKS, emptyBlock, emptyPage, pageBody } from "./cmsPages";
// add-page-sections: the new block types in the editor model and the save body.
describe("cmsPages: sections", () => {
it("offers the four new types, the server's icon list and looks", () => {
expect(BLOCK_TYPES).toEqual(expect.arrayContaining(["FEATURES", "STEPS", "TRUST", "CTA"]));
expect(Object.keys(ICONS).sort()).toEqual(["calendar", "cloud", "headset", "lock", "monitor", "phone", "shield-check", "star", "tv", "users", "wifi", "wrench"]);
expect(LOOKS).toEqual(["DEFAULT", "MUTED", "DARK", "ACCENT"]);
});
it("starts each card section with the minimum cards and only the fields its type uses", () => {
expect(emptyBlock("FEATURES")).toEqual({ type: "FEATURES", heading: "", text: "", cards: [{ icon: "star", title: "", text: "" }, { icon: "star", title: "", text: "" }] });
expect(emptyBlock("STEPS")).toEqual({ type: "STEPS", heading: "", cards: [{ title: "", text: "" }, { title: "", text: "" }] });
expect(emptyBlock("TRUST")).toEqual({ type: "TRUST", cards: [{ title: "" }, { title: "" }] });
expect(emptyBlock("CTA")).toEqual({ type: "CTA", heading: "", text: "", buttonLabel: "", buttonUrl: "" });
});
it("trims card fields and drops a default look on save", () => {
const body = pageBody({
...emptyPage(), title: "T", slug: "t",
blocks: [
{ type: "STEPS", heading: " How ", look: "DEFAULT", cards: [{ title: " Survey ", text: " We visit. " }, { title: "Fit", text: "" }] },
{ type: "CTA", heading: "Go", look: "DARK", buttonLabel: "Join", buttonUrl: "#signup" },
],
});
expect(body.blocks).toEqual([
{ type: "STEPS", heading: "How", cards: [{ title: "Survey", text: "We visit." }, { title: "Fit", text: "" }] },
{ type: "CTA", heading: "Go", look: "DARK", buttonLabel: "Join", buttonUrl: "#signup" },
]);
});
});
+41 -2
View File
@@ -1,9 +1,21 @@
// 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" | "HTML";
import { Calendar, Cloud, Headset, Lock, Monitor, Phone, ShieldCheck, Star, Tv, Users, Wifi, Wrench, type LucideIcon } from "lucide-react";
export const BLOCK_TYPES: BlockType[] = ["HERO", "RICH_TEXT", "IMAGE_TEXT", "FAQ", "FORM", "HTML"];
export type BlockType = "HERO" | "RICH_TEXT" | "IMAGE_TEXT" | "FAQ" | "FORM" | "HTML" | "FEATURES" | "STEPS" | "TRUST" | "CTA";
export const BLOCK_TYPES: BlockType[] = ["HERO", "RICH_TEXT", "IMAGE_TEXT", "FEATURES", "STEPS", "TRUST", "CTA", "FAQ", "FORM", "HTML"];
/** Feature-card icons by the name services-counter accepts (add-page-sections). */
export const ICONS: Record<string, LucideIcon> = {
wifi: Wifi, phone: Phone, tv: Tv, monitor: Monitor, lock: Lock, "shield-check": ShieldCheck,
cloud: Cloud, wrench: Wrench, headset: Headset, users: Users, calendar: Calendar, star: Star,
};
/** Section looks: DEFAULT renders as before; the others are full-width bands. */
export const LOOKS = ["DEFAULT", "MUTED", "DARK", "ACCENT"] as const;
export type Look = (typeof LOOKS)[number];
export const BLOCK_LABELS: Record<BlockType, string> = {
HERO: "PagesBlockHero",
@@ -12,8 +24,19 @@ export const BLOCK_LABELS: Record<BlockType, string> = {
FAQ: "PagesBlockFaq",
FORM: "PagesBlockForm",
HTML: "PagesBlockHtml",
FEATURES: "PagesBlockFeatures",
STEPS: "PagesBlockSteps",
TRUST: "PagesBlockTrust",
CTA: "PagesBlockCta",
};
/** A FEATURES (icon, title, text), STEPS (title, text) or TRUST (title) card. */
export interface CmsCard {
icon?: string;
title: string;
text?: string;
}
export interface CmsFaqItem {
question: string;
answer: string;
@@ -31,6 +54,8 @@ export interface CmsBlock {
items?: CmsFaqItem[];
/** Optional link name: the block's id on the page, the target of a #name link (add-page-anchor-links). */
anchor?: string;
look?: Look;
cards?: CmsCard[];
/** The FORM block's event (a Snowflake uuid, kept as a string). */
eventUuid?: string | null;
}
@@ -80,6 +105,14 @@ export function emptyBlock(type: BlockType): CmsBlock {
return { type, heading: "", eventUuid: null };
case "HTML":
return { type, html: "" };
case "FEATURES":
return { type, heading: "", text: "", cards: [{ icon: "star", title: "", text: "" }, { icon: "star", title: "", text: "" }] };
case "STEPS":
return { type, heading: "", cards: [{ title: "", text: "" }, { title: "", text: "" }] };
case "TRUST":
return { type, cards: [{ title: "" }, { title: "" }] };
case "CTA":
return { type, heading: "", text: "", buttonLabel: "", buttonUrl: "" };
default:
return { type };
}
@@ -113,6 +146,12 @@ export function pageBody(draft: CmsPageDraft) {
out.items = (val as CmsFaqItem[]).map((it) => ({ question: it.question.trim(), answer: it.answer.trim() }));
continue;
}
if (key === "cards") {
// Trimmed but kept, so a blank card field is refused at its own path.
out.cards = (val as CmsCard[]).map((c) => Object.fromEntries(Object.entries(c).map(([k, x]) => [k, typeof x === "string" ? x.trim() : x])));
continue;
}
if (key === "look" && val === "DEFAULT") continue;
const v = typeof val === "string" ? val.trim() : val;
if (!blank(v)) out[key] = v;
}