From 469e2886a3bfbd89eba7cb55c982b6111d9bcee7 Mon Sep 17 00:00:00 2001 From: Mario Santos <34552881+SantosGuillamot@users.noreply.github.com> Date: Fri, 30 Sep 2022 05:25:35 +0200 Subject: [PATCH] Remove view.js file from navigation-submenu block (#44570) --- .../src/navigation-submenu/view.js | 67 ------------------- 1 file changed, 67 deletions(-) delete mode 100644 packages/block-library/src/navigation-submenu/view.js diff --git a/packages/block-library/src/navigation-submenu/view.js b/packages/block-library/src/navigation-submenu/view.js deleted file mode 100644 index 264c51fc8dc5a8..00000000000000 --- a/packages/block-library/src/navigation-submenu/view.js +++ /dev/null @@ -1,67 +0,0 @@ -const closeSubmenus = ( element ) => { - element - .querySelectorAll( '[aria-expanded="true"]' ) - .forEach( ( toggle ) => { - toggle.setAttribute( 'aria-expanded', 'false' ); - } ); -}; - -const toggleSubmenuOnClick = ( event ) => { - const buttonToggle = event.target.closest( '[aria-expanded]' ); - const isSubmenuOpen = buttonToggle.getAttribute( 'aria-expanded' ); - - if ( isSubmenuOpen === 'true' ) { - closeSubmenus( buttonToggle.closest( '.wp-block-navigation-submenu' ) ); - } else { - // Close all sibling submenus. - const parentElement = buttonToggle.closest( - '.wp-block-navigation-submenu' - ); - const parentList = - buttonToggle.closest( '.wp-block-navigation__submenu-container' ) || - buttonToggle.closest( '.wp-block-navigation__container' ); - Array.from( parentList.children ).forEach( ( child ) => { - if ( child !== parentElement ) { - closeSubmenus( child ); - } - } ); - // Open submenu. - buttonToggle.setAttribute( 'aria-expanded', 'true' ); - } -}; - -const submenuButtons = document.querySelectorAll( - '.wp-block-navigation-submenu__toggle' -); - -submenuButtons.forEach( ( button ) => { - button.addEventListener( 'click', toggleSubmenuOnClick ); -} ); - -// Close on click outside. -document.addEventListener( 'click', function ( event ) { - const navigationBlocks = document.querySelectorAll( - '.wp-block-navigation' - ); - navigationBlocks.forEach( ( block ) => { - if ( ! block.contains( event.target ) ) { - closeSubmenus( block ); - } - } ); -} ); -// Close on focus outside or escape key. -document.addEventListener( 'keyup', function ( event ) { - const submenuBlocks = document.querySelectorAll( - '.wp-block-navigation-submenu' - ); - submenuBlocks.forEach( ( block ) => { - if ( ! block.contains( event.target ) ) { - closeSubmenus( block ); - } else if ( event.key === 'Escape' ) { - const toggle = block.querySelector( '[aria-expanded="true"]' ); - closeSubmenus( block ); - // Focus the submenu trigger so focus does not get trapped in the closed submenu. - toggle?.focus(); - } - } ); -} );