Skip to content

Commit

Permalink
Add aria-label attribute to navigation block only when it is open (#…
Browse files Browse the repository at this point in the history
…54816)

* Add `aria-label` only when is open

* Remove unnecessary `label` property in edit

* Escape translation

* Move navigation context to `wp_json_encode`

* Add `wp_json_encode` flags
  • Loading branch information
SantosGuillamot authored Oct 10, 2023
1 parent 3a123e7 commit f6b21db
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
1 change: 0 additions & 1 deletion packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,6 @@ function Navigation( {
<ResponsiveWrapper
id={ clientId }
onToggle={ setResponsiveMenuVisibility }
label={ __( 'Menu' ) }
hasIcon={ hasIcon }
icon={ icon }
isOpen={ isResponsiveMenuOpen }
Expand Down
25 changes: 19 additions & 6 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -696,9 +696,22 @@ function render_block_core_navigation( $attributes, $content, $block ) {
$responsive_dialog_directives = '';
$close_button_directives = '';
if ( $should_load_view_script ) {
$nav_element_context = wp_json_encode(
array(
'core' => array(
'navigation' => array(
'overlayOpenedBy' => array(),
'type' => 'overlay',
'roleAttribute' => '',
'ariaLabel' => __( 'Menu' ),
),
),
),
JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP
);
$nav_element_directives = '
data-wp-interactive
data-wp-context=\'{ "core": { "navigation": { "overlayOpenedBy": {}, "type": "overlay", "roleAttribute": "" } } }\'
data-wp-context=\'' . $nav_element_context . '\'
';
$open_button_directives = '
data-wp-on--click="actions.core.navigation.openMenuOnClick"
Expand All @@ -714,6 +727,7 @@ function render_block_core_navigation( $attributes, $content, $block ) {
';
$responsive_dialog_directives = '
data-wp-bind--aria-modal="selectors.core.navigation.ariaModal"
data-wp-bind--aria-label="selectors.core.navigation.ariaLabel"
data-wp-bind--role="selectors.core.navigation.roleAttribute"
data-wp-effect="effects.core.navigation.focusFirstElement"
';
Expand All @@ -723,11 +737,11 @@ function render_block_core_navigation( $attributes, $content, $block ) {
}

$responsive_container_markup = sprintf(
'<button aria-haspopup="true" %3$s class="%6$s" %11$s>%9$s</button>
<div class="%5$s" style="%7$s" id="%1$s" %12$s>
'<button aria-haspopup="true" %3$s class="%6$s" %10$s>%8$s</button>
<div class="%5$s" style="%7$s" id="%1$s" %11$s>
<div class="wp-block-navigation__responsive-close" tabindex="-1">
<div class="wp-block-navigation__responsive-dialog" aria-label="%8$s" %13$s>
<button %4$s class="wp-block-navigation__responsive-container-close" %14$s>%10$s</button>
<div class="wp-block-navigation__responsive-dialog" %12$s>
<button %4$s class="wp-block-navigation__responsive-container-close" %13$s>%9$s</button>
<div class="wp-block-navigation__responsive-container-content" id="%1$s-content">
%2$s
</div>
Expand All @@ -741,7 +755,6 @@ function render_block_core_navigation( $attributes, $content, $block ) {
esc_attr( implode( ' ', $responsive_container_classes ) ),
esc_attr( implode( ' ', $open_button_classes ) ),
esc_attr( safecss_filter_attr( $colors['overlay_inline_styles'] ) ),
__( 'Menu' ),
$toggle_button_content,
$toggle_close_button_content,
$open_button_directives,
Expand Down
7 changes: 7 additions & 0 deletions packages/block-library/src/navigation/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ wpStore( {
? 'true'
: null;
},
ariaLabel: ( store ) => {
const { context, selectors } = store;
return context.core.navigation.type === 'overlay' &&
selectors.core.navigation.isMenuOpen( store )
? context.core.navigation.ariaLabel
: null;
},
isMenuOpen: ( { context } ) =>
// The menu is opened if either `click`, `hover` or `focus` is true.
Object.values(
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/query/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ function render_block_core_query( $attributes, $content, $block ) {
'loadedText' => __( 'Page Loaded.' ),
),
),
)
),
JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP
)
);
$content = $p->get_updated_html();
Expand Down

0 comments on commit f6b21db

Please sign in to comment.