Skip to content

Commit

Permalink
Fix useZoomOut hook behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
jeryj committed Oct 16, 2024
1 parent b7f3d9a commit 82a5f29
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions packages/block-editor/src/hooks/use-zoom-out.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
import { useEffect, useRef } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -20,14 +20,20 @@ export function useZoomOut( zoomOut = true ) {
useDispatch( blockEditorStore )
);
const { isZoomOut } = unlock( useSelect( blockEditorStore ) );
const isZoomOutOnMountRef = useRef();

useEffect( () => {
const isZoomOutOnMount = isZoomOut();
isZoomOutOnMountRef.current = isZoomOut();

return () => {
if ( isZoomOutOnMount ) {
setZoomLevel( 50 );
} else {
// If we zoomed out for the user, and we are still in zoom out on unmount,
// return them to the default zoom level.
// - Open Inserter
// - Switch to pattern tab
// - Sets Zoom level 50
// - Close inserter
// - Zoom level resets to 100
if ( ! isZoomOutOnMountRef.current && isZoomOut() ) {
resetZoomLevel();
}
};
Expand All @@ -36,8 +42,14 @@ export function useZoomOut( zoomOut = true ) {
useEffect( () => {
if ( zoomOut ) {
setZoomLevel( 50 );
} else {
}
// Only reset zoom level if we were not in zoom out on mount:
// - Enter Zoom Level 50
// - Open inserter
// - Switch to Blocks Tab
// - Zoom level 50 should be persisted
else if ( ! zoomOut && ! isZoomOutOnMountRef.current ) {
resetZoomLevel();
}
}, [ zoomOut, setZoomLevel, resetZoomLevel ] );
}, [ zoomOut, setZoomLevel, resetZoomLevel, isZoomOutOnMountRef ] );
}

0 comments on commit 82a5f29

Please sign in to comment.