From 5180961988b2a06b171aacfb39efb203699f44d9 Mon Sep 17 00:00:00 2001 From: Luis Herranz Date: Thu, 25 May 2023 12:18:07 +0200 Subject: [PATCH 1/8] Set correct aria-expanded on hover --- lib/experimental/interactivity-api/blocks.php | 4 +++- .../block-library/src/navigation/interactivity.js | 13 ++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/experimental/interactivity-api/blocks.php b/lib/experimental/interactivity-api/blocks.php index 3ad6d13d660fb..feef76fd94ec3 100644 --- a/lib/experimental/interactivity-api/blocks.php +++ b/lib/experimental/interactivity-api/blocks.php @@ -192,10 +192,12 @@ function gutenberg_block_core_navigation_add_directives_to_submenu( $w ) { ) ) ) { // Add directives to the parent `
  • `. - $w->set_attribute( 'data-wp-context', '{ "core": { "navigation": { "isMenuOpen": false, "overlay": false } } }' ); + $w->set_attribute( 'data-wp-context', '{ "core": { "navigation": { "isMenuOpen": false, "overlay": false, "isHovering": false } } }' ); $w->set_attribute( 'data-wp-effect', 'effects.core.navigation.initMenu' ); $w->set_attribute( 'data-wp-on.focusout', 'actions.core.navigation.handleMenuFocusout' ); $w->set_attribute( 'data-wp-on.keydown', 'actions.core.navigation.handleMenuKeydown' ); + $w->set_attribute( 'data-wp-on.mouseover', 'actions.core.navigation.openMenuOnHover' ); + $w->set_attribute( 'data-wp-on.mouseout', 'actions.core.navigation.closeMenuOnHover' ); // Add directives to the toggle submenu button. if ( $w->next_tag( diff --git a/packages/block-library/src/navigation/interactivity.js b/packages/block-library/src/navigation/interactivity.js index 80152762c9cd6..26a82f6591ade 100644 --- a/packages/block-library/src/navigation/interactivity.js +++ b/packages/block-library/src/navigation/interactivity.js @@ -68,7 +68,10 @@ store( { } }, closeMenu: ( { context } ) => { - if ( context.core.navigation.isMenuOpen ) { + if ( + context.core.navigation.isMenuOpen && + ! context.core.navigation.isHovering + ) { context.core.navigation.isMenuOpen = false; if ( context.core.navigation.modal.contains( @@ -86,6 +89,14 @@ store( { } } }, + openMenuOnHover( { actions, context, ref } ) { + context.core.navigation.isHovering = true; + actions.core.navigation.openMenu( { context, ref } ); + }, + closeMenuOnHover( { actions, context } ) { + context.core.navigation.isHovering = false; + actions.core.navigation.closeMenu( { context } ); + }, toggleMenu: ( { context, actions, ref } ) => { if ( context.core.navigation.isMenuOpen ) { actions.core.navigation.closeMenu( { context } ); From dfa4425d9213c51f0255cff5744bb6bc9fb54906 Mon Sep 17 00:00:00 2001 From: Luis Herranz Date: Tue, 30 May 2023 11:06:05 +0200 Subject: [PATCH 2/8] Store both `click` and `hover` in `isMenuOpen` --- lib/experimental/interactivity-api/blocks.php | 51 ++++--- .../src/navigation/interactivity.js | 137 +++++++++--------- 2 files changed, 97 insertions(+), 91 deletions(-) diff --git a/lib/experimental/interactivity-api/blocks.php b/lib/experimental/interactivity-api/blocks.php index feef76fd94ec3..e9e1b768aa462 100644 --- a/lib/experimental/interactivity-api/blocks.php +++ b/lib/experimental/interactivity-api/blocks.php @@ -35,7 +35,7 @@ function gutenberg_block_core_file_add_directives_to_content( $block_content, $b * * * * @param string $block_content Markup of the navigation block. + * @param array $block Block object. * * @return string Navigation block markup with the proper directives */ -function gutenberg_block_core_navigation_add_directives_to_markup( $block_content, $block, $instance ) { +function gutenberg_block_core_navigation_add_directives_to_markup( $block_content, $block ) { $w = new WP_HTML_Tag_Processor( $block_content ); // Add directives to the `
  • * - * @param string $w Markup of the navigation block. + * @param string $w Markup of the navigation block. + * @param array $block_attributes Block attributes. * * @return void */ @@ -198,7 +200,7 @@ function gutenberg_block_core_navigation_add_directives_to_submenu( $w, $block_a $w->set_attribute( 'data-wp-effect', 'effects.core.navigation.initMenu' ); $w->set_attribute( 'data-wp-on.focusout', 'actions.core.navigation.handleMenuFocusout' ); $w->set_attribute( 'data-wp-on.keydown', 'actions.core.navigation.handleMenuKeydown' ); - if ( ! isset( $block_attributes[ 'openSubmenusOnClick' ] ) ) { + if ( ! isset( $block_attributes['openSubmenusOnClick'] ) ) { $w->set_attribute( 'data-wp-on.mouseenter', 'actions.core.navigation.openMenuOnHover' ); $w->set_attribute( 'data-wp-on.mouseleave', 'actions.core.navigation.closeMenuOnHover' ); } @@ -219,7 +221,7 @@ function gutenberg_block_core_navigation_add_directives_to_submenu( $w, $block_a } }; -add_filter( 'render_block_core/navigation', 'gutenberg_block_core_navigation_add_directives_to_markup', 10, 3 ); +add_filter( 'render_block_core/navigation', 'gutenberg_block_core_navigation_add_directives_to_markup', 10, 2 ); /** * Replaces view script for the File, Navigation, and Image blocks with version using Interactivity API. From b2c51183c284432e8a05ab25bd6e5a1fa28859f7 Mon Sep 17 00:00:00 2001 From: Luis Herranz Date: Tue, 30 May 2023 11:51:49 +0200 Subject: [PATCH 7/8] Just in case openSubmenusOnClick exists but it's false --- lib/experimental/interactivity-api/blocks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/interactivity-api/blocks.php b/lib/experimental/interactivity-api/blocks.php index d02fa9682f3f0..d9a2b1b2718a8 100644 --- a/lib/experimental/interactivity-api/blocks.php +++ b/lib/experimental/interactivity-api/blocks.php @@ -200,7 +200,7 @@ function gutenberg_block_core_navigation_add_directives_to_submenu( $w, $block_a $w->set_attribute( 'data-wp-effect', 'effects.core.navigation.initMenu' ); $w->set_attribute( 'data-wp-on.focusout', 'actions.core.navigation.handleMenuFocusout' ); $w->set_attribute( 'data-wp-on.keydown', 'actions.core.navigation.handleMenuKeydown' ); - if ( ! isset( $block_attributes['openSubmenusOnClick'] ) ) { + if ( ! isset( $block_attributes['openSubmenusOnClick'] ) || false === $block_attributes['openSubmenusOnClick'] ) { $w->set_attribute( 'data-wp-on.mouseenter', 'actions.core.navigation.openMenuOnHover' ); $w->set_attribute( 'data-wp-on.mouseleave', 'actions.core.navigation.closeMenuOnHover' ); } From 07b41f91b3381a4e61c5f92556c3d48abf8004b8 Mon Sep 17 00:00:00 2001 From: Luis Herranz Date: Tue, 30 May 2023 13:23:17 +0200 Subject: [PATCH 8/8] Switch to isMenuOpen selector in roleAttribute --- packages/block-library/src/navigation/interactivity.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/navigation/interactivity.js b/packages/block-library/src/navigation/interactivity.js index deb364d0f42b3..dfc9cb0f9c4a5 100644 --- a/packages/block-library/src/navigation/interactivity.js +++ b/packages/block-library/src/navigation/interactivity.js @@ -73,9 +73,9 @@ store( { selectors: { core: { navigation: { - roleAttribute: ( { context } ) => + roleAttribute: ( { context, selectors } ) => context.core.navigation.overlay && - context.core.navigation.isMenuOpen + selectors.core.navigation.isMenuOpen( { context } ) ? 'dialog' : '', isMenuOpen: ( { context } ) =>