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

Mobile BlockToolbar: improve useSelect for fewer rerenders #46697

Merged
merged 1 commit into from
Dec 21, 2022
Merged
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
19 changes: 8 additions & 11 deletions packages/block-editor/src/components/block-toolbar/index.native.js
Original file line number Diff line number Diff line change
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