Skip to content

Commit

Permalink
Use useState so resizes re-render
Browse files Browse the repository at this point in the history
  • Loading branch information
ajlende committed Dec 3, 2024
1 parent 9eadab8 commit 5da995a
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions packages/block-editor/src/components/iframe/use-scale-canvas.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useEffect, useRef, useCallback } from '@wordpress/element';
import { useEffect, useRef, useCallback, useState } from '@wordpress/element';
import { useReducedMotion, useResizeObserver } from '@wordpress/compose';

/**
Expand Down Expand Up @@ -120,6 +120,8 @@ function getAnimationKeyframes( transitionFrom, transitionTo ) {
];
}

const NULL_SIZE = { width: null, height: null };

function extractSize( entries ) {
const contentBlockSize = entries.at( -1 ).contentBoxSize[ 0 ];
return {
Expand Down Expand Up @@ -153,13 +155,16 @@ export function useScaleCanvas( {
maxContainerWidth = 750,
scale,
} ) {
const contentRectRef = useRef( { width: null, height: null } );
const [ { height: contentHeight }, setContentRect ] = useState( NULL_SIZE );
const contentRefCallback = useResizeObserver( ( entries ) => {
contentRectRef.current = extractSize( entries );
setContentRect( extractSize( entries ) );
} );
const containerRectRef = useRef( { width: null, height: null } );
const [
{ width: containerWidth, height: containerHeight },
setContainerRect,
] = useState( NULL_SIZE );
const containerRefCallback = useResizeObserver( ( entries ) => {
containerRectRef.current = extractSize( entries );
setContainerRect( extractSize( entries ) );
} );

const initialContainerWidthRef = useRef( 0 );
Expand All @@ -174,19 +179,19 @@ export function useScaleCanvas( {

useEffect( () => {
if ( ! isZoomedOut ) {
initialContainerWidthRef.current = containerRectRef.current.width;
initialContainerWidthRef.current = containerWidth;
}
}, [ isZoomedOut ] );
}, [ containerWidth, isZoomedOut ] );

const scaleContainerWidth = Math.max(
initialContainerWidthRef.current,
containerRectRef.current.width
containerWidth
);

const scaleValue = isAutoScaled
? calculateScale( {
frameSize,
containerWidth: containerRectRef.current.width,
containerWidth,
maxContainerWidth,
scaleContainerWidth,
} )
Expand Down Expand Up @@ -340,9 +345,9 @@ export function useScaleCanvas( {
// exiting.
transitionFromRef.current.scaleValue = calculateScale( {
frameSize: transitionFromRef.current.frameSize,
containerWidth: containerRectRef.current.width,
containerWidth,
maxContainerWidth,
scaleContainerWidth: containerRectRef.current.width,
scaleContainerWidth: containerWidth,
} );
}

Expand All @@ -364,17 +369,17 @@ export function useScaleCanvas( {

iframeDocument.documentElement.style.setProperty(
'--wp-block-editor-iframe-zoom-out-content-height',
`${ contentRectRef.current.height }px`
`${ contentHeight }px`
);

iframeDocument.documentElement.style.setProperty(
'--wp-block-editor-iframe-zoom-out-inner-height',
`${ containerRectRef.current.height }px`
`${ containerHeight }px`
);

iframeDocument.documentElement.style.setProperty(
'--wp-block-editor-iframe-zoom-out-container-width',
`${ containerRectRef.current.width }px`
`${ containerWidth }px`
);
iframeDocument.documentElement.style.setProperty(
'--wp-block-editor-iframe-zoom-out-scale-container-width',
Expand Down Expand Up @@ -421,7 +426,7 @@ export function useScaleCanvas( {
// the most accurate.
transitionFromRef.current.containerHeight =
transitionFromRef.current.containerHeight ??
containerRectRef.current.height; // Use containerHeight, as it's the previous container height value if none was set.
containerHeight; // Use containerHeight, as it's the previous container height value if none was set.
transitionFromRef.current.scrollTop =
iframeDocument.documentElement.scrollTop;
transitionFromRef.current.scrollHeight =
Expand Down Expand Up @@ -456,6 +461,9 @@ export function useScaleCanvas( {
scaleValue,
frameSize,
iframeDocument,
contentHeight,
containerWidth,
containerHeight,
maxContainerWidth,
scaleContainerWidth,
] );
Expand Down

0 comments on commit 5da995a

Please sign in to comment.