Skip to content

Commit

Permalink
Fix: Show UI to update custom links on the sidebar navigation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Mar 30, 2023
1 parent 90258ed commit cfb65d3
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { store as blockEditorStore } from '../../store';
import { updateAttributes } from './update-attributes';
import { LinkUI } from './link-ui';
import { useInsertedBlock } from './use-inserted-block';
import { useListViewContext } from './context';

const BLOCKS_WITH_LINK_UI_SUPPORT = [
'core/navigation-link',
Expand Down Expand Up @@ -90,6 +91,8 @@ const ListViewBlockContents = forwardRef(
hasExistingLinkValue,
] );

const { renderAdditionalBlockUI } = useListViewContext();

const isBlockMoveTarget =
blockMovingClientId && selectedBlockInBlockEditor === clientId;

Expand All @@ -107,6 +110,7 @@ const ListViewBlockContents = forwardRef(

return (
<>
{ renderAdditionalBlockUI && renderAdditionalBlockUI( block ) }
{ isLinkUIOpen && (
<LinkUI
clientId={ lastInsertedBlockClientId }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ function OffCanvasEditor(
LeafMoreMenu,
description = __( 'Block navigation structure' ),
onSelect,
renderAdditionalBlockUI,
},
ref
) {
Expand Down Expand Up @@ -200,6 +201,7 @@ function OffCanvasEditor(
expand,
collapse,
LeafMoreMenu,
renderAdditionalBlockUI,
} ),
[
isMounted.current,
Expand All @@ -208,6 +210,7 @@ function OffCanvasEditor(
expand,
collapse,
LeafMoreMenu,
renderAdditionalBlockUI,
]
);

Expand Down
1 change: 1 addition & 0 deletions packages/edit-site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@wordpress/core-data": "file:../core-data",
"@wordpress/data": "file:../data",
"@wordpress/deprecated": "file:../deprecated",
"@wordpress/dom": "file:../dom",
"@wordpress/editor": "file:../editor",
"@wordpress/element": "file:../element",
"@wordpress/hooks": "file:../hooks",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,49 @@ import {
store as blockEditorStore,
BlockList,
BlockTools,
__experimentalLinkControl as LinkControl,
} from '@wordpress/block-editor';
import { useDispatch, useSelect } from '@wordpress/data';
import { createBlock } from '@wordpress/blocks';
import { useCallback } from '@wordpress/element';
import { useCallback, useState } from '@wordpress/element';
import { Popover } from '@wordpress/components';
import { __unstableStripHTML as stripHTML } from '@wordpress/dom';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { unlock } from '../../private-apis';
import { NavigationMenuLoader } from './loader';

function renderAdditionalBlockUI( block, onClose ) {
const { updateBlockAttributes } = useDispatch( blockEditorStore );
const { label, url, opensInNewTab } = block.attributes;
const link = {
url,
opensInNewTab,
title: label && stripHTML( label ),
};
return (
<Popover placement="bottom" shift onClose={ onClose }>
<LinkControl
hasTextControl
hasRichPreviews
value={ link }
onChange={ ( updatedValue ) => {
updateBlockAttributes( block.clientId, {
label: updatedValue.title,
url: updatedValue.url,
opensInNewTab: updatedValue.opensInNewTab,
} );
onClose();
} }
onCancel={ onClose }
/>
</Popover>
);
}

export default function NavigationMenuContent( { rootClientId, onSelect } ) {
const { clientIdsTree, isLoading } = useSelect(
( select ) => {
Expand All @@ -35,6 +67,23 @@ export default function NavigationMenuContent( { rootClientId, onSelect } ) {
const { replaceBlock, __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );

const [ customLinkEditPopoverOpenId, setIsCustomLinkEditPopoverOpenId ] =
useState( false );

const renderAdditionalBlockUICallback = useCallback(
( block ) => {
if (
customLinkEditPopoverOpenId &&
block.clientId === customLinkEditPopoverOpenId
) {
return renderAdditionalBlockUI( block, () => {
setIsCustomLinkEditPopoverOpenId( false );
} );
}
},
[ customLinkEditPopoverOpenId, setIsCustomLinkEditPopoverOpenId ]
);

const { OffCanvasEditor, LeafMoreMenu } = unlock( blockEditorPrivateApis );

const offCanvasOnselect = useCallback(
Expand All @@ -48,11 +97,22 @@ export default function NavigationMenuContent( { rootClientId, onSelect } ) {
block.clientId,
createBlock( 'core/navigation-link', block.attributes )
);
} else if (
block.name === 'core/navigation-link' &&
block.attributes.kind === 'custom' &&
block.attributes.url
) {
setIsCustomLinkEditPopoverOpenId( block.clientId );
} else {
onSelect( block );
}
},
[ onSelect, __unstableMarkNextChangeAsNotPersistent, replaceBlock ]
[
onSelect,
__unstableMarkNextChangeAsNotPersistent,
replaceBlock,
setIsCustomLinkEditPopoverOpenId,
]
);

// The hidden block is needed because it makes block edit side effects trigger.
Expand All @@ -66,6 +126,7 @@ export default function NavigationMenuContent( { rootClientId, onSelect } ) {
onSelect={ offCanvasOnselect }
LeafMoreMenu={ LeafMoreMenu }
showAppender={ false }
renderAdditionalBlockUI={ renderAdditionalBlockUICallback }
/>
) }
<div style={ { visibility: 'hidden' } }>
Expand Down

0 comments on commit cfb65d3

Please sign in to comment.