Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor useZoomOut hook to use new state
  • Loading branch information
getdave committed Sep 18, 2024
commit 8884052612f2924a8f2c514be9f1208f892e7981
4 changes: 2 additions & 2 deletions packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1027,11 +1027,11 @@ _Parameters_

### useZoomOut

A hook used to set the editor mode to zoomed out mode, invoking the hook sets the mode.
A hook used to set the zoomed out view, invoking the hook sets the mode.

_Parameters_

- _zoomOut_ `boolean`: If we should enter into zoomOut mode or not
- _zoomOut_ `boolean`: If we should zoom out or not.

### Warning

Expand Down
45 changes: 23 additions & 22 deletions packages/block-editor/src/hooks/use-zoom-out.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,47 @@ import { useEffect, useRef } from '@wordpress/element';
* Internal dependencies
*/
import { store as blockEditorStore } from '../store';
import { unlock } from '../lock-unlock';

/**
* A hook used to set the editor mode to zoomed out mode, invoking the hook sets the mode.
* A hook used to set the zoomed out view, invoking the hook sets the mode.
*
* @param {boolean} zoomOut If we should enter into zoomOut mode or not
* @param {boolean} zoomOut If we should zoom out or not.
*/
export function useZoomOut( zoomOut = true ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me this is an example of a premature abstraction. A public API was created for a "mode" which was not yet considered stable (as indicated by the fact that the selector/action for the modes are __unstable.

We now have to maintain this API but make it work with the new concept which is not ideal.

const { __unstableSetEditorMode } = useDispatch( blockEditorStore );
const { __unstableGetEditorMode } = useSelect( blockEditorStore );
const { setZoomOut } = unlock( useDispatch( blockEditorStore ) );
const { isZoomOut } = unlock( useSelect( blockEditorStore ) );

const originalEditingModeRef = useRef( null );
const mode = __unstableGetEditorMode();
const originalIsZoomOutRef = useRef( null );
const currentZoomOutState = isZoomOut();

useEffect( () => {
// Only set this on mount so we know what to return to when we unmount.
if ( ! originalEditingModeRef.current ) {
originalEditingModeRef.current = mode;
if ( ! originalIsZoomOutRef.current ) {
originalIsZoomOutRef.current = currentZoomOutState;
}

return () => {
// We need to use __unstableGetEditorMode() here and not `mode`, as mode may not update on unmount
if (
__unstableGetEditorMode() === 'compose' &&
__unstableGetEditorMode() !== originalEditingModeRef.current
) {
__unstableSetEditorMode( originalEditingModeRef.current );
// We need to use isZoomOut() here and not `currentZoomOutState`, as currentZoomOutState may not update on unmount
if ( isZoomOut() && isZoomOut() !== originalIsZoomOutRef.current ) {
setZoomOut( originalIsZoomOutRef.current );
}
};
}, [] );
}, [ currentZoomOutState, isZoomOut, setZoomOut ] );

// The effect opens the zoom-out view if we want it open and it's not currently in zoom-out mode.
// The effect opens the zoom-out view if we want it open and the canvas is not currently zoomed-out.
useEffect( () => {
if ( zoomOut && mode !== 'compose' ) {
__unstableSetEditorMode( 'compose' );
if ( zoomOut && currentZoomOutState === false ) {
// __unstableSetEditorMode( 'compose' );
setZoomOut( true );
} else if (
! zoomOut &&
__unstableGetEditorMode() === 'compose' &&
originalEditingModeRef.current !== mode
isZoomOut() &&
originalIsZoomOutRef.current !== currentZoomOutState
) {
__unstableSetEditorMode( originalEditingModeRef.current );
setZoomOut( originalIsZoomOutRef.current );
}
}, [ __unstableGetEditorMode, __unstableSetEditorMode, zoomOut ] ); // Mode is deliberately excluded from the dependencies so that the effect does not run when mode changes.
// currentZoomOutState is deliberately excluded from the dependencies so that the effect does not run when mode changes.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ isZoomOut, setZoomOut, zoomOut ] );
}