diff --git a/app/soapbox/components/safe-embed.tsx b/app/soapbox/components/safe-embed.tsx index 2404ea7ae..23fbded14 100644 --- a/app/soapbox/components/safe-embed.tsx +++ b/app/soapbox/components/safe-embed.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useRef } from 'react'; +import React, { useCallback, useEffect, useRef, useState } from 'react'; interface ISafeEmbed { /** Styles for the outer frame element. */ @@ -19,6 +19,13 @@ const SafeEmbed: React.FC = ({ html, }) => { const iframe = useRef(null); + const [height, setHeight] = useState(undefined); + + const handleMessage = useCallback((e: MessageEvent) => { + if (e.data?.type === 'setHeight') { + setHeight(e.data?.height); + } + }, []); useEffect(() => { const iframeDocument = iframe.current?.contentWindow?.document; @@ -29,11 +36,19 @@ const SafeEmbed: React.FC = ({ iframeDocument.close(); iframeDocument.body.style.margin = '0'; + iframe.current?.contentWindow?.addEventListener('message', handleMessage); + const innerFrame = iframeDocument.querySelector('iframe'); if (innerFrame) { innerFrame.width = '100%'; } } + + return () => { + if (iframeDocument) { + iframe.current?.contentWindow?.removeEventListener('message', handleMessage); + } + }; }, [iframe.current, html]); return ( @@ -41,6 +56,7 @@ const SafeEmbed: React.FC = ({ ref={iframe} className={className} sandbox={sandbox} + height={height} title={title} /> );