Skip to content

Commit

Permalink
Enable easier drag and drop for navigation building (#45906)
Browse files Browse the repository at this point in the history
* tweaks the way navigation link and navigation submenu work to enable drag and drop in list view

* Adds deps list to effects for submenu and navigation link blocks.

Co-authored-by: Daniel Richards <677833+talldan@users.noreply.github.com>
Co-authored-by: Maggie <3593343+MaggieCabrera@users.noreply.github.com>
Co-authored-by: Ben Dwyer <275961+scruffian@users.noreply.github.com>

Co-authored-by: Daniel Richards <677833+talldan@users.noreply.github.com>
Co-authored-by: Maggie <3593343+MaggieCabrera@users.noreply.github.com>
Co-authored-by: Ben Dwyer <275961+scruffian@users.noreply.github.com>
  • Loading branch information
4 people authored Nov 21, 2022
1 parent 5122e65 commit 37fc2a1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
25 changes: 23 additions & 2 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
useBlockProps,
store as blockEditorStore,
getColorClassName,
useInnerBlocksProps,
} from '@wordpress/block-editor';
import { isURL, prependHTTP, safeDecodeURI } from '@wordpress/url';
import {
Expand Down Expand Up @@ -527,7 +528,9 @@ export default function NavigationLinkEdit( {
const newSubmenu = createBlock(
'core/navigation-submenu',
attributes,
innerBlocks
innerBlocks.length > 0
? innerBlocks
: [ createBlock( 'core/navigation-link' ) ]
);
replaceBlock( clientId, newSubmenu );
}
Expand All @@ -540,11 +543,14 @@ export default function NavigationLinkEdit( {
if ( ! url ) {
setIsLinkOpen( true );
}
}, [ url ] );

useEffect( () => {
// If block has inner blocks, transform to Submenu.
if ( hasChildren ) {
transformToSubmenu();
}
}, [] );
}, [ hasChildren ] );

/**
* The hook shouldn't be necessary but due to a focus loss happening
Expand Down Expand Up @@ -675,6 +681,20 @@ export default function NavigationLinkEdit( {
onKeyDown,
} );

const ALLOWED_BLOCKS = [
'core/navigation-link',
'core/navigation-submenu',
];
const DEFAULT_BLOCK = {
name: 'core/navigation-link',
};
const innerBlocksProps = useInnerBlocksProps( blockProps, {
allowedBlocks: ALLOWED_BLOCKS,
__experimentalDefaultBlock: DEFAULT_BLOCK,
__experimentalDirectInsert: true,
renderAppender: false,
} );

if ( ! url || isInvalid || isDraft ) {
blockProps.onClick = () => setIsLinkOpen( true );
}
Expand Down Expand Up @@ -915,6 +935,7 @@ export default function NavigationLinkEdit( {
</Popover>
) }
</a>
<div { ...innerBlocksProps } />
</div>
</Fragment>
);
Expand Down
7 changes: 7 additions & 0 deletions packages/block-library/src/navigation-submenu/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,13 @@ export default function NavigationSubmenuEdit( {
replaceBlock( clientId, newLinkBlock );
}

useEffect( () => {
// If block is empty, transform to Navigation Link.
if ( ! hasChildren ) {
transformToLink();
}
}, [ hasChildren ] );

const canConvertToLink =
! selectedBlockHasChildren || onlyDescendantIsEmptyLink;

Expand Down

0 comments on commit 37fc2a1

Please sign in to comment.