-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
New block: Custom Navigation Menu #5036
Closed
Closed
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
8b5de5d
Start work
ryelle 1b737f2
Add a “placeholder” component to hold the menu selection
ryelle a991050
Display menu items when a menu is selected
ryelle adecbd7
Extract select control back into parent component to reuse it in Insp…
ryelle 2b409fe
Add php registration to set the stage for dynamic rendering
ryelle 3186275
Add styling for the list view
ryelle 7f32e11
Add a button-link to the customizer for empty menus
ryelle 0d267d1
Support horizontal nested menu
ryelle 01ad1a3
Sort menus by the defined order
ryelle 38f28d8
Both horizontal & vertical should support nesting
ryelle 7b931a6
Change how classes are applied to the container & child `ul`s
ryelle 253b21e
Update to new location of SelectControl
ryelle 9b794cb
Add meta data to the editor display
ryelle 0a8f76e
Add dynamic rendering for the menu block front-end display
ryelle 8a8f88c
Add the has-children class to items
ryelle 6f18c00
Style nested menus
ryelle eb9f163
Sort menu items
ryelle 44ac0dc
Fix padding around full-width menus
ryelle 1bf805e
Address PR feedback
ryelle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
import { filter, forEach, isEmpty, isUndefined } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component } from '@wordpress/element'; | ||
import { | ||
Button, | ||
Placeholder, | ||
Toolbar, | ||
SelectControl, | ||
Spinner, | ||
withAPIData, | ||
} from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './editor.scss'; | ||
import './style.scss'; | ||
import BlockControls from '../../block-controls'; | ||
import BlockAlignmentToolbar from '../../block-alignment-toolbar'; | ||
import InspectorControls from '../../inspector-controls'; | ||
import MenuItem from './item'; | ||
import MenuPlaceholder from './placeholder'; | ||
|
||
function getOptionsFromMenu( menus, selected ) { | ||
if ( ! menus ) { | ||
return []; | ||
} | ||
|
||
const options = menus.map( item => ( { | ||
value: item.id, | ||
label: item.name, | ||
} ) ); | ||
if ( ! selected ) { | ||
options.unshift( { value: '', label: __( 'Select' ) } ); | ||
} | ||
return options; | ||
} | ||
|
||
function sortMenuItems( items ) { | ||
return [ ...items ].sort( ( a, b ) => { | ||
return a.menu_order - b.menu_order; | ||
} ); | ||
} | ||
|
||
function getMenuChildren( list, data ) { | ||
forEach( list, function( item, i ) { | ||
const children = filter( data, { menu_item_parent: item.id } ); | ||
list[ i ].children = getMenuChildren( children, data ); | ||
} ); | ||
return sortMenuItems( list ); | ||
} | ||
|
||
function getMenuWithChildren( data ) { | ||
const items = filter( data, { menu_item_parent: 0 } ); | ||
const sorted = sortMenuItems( items ); | ||
return getMenuChildren( sorted, data ); | ||
} | ||
|
||
class NavigationMenuBlock extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
|
||
this.setMenu = this.setMenu.bind( this ); | ||
this.renderMenu = this.renderMenu.bind( this ); | ||
this.renderMenuLevel = this.renderMenuLevel.bind( this ); | ||
} | ||
|
||
setMenu( value ) { | ||
const { setAttributes } = this.props; | ||
if ( value ) { | ||
setAttributes( { selected: value } ); | ||
} | ||
} | ||
|
||
renderMenuLevel( items ) { | ||
if ( ! items || items.length < 1 ) { | ||
return null; | ||
} | ||
|
||
const { layout } = this.props.attributes; | ||
const isTopLevel = items[ 0 ].menu_item_parent === 0; | ||
const isHorizontal = layout === 'horizontal'; | ||
const className = classnames( { | ||
[ this.props.className ]: isTopLevel, | ||
'is-horizontal': isHorizontal, | ||
'is-vertical': ! isHorizontal, | ||
} ); | ||
|
||
return ( | ||
<ul | ||
key="navigation-menu" | ||
className={ className } | ||
> | ||
{ items.map( ( item, i ) => { | ||
return ( | ||
<MenuItem key={ i } item={ item }> | ||
{ item.children && this.renderMenuLevel( item.children ) } | ||
</MenuItem> | ||
); | ||
} ) } | ||
</ul> | ||
); | ||
} | ||
|
||
renderMenu() { | ||
const { data, isLoading } = this.props.items; | ||
|
||
if ( isUndefined( data ) || isEmpty( data ) || isLoading ) { | ||
return ( | ||
<Placeholder | ||
key="navigation-menu" | ||
icon="menu" | ||
label={ __( 'Navigation Menu' ) } | ||
> | ||
{ ! Array.isArray( data ) ? | ||
<Spinner /> : | ||
<Button href="customize.php?autofocus[panel]=nav_menus" target="_blank"> | ||
{ __( 'No items found in this menu.' ) } | ||
</Button> | ||
} | ||
</Placeholder> | ||
); | ||
} | ||
|
||
const nestedData = getMenuWithChildren( data ); | ||
return this.renderMenuLevel( nestedData ); | ||
} | ||
|
||
render() { | ||
const { attributes, focus, setAttributes } = this.props; | ||
const { align, layout, selected } = attributes; | ||
const { data: menus, isLoading } = this.props.menus; | ||
|
||
const menuSelectControl = ( | ||
<SelectControl | ||
label={ __( 'Select an existing menu' ) } | ||
value={ selected } | ||
onChange={ this.setMenu } | ||
options={ getOptionsFromMenu( menus, selected ) } | ||
/> | ||
); | ||
|
||
const inspectorControls = focus && ( | ||
<InspectorControls key="inspector"> | ||
<h3>{ __( 'Menu Settings' ) }</h3> | ||
{ ! isLoading && menuSelectControl } | ||
</InspectorControls> | ||
); | ||
|
||
const layoutControls = [ | ||
{ | ||
icon: 'list-view', | ||
title: __( 'Vertical View' ), | ||
onClick: () => setAttributes( { layout: 'vertical' } ), | ||
isActive: layout === 'vertical', | ||
}, | ||
{ | ||
icon: 'grid-view', | ||
title: __( 'Horizontal View' ), | ||
onClick: () => setAttributes( { layout: 'horizontal' } ), | ||
isActive: layout === 'horizontal', | ||
}, | ||
]; | ||
|
||
let displayBlock = ( | ||
<MenuPlaceholder | ||
key="navigation-menu" | ||
menus={ menus } | ||
selected={ false } | ||
setMenu={ this.setMenu } > | ||
{ menuSelectControl } | ||
</MenuPlaceholder> | ||
); | ||
|
||
if ( !! selected ) { | ||
displayBlock = this.renderMenu(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could avoid having to implement |
||
} | ||
|
||
return [ | ||
inspectorControls, | ||
focus && ( | ||
<BlockControls key="controls"> | ||
<BlockAlignmentToolbar | ||
value={ align } | ||
onChange={ ( nextAlign ) => { | ||
setAttributes( { align: nextAlign } ); | ||
} } | ||
controls={ [ 'center', 'wide', 'full' ] } | ||
/> | ||
<Toolbar controls={ layoutControls } /> | ||
</BlockControls> | ||
), | ||
displayBlock, | ||
]; | ||
} | ||
} | ||
|
||
export default withAPIData( props => { | ||
const { selected } = props.attributes; | ||
const queryString = selected ? `menus=${ selected }` : ''; | ||
const itemsUrl = selected ? `/wp/v2/menu-items?${ queryString }` : false; | ||
|
||
return { | ||
menus: '/wp/v2/menus', | ||
items: itemsUrl, | ||
}; | ||
} )( NavigationMenuBlock ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
.gutenberg .wp-block-navigation-menu { | ||
padding-left: 2.5em; | ||
&.is-vertical ul { | ||
padding-left: 2.5em; | ||
} | ||
&.is-horizontal { | ||
padding-left: 0; | ||
ul { | ||
border: 1px solid $light-gray-500; | ||
border-top: none; | ||
background: white; | ||
|
||
ul { | ||
border: none; | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
.gutenberg [data-align="full"] .wp-block-navigation-menu { | ||
padding-left: 2em; | ||
padding-right: 2em; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './editor.scss'; | ||
import './style.scss'; | ||
import NavMenuBlock from './block'; | ||
|
||
export const name = 'core/navigation-menu'; | ||
|
||
export const settings = { | ||
title: __( 'Navigation Menu' ), | ||
|
||
description: __( 'Show a list of menu items.' ), | ||
|
||
icon: 'menu', | ||
|
||
category: 'widgets', | ||
|
||
keywords: [ __( 'navigation menu' ) ], | ||
|
||
supports: { | ||
html: false, | ||
}, | ||
|
||
getEditWrapperProps( attributes ) { | ||
const { align } = attributes; | ||
if ( 'left' === align || 'right' === align || 'wide' === align || 'full' === align ) { | ||
return { 'data-align': align }; | ||
} | ||
}, | ||
|
||
edit: NavMenuBlock, | ||
|
||
save() { | ||
return null; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
/** | ||
* Server-side rendering of the `core/navigation-menu` block. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Renders the `core/navigation-menu` block on server. | ||
* | ||
* @param array $attributes The block attributes. | ||
* | ||
* @return string Returns the post content with navigtion menu added. | ||
*/ | ||
function gutenberg_render_block_core_navigation_menu( $attributes ) { | ||
|
||
$menu_id = (int) $attributes['selected']; | ||
|
||
if ( ! $menu_id ) { | ||
return ''; | ||
} | ||
|
||
$menu = wp_get_nav_menu_object( $menu_id ); | ||
|
||
if ( ! $menu ) { | ||
return ''; | ||
} | ||
|
||
$class = "wp-block-navigation-menu align{$attributes['align']}"; | ||
if ( isset( $attributes['layout'] ) && 'horizontal' === $attributes['layout'] ) { | ||
$class .= ' is-horizontal'; | ||
} | ||
|
||
return wp_nav_menu( array( | ||
'menu' => $menu, | ||
'menu_class' => $class, | ||
'fallback_cb' => false, | ||
'echo' => false, | ||
) ); | ||
} | ||
|
||
register_block_type( 'core/navigation-menu', array( | ||
'attributes' => array( | ||
'selected' => array( | ||
'type' => 'int', | ||
), | ||
'layout' => array( | ||
'type' => 'string', | ||
'default' => 'vertical', | ||
), | ||
'align' => array( | ||
'type' => 'string', | ||
'default' => 'center', | ||
), | ||
), | ||
'render_callback' => 'gutenberg_render_block_core_navigation_menu', | ||
) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { Component } from '@wordpress/element'; | ||
import { decodeEntities } from '@wordpress/utils'; | ||
|
||
class MenuItem extends Component { | ||
render() { | ||
const { children, item } = this.props; | ||
const classes = classnames( item.classes, { | ||
'menu-item-has-children': !! children, | ||
} ); | ||
|
||
return ( | ||
<li className={ classes }> | ||
<a title={ item.title } rel={ item.xfn } href={ item.url } target="_blank"> | ||
{ decodeEntities( item.title.trim() ) || __( '(Untitled)' ) } | ||
</a> | ||
{ children } | ||
</li> | ||
); | ||
} | ||
} | ||
|
||
export default MenuItem; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The link to edit menus would be useful even when there are menus already created. What if none of the existing menus are the ones you want?