Skip to content

Commit

Permalink
Zoom out: fix for inserter (WordPress#67495)
Browse files Browse the repository at this point in the history
Makes previousIsZoomedOut a proper ref, and assumes a starting zoom level of 1.
  • Loading branch information
ellatrix authored and im3dabasia committed Dec 4, 2024
1 parent ad594e1 commit 416f2ad
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions packages/block-editor/src/components/iframe/use-scale-canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
* WordPress dependencies
*/
import { useEffect, useRef, useCallback } from '@wordpress/element';
import {
usePrevious,
useReducedMotion,
useResizeObserver,
} from '@wordpress/compose';
import { useReducedMotion, useResizeObserver } from '@wordpress/compose';

/**
* @typedef {Object} TransitionState
Expand Down Expand Up @@ -284,28 +280,35 @@ export function useScaleCanvas( {
transitionFromRef.current = transitionToRef.current;
}, [ iframeDocument ] );

const previousIsZoomedOut = usePrevious( isZoomedOut );
const previousIsZoomedOut = useRef( false );

/**
* Runs when zoom out mode is toggled, and sets the startAnimation flag
* so the animation will start when the next useEffect runs. We _only_
* want to animate when the zoom out mode is toggled, not when the scale
* changes due to the container resizing.
*/
useEffect( () => {
if ( ! iframeDocument || previousIsZoomedOut === isZoomedOut ) {
return;
}
const trigger =
iframeDocument && previousIsZoomedOut.current !== isZoomedOut;

if ( isZoomedOut ) {
iframeDocument.documentElement.classList.add( 'is-zoomed-out' );
previousIsZoomedOut.current = isZoomedOut;

if ( ! trigger ) {
return;
}

startAnimationRef.current = true;

if ( ! isZoomedOut ) {
return;
}

iframeDocument.documentElement.classList.add( 'is-zoomed-out' );
return () => {
iframeDocument.documentElement.classList.remove( 'is-zoomed-out' );
};
}, [ iframeDocument, isZoomedOut, previousIsZoomedOut ] );
}, [ iframeDocument, isZoomedOut ] );

/**
* This handles:
Expand Down

0 comments on commit 416f2ad

Please sign in to comment.