diff --git a/packages/block-editor/src/components/iframe/content.scss b/packages/block-editor/src/components/iframe/content.scss index d33ed7818aa2a..1d01f84b5fd1f 100644 --- a/packages/block-editor/src/components/iframe/content.scss +++ b/packages/block-editor/src/components/iframe/content.scss @@ -23,10 +23,10 @@ $frame-size: var(--wp-block-editor-iframe-zoom-out-frame-size); $inner-height: var(--wp-block-editor-iframe-zoom-out-inner-height); $content-height: var(--wp-block-editor-iframe-zoom-out-content-height); - $outer-container-width: var(--wp-block-editor-iframe-zoom-out-outer-container-width); + $scale-container-width: var(--wp-block-editor-iframe-zoom-out-scale-container-width); $container-width: var(--wp-block-editor-iframe-zoom-out-container-width, 100vw); // Apply an X translation to center the scaled content within the available space. - transform: translateX(calc((#{$outer-container-width} - #{$container-width}) / 2 / #{$scale})); + transform: translateX(calc((#{$scale-container-width} - #{$container-width}) / 2 / #{$scale})); scale: #{$scale}; background-color: $gray-300; diff --git a/packages/block-editor/src/components/iframe/index.js b/packages/block-editor/src/components/iframe/index.js index b72d86ef8e0f8..b14514f934fb3 100644 --- a/packages/block-editor/src/components/iframe/index.js +++ b/packages/block-editor/src/components/iframe/index.js @@ -122,7 +122,7 @@ function Iframe( { }, [] ); const { styles = '', scripts = '' } = resolvedAssets; const [ iframeDocument, setIframeDocument ] = useState(); - const initialContainerWidth = useRef(); + const initialContainerWidth = useRef( 0 ); const [ bodyClasses, setBodyClasses ] = useState( [] ); const clearerRef = useBlockSelectionClearer(); const [ before, writingFlowRef, after ] = useWritingFlow(); @@ -247,6 +247,11 @@ function Iframe( { } }, [ containerWidth, isZoomedOut ] ); + const scaleContainerWidth = Math.max( + initialContainerWidth.current, + containerWidth + ); + const disabledRef = useDisabled( { isDisabled: ! readonly } ); const bodyRef = useMergeRefs( [ useBubbleEvents( iframeDocument ), @@ -299,17 +304,6 @@ function Iframe( { useEffect( () => cleanup, [ cleanup ] ); const zoomOutAnimationClassnameRef = useRef( null ); - const handleZoomOutAnimationClassname = () => { - clearTimeout( zoomOutAnimationClassnameRef.current ); - - iframeDocument.documentElement.classList.add( 'zoom-out-animation' ); - - zoomOutAnimationClassnameRef.current = setTimeout( () => { - iframeDocument.documentElement.classList.remove( - 'zoom-out-animation' - ); - }, 400 ); // 400ms should match the animation speed used in components/iframe/content.scss - }; // Toggle zoom out CSS Classes only when zoom out mode changes. We could add these into the useEffect // that controls settings the CSS variables, but then we would need to do more work to ensure we're @@ -320,6 +314,20 @@ function Iframe( { return; } + const handleZoomOutAnimationClassname = () => { + clearTimeout( zoomOutAnimationClassnameRef.current ); + + iframeDocument.documentElement.classList.add( + 'zoom-out-animation' + ); + + zoomOutAnimationClassnameRef.current = setTimeout( () => { + iframeDocument.documentElement.classList.remove( + 'zoom-out-animation' + ); + }, 400 ); // 400ms should match the animation speed used in components/iframe/content.scss + }; + handleZoomOutAnimationClassname(); iframeDocument.documentElement.classList.add( 'is-zoomed-out' ); @@ -349,10 +357,7 @@ function Iframe( { scale === 'default' ? ( Math.min( containerWidth, maxWidth ) - parseInt( frameSize ) * 2 ) / - Math.max( - initialContainerWidth.current, - containerWidth - ) + scaleContainerWidth : scale ); @@ -374,15 +379,10 @@ function Iframe( { `${ containerWidth }px` ); iframeDocument.documentElement.style.setProperty( - '--wp-block-editor-iframe-zoom-out-outer-container-width', - `${ Math.max( initialContainerWidth.current, containerWidth ) }px` + '--wp-block-editor-iframe-zoom-out-scale-container-width', + `${ scaleContainerWidth }px` ); - // iframeDocument.documentElement.style.setProperty( - // '--wp-block-editor-iframe-zoom-out-outer-container-width', - // `${ Math.max( initialContainerWidth.current, containerWidth ) }px` - // ); - return () => { iframeDocument.documentElement.style.removeProperty( '--wp-block-editor-iframe-zoom-out-scale' @@ -400,7 +400,7 @@ function Iframe( { '--wp-block-editor-iframe-zoom-out-container-width' ); iframeDocument.documentElement.style.removeProperty( - '--wp-block-editor-iframe-zoom-out-outer-container-width' + '--wp-block-editor-iframe-zoom-out-scale-container-width' ); }; }, [ @@ -412,6 +412,7 @@ function Iframe( { containerWidth, windowInnerWidth, isZoomedOut, + scaleContainerWidth, ] ); // Make sure to not render the before and after focusable div elements in view @@ -501,12 +502,8 @@ function Iframe( { isZoomedOut && 'is-zoomed-out' ) } style={ { - '--wp-block-editor-iframe-zoom-out-outer-container-width': - isZoomedOut && - `${ Math.max( - initialContainerWidth.current, - containerWidth - ) }px`, + '--wp-block-editor-iframe-zoom-out-scale-container-width': + isZoomedOut && `${ scaleContainerWidth }px`, } } > { iframe } diff --git a/packages/block-editor/src/components/iframe/style.scss b/packages/block-editor/src/components/iframe/style.scss index d05be2f3977b9..5dd30c338057e 100644 --- a/packages/block-editor/src/components/iframe/style.scss +++ b/packages/block-editor/src/components/iframe/style.scss @@ -9,8 +9,8 @@ } .block-editor-iframe__scale-container.is-zoomed-out { - $outer-container-width: var(--wp-block-editor-iframe-zoom-out-outer-container-width, 100vw); - width: $outer-container-width; + $scale-container-width: var(--wp-block-editor-iframe-zoom-out-scale-container-width, 100vw); + width: $scale-container-width; // Position the iframe so that it is always aligned with the right side so that // the scrollbar is always visible on the right side position: absolute;