"use client"; import React, { useEffect, useRef } from "react"; import { IframeEmbedProps, bgColorMap } from "./types"; /** * IframeEmbed — A generic, responsive iframe component for embedding third-party content * such as Brand Featured badges, widgets, or any external HTML page. * * Features: * - **Auto-resize:** Listens for `postMessage` events with an `{ iframeHeight: number }` payload * from the embedded page and adjusts the iframe height dynamically. * - **Background color:** Supports predefined background colors (`gray90`, `green`, `white`) * behind the iframe, defaulting to transparent. * - **Lazy loading:** The iframe uses `loading="lazy"` for performance. * - **Anchor support:** An optional `anchorId` is applied to the wrapping `
` for * deep-linking or scroll-to navigation. * * @example * ```tsx * * ``` */ export const IframeEmbed: React.FC = ({ iframeUrl, anchorId, backgroundColor, height, }) => { const iframeRef = useRef(null); useEffect(() => { if (!iframeUrl) return; const handleMessage = (event: MessageEvent) => { const height = event.data?.iframeHeight; if ( typeof height === "number" && Number.isFinite(height) && height > 0 && height <= 10000 && iframeRef.current ) { iframeRef.current.style.height = `${height}px`; } }; window.addEventListener("message", handleMessage); return () => window.removeEventListener("message", handleMessage); }, [iframeUrl]); if (!iframeUrl) return null; return (