/** * External dependencies */ import clsx from 'clsx'; import { useState, useRef, useEffect } from '@wordpress/element'; import { Spinner } from '@woocommerce/components'; import { useResizeObserver } from '@wordpress/compose'; import { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore No types for this exist yet. __unstableMotion as motion, } from '@wordpress/components'; /** * Internal dependencies */ import { getAdminSetting } from '~/utils/admin-settings'; import ResizableFrame from '~/customize-store/assembler-hub/resizable-frame'; import type { MainContentComponentProps } from '../xstate'; import './site-preview.scss'; export const SitePreviewPage = ( props: MainContentComponentProps ) => { const siteUrl = getAdminSetting( 'siteUrl' ) + '?site-preview=1'; const [ isLoading, setIsLoading ] = useState( true ); const iframeRef = useRef< HTMLIFrameElement >( null ); const [ frameResizer, frameSize ] = useResizeObserver(); const [ isResizableFrameOversized, setIsResizableFrameOversized ] = useState( false ); useEffect( () => { const iframeContentWindow = iframeRef.current?.contentWindow; const beforeUnloadHandler = () => { setIsLoading( true ); }; if ( iframeContentWindow ) { iframeContentWindow.addEventListener( 'beforeunload', beforeUnloadHandler ); } return () => { if ( iframeContentWindow ) { iframeContentWindow.removeEventListener( 'beforeunload', beforeUnloadHandler ); } }; // IsLoading is a dependency because we want to reset it when the iframe reloads. }, [ iframeRef, setIsLoading, isLoading ] ); return (
{ frameResizer } { !! frameSize.width && ( { isLoading && (
) }