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

Navigation Editor: Allow menu renaming #29012

Merged
merged 23 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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: 22 additions & 0 deletions packages/edit-navigation/src/components/name-display/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* WordPress dependencies
*/
import { ToolbarGroup, ToolbarButton } from '@wordpress/components';
import { BlockControls } from '@wordpress/block-editor';
/**
* Internal dependencies
*/
import { useNavigationEditorMenu } from '../../hooks';

export default function NameDisplay( { setIsMenuNameEditFocused } ) {
const { menuName } = useNavigationEditorMenu();
return (
<BlockControls>
<ToolbarGroup>
<ToolbarButton onClick={ setIsMenuNameEditFocused }>
grzim marked this conversation as resolved.
Show resolved Hide resolved
grzim marked this conversation as resolved.
Show resolved Hide resolved
{ menuName }
</ToolbarButton>
</ToolbarGroup>
</BlockControls>
);
}
58 changes: 41 additions & 17 deletions packages/edit-navigation/src/components/name-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,58 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useEffect, useState } from '@wordpress/element';
import { useEffect, useRef, useState } from '@wordpress/element';
/**
* Internal dependencies
*/
import { BlockControls } from '@wordpress/block-editor';
import { ToolbarGroup, ToolbarItem } from '@wordpress/components';
import { InspectorControls } from '@wordpress/block-editor';
import { PanelBody, BaseControl } from '@wordpress/components';
import { useMenuEntity, useNavigationEditorMenu } from '../../hooks';
import { useInstanceId } from '@wordpress/compose';

export function NameEditor() {
export function NameEditor( {
setIsMenuNameEditFocused,
isMenuNameEditFocused,
} ) {
const { menuName, menuId } = useNavigationEditorMenu();
const { editMenuName } = useMenuEntity( menuId );
talldan marked this conversation as resolved.
Show resolved Hide resolved
const inputRef = useRef();
const [ tmpMenuName, setTmpMenuName ] = useState( menuName );
grzim marked this conversation as resolved.
Show resolved Hide resolved
const instanceId = useInstanceId( NameEditor );
const id = `components-edit-navigation-name-editor__input-${ instanceId }`;
useEffect( () => setTmpMenuName( menuName ), [ menuName ] );
useEffect( () => {
if ( isMenuNameEditFocused ) inputRef.current.focus();
}, [ isMenuNameEditFocused ] );
return (
<>
<BlockControls>
<ToolbarGroup>
<ToolbarItem
as="input"
value={ tmpMenuName }
onChange={ ( { target: { value } } ) => {
setTmpMenuName( value );
editMenuName( value );
} }
aria-label={ __( 'Edit menu name' ) }
/>
</ToolbarGroup>
</BlockControls>
<InspectorControls>
<PanelBody title={ __( 'Menu Settings' ) }>
<BaseControl label={ __( 'Name' ) } id={ id }>
grzim marked this conversation as resolved.
Show resolved Hide resolved
<div>
<input
grzim marked this conversation as resolved.
Show resolved Hide resolved
ref={ inputRef }
id={ id }
onBlur={ () =>
setIsMenuNameEditFocused( false )
}
className="components-text-control__input"
value={ tmpMenuName }
onChange={ ( { target: { value } } ) => {
setTmpMenuName( value );
editMenuName( value );
} }
aria-label={ __( 'Edit menu name' ) }
/>
</div>
</BaseControl>
<div className="edit-navigation-name-editor__edit-name-description">
{ __(
'A short, descriptive name used to refer to this menu elsewhere.'
) }
</div>
</PanelBody>
</InspectorControls>
</>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.edit-navigation-name-editor__edit-name-description {
font-size: $helptext-font-size;
font-style: normal;
color: $gray-700;
}
18 changes: 14 additions & 4 deletions packages/edit-navigation/src/filters/add-menu-name-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,31 @@
/**
* Internal dependencies
*/
import { useState } from '@wordpress/element';
import { NameEditor } from '../components/name-editor';
import { addFilter } from '@wordpress/hooks';
import { createHigherOrderComponent } from '@wordpress/compose';
import { useNavigationEditor } from '../hooks';
import NameDisplay from '../components/name-display';

const addMenuNameEditor = createHigherOrderComponent(
( BlockEdit ) => ( props ) => {
const [ isMenuNameEditFocused, setIsMenuNameEditFocused ] = useState(
false
);
if ( props.name !== 'core/navigation' ) {
return <BlockEdit { ...props } />;
}
const { menuName } = useNavigationEditor();
return (
<>
<BlockEdit { ...props } menuName={ menuName } />
<NameEditor { ...props } />
<BlockEdit { ...props } />
<NameDisplay
setIsMenuNameEditFocused={ setIsMenuNameEditFocused }
/>
<NameEditor
{ ...props }
setIsMenuNameEditFocused={ setIsMenuNameEditFocused }
isMenuNameEditFocused={ isMenuNameEditFocused }
/>
</>
);
},
Expand Down