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

Account for top toolbar block selection with no block toolbar #57291

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -2,47 +2,76 @@
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { isUnmodifiedDefaultBlock } from '@wordpress/blocks';
import {
getBlockType,
hasBlockSupport,
isUnmodifiedDefaultBlock,
} from '@wordpress/blocks';

/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../store';
import { unlock } from '../lock-unlock';
import { useHasAnyBlockControls } from '../components/block-controls/use-has-block-controls';

/**
* Returns true if the block toolbar should be able to receive focus.
*
* @return {boolean} Whether the block toolbar should be able to receive focus
*/
export function useCanBlockToolbarBeFocused() {
return useSelect( ( select ) => {
const {
__unstableGetEditorMode,
getBlock,
getSettings,
getSelectedBlockClientId,
getFirstMultiSelectedBlockClientId,
} = unlock( select( blockEditorStore ) );
const hasAnyBlockControls = useHasAnyBlockControls();

const selectedBlockId =
getFirstMultiSelectedBlockClientId() || getSelectedBlockClientId();
const isEmptyDefaultBlock = isUnmodifiedDefaultBlock(
getBlock( selectedBlockId ) || {}
);
const { isToolbarEnabled, isDefaultEditingMode, maybeShowBlockToolbar } =
useSelect( ( select ) => {
const {
__unstableGetEditorMode,
getBlock,
getBlockName,
getBlockEditingMode,
getSettings,
getSelectedBlockClientId,
getFirstMultiSelectedBlockClientId,
} = unlock( select( blockEditorStore ) );

// Fixed Toolbar can be focused when:
// - a block is selected
// - fixed toolbar is on
// Block Toolbar Popover can be focused when:
// - a block is selected
// - we are in edit mode
// - it is not an empty default block
return (
!! selectedBlockId &&
( getSettings().hasFixedToolbar ||
( __unstableGetEditorMode() === 'edit' &&
! isEmptyDefaultBlock ) )
);
}, [] );
const selectedBlockId =
getFirstMultiSelectedBlockClientId() ||
getSelectedBlockClientId();
const isEmptyDefaultBlock = isUnmodifiedDefaultBlock(
getBlock( selectedBlockId ) || {}
);
const blockType =
selectedBlockId &&
getBlockType( getBlockName( selectedBlockId ) );

// Fixed Toolbar can be focused when:
// - a block is selected
// - the block has tools
// - fixed toolbar is on
// Block Toolbar Popover can be focused when:
// - a block is selected
// - the block has tools
// - we are in edit mode
// - it is not an empty default block
return {
isToolbarEnabled:
blockType &&
hasBlockSupport( blockType, '__experimentalToolbar', true ),
isDefaultEditingMode:
getBlockEditingMode( selectedBlockId ) === 'default',
maybeShowBlockToolbar:
getSettings().hasFixedToolbar ||
( __unstableGetEditorMode() === 'edit' &&
! isEmptyDefaultBlock ),
};
}, [] );

// The same check used in <BlockToolbar /> to see if it should return null.
// Should we combine these into their own hook so they stay consistent?
const noBlockToolbar =
! isToolbarEnabled ||
( ! isDefaultEditingMode && ! hasAnyBlockControls );
Comment on lines +70 to +74
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think having the "should we show the block toolbar" check within <BlockToolbar /> and this check should be combined into a new "showBlockToolbar" hook that can get used here.


return ! noBlockToolbar && maybeShowBlockToolbar;
}
10 changes: 7 additions & 3 deletions packages/edit-site/src/components/header-edit-mode/index.js
Expand Up @@ -9,6 +9,7 @@ import classnames from 'classnames';
import { useViewportMatch, useReducedMotion } from '@wordpress/compose';
import {
BlockToolbar,
privateApis as blockEditorPrivateApis,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
Expand Down Expand Up @@ -43,6 +44,7 @@ import { unlock } from '../../lock-unlock';
import { FOCUSABLE_ENTITIES } from '../../utils/constants';

const { PostViewLink, PreviewDropdown } = unlock( editorPrivateApis );
const { useCanBlockToolbarBeFocused } = unlock( blockEditorPrivateApis );

export default function HeaderEditMode() {
const {
Expand Down Expand Up @@ -99,7 +101,7 @@ export default function HeaderEditMode() {
const [ isBlockToolsCollapsed, setIsBlockToolsCollapsed ] =
useState( true );

const hasBlockSelected = !! blockSelectionStart;
const hasBlockToolbar = useCanBlockToolbarBeFocused();

useEffect( () => {
// If we have a new block selection, show the block tools
Expand Down Expand Up @@ -154,7 +156,7 @@ export default function HeaderEditMode() {
ref={ blockToolbarRef }
name="block-toolbar"
/>
{ hasBlockSelected && (
{ hasBlockToolbar && (
<Button
className="edit-site-header-edit-mode__block-tools-toggle"
icon={
Expand Down Expand Up @@ -183,7 +185,9 @@ export default function HeaderEditMode() {
'edit-site-header-edit-mode__center',
{
'is-collapsed':
! isBlockToolsCollapsed && isLargeViewport,
! isBlockToolsCollapsed &&
isLargeViewport &&
hasBlockToolbar,
}
) }
>
Expand Down