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: Add overlay close button to icon toggle control #43067

Merged
merged 6 commits into from
Aug 25, 2022
Merged
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
17 changes: 14 additions & 3 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
import { __, sprintf } from '@wordpress/i18n';
import { speak } from '@wordpress/a11y';
import { createBlock } from '@wordpress/blocks';
import { close, Icon } from '@wordpress/icons';

/**
* Internal dependencies
Expand Down Expand Up @@ -465,15 +466,25 @@ function Navigation( {
setOverlayMenuPreview( ! overlayMenuPreview );
} }
>
{ hasIcon && <OverlayMenuIcon /> }
{ ! hasIcon && <span>{ __( 'Menu' ) }</span> }
{ hasIcon && (
<>
<OverlayMenuIcon />
<Icon icon={ close } />
</>
) }
{ ! hasIcon && (
<>
<span>{ __( 'Menu' ) }</span>
<span>{ __( 'Close' ) }</span>
</>
) }
</Button>
) }
{ overlayMenuPreview && (
<ToggleControl
label={ __( 'Show icon button' ) }
help={ __(
'Configure the visual appearance of the button opening the overlay menu.'
'Configure the visual appearance of the button opening and closing the overlay menu.'
) }
onChange={ ( value ) =>
setAttributes( { hasIcon: value } )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default function ResponsiveWrapper( {
{ ! isOpen && (
<Button
aria-haspopup="true"
aria-label={ __( 'Open menu' ) }
aria-label={ hasIcon && __( 'Open menu' ) }
className={ openButtonClasses }
onClick={ () => onToggle( true ) }
>
Expand All @@ -104,10 +104,15 @@ export default function ResponsiveWrapper( {
<div { ...dialogProps }>
<Button
className="wp-block-navigation__responsive-container-close"
aria-label={ __( 'Close menu' ) }
aria-label={ hasIcon && __( 'Close menu' ) }
onClick={ () => onToggle( false ) }
>
<Icon icon={ close } />
{ hasIcon && <Icon icon={ close } /> }
{ ! hasIcon && (
<span className="wp-block-navigation__toggle_button_label">
{ __( 'Close' ) }
</span>
) }
</Button>
<div
className="wp-block-navigation__responsive-container-content"
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/navigation/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ body.editor-styles-wrapper
.wp-block-navigation__overlay-menu-preview {
display: flex;
align-items: center;
justify-content: space-between;
width: 100%;
background-color: $gray-100;
padding: 0 $grid-unit-30;
Expand Down
21 changes: 13 additions & 8 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -610,16 +610,20 @@ function render_block_core_navigation( $attributes, $content, $block ) {
$is_hidden_by_default ? 'always-shown' : '',
);

$toggle_button_icon = '<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><rect x="4" y="7.5" width="16" height="1.5" /><rect x="4" y="15" width="16" height="1.5" /></svg>';
$should_display_icon_label = isset( $attributes['hasIcon'] ) && true === $attributes['hasIcon'];
$toggle_button_content = $should_display_icon_label ? $toggle_button_icon : 'Menu';
$should_display_icon_label = isset( $attributes['hasIcon'] ) && true === $attributes['hasIcon'];
$toggle_button_icon = '<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><rect x="4" y="7.5" width="16" height="1.5" /><rect x="4" y="15" width="16" height="1.5" /></svg>';
$toggle_button_content = $should_display_icon_label ? $toggle_button_icon : __( 'Menu' );
$toggle_close_button_icon = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false"><path d="M13 11.8l6.1-6.3-1-1-6.1 6.2-6.1-6.2-1 1 6.1 6.3-6.5 6.7 1 1 6.5-6.6 6.5 6.6 1-1z"></path></svg>';
$toggle_close_button_content = $should_display_icon_label ? $toggle_close_button_icon : __( 'Close' );
$toggle_aria_label_open = $should_display_icon_label ? 'aria-label="' . __( 'Open menu' ) . '"' : ''; // Open button label.
$toggle_aria_label_close = $should_display_icon_label ? 'aria-label="' . __( 'Close menu' ) . '"' : ''; // Close button label.

$responsive_container_markup = sprintf(
'<button aria-haspopup="true" aria-label="%3$s" class="%6$s" data-micromodal-trigger="%1$s">%9$s</button>
'<button aria-haspopup="true" %3$s class="%6$s" data-micromodal-trigger="%1$s">%9$s</button>
<div class="%5$s" style="%7$s" id="%1$s">
<div class="wp-block-navigation__responsive-close" tabindex="-1" data-micromodal-close>
<div class="wp-block-navigation__responsive-dialog" aria-label="%8$s">
<button aria-label="%4$s" data-micromodal-close class="wp-block-navigation__responsive-container-close"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false"><path d="M13 11.8l6.1-6.3-1-1-6.1 6.2-6.1-6.2-1 1 6.1 6.3-6.5 6.7 1 1 6.5-6.6 6.5 6.6 1-1z"></path></svg></button>
<button %4$s data-micromodal-close class="wp-block-navigation__responsive-container-close">%10$s</button>
<div class="wp-block-navigation__responsive-container-content" id="%1$s-content">
%2$s
</div>
Expand All @@ -628,13 +632,14 @@ function render_block_core_navigation( $attributes, $content, $block ) {
</div>',
esc_attr( $modal_unique_id ),
$inner_blocks_html,
__( 'Open menu' ), // Open button label.
__( 'Close menu' ), // Close button label.
$toggle_aria_label_open,
$toggle_aria_label_close,
esc_attr( implode( ' ', $responsive_container_classes ) ),
esc_attr( implode( ' ', $open_button_classes ) ),
safecss_filter_attr( $colors['overlay_inline_styles'] ),
__( 'Menu' ),
$toggle_button_content
$toggle_button_content,
$toggle_close_button_content
);

return sprintf(
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/navigation/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@ button.wp-block-navigation-item__content {
border: none;
margin: 0;
padding: 0;
text-transform: inherit;
Copy link
Member Author

Choose a reason for hiding this comment

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

This is to fix a separate issue I noticed where the text version of these buttons doesn't inherit the text-transform from the navigation block, e.g. if text-transform: uppercase is set on the navigation block, this wasn't being applied to the text label.


svg {
fill: currentColor;
Expand Down