diff --git a/packages/edit-site/src/components/block-editor/resizable-editor.js b/packages/edit-site/src/components/block-editor/resizable-editor.js index 2e76b5a2a1e98..6dcf434ff0606 100644 --- a/packages/edit-site/src/components/block-editor/resizable-editor.js +++ b/packages/edit-site/src/components/block-editor/resizable-editor.js @@ -1,7 +1,7 @@ /** * WordPress dependencies */ -import { useState, useEffect, useRef, useCallback } from '@wordpress/element'; +import { useState, useRef, useCallback } from '@wordpress/element'; import { ResizableBox } from '@wordpress/components'; import { __experimentalUseResizeCanvas as useResizeCanvas, @@ -11,7 +11,7 @@ import { store as blockEditorStore, } from '@wordpress/block-editor'; import { useSelect } from '@wordpress/data'; -import { useMergeRefs } from '@wordpress/compose'; +import { useMergeRefs, useResizeObserver } from '@wordpress/compose'; /** * Internal dependencies @@ -19,11 +19,6 @@ import { useMergeRefs } from '@wordpress/compose'; import { store as editSiteStore } from '../../store'; import ResizeHandle from './resize-handle'; -const DEFAULT_STYLES = { - width: '100%', - height: '100%', -}; - // Removes the inline styles in the drag handles. const HANDLE_STYLES_OVERRIDE = { position: undefined, @@ -38,6 +33,7 @@ const HANDLE_STYLES_OVERRIDE = { }; function ResizableEditor( { enableResizing, settings, children, ...props } ) { + const [ resizeObserver, sizes ] = useResizeObserver(); const { deviceType, isZoomOutMode } = useSelect( ( select ) => ( { deviceType: @@ -49,60 +45,21 @@ function ResizableEditor( { enableResizing, settings, children, ...props } ) { [] ); const deviceStyles = useResizeCanvas( deviceType ); - const [ width, setWidth ] = useState( DEFAULT_STYLES.width ); - const [ height, setHeight ] = useState( DEFAULT_STYLES.height ); + const [ width, setWidth ] = useState( '100%' ); const iframeRef = useRef(); const mouseMoveTypingResetRef = useMouseMoveTypingReset(); const ref = useMergeRefs( [ iframeRef, mouseMoveTypingResetRef ] ); - useEffect( - function autoResizeIframeHeight() { - if ( ! iframeRef.current || ! enableResizing ) { - return; - } - - const iframe = iframeRef.current; - - function setFrameHeight() { - setHeight( iframe.contentDocument.body.scrollHeight ); - } - - let resizeObserver; - - function registerObserver() { - resizeObserver?.disconnect(); - - resizeObserver = new iframe.contentWindow.ResizeObserver( - setFrameHeight - ); - - // Observe the body, since the `html` element seems to always - // have a height of `100%`. - resizeObserver.observe( iframe.contentDocument.body ); - setFrameHeight(); - } - - iframe.addEventListener( 'load', registerObserver ); - - return () => { - resizeObserver?.disconnect(); - iframe.removeEventListener( 'load', registerObserver ); - }; - }, - [ enableResizing, iframeRef.current ] - ); - const resizeWidthBy = useCallback( ( deltaPixels ) => { if ( iframeRef.current ) { setWidth( iframeRef.current.offsetWidth + deltaPixels ); } }, [] ); - return ( { setWidth( element.style.width ); @@ -141,21 +98,18 @@ function ResizableEditor( { enableResizing, settings, children, ...props } ) { >