Skip to content

Commit

Permalink
Navigation: Extract components (#45850)
Browse files Browse the repository at this point in the history
  • Loading branch information
scruffian committed Nov 18, 2022
1 parent 8e83fff commit b9da345
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 78 deletions.
120 changes: 42 additions & 78 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
useMemo,
} from '@wordpress/element';
import {
__experimentalOffCanvasEditor as OffCanvasEditor,
InspectorControls,
useBlockProps,
__experimentalRecursionProvider as RecursionProvider,
Expand All @@ -37,8 +36,6 @@ import {
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
Button,
Spinner,
__experimentalHStack as HStack,
__experimentalHeading as Heading,
} from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { speak } from '@wordpress/a11y';
Expand All @@ -53,7 +50,6 @@ import useNavigationEntities from '../use-navigation-entities';
import Placeholder from './placeholder';
import ResponsiveWrapper from './responsive-wrapper';
import NavigationInnerBlocks from './inner-blocks';
import NavigationMenuSelector from './navigation-menu-selector';
import NavigationMenuNameControl from './navigation-menu-name-control';
import UnsavedInnerBlocks from './unsaved-inner-blocks';
import NavigationMenuDeleteControl from './navigation-menu-delete-control';
Expand All @@ -69,6 +65,7 @@ import useCreateNavigationMenu from './use-create-navigation-menu';
import { useInnerBlocks } from './use-inner-blocks';
import { detectColors } from './utils';
import ManageMenusButton from './manage-menus-button';
import MenuInspectorControls from './menu-inspector-controls';

function Navigation( {
attributes,
Expand Down Expand Up @@ -663,84 +660,26 @@ function Navigation( {
// that automatically saves the menu as an entity when changes are made to the inner blocks.
const hasUnsavedBlocks = hasUncontrolledInnerBlocks && ! isEntityAvailable;

const WrappedNavigationMenuSelector = ( { currentMenuId } ) => (
<NavigationMenuSelector
currentMenuId={ currentMenuId }
clientId={ clientId }
onSelectNavigationMenu={ ( menuId ) => {
handleUpdateMenu( menuId );
} }
onSelectClassicMenu={ async ( classicMenu ) => {
const navMenu = await convertClassicMenu(
classicMenu.id,
classicMenu.name,
'draft'
);
if ( navMenu ) {
handleUpdateMenu( navMenu.id, {
focusNavigationBlock: true,
} );
}
} }
onCreateNew={ createUntitledEmptyNavigationMenu }
createNavigationMenuIsSuccess={ createNavigationMenuIsSuccess }
createNavigationMenuIsError={ createNavigationMenuIsError }
/* translators: %s: The name of a menu. */
actionLabel={ __( "Switch to '%s'" ) }
/>
);

const isManageMenusButtonDisabled =
! hasManagePermissions || ! hasResolvedNavigationMenus;

const MenuInspectorControls = ( { currentMenuId = null } ) => (
<InspectorControls>
<PanelBody
title={
isOffCanvasNavigationEditorEnabled ? null : __( 'Menu' )
}
>
{ isOffCanvasNavigationEditorEnabled ? (
<>
<HStack className="wp-block-navigation-off-canvas-editor__header">
<Heading
className="wp-block-navigation-off-canvas-editor__title"
level={ 2 }
>
{ __( 'Menu' ) }
</Heading>
<WrappedNavigationMenuSelector
currentMenuId={ currentMenuId }
/>
</HStack>
{ currentMenuId && isNavigationMenuMissing ? (
<p>{ __( 'Select or create a menu' ) }</p>
) : (
<OffCanvasEditor
blocks={ innerBlocks }
isExpanded={ true }
selectBlockInCanvas={ false }
/>
) }
</>
) : (
<>
<WrappedNavigationMenuSelector
currentMenuId={ currentMenuId }
/>
<ManageMenusButton
disabled={ isManageMenusButtonDisabled }
/>
</>
) }
</PanelBody>
</InspectorControls>
);

if ( hasUnsavedBlocks && ! isCreatingNavigationMenu ) {
return (
<TagName { ...blockProps }>
<MenuInspectorControls currentMenuId={ ref } />
<MenuInspectorControls
clientId={ clientId }
convertClassicMenu={ convertClassicMenu }
createNavigationMenuIsSuccess={
createNavigationMenuIsSuccess
}
createNavigationMenuIsError={ createNavigationMenuIsError }
currentMenuId={ ref }
handleUpdateMenu={ handleUpdateMenu }
isNavigationMenuMissing={ isNavigationMenuMissing }
innerBlocks={ innerBlocks }
isManageMenusButtonDisabled={ isManageMenusButtonDisabled }
onCreateNew={ createUntitledEmptyNavigationMenu }
/>
{ stylingInspectorControls }
<ResponsiveWrapper
id={ clientId }
Expand Down Expand Up @@ -770,7 +709,19 @@ function Navigation( {
if ( ref && isNavigationMenuMissing ) {
return (
<TagName { ...blockProps }>
<MenuInspectorControls />
<MenuInspectorControls
clientId={ clientId }
convertClassicMenu={ convertClassicMenu }
createNavigationMenuIsSuccess={
createNavigationMenuIsSuccess
}
createNavigationMenuIsError={ createNavigationMenuIsError }
handleUpdateMenu={ handleUpdateMenu }
isNavigationMenuMissing={ isNavigationMenuMissing }
innerBlocks={ innerBlocks }
isManageMenusButtonDisabled={ isManageMenusButtonDisabled }
onCreateNew={ createUntitledEmptyNavigationMenu }
/>
<Warning>
{ __(
'Navigation menu has been deleted or is unavailable. '
Expand Down Expand Up @@ -845,7 +796,20 @@ function Navigation( {
return (
<EntityProvider kind="postType" type="wp_navigation" id={ ref }>
<RecursionProvider uniqueId={ recursionId }>
<MenuInspectorControls currentMenuId={ ref } />
<MenuInspectorControls
clientId={ clientId }
convertClassicMenu={ convertClassicMenu }
createNavigationMenuIsSuccess={
createNavigationMenuIsSuccess
}
createNavigationMenuIsError={ createNavigationMenuIsError }
currentMenuId={ ref }
handleUpdateMenu={ handleUpdateMenu }
isNavigationMenuMissing={ isNavigationMenuMissing }
innerBlocks={ innerBlocks }
isManageMenusButtonDisabled={ isManageMenusButtonDisabled }
onCreateNew={ createUntitledEmptyNavigationMenu }
/>
{ stylingInspectorControls }
{ isEntityAvailable && (
<InspectorControls __experimentalGroup="advanced">
Expand Down
135 changes: 135 additions & 0 deletions packages/block-library/src/navigation/edit/menu-inspector-controls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/**
* WordPress dependencies
*/
import {
__experimentalOffCanvasEditor as OffCanvasEditor,
InspectorControls,
} from '@wordpress/block-editor';
import {
PanelBody,
__experimentalHStack as HStack,
__experimentalHeading as Heading,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import ManageMenusButton from './manage-menus-button';
import NavigationMenuSelector from './navigation-menu-selector';

const WrappedNavigationMenuSelector = ( {
clientId,
currentMenuId,
handleUpdateMenu,
convertClassicMenu,
onCreateNew,
createNavigationMenuIsSuccess,
createNavigationMenuIsError,
} ) => (
<NavigationMenuSelector
currentMenuId={ currentMenuId }
clientId={ clientId }
onSelectNavigationMenu={ ( menuId ) => {
handleUpdateMenu( menuId );
} }
onSelectClassicMenu={ async ( classicMenu ) => {
const navMenu = await convertClassicMenu(
classicMenu.id,
classicMenu.name,
'draft'
);
if ( navMenu ) {
handleUpdateMenu( navMenu.id, {
focusNavigationBlock: true,
} );
}
} }
onCreateNew={ onCreateNew }
createNavigationMenuIsSuccess={ createNavigationMenuIsSuccess }
createNavigationMenuIsError={ createNavigationMenuIsError }
/* translators: %s: The name of a menu. */
actionLabel={ __( "Switch to '%s'" ) }
/>
);
const MenuInspectorControls = ( {
clientId,
convertClassicMenu,
createNavigationMenuIsSuccess,
createNavigationMenuIsError,
currentMenuId = null,
handleUpdateMenu,
isNavigationMenuMissing,
innerBlocks,
isManageMenusButtonDisabled,
onCreateNew,
} ) => {
const isOffCanvasNavigationEditorEnabled =
window?.__experimentalEnableOffCanvasNavigationEditor === true;

return (
<InspectorControls>
<PanelBody
title={
isOffCanvasNavigationEditorEnabled ? null : __( 'Menu' )
}
>
{ isOffCanvasNavigationEditorEnabled ? (
<>
<HStack className="wp-block-navigation-off-canvas-editor__header">
<Heading
className="wp-block-navigation-off-canvas-editor__title"
level={ 2 }
>
{ __( 'Menu' ) }
</Heading>
<WrappedNavigationMenuSelector
clientId={ clientId }
currentMenuId={ currentMenuId }
handleUpdateMenu={ handleUpdateMenu }
convertClassicMenu={ convertClassicMenu }
onCreateNew={ onCreateNew }
createNavigationMenuIsSuccess={
createNavigationMenuIsSuccess
}
createNavigationMenuIsError={
createNavigationMenuIsError
}
/>
</HStack>
{ currentMenuId && isNavigationMenuMissing ? (
<p>{ __( 'Select or create a menu' ) }</p>
) : (
<OffCanvasEditor
blocks={ innerBlocks }
isExpanded={ true }
selectBlockInCanvas={ false }
/>
) }
</>
) : (
<>
<WrappedNavigationMenuSelector
clientId={ clientId }
currentMenuId={ currentMenuId }
handleUpdateMenu={ handleUpdateMenu }
convertClassicMenu={ convertClassicMenu }
onCreateNew={ onCreateNew }
createNavigationMenuIsSuccess={
createNavigationMenuIsSuccess
}
createNavigationMenuIsError={
createNavigationMenuIsError
}
/>
<ManageMenusButton
disabled={ isManageMenusButtonDisabled }
/>
</>
) }
</PanelBody>
</InspectorControls>
);
};

export default MenuInspectorControls;

0 comments on commit b9da345

Please sign in to comment.