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 (