Skip to content

Commit

Permalink
Mobile BlockToolbar: improve useSelect for fewer rerenders (#46697)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnajdr committed Dec 21, 2022
1 parent fdf619d commit 261e02d
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions packages/block-editor/src/components/block-toolbar/index.native.js
Expand Up @@ -11,31 +11,28 @@ import UngroupButton from '../ungroup-button';
import { store as blockEditorStore } from '../../store';

export default function BlockToolbar() {
const { blockClientIds, isValid, mode } = useSelect( ( select ) => {
const { isSelected, isValidAndVisual } = useSelect( ( select ) => {
const { getBlockMode, getSelectedBlockClientIds, isBlockValid } =
select( blockEditorStore );
const selectedBlockClientIds = getSelectedBlockClientIds();

return {
blockClientIds: selectedBlockClientIds,
isValid:
isSelected: selectedBlockClientIds.length > 0,
isValidAndVisual:
selectedBlockClientIds.length === 1
? isBlockValid( selectedBlockClientIds[ 0 ] )
: null,
mode:
selectedBlockClientIds.length === 1
? getBlockMode( selectedBlockClientIds[ 0 ] )
: null,
? isBlockValid( selectedBlockClientIds[ 0 ] ) &&
getBlockMode( selectedBlockClientIds[ 0 ] ) === 'visual'
: false,
};
}, [] );

if ( blockClientIds.length === 0 ) {
if ( ! isSelected ) {
return null;
}

return (
<>
{ mode === 'visual' && isValid && (
{ isValidAndVisual && (
<>
<UngroupButton />
<BlockControls.Slot group="block" />
Expand Down

1 comment on commit 261e02d

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3750668172
📝 Reported issues:

Please sign in to comment.