Skip to content

Commit

Permalink
Add link to empty submenu and default navigation class.
Browse files Browse the repository at this point in the history
  • Loading branch information
tellthemachines committed Aug 31, 2021
1 parent 917a2ec commit be6def2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
17 changes: 16 additions & 1 deletion packages/block-library/src/navigation-submenu/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { placeCaretAtHorizontalEdge } from '@wordpress/dom';
import { link as linkIcon } from '@wordpress/icons';
import { store as coreStore } from '@wordpress/core-data';
import { speak } from '@wordpress/a11y';
import { createBlock } from '@wordpress/blocks';

/**
* Internal dependencies
Expand Down Expand Up @@ -291,6 +292,7 @@ export default function NavigationSubmenuEdit( {
};
const { showSubmenuIcon } = context;
const { saveEntityRecord } = useDispatch( coreStore );
const { insertBlock } = useDispatch( blockEditorStore );
const [ isLinkOpen, setIsLinkOpen ] = useState( false );
const listItemRef = useRef( null );
const isDraggingWithin = useIsDraggingWithin( listItemRef );
Expand All @@ -303,6 +305,7 @@ export default function NavigationSubmenuEdit( {
isParentOfSelectedBlock,
isImmediateParentOfSelectedBlock,
hasDescendants,
numberOfDescendants,
selectedBlockHasDescendants,
userCanCreatePages,
userCanCreatePosts,
Expand Down Expand Up @@ -335,6 +338,7 @@ export default function NavigationSubmenuEdit( {
false
),
hasDescendants: !! descendants,
numberOfDescendants: descendants,
selectedBlockHasDescendants: !! getClientIdsOfDescendants( [
selectedBlockId,
] )?.length,
Expand Down Expand Up @@ -429,7 +433,7 @@ export default function NavigationSubmenuEdit( {

const blockProps = useBlockProps( {
ref: listItemRef,
className: classnames( {
className: classnames( 'wp-block-navigation-item', {
'is-editing': isSelected || isParentOfSelectedBlock,
'is-dragging-within': isDraggingWithin,
'has-link': !! url,
Expand All @@ -455,6 +459,17 @@ export default function NavigationSubmenuEdit( {
pull( ALLOWED_BLOCKS, 'core/navigation-submenu' );
}

// Add a link when creating a new Submenu.
useEffect( () => {
if ( ! hasDescendants ) {
insertBlock(
createBlock( 'core/navigation-link' ),
numberOfDescendants,
clientId
);
}
}, [] );

const innerBlocksProps = useInnerBlocksProps(
{
className: classnames( 'wp-block-navigation__submenu-container', {
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/navigation-submenu/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function render_block_core_navigation_submenu( $attributes, $content, $block ) {

$wrapper_attributes = get_block_wrapper_attributes(
array(
'class' => $css_classes . ( $has_submenu ? ' has-child' : '' ) .
'class' => $css_classes . ' wp-block-navigation-item' . ( $has_submenu ? ' has-child' : '' ) .
( $is_active ? ' current-menu-item' : '' ),
'style' => $style_attribute,
)
Expand Down

0 comments on commit be6def2

Please sign in to comment.