Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
20 lines
978 B
TypeScript
20 lines
978 B
TypeScript
"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` }} />;
|
|
}
|