-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor useCanContextualToolbarShow for simplicity and clarity (#56914)
* Refactor useCanContextualToolbarShow for simplicity and clarity - Rename to useCanBlockToolbarBeFocused: Each usage of useCanContextualTool barShow ended up as one blockToolbarCanBeFocused const. - The scenarios of when the block toolbar can be focused was much simpler t han the implemented checks. Refactored for the scenarios in which it can be focused. * Update packages/block-editor/src/utils/use-can-block-toolbar-be-focused.js Co-authored-by: Andrei Draganescu <[email protected]> --------- Co-authored-by: Ben Dwyer <[email protected]> Co-authored-by: Andrei Draganescu <[email protected]>
- Loading branch information
1 parent
395b18f
commit 1ef449f
Showing
6 changed files
with
57 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
packages/block-editor/src/utils/use-can-block-toolbar-be-focused.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
import { isUnmodifiedDefaultBlock } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as blockEditorStore } from '../store'; | ||
import { unlock } from '../lock-unlock'; | ||
|
||
/** | ||
* 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 selectedBlockId = | ||
getFirstMultiSelectedBlockClientId() || getSelectedBlockClientId(); | ||
const isEmptyDefaultBlock = isUnmodifiedDefaultBlock( | ||
getBlock( selectedBlockId ) || {} | ||
); | ||
|
||
// 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 ) ) | ||
); | ||
}, [] ); | ||
} |
85 changes: 0 additions & 85 deletions
85
packages/block-editor/src/utils/use-should-contextual-toolbar-show.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters