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: menu links separator #25339

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 5 additions & 2 deletions packages/block-library/src/navigation-link/block.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"name": "core/navigation-link",
"category": "design",
"parent": [ "core/navigation" ],
"parent": [
"core/navigation"
],
"attributes": {
"label": {
"type": "string"
Expand Down Expand Up @@ -36,7 +38,8 @@
"customBackgroundColor",
"fontSize",
"customFontSize",
"showSubmenuIcon"
"showSubmenuIcon",
"separator"
],
"supports": {
"reusable": false,
Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ function NavigationLinkEdit( {
isParentOfSelectedBlock,
setAttributes,
showSubmenuIcon,
separator,
insertLinkBlock,
textColor,
backgroundColor,
Expand Down Expand Up @@ -314,6 +315,7 @@ function NavigationLinkEdit( {
backgroundColor: rgbBackgroundColor,
} }
ref={ listItemRef }
data-separator={ separator }
>
<div className="wp-block-navigation-link__content">
<RichText
Expand Down Expand Up @@ -487,6 +489,7 @@ export default compose( [
.length;
const showSubmenuIcon =
!! navigationBlockAttributes.showSubmenuIcon && hasDescendants;
const separator = navigationBlockAttributes.separator;
const isParentOfSelectedBlock = hasSelectedInnerBlock( clientId, true );
const isImmediateParentOfSelectedBlock = hasSelectedInnerBlock(
clientId,
Expand All @@ -503,6 +506,7 @@ export default compose( [
hasDescendants,
selectedBlockHasDescendants,
showSubmenuIcon,
separator,
textColor: navigationBlockAttributes.textColor,
backgroundColor: navigationBlockAttributes.backgroundColor,
userCanCreatePages: select( 'core' ).canUser( 'create', 'pages' ),
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/navigation-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ function render_block_core_navigation_link( $attributes, $content, $block ) {
$font_sizes['css_classes']
);
$style_attribute = ( $colors['inline_styles'] || $font_sizes['inline_styles'] );
$separator = ! empty( $block->context['separator'] ) ? ' data-separator="' . esc_attr( $block->context['separator'] ) . '"' : '';

$css_classes = trim( implode( ' ', $classes ) );
$has_submenu = count( $block->inner_blocks ) > 0;
Expand All @@ -128,7 +129,7 @@ function render_block_core_navigation_link( $attributes, $content, $block ) {
};

$html = '<li class="' . esc_attr( $css_classes . ( $has_submenu ? ' has-child' : '' ) ) .
( $is_active ? ' current-menu-item' : '' ) . '"' . $style_attribute . '>' .
( $is_active ? ' current-menu-item' : '' ) . '"' . $style_attribute . $separator . '>' .
'<a class="wp-block-navigation-link__content" ';

// Start appending HTML attributes to anchor tag.
Expand Down
12 changes: 10 additions & 2 deletions packages/block-library/src/navigation/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
"showSubmenuIcon": {
"type": "boolean",
"default": true
},
"separator": {
"type": "string",
"default": ""
}
},
"providesContext": {
Expand All @@ -38,10 +42,14 @@
"customBackgroundColor": "customBackgroundColor",
"fontSize": "fontSize",
"customFontSize": "customFontSize",
"showSubmenuIcon": "showSubmenuIcon"
"showSubmenuIcon": "showSubmenuIcon",
"separator": "separator"
},
"supports": {
"align": [ "wide", "full" ],
"align": [
"wide",
"full"
],
"anchor": true,
"html": false,
"inserter": true,
Expand Down
11 changes: 11 additions & 0 deletions packages/block-library/src/navigation/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

export const SEPARATOR_OPTIONS = [
{ value: '', label: __( 'None' ) },
{ value: '/', label: __( 'Forward Slash ( / )' ) },
{ value: '•', label: __( 'Bullet ( • )' ) },
{ value: '|', label: __( 'Pipe ( | )' ) },
];
15 changes: 14 additions & 1 deletion packages/block-library/src/navigation/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
ToggleControl,
Toolbar,
ToolbarGroup,
SelectControl,
} from '@wordpress/components';
import { compose } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
Expand All @@ -32,6 +33,7 @@ import useBlockNavigator from './use-block-navigator';
import BlockColorsStyleSelector from './block-colors-selector';
import * as navIcons from './icons';
import NavigationPlaceholder from './placeholder';
import { SEPARATOR_OPTIONS } from './constants';

function Navigation( {
selectedBlockHasDescendants,
Expand Down Expand Up @@ -131,7 +133,6 @@ function Navigation( {
: navIcons.justifyLeftIcon
}
label={ __( 'Change items justification' ) }
isCollapsed
controls={ [
{
icon: navIcons.justifyLeftIcon,
Expand Down Expand Up @@ -164,6 +165,18 @@ function Navigation( {
</BlockControls>
{ navigatorModal }
<InspectorControls>
{ 'horizontal' === attributes.orientation && (
<PanelBody title={ __( 'Separators' ) }>
<SelectControl
label={ __( 'Separator' ) }
onChange={ ( value ) => {
setAttributes( { separator: value } );
} }
options={ SEPARATOR_OPTIONS }
value={ attributes.separator }
/>
</PanelBody>
) }
<PanelBody title={ __( 'Display settings' ) }>
<ToggleControl
checked={ attributes.showSubmenuIcon }
Expand Down
14 changes: 14 additions & 0 deletions packages/block-library/src/navigation/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ $navigation-item-height: 46px;
}
}

// Move Separators to before element cause after is used for outlining the block.
.wp-block-navigation > .wp-block-navigation__container > .wp-block-navigation-link {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we move ::before to wp-block-navigation-link__label, then we might be able to have better control over its placement within the block's foot print. I tried to hack it in, and kind of get it working, but then it breaks the data-separator.

&::before {
content: attr(data-separator);
}

&:first-child::before {
display: none;
}
&::after {
content: "";
}
}

/**
* Colors Selector component
*/
Expand Down
11 changes: 11 additions & 0 deletions packages/block-library/src/navigation/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
}
}

// Separators.
.wp-block-navigation > .wp-block-navigation__container > .wp-block-navigation-link {
&::after {
content: attr(data-separator);
}

&:last-child::after {
display: none;
}
}

// Justification.
.items-justified-left > ul {
justify-content: flex-start;
Expand Down
21 changes: 11 additions & 10 deletions packages/e2e-tests/fixtures/blocks/core__navigation.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[
{
"clientId": "_clientId_0",
"name": "core/navigation",
"isValid": true,
"attributes": {
"showSubmenuIcon": true
},
"innerBlocks": [],
"originalContent": ""
}
{
"clientId": "_clientId_0",
"name": "core/navigation",
"isValid": true,
"attributes": {
"separator": "",
"showSubmenuIcon": true
},
"innerBlocks": [],
"originalContent": ""
}
]