Skip to content

Commit

Permalink
Refactor to have an onSelect event. (+1 squashed commit)
Browse files Browse the repository at this point in the history
Squashed commits:
[d32556f] Add ability to open the editor on the selected post on navigation.
  • Loading branch information
jorgefilipecosta committed Feb 1, 2023
1 parent 88335db commit d3acf8c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
17 changes: 16 additions & 1 deletion packages/edit-site/src/components/navigation-inspector/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,21 @@ import NavigationMenu from './navigation-menu';

const NAVIGATION_MENUS_QUERY = [ { per_page: -1, status: 'publish' } ];

export default function NavigationInspector() {
function SelectionEffect( { onSelect } ) {
const selectedBlock = useSelect( ( select ) => {
const { getSelectedBlock } = select( blockEditorStore );
return getSelectedBlock();
} );
// When a navigation item is selected call the onSelect callback.
useEffect( () => {
if ( selectedBlock ) {
onSelect( selectedBlock );
}
}, [ selectedBlock ] );
return null;
}

export default function NavigationInspector( { onSelect } ) {
const {
selectedNavigationBlockId,
clientIdToRef,
Expand Down Expand Up @@ -207,6 +221,7 @@ export default function NavigationInspector() {
onInput={ onInput }
>
<NavigationMenu innerBlocks={ publishedInnerBlocks } />
{ onSelect && <SelectionEffect onSelect={ onSelect } /> }
</BlockEditorProvider>
) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ export default function NavigationMenu( { innerBlocks } ) {
} );
}, [ updateBlockListSettings, innerBlocks ] );

return <OffCanvasEditor blocks={ innerBlocks } />;
return <OffCanvasEditor blocks={ innerBlocks } selectBlockInCanvas={ true } />;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,42 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useCallback } from '@wordpress/element';

/**
* Internal dependencies
*/
import SidebarNavigationScreen from '../sidebar-navigation-screen';
import NavigationInspector from '../navigation-inspector';
import { useHistory } from '../routes';

export default function SidebarNavigationScreenNavigationMenus() {
const history = useHistory();
const onSelect = useCallback(
( selectedBlock ) => {
const { attributes } = selectedBlock;
if (
attributes.kind === 'post-type' &&
attributes.id &&
attributes.type &&
history
) {
history.push( {
postType: attributes.type,
postId: attributes.id,
} );
}
},
[ history ]
);
return (
<SidebarNavigationScreen
path="/navigation"
parentTitle={ __( 'Design' ) }
title={ __( 'Navigation' ) }
content={
<div className="edit-site-sidebar-navigation-screen-navigation-menus">
<NavigationInspector />
<NavigationInspector onSelect={ onSelect } />
</div>
}
/>
Expand Down

0 comments on commit d3acf8c

Please sign in to comment.