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

Transform Nav Links with children into Submenus #34831

Merged
merged 1 commit into from
Sep 30, 2021
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
65 changes: 8 additions & 57 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import { displayShortcut, isKeyboardEvent, ENTER } from '@wordpress/keycodes';
import { __, sprintf } from '@wordpress/i18n';
import {
BlockControls,
InnerBlocks,
__experimentalUseInnerBlocksProps as useInnerBlocksProps,
InspectorControls,
RichText,
__experimentalLinkControl as LinkControl,
Expand All @@ -45,11 +43,8 @@ import { store as coreStore } from '@wordpress/core-data';
/**
* Internal dependencies
*/
import { ItemSubmenuIcon } from './icons';
import { name } from './block.json';

const ALLOWED_BLOCKS = [ 'core/navigation-link' ];

const MAX_NESTING = 5;

/**
Expand Down Expand Up @@ -289,7 +284,6 @@ export default function NavigationLinkEdit( {
url,
opensInNewTab,
};
const { showSubmenuIcon } = context;
const { saveEntityRecord } = useDispatch( coreStore );
const {
replaceBlock,
Expand All @@ -306,9 +300,7 @@ export default function NavigationLinkEdit( {
isAtMaxNesting,
isTopLevelLink,
isParentOfSelectedBlock,
isImmediateParentOfSelectedBlock,
hasDescendants,
selectedBlockHasDescendants,
userCanCreatePages,
userCanCreatePosts,
} = useSelect(
Expand Down Expand Up @@ -384,14 +376,18 @@ export default function NavigationLinkEdit( {
replaceBlock( clientId, newSubmenu );
}

// Show the LinkControl on mount if the URL is empty
// ( When adding a new menu item)
// This can't be done in the useState call because it conflicts
// with the autofocus behavior of the BlockListBlock component.
useEffect( () => {
// Show the LinkControl on mount if the URL is empty
// ( When adding a new menu item)
// This can't be done in the useState call because it conflicts
// with the autofocus behavior of the BlockListBlock component.
if ( ! url ) {
setIsLinkOpen( true );
}
// If block has inner blocks, transform to Submenu.
if ( hasDescendants ) {
transformToSubmenu();
}
}, [] );

/**
Expand Down Expand Up @@ -519,45 +515,6 @@ export default function NavigationLinkEdit( {
blockProps.onClick = () => setIsLinkOpen( true );
}

// Always use overlay colors for submenus
const innerBlocksColors = getColors( context, true );
const innerBlocksProps = useInnerBlocksProps(
{
className: classnames(
'wp-block-navigation-link__container',
'wp-block-navigation__submenu-container',
{
'is-parent-of-selected-block': isParentOfSelectedBlock,
'has-text-color': !! (
innerBlocksColors.textColor ||
innerBlocksColors.customTextColor
),
[ `has-${ innerBlocksColors.textColor }-color` ]: !! innerBlocksColors.textColor,
'has-background': !! (
innerBlocksColors.backgroundColor ||
innerBlocksColors.customBackgroundColor
),
[ `has-${ innerBlocksColors.backgroundColor }-background-color` ]: !! innerBlocksColors.backgroundColor,
}
),
style: {
color: innerBlocksColors.customTextColor,
backgroundColor: innerBlocksColors.customBackgroundColor,
},
},
{
allowedBlocks: ALLOWED_BLOCKS,
renderAppender:
( isSelected && hasDescendants ) ||
( isImmediateParentOfSelectedBlock &&
! selectedBlockHasDescendants ) ||
// Show the appender while dragging to allow inserting element between item and the appender.
hasDescendants
? InnerBlocks.DefaultAppender
: false,
}
);

const classes = classnames(
'wp-block-navigation-link__content',
'wp-block-navigation-item__content',
Expand Down Expand Up @@ -727,13 +684,7 @@ export default function NavigationLinkEdit( {
/>
</Popover>
) }
{ hasDescendants && showSubmenuIcon && (
<span className="wp-block-navigation-link__submenu-icon wp-block-navigation__submenu-icon">
<ItemSubmenuIcon />
</span>
) }
</a>
<div { ...innerBlocksProps } />
</div>
</Fragment>
);
Expand Down