From ef1c19820b305c53ee664b77f4c9b17269a1cbe4 Mon Sep 17 00:00:00 2001 From: Ronald Langeveld Date: Wed, 21 Feb 2024 17:28:47 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fixed=20iFrame=20not=20keeping?= =?UTF-8?q?=20scroll=20position=20on=20update=20(#19725)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit no issue - Added position tracking in the iFrame to allow it to remain in the same place in the preview after making changes. --- .../src/utils/IframeBuffering.tsx | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/apps/admin-x-settings/src/utils/IframeBuffering.tsx b/apps/admin-x-settings/src/utils/IframeBuffering.tsx index 9ca8b52ff4..a9ce6d72eb 100644 --- a/apps/admin-x-settings/src/utils/IframeBuffering.tsx +++ b/apps/admin-x-settings/src/utils/IframeBuffering.tsx @@ -12,7 +12,8 @@ type IframeBufferingProps = { const IframeBuffering: React.FC = ({generateContent, className, height, width, parentClassName, testId, addDelay = false}) => { const [visibleIframeIndex, setVisibleIframeIndex] = useState(0); - const iframes = [useRef(null), useRef(null)]; + const iframes = [useRef(null), useRef(null)]; // eslint-disable-line + const [scrollPosition, setScrollPosition] = useState(0); useEffect(() => { const invisibleIframeIndex = visibleIframeIndex === 0 ? 1 : 0; @@ -44,11 +45,32 @@ const IframeBuffering: React.FC = ({generateContent, class // eslint-disable-next-line react-hooks/exhaustive-deps }, [generateContent]); + useEffect(() => { + const iframe = iframes[visibleIframeIndex].current; + const onScroll = () => { + setScrollPosition(iframe?.contentWindow?.scrollY || 0); + }; + + iframe?.contentWindow?.addEventListener('scroll', onScroll); + + return () => { + iframe?.contentWindow?.removeEventListener('scroll', onScroll); + }; + }, [visibleIframeIndex, iframes]); + + useEffect(() => { + const iframe = iframes[visibleIframeIndex].current; + + if (iframe) { + iframe.contentWindow?.scrollTo(0, scrollPosition); + } + }, [scrollPosition, visibleIframeIndex, iframes]); + return (