Optimised for iframe (#19573)

This commit is contained in:
Princi Vershwal 2024-01-24 20:20:32 +05:30 committed by GitHub
parent 1211ec3671
commit a10a64c633
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -14,7 +14,6 @@ const PortalFrame: React.FC<PortalFrameProps> = ({href, onDestroyed, selectedTab
const iframeRef = useRef<HTMLIFrameElement>(null);
const [hasLoaded, setHasLoaded] = useState<boolean>(false);
const [isInvisible, setIsInvisible] = useState<boolean>(true);
const makeVisible = useCallback(() => {
setTimeout(() => {
if (iframeRef.current) {
@ -22,29 +21,24 @@ const PortalFrame: React.FC<PortalFrameProps> = ({href, onDestroyed, selectedTab
}
}, 300);
}, [iframeRef]);
useEffect(() => {
const messageListener = (event: MessageEvent) => {
if (!href) {
return;
}
const originURL = new URL(event.origin);
if (originURL.origin === new URL(href).origin) {
if (event?.data?.type === 'portal-preview-ready') {
makeVisible();
}
}
};
window.addEventListener('message', messageListener, true);
return () => {
window.removeEventListener('message', messageListener, true);
onDestroyed?.();
};
}, [href, onDestroyed, makeVisible, hasLoaded]);
if (!href) {
return null;
}
@ -67,4 +61,4 @@ const PortalFrame: React.FC<PortalFrameProps> = ({href, onDestroyed, selectedTab
);
};
export default PortalFrame;
export default PortalFrame;