Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only enter the zoom-out mode when not in the focus mode #56885

Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/inserter/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function InserterLibrary(
__experimentalShouldZoomPatterns = false,
onSelect = noop,
shouldFocusBlock = false,
onSelectTab,
},
ref
) {
Expand All @@ -48,6 +49,7 @@ function InserterLibrary(
return (
<InserterMenu
onSelect={ onSelect }
onSelectTab={ onSelectTab }
rootClientId={ destinationRootClientId }
clientId={ clientId }
isAppender={ isAppender }
Expand Down
13 changes: 3 additions & 10 deletions packages/block-editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from '@wordpress/element';
import { VisuallyHidden, SearchControl, Popover } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useSelect, useDispatch } from '@wordpress/data';
import { useSelect } from '@wordpress/data';
import { useDebouncedInput } from '@wordpress/compose';

/**
Expand Down Expand Up @@ -47,6 +47,7 @@ function InserterMenu(
prioritizePatterns,
__experimentalOnPatternCategorySelection,
__experimentalShouldZoomPatterns = false,
onSelectTab,
Copy link
Member Author

Choose a reason for hiding this comment

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

We might want to mark this private if we ever want to land this on trunk.

},
ref
) {
Expand All @@ -59,8 +60,6 @@ function InserterMenu(
const [ selectedMediaCategory, setSelectedMediaCategory ] =
useState( null );
const [ selectedTab, setSelectedTab ] = useState( null );
const { __unstableGetEditorMode } = useSelect( blockEditorStore );
const { __unstableSetEditorMode } = useDispatch( blockEditorStore );

const [ destinationRootClientId, onInsertBlocks, onToggleInsertionPoint ] =
useInsertionPoint( {
Expand Down Expand Up @@ -234,14 +233,8 @@ function InserterMenu(
setSelectedPatternCategory( null );
}

// Enter the zoom-out mode when selecting the patterns tab and exit otherwise.
if ( value === 'patterns' ) {
__unstableSetEditorMode( 'zoom-out' );
} else if ( __unstableGetEditorMode() === 'zoom-out' ) {
__unstableSetEditorMode( 'edit' );
}

setSelectedTab( value );
onSelectTab?.( value );
};

const className = classnames( 'block-editor-inserter__menu', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,28 @@
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { Button, VisuallyHidden } from '@wordpress/components';
import { __experimentalLibrary as Library } from '@wordpress/block-editor';
import {
__experimentalLibrary as Library,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { close } from '@wordpress/icons';
import {
useViewportMatch,
__experimentalUseDialog as useDialog,
} from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
import { useEffect, useRef } from '@wordpress/element';
import { useEffect, useRef, useCallback } from '@wordpress/element';
import { privateApis as routerPrivateApis } from '@wordpress/router';

/**
* Internal dependencies
*/
import { store as editSiteStore } from '../../store';
import { useHasEditorCanvasContainer } from '../editor-canvas-container';
import { FOCUSABLE_ENTITIES } from '../../utils/constants';
import { unlock } from '../../lock-unlock';

const { useLocation } = unlock( routerPrivateApis );

export default function InserterSidebar() {
const { closeGeneralSidebar, setIsInserterOpened } =
Expand All @@ -24,6 +33,17 @@ export default function InserterSidebar() {
( select ) => select( editSiteStore ).__experimentalGetInsertionPoint(),
[]
);
const isFocusMode = useSelect( ( select ) =>
FOCUSABLE_ENTITIES.includes(
select( editSiteStore ).getEditedPostType()
)
);
const { __unstableSetEditorMode: setEditorMode } =
useDispatch( blockEditorStore );
const { __unstableGetEditorMode: getEditorMode } =
useSelect( blockEditorStore );
const hasEditorCanvasContainer = useHasEditorCanvasContainer();
const location = useLocation();

const isMobile = useViewportMatch( 'medium', '<' );
const TagName = ! isMobile ? VisuallyHidden : 'div';
Expand All @@ -45,6 +65,32 @@ export default function InserterSidebar() {
libraryRef.current.focusSearch();
}, [] );

const handleSelectTab = useCallback(
( tab ) => {
// Only enter the zoom-out mode when it's not in the focus mode.
if ( ! hasEditorCanvasContainer && ! isFocusMode ) {
// Enter the zoom-out mode when selecting the patterns tab and exit otherwise.
if ( tab === 'patterns' ) {
setEditorMode( 'zoom-out' );
} else if ( getEditorMode() === 'zoom-out' ) {
setEditorMode( 'edit' );
}
}
},
[ getEditorMode, setEditorMode, isFocusMode, hasEditorCanvasContainer ]
);

const previousLocationRef = useRef( location );
useEffect(
function closeInserterOnNavigate() {
Copy link
Member Author

Choose a reason for hiding this comment

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

Ideally, we should find a better way and place to manage this when we have a better routing system.

if ( location !== previousLocationRef.current ) {
previousLocationRef.current = location;
setIsInserterOpened( false );
}
},
[ location, setIsInserterOpened ]
);

return (
<div
ref={ inserterDialogRef }
Expand Down Expand Up @@ -72,6 +118,7 @@ export default function InserterSidebar() {
}
__experimentalShouldZoomPatterns
ref={ libraryRef }
onSelectTab={ handleSelectTab }
/>
</div>
</div>
Expand Down
Loading