|
| 1 | +{/* |
| 2 | + DomoEmbed — reusable component for embedding Domo-hosted content in MDX pages. |
| 3 | +
|
| 4 | + USAGE |
| 5 | + ----- |
| 6 | + Import and drop into any MDX page: |
| 7 | +
|
| 8 | + import { DomoEmbed } from "/snippets/DomoEmbed.mdx"; |
| 9 | +
|
| 10 | + <DomoEmbed |
| 11 | + src="https://embed.domo.com/embed/pages/XXXXX" |
| 12 | + width="100%" |
| 13 | + initialHeight="600" |
| 14 | + /> |
| 15 | +
|
| 16 | + PROPS |
| 17 | + ----- |
| 18 | + src (required) — The Domo embed URL (cards or pages endpoint). |
| 19 | + width (optional) — CSS width of the iframe. Defaults to "100%". |
| 20 | + initialHeight (optional) — Starting height in pixels before auto-resize fires. Defaults to "600". |
| 21 | +
|
| 22 | + AUTO-RESIZE |
| 23 | + ----------- |
| 24 | + Domo's embed runtime sends a postMessage event containing the rendered page height. |
| 25 | + This component listens for that message and updates the iframe height automatically, |
| 26 | + eliminating scroll traps and double scrollbars. A 15px buffer is added to account |
| 27 | + for sub-pixel rounding and scrollbar gutter width. |
| 28 | +
|
| 29 | + For the underlying mechanism and plain-HTML equivalents, see: |
| 30 | + /portal/embed/embed-in-sites-and-apps/dynamic-iframe-height |
| 31 | +
|
| 32 | + NOTE: The embed URL must come from a Domo instance that allows embedding on this domain. |
| 33 | + If the iframe appears blank, check the source instance's CSP / embed allowlist settings. |
| 34 | +*/} |
| 35 | + |
| 36 | +import { useEffect, useRef } from "react"; |
| 37 | + |
| 38 | +export const DomoEmbed = ({ src, width = "100%", initialHeight = "600" }) => { |
| 39 | + const iframeRef = useRef(null); |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + function handleMessage(event) { |
| 43 | + if ( |
| 44 | + iframeRef.current && |
| 45 | + iframeRef.current.contentWindow === event.source && |
| 46 | + event.data && |
| 47 | + event.data.params && |
| 48 | + event.data.params.height |
| 49 | + ) { |
| 50 | + iframeRef.current.style.height = event.data.params.height + 15 + "px"; |
| 51 | + } |
| 52 | + } |
| 53 | + window.addEventListener("message", handleMessage); |
| 54 | + return () => window.removeEventListener("message", handleMessage); |
| 55 | + }, []); |
| 56 | + |
| 57 | + return ( |
| 58 | + <iframe |
| 59 | + ref={iframeRef} |
| 60 | + src={src} |
| 61 | + width={width} |
| 62 | + height={initialHeight} |
| 63 | + marginHeight="0" |
| 64 | + marginWidth="0" |
| 65 | + frameBorder="0" |
| 66 | + style={{ display: "block" }} |
| 67 | + /> |
| 68 | + ); |
| 69 | +}; |
0 commit comments