diff --git a/packages/editor/src/components/visual-editor/index.js b/packages/editor/src/components/visual-editor/index.js index 1c8eb5c3b77640..6a5466a265c3eb 100644 --- a/packages/editor/src/components/visual-editor/index.js +++ b/packages/editor/src/components/visual-editor/index.js @@ -16,7 +16,7 @@ import { privateApis as blockEditorPrivateApis, __experimentalUseResizeCanvas as useResizeCanvas, } from '@wordpress/block-editor'; -import { useEffect, useRef, useMemo } from '@wordpress/element'; +import { useEffect, useRef, useMemo, useState } from '@wordpress/element'; import { useSelect } from '@wordpress/data'; import { parse } from '@wordpress/blocks'; import { store as coreStore } from '@wordpress/core-data'; @@ -106,7 +106,10 @@ function VisualEditor( { contentRef, className, } ) { - const [ resizeObserver, sizes ] = useResizeObserver(); + const [ contentHeight, setContentHeight ] = useState( '' ); + const effectContentHeight = useResizeObserver( ( [ entry ] ) => { + setContentHeight( entry.borderBoxSize[ 0 ].blockSize ); + } ); const isMobileViewport = useViewportMatch( 'small', '<' ); const { renderingMode, @@ -323,21 +326,6 @@ function VisualEditor( { .is-root-container.alignfull { max-width: none; margin-left: auto; margin-right: auto;} .is-root-container.alignfull:where(.is-layout-flow) > :not(.alignleft):not(.alignright) { max-width: none;}`; - const localRef = useRef(); - const typewriterRef = useTypewriter(); - contentRef = useMergeRefs( [ - localRef, - contentRef, - renderingMode === 'post-only' ? typewriterRef : null, - useFlashEditableBlocks( { - isEnabled: renderingMode === 'template-locked', - } ), - useSelectNearestEditableBlock( { - isEnabled: renderingMode === 'template-locked', - } ), - useZoomOutModeExit(), - ] ); - const forceFullHeight = postType === NAVIGATION_POST_TYPE; const enableResizing = [ @@ -368,6 +356,24 @@ function VisualEditor( { ]; }, [ styles, enableResizing ] ); + const localRef = useRef(); + const typewriterRef = useTypewriter(); + contentRef = useMergeRefs( [ + localRef, + contentRef, + renderingMode === 'post-only' ? typewriterRef : null, + useFlashEditableBlocks( { + isEnabled: renderingMode === 'template-locked', + } ), + useSelectNearestEditableBlock( { + isEnabled: renderingMode === 'template-locked', + } ), + useZoomOutModeExit(), + // Avoid resize listeners when not needed, these will trigger + // unnecessary re-renders when animating the iframe width. + enableResizing ? effectContentHeight : null, + ] ); + return (