Files
co-lite/solvway-admin-portal/app/components/pages/CmsBlocks.tsx
T
ngqileandClaude Opus 5.5 b55b8d2d86 feat(pages): a hero can run full width with points and a form card heading
A full-width hero spans the window with a larger serif headline and a lighter
left-to-right shade. It may list up to 6 points and give its form card a heading
and text. Full-width neighbours join with no gap. Custom HTML frames now shrink
to 20 px and re-report their height when the page asks, so a short top bar no
longer leaves a blank band.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
2026-12-04 09:46:32 +08:00

296 lines
17 KiB
TypeScript

import type { CSSProperties, ReactNode } from "react";
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";
import { HtmlFrame } from "./HtmlFrame";
import { EditContext, EditSection, HeroShade, HtmlSpot, ImageSpot, RichSpot, Text, stopNavigation, type PageEdit } from "./CmsEdit";
// 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. 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 };
export interface Branding {
code: string;
name: string;
logoImageId?: number | null;
accentColor?: string | null;
}
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, index, edit }: { block: CmsBlock; index: number; edit?: boolean }) {
// In edit mode a button with a label shows whatever its link, so its label can be typed in place.
if (!block.buttonLabel || (!edit && !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 px-5 text-sm font-medium hover:opacity-90 focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none ${colours}`}>
<Text as="span" index={index} path="buttonLabel" value={block.buttonLabel} max={40} code="PagesButtonLabel" />
</a>
);
}
function Block({ block, index, company, slug, preview, edit }: { block: PublicBlock; index: number; company: string; slug: string; preview?: boolean; edit?: boolean }) {
const image = block.imageId ? imageSrc(block.imageId) : undefined;
const button = <ButtonLink block={block} index={index} edit={edit} />;
switch (block.type) {
case "HERO": {
const wide = !!block.fullWidth;
const points = block.cards?.length ? (
<ul className="space-y-2">
{block.cards.map((card, i) => (
<li key={i} className="flex items-baseline gap-3 before:size-2 before:shrink-0 before:rounded-full before:bg-primary">
<Text as="span" index={index} path={`cards.${i}.title`} value={card.title} max={80} code="PagesPointN" n={i + 1} />
</li>
))}
</ul>
) : null;
const words = (
<>
<Text as="h2" index={index} path="heading" value={block.heading} max={120} code="PagesHeading"
className={wide ? "font-serif text-4xl leading-tight font-semibold sm:text-5xl" : "text-3xl font-semibold sm:text-4xl"} />
<Text as="p" index={index} path="text" value={block.text} max={300} code="PagesText" className="max-w-2xl text-lg" />
{points}
<div>{button}</div>
</>
);
// add-hero-signup-form: with an event, the sign-up form sits in a card beside the text (below it on phones).
// add-hero-full-width: the card may open with its own heading and text.
const card = block.eventUuid ? (
<div className="space-y-4 rounded-lg border bg-card p-6 text-card-foreground shadow-card">
{block.formHeading || block.formText || edit ? (
<div className="space-y-1">
<Text as="h3" index={index} path="formHeading" value={block.formHeading} max={80} code="PagesFormHeading" className="font-serif text-2xl font-semibold" />
<Text as="p" index={index} path="formText" value={block.formText} max={200} code="PagesFormText" className="text-sm text-muted-foreground" />
</div>
) : null}
<LandingForm companyCode={company} slug={slug} index={index} info={block.closed ? undefined : block.form} preview={preview} />
</div>
) : null;
const back = bandOf(block.look) ? "" : "bg-muted";
if (wide) {
// Edge to edge; the shade is darkest behind the text and clears toward the far side. The text is centred in the
// hero's opening height, not beside a long form, so it stays in the first screen.
const grid = `mx-auto grid w-full max-w-6xl items-start gap-8 px-4 pt-16 pb-12 ${card ? "md:grid-cols-[minmax(0,1fr)_minmax(0,28rem)]" : ""}`;
return (
<section className={`relative overflow-hidden ${back}`}>
<ImageSpot index={index} src={image} className="absolute inset-0 h-full w-full object-cover" corner />
<HeroShade index={index} image={!!image} shaded="relative bg-gradient-to-r from-black/75 via-black/45 to-black/10 text-white" plain="">
<div className={grid}>
<div className="flex flex-col justify-center gap-4 md:min-h-[32rem]">{words}</div>
{card}
</div>
</HeroShade>
</section>
);
}
if (card) {
// Top padding leaves room for the editor's image button in the corner.
const grid = "grid items-start gap-6 px-6 pt-16 pb-6 md:grid-cols-[minmax(0,1fr)_minmax(0,26rem)] md:px-10 md:pb-10";
return (
<section className={`relative overflow-hidden rounded-lg ${back}`}>
<ImageSpot index={index} src={image} className="absolute inset-0 h-full w-full object-cover" corner />
<HeroShade index={index} image={!!image} shaded={`relative ${grid} bg-gradient-to-r from-black/80 via-black/60 to-black/40 text-white sm:min-h-96`} plain={grid}>
<div className="flex flex-col gap-3">{words}</div>
{card}
</HeroShade>
</section>
);
}
return (
<section className={`relative overflow-hidden rounded-lg ${back}`}>
<ImageSpot index={index} src={image} className="absolute inset-0 h-full w-full object-cover" corner />
<HeroShade index={index} image={!!image}
shaded="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"
plain="flex flex-col gap-3 p-6">
{words}
</HeroShade>
</section>
);
}
case "RICH_TEXT":
return block.html || edit ? (
<section><RichSpot index={index} html={block.html} className={PROSE}>{block.html ? <SafeHtml html={block.html} className={PROSE} /> : null}</RichSpot></section>
) : null;
case "IMAGE_TEXT":
return (
<section className="grid items-center gap-6 md:grid-cols-2">
{image || edit ? (
<div className={block.imagePosition === "RIGHT" ? "md:order-2" : undefined}>
<ImageSpot index={index} src={image} className="w-full rounded-lg object-cover" />
</div>
) : null}
<div className="space-y-3">
<Text as="h2" index={index} path="heading" value={block.heading} max={120} code="PagesHeading" className="text-2xl font-semibold" />
<RichSpot index={index} html={block.html} className={PROSE}>{block.html ? <SafeHtml html={block.html} className={PROSE} /> : null}</RichSpot>
{button}
</div>
</section>
);
case "FAQ":
return block.items?.length ? (
<section className="space-y-3">
<Text as="h2" index={index} path="heading" value={block.heading} max={120} code="PagesHeading" className="text-2xl font-semibold" />
<div className="divide-y rounded-lg border">
{block.items.map((item, i) => {
const question = <Text as="span" index={index} path={`items.${i}.question`} value={item.question} max={300} code="PagesQuestionN" n={i + 1} />;
const answer = <Text as="p" index={index} path={`items.${i}.answer`} value={item.answer} max={2000} code="PagesAnswerN" n={i + 1} multiline className="mt-2 whitespace-pre-line text-muted-foreground" />;
// In edit mode every answer shows: a <summary> would toggle on a click or a typed space.
return edit ? (
<div key={i} className="p-4"><p className="font-medium">{question}</p>{answer}</div>
) : (
<details key={i} name={`faq-${index}`} className="group p-4">
<summary className="cursor-pointer font-medium">{item.question}</summary>
{answer}
</details>
);
})}
</div>
</section>
) : null;
case "FORM":
return (
<section className="mx-auto w-full max-w-md space-y-4 rounded-lg border bg-card p-6 shadow-card">
<Text as="h2" index={index} path="heading" value={block.heading} max={120} code="PagesHeading" className="text-2xl font-semibold" />
<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">
<Text as="h2" index={index} path="heading" value={block.heading} max={120} code="PagesHeading" className="text-2xl font-semibold sm:text-3xl" />
<Text as="p" index={index} path="text" value={block.text} max={300} code="PagesText" className="text-lg opacity-80" />
</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}
<Text as="h3" index={index} path={`cards.${i}.title`} value={card.title} max={80} code="PagesCardTitle" className="text-lg font-semibold" />
<Text as="p" index={index} path={`cards.${i}.text`} value={card.text} max={300} code="PagesCardText" className="text-muted-foreground" />
</li>
);
})}
</ul>
</section>
);
case "STEPS":
return (
<section className="space-y-6">
<Text as="h2" index={index} path="heading" value={block.heading} max={120} code="PagesHeading" className="text-2xl font-semibold sm:text-3xl" />
<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">
<Text as="h3" index={index} path={`cards.${i}.title`} value={card.title} max={80} code="PagesCardTitle" className="text-lg font-semibold" />
<Text as="p" index={index} path={`cards.${i}.text`} value={card.text} max={300} code="PagesCardText" className="opacity-80" />
</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}><Text as="span" index={index} path={`cards.${i}.title`} value={card.title} max={80} code="PagesCardTitle" /></li>)}
</ul>
</section>
);
case "CTA":
return (
<section className="flex flex-col items-center gap-4 py-4 text-center">
<Text as="h2" index={index} path="heading" value={block.heading} max={120} code="PagesHeading" className="text-2xl font-semibold sm:text-3xl" />
<Text as="p" index={index} path="text" value={block.text} max={300} code="PagesText" className="max-w-2xl text-lg opacity-80" />
{button}
</section>
);
default:
return null;
}
}
const WIDTH = "mx-auto w-full max-w-5xl px-4";
/** Blocks that run the full window width: Custom HTML, and a full-width hero. Two of them in a row join with no gap. */
const bleeds = (block: PublicBlock) => block.type === "HTML" || (block.type === "HERO" && !!block.fullWidth);
/** A block in the page width; with a non-default look, inside a full-width band of that colour. */
function Section({ block, index, children }: { block: PublicBlock; index: number; children: ReactNode }) {
const band = bandOf(block.look);
// add-hero-full-width: a full-width hero sets its own width.
if (bleeds(block)) return <EditSection index={index}><div id={anchorId(block.anchor)}>{children}</div></EditSection>;
if (!band) return <EditSection index={index}><div id={anchorId(block.anchor)} className={WIDTH}>{children}</div></EditSection>;
return (
<EditSection index={index}>
<div id={anchorId(block.anchor)} data-look={band} className={`py-12 ${BANDS[band]}`}>
<div className={WIDTH}>{children}</div>
</div>
</EditSection>
);
}
/**
* 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, edit }: {
company: Branding;
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;
/** The on-page editor's canvas (add-page-visual-editor): text edits in place and nothing navigates. */
edit?: PageEdit;
}) {
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;
const body = (
<div style={style} className="min-h-svh bg-background" onClickCapture={edit ? stopNavigation : undefined}>
{banner}
{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
<img src={imageSrc(company.logoImageId)} alt={company.name} className="h-10 w-auto" />
) : (
<span className="text-lg font-semibold">{company.name}</span>
)}
</div>
</header>}
<main className={page.hideHeader ? "pb-8" : "py-8"}>
<h1 className={page.hideHeader ? "sr-only" : `${WIDTH} mb-10 text-3xl font-semibold sm:text-4xl`}>{page.title}</h1>
<div>
{page.blocks.map((block, i) => (
<div key={i} className={i > 0 && !(bleeds(block) && bleeds(page.blocks[i - 1])) ? "mt-10" : undefined}>
{block.type === "HTML"
? <HtmlSpot index={i}><HtmlFrame id={anchorId(block.anchor)} src={`${frames}/${i}${edit ? `?v=${edit.frameVersion}` : ""}`} title={page.title} /></HtmlSpot>
: <Section block={block} index={i}><Block block={block} index={i} company={company.code} slug={page.slug} preview={preview} edit={!!edit} /></Section>}
</div>
))}
</div>
</main>
</div>
);
return edit ? <EditContext.Provider value={edit}>{body}</EditContext.Provider> : body;
}