Files
co-lite/solvway-admin-portal/app/components/pages/HtmlFrame.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

33 lines
2.0 KiB
TypeScript

"use client";
import { useEffect, useRef, useState } from "react";
import { FRAME_ASK_MESSAGE, FRAME_SANDBOX, frameAnchor, nextFrameHeight, START_HEIGHT, type FrameHeight } from "@/app/lib/frameHtml";
// add-hero-full-width: the frame may have reported before this page listened; ask on mount, and again when it loads.
const ask = (frame: HTMLIFrameElement | null) => frame?.contentWindow?.postMessage({ type: FRAME_ASK_MESSAGE }, "*");
/** A Custom HTML block: the sealed frame, sized by height reports and scrolled to by #name requests from that frame's own window only. */
export function HtmlFrame({ id, src, title }: { id?: string; src: string; title: string }) {
const ref = useRef<HTMLIFrameElement>(null);
const [size, setSize] = useState<FrameHeight>({ height: START_HEIGHT, steps: [] });
useEffect(() => {
// The frame's parent window: the page's, or the editor canvas's when rendered into it (add-page-visual-editor).
const host = ref.current?.ownerDocument.defaultView;
if (!host) return;
const onMessage = (e: MessageEvent) => {
const frame = ref.current;
if (!frame || e.source !== frame.contentWindow) return;
const anchor = frameAnchor(e.data);
if (!anchor) return setSize((s) => nextFrameHeight(s, e.data));
// add-page-anchor-links: the frame's own target first, else a named block of this page.
if (anchor.y !== null) return host.scrollTo({ top: frame.getBoundingClientRect().top + host.scrollY + anchor.y, behavior: "smooth" });
const block = host.document.getElementById(anchor.name);
if (block?.closest("main")) block.scrollIntoView({ behavior: "smooth" });
};
host.addEventListener("message", onMessage);
ask(ref.current);
return () => host.removeEventListener("message", onMessage);
}, []);
return <iframe ref={ref} id={id} src={src} title={title} sandbox={FRAME_SANDBOX} loading="lazy" onLoad={(e) => ask(e.currentTarget)} className="block w-full border-0" style={{ height: `${size.height}px` }} />;
}