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

Reposition navigation selector #45555

Merged
merged 11 commits into from
Nov 9, 2022
Merged
292 changes: 158 additions & 134 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ 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 Down Expand Up @@ -650,56 +652,82 @@ function Navigation( {
// Consider this state as 'unsaved' and offer an uncontrolled version of inner blocks,
// 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'" ) }
/>
);

if ( hasUnsavedBlocks ) {
return (
<TagName { ...blockProps }>
<InspectorControls>
<PanelBody title={ __( 'Menu' ) }>
<NavigationMenuSelector
currentMenuId={ ref }
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
}
/* translators: %s: The name of a menu. */
actionLabel={ __( "Switch to '%s'" ) }
/>
{ isOffCanvasNavigationEditorEnabled && (
<OffCanvasEditor
blocks={ innerBlocks }
isExpanded={ true }
selectBlockInCanvas={ false }
/>
) }
{ ! isOffCanvasNavigationEditorEnabled && (
<Button
variant="link"
disabled={
! hasManagePermissions ||
! hasResolvedNavigationMenus
}
href={ addQueryArgs( 'edit.php', {
post_type: 'wp_navigation',
} ) }
>
{ __( 'Manage menus' ) }
</Button>
<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={ ref }
/>
</HStack>
<OffCanvasEditor
blocks={ innerBlocks }
isExpanded={ true }
selectBlockInCanvas={ false }
/>
</>
) : (
<>
<WrappedNavigationMenuSelector
currentMenuId={ ref }
/>
<Button
variant="link"
disabled={
! hasManagePermissions ||
! hasResolvedNavigationMenus
}
href={ addQueryArgs( 'edit.php', {
post_type: 'wp_navigation',
} ) }
>
{ __( 'Manage menus' ) }
</Button>
</>
) }
</PanelBody>
</InspectorControls>
Expand Down Expand Up @@ -746,45 +774,46 @@ function Navigation( {
return (
<TagName { ...blockProps }>
<InspectorControls>
<PanelBody title={ __( 'Menu' ) }>
<NavigationMenuSelector
currentMenuId={ null }
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
}
/* translators: %s: The name of a menu. */
actionLabel={ __( "Switch to '%s'" ) }
/>
{ ! isOffCanvasNavigationEditorEnabled && (
<Button
variant="link"
disabled={
! hasManagePermissions ||
! hasResolvedNavigationMenus
}
href={ addQueryArgs( 'edit.php', {
post_type: 'wp_navigation',
} ) }
>
{ __( 'Manage menus' ) }
</Button>
<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={ null }
/>
</HStack>
<p>Select or create a menu</p>
</>
) : (
<>
<WrappedNavigationMenuSelector
currentMenuId={ null }
/>
<Button
variant="link"
disabled={
! hasManagePermissions ||
! hasResolvedNavigationMenus
}
href={ addQueryArgs( 'edit.php', {
post_type: 'wp_navigation',
} ) }
>
{ __( 'Manage menus' ) }
</Button>
</>
) }
</PanelBody>
</InspectorControls>
Expand Down Expand Up @@ -863,55 +892,50 @@ function Navigation( {
<EntityProvider kind="postType" type="wp_navigation" id={ ref }>
<RecursionProvider uniqueId={ recursionId }>
<InspectorControls>
<PanelBody title={ __( 'Menu' ) }>
<NavigationMenuSelector
currentMenuId={ ref }
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'" ) }
/>
{ isOffCanvasNavigationEditorEnabled && (
<OffCanvasEditor
blocks={ innerBlocks }
isExpanded={ true }
selectBlockInCanvas={ false }
/>
) }
{ ! isOffCanvasNavigationEditorEnabled && (
<Button
variant="link"
disabled={
! hasManagePermissions ||
! hasResolvedNavigationMenus
}
href={ addQueryArgs( 'edit.php', {
post_type: 'wp_navigation',
} ) }
>
{ __( 'Manage menus' ) }
</Button>
<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={ ref }
/>
</HStack>
<OffCanvasEditor
blocks={ innerBlocks }
isExpanded={ true }
selectBlockInCanvas={ false }
/>
</>
) : (
<>
<WrappedNavigationMenuSelector
currentMenuId={ ref }
/>
<Button
variant="link"
disabled={
! hasManagePermissions ||
! hasResolvedNavigationMenus
}
href={ addQueryArgs( 'edit.php', {
post_type: 'wp_navigation',
} ) }
>
{ __( 'Manage menus' ) }
</Button>
</>
) }
</PanelBody>
</InspectorControls>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
VisuallyHidden,
} from '@wordpress/components';
import { useEntityProp } from '@wordpress/core-data';
import { Icon, chevronUp, chevronDown } from '@wordpress/icons';
import { Icon, chevronUp, chevronDown, moreVertical } from '@wordpress/icons';
import { __, sprintf } from '@wordpress/i18n';
import { decodeEntities } from '@wordpress/html-entities';
import { useEffect, useMemo, useState } from '@wordpress/element';
Expand All @@ -31,6 +31,9 @@ function NavigationMenuSelector( {
createNavigationMenuIsError,
toggleProps = {},
} ) {
const isOffCanvasNavigationEditorEnabled =
window?.__experimentalEnableOffCanvasNavigationEditor === true;

/* translators: %s: The name of a menu. */
const createActionLabel = __( "Create from '%s'" );

Expand Down Expand Up @@ -161,11 +164,19 @@ function NavigationMenuSelector( {

return (
<DropdownMenu
className="wp-block-navigation__navigation-selector"
className={
isOffCanvasNavigationEditorEnabled
? ''
: 'wp-block-navigation__navigation-selector'
}
label={ selectorLabel }
text={ selectorLabel }
icon={ null }
toggleProps={ toggleProps }
text={ isOffCanvasNavigationEditorEnabled ? '' : selectorLabel }
icon={ isOffCanvasNavigationEditorEnabled ? moreVertical : null }
toggleProps={
isOffCanvasNavigationEditorEnabled
? { isSmall: true }
: toggleProps
}
>
{ ( { onClose } ) => (
<>
Expand Down
7 changes: 7 additions & 0 deletions packages/block-library/src/navigation/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,13 @@ body.editor-styles-wrapper
color: inherit;
}

.components-heading.wp-block-navigation-off-canvas-editor__title {
margin: 0;
}
.wp-block-navigation-off-canvas-editor__header {
margin-bottom: $grid-unit-10;
}

// Customize the mobile editing.
// This can be revisited in the future, but for now, inherit design from the parent.
.is-menu-open .wp-block-navigation__responsive-container-content * {
Expand Down