Skip to content

Commit

Permalink
Nav block: extract hook for inner blocks (#42743)
Browse files Browse the repository at this point in the history
* Initial refactor

* Extract unrelated submenus concept from hook
  • Loading branch information
getdave authored Aug 1, 2022
1 parent 6d62fa9 commit d53ed34
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 33 deletions.
41 changes: 8 additions & 33 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
} from '@wordpress/block-editor';
import { EntityProvider } from '@wordpress/core-data';

import { useDispatch, useSelect, useRegistry } from '@wordpress/data';
import { useDispatch, useRegistry } from '@wordpress/data';
import {
PanelBody,
ToggleControl,
Expand Down Expand Up @@ -61,8 +61,7 @@ import useConvertClassicToBlockMenu, {
CLASSIC_MENU_CONVERSION_SUCCESS,
} from './use-convert-classic-menu-to-block-menu';
import useCreateNavigationMenu from './use-create-navigation-menu';

const EMPTY_ARRAY = [];
import { useInnerBlocks } from './use-inner-blocks';

function getComputedStyle( node ) {
return node.ownerDocument.defaultView.getComputedStyle( node );
Expand Down Expand Up @@ -190,37 +189,13 @@ function Navigation( {
hasUncontrolledInnerBlocks,
uncontrolledInnerBlocks,
isInnerBlockSelected,
hasSubmenus,
} = useSelect(
( select ) => {
const { getBlock, getBlocks, hasSelectedInnerBlock } =
select( blockEditorStore );

// This relies on the fact that `getBlock` won't return controlled
// inner blocks, while `getBlocks` does. It might be more stable to
// introduce a selector like `getUncontrolledInnerBlocks`, just in
// case `getBlock` is fixed.
const _uncontrolledInnerBlocks = getBlock( clientId ).innerBlocks;
const _hasUncontrolledInnerBlocks =
!! _uncontrolledInnerBlocks?.length;
const _controlledInnerBlocks = _hasUncontrolledInnerBlocks
? EMPTY_ARRAY
: getBlocks( clientId );
const innerBlocks = _hasUncontrolledInnerBlocks
? _uncontrolledInnerBlocks
: _controlledInnerBlocks;

return {
hasSubmenus: !! innerBlocks.find(
( block ) => block.name === 'core/navigation-submenu'
),
hasUncontrolledInnerBlocks: _hasUncontrolledInnerBlocks,
uncontrolledInnerBlocks: _uncontrolledInnerBlocks,
isInnerBlockSelected: hasSelectedInnerBlock( clientId, true ),
};
},
[ clientId ]
innerBlocks,
} = useInnerBlocks( clientId );

const hasSubmenus = !! innerBlocks.find(
( block ) => block.name === 'core/navigation-submenu'
);

const {
replaceInnerBlocks,
selectBlock,
Expand Down
39 changes: 39 additions & 0 deletions packages/block-library/src/navigation/edit/use-inner-blocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';

const EMPTY_ARRAY = [];

export function useInnerBlocks( clientId ) {
return useSelect(
( select ) => {
const { getBlock, getBlocks, hasSelectedInnerBlock } =
select( blockEditorStore );

// This relies on the fact that `getBlock` won't return controlled
// inner blocks, while `getBlocks` does. It might be more stable to
// introduce a selector like `getUncontrolledInnerBlocks`, just in
// case `getBlock` is fixed.
const _uncontrolledInnerBlocks = getBlock( clientId ).innerBlocks;

const _hasUncontrolledInnerBlocks =
!! _uncontrolledInnerBlocks?.length;
const _controlledInnerBlocks = _hasUncontrolledInnerBlocks
? EMPTY_ARRAY
: getBlocks( clientId );

return {
innerBlocks: _hasUncontrolledInnerBlocks
? _uncontrolledInnerBlocks
: _controlledInnerBlocks,
hasUncontrolledInnerBlocks: _hasUncontrolledInnerBlocks,
uncontrolledInnerBlocks: _uncontrolledInnerBlocks,
controlledInnerBlocks: _controlledInnerBlocks,
isInnerBlockSelected: hasSelectedInnerBlock( clientId, true ),
};
},
[ clientId ]
);
}

0 comments on commit d53ed34

Please sign in to comment.