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

Enable easier drag and drop for navigation building #45906

Merged
merged 2 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 20 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 @@ -544,7 +547,7 @@ export default function NavigationLinkEdit( {
if ( hasChildren ) {
transformToSubmenu();
}
}, [] );
} );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the dependency list was removed?

This code wasn't great in trunk, so there's an opportunity to improve it.

I think it'd be better to split it into two effects. A first for the setIsLinkOpen part, which would have url as a dep, the other for the transformToSubmenu part which would have hasChildren as a dep.


/**
* The hook shouldn't be necessary but due to a focus loss happening
Expand Down Expand Up @@ -675,6 +678,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 +932,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();
}
} );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing dependencies here too.


const canConvertToLink =
! selectedBlockHasChildren || onlyDescendantIsEmptyLink;

Expand Down