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

Block Hooks: Fix toggle with Navigation block #59277

Closed
wants to merge 4 commits into from
Closed
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
31 changes: 29 additions & 2 deletions packages/block-editor/src/hooks/block-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import { store as blockEditorStore } from '../store';

const EMPTY_OBJECT = {};

function isNavigationBlock( block ) {
return block.name === 'core/navigation';
}

function BlockHooksControlPure( { name, clientId } ) {
const blockTypes = useSelect(
( select ) => select( blocksStore ).getBlockTypes(),
Expand All @@ -33,14 +37,29 @@ function BlockHooksControlPure( { name, clientId } ) {
[ blockTypes, name ]
);

const getInnerBlocks = useSelect( ( select ) => {
const { getBlock, getBlocks } = select( blockEditorStore );

return ( cId ) => {
return {
controlledInnerBlocks: getBlocks( cId ),
uncontrolledInnerBlocks: getBlock( cId )?.innerBlocks,
};
};
} );

const { blockIndex, rootClientId, innerBlocksLength } = useSelect(
( select ) => {
const { getBlock, getBlockIndex, getBlockRootClientId } =
select( blockEditorStore );
const { uncontrolledInnerBlocks, controlledInnerBlocks } =
getInnerBlocks( clientId );

return {
blockIndex: getBlockIndex( clientId ),
innerBlocksLength: getBlock( clientId )?.innerBlocks?.length,
innerBlocksLength: isNavigationBlock( getBlock( clientId ) )
? controlledInnerBlocks?.length
: uncontrolledInnerBlocks?.length,
rootClientId: getBlockRootClientId( clientId ),
};
},
Expand Down Expand Up @@ -77,7 +96,15 @@ function BlockHooksControlPure( { name, clientId } ) {
// Any of the current block's child blocks (with the right block type) qualifies
// as a hooked first or last child block, as the block might've been automatically
// inserted and then moved around a bit by the user.
candidates = getBlock( clientId ).innerBlocks;
const {
uncontrolledInnerBlocks,
controlledInnerBlocks,
} = getInnerBlocks( clientId );
candidates = isNavigationBlock(
getBlock( clientId )
)
? controlledInnerBlocks
: uncontrolledInnerBlocks;
break;
}

Expand Down
Loading