From 18eb4ad22e8275039aa5b06c74b4f9ca75c5c3e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Rialland?= Date: Tue, 30 Aug 2022 15:58:26 +0200 Subject: [PATCH] =?UTF-8?q?Fix=20a=20bug=20on=20resize=20in=20d=C3=A9velop?= =?UTF-8?q?peur/iframe=20page?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- site/source/hooks/useIframeResizer.ts | 12 ++++++++++++ site/source/pages/integration/Iframe.tsx | 6 +----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/site/source/hooks/useIframeResizer.ts b/site/source/hooks/useIframeResizer.ts index c4ae5436c..c9686fa72 100644 --- a/site/source/hooks/useIframeResizer.ts +++ b/site/source/hooks/useIframeResizer.ts @@ -1,11 +1,23 @@ import { useEffect } from 'react' +function isIframe() { + try { + return window.self !== window.top + } catch (e) { + return true + } +} + export function useIframeResizer() { useEffect(() => { // The code below communicate with the iframe.js script on a host site // to automatically resize the iframe when its inner content height // change. + if (!isIframe()) { + return + } + const minHeight = 700 // Also used in iframe.js const observer = new ResizeObserver(([entry]) => { const value = Math.max(minHeight, entry.contentRect.height) diff --git a/site/source/pages/integration/Iframe.tsx b/site/source/pages/integration/Iframe.tsx index 24f8de9d1..b4416d0b7 100644 --- a/site/source/pages/integration/Iframe.tsx +++ b/site/source/pages/integration/Iframe.tsx @@ -65,11 +65,7 @@ function IntegrationCustomizer() { window.addEventListener( 'message', function (evt: MessageEvent<{ kind: string; value: string }>) { - if ( - iframeRef.current && - evt.data.kind === 'resize-height' && - iframeRef.current.contentDocument?.readyState === 'complete' - ) { + if (iframeRef.current && evt.data.kind === 'resize-height') { iframeRef.current.style.height = evt.data.value + 'px' } }