summaryrefslogtreecommitdiffstatshomepage
path: root/src/js/_enqueues/admin/common.js
diff options
context:
space:
mode:
authorJoe Dolson <joedolson@git.wordpress.org>2024-06-20 19:39:50 +0000
committerJoe Dolson <joedolson@git.wordpress.org>2024-06-20 19:39:50 +0000
commita2549c322ed8e176b379120a1dd6292860354750 (patch)
treec4650784a2b3d7892f54eef74d8d1757fc6eaebf /src/js/_enqueues/admin/common.js
parent2358de1767168232ff0e7c17e550b8a99f96002e (diff)
downloadwordpress-a2549c322ed8e176b379120a1dd6292860354750.tar.gz
wordpress-a2549c322ed8e176b379120a1dd6292860354750.zip
Administration: A11y: Use `aria-haspopup` only if item has popup behavior.
Update behavior of admin menu so that the `aria-haspopup` attribute is only applied in responsive mode, when there is a popup behavior present. Add `aria-expanded` attributes to report current popup state. Props afercia, khokansardar, ryokuhi, joedolson. Fixes #43095. git-svn-id: https://develop.svn.wordpress.org/trunk@58449 602fd350-edb4-49c9-b593-d223f7449a82
Diffstat (limited to 'src/js/_enqueues/admin/common.js')
-rw-r--r--src/js/_enqueues/admin/common.js57
1 files changed, 33 insertions, 24 deletions
diff --git a/src/js/_enqueues/admin/common.js b/src/js/_enqueues/admin/common.js
index 73e7e9caf1..b2d8b56c63 100644
--- a/src/js/_enqueues/admin/common.js
+++ b/src/js/_enqueues/admin/common.js
@@ -903,27 +903,6 @@ $( function() {
});
/**
- * Handles the `aria-haspopup` attribute on the current menu item when it has a submenu.
- *
- * @since 4.4.0
- *
- * @return {void}
- */
- function currentMenuItemHasPopup() {
- var $current = $( 'a.wp-has-current-submenu' );
-
- if ( 'folded' === menuState ) {
- // When folded or auto-folded and not responsive view, the current menu item does have a fly-out sub-menu.
- $current.attr( 'aria-haspopup', 'true' );
- } else {
- // When expanded or in responsive view, reset aria-haspopup.
- $current.attr( 'aria-haspopup', 'false' );
- }
- }
-
- $document.on( 'wp-menu-state-set wp-collapse-menu wp-responsive-activate wp-responsive-deactivate', currentMenuItemHasPopup );
-
- /**
* Ensures an admin submenu is within the visual viewport.
*
* @since 4.1.0
@@ -1695,8 +1674,10 @@ $( function() {
// Modify functionality based on custom activate/deactivate event.
$document.on( 'wp-responsive-activate.wp-responsive', function() {
self.activate();
+ self.toggleAriaHasPopup( 'add' );
}).on( 'wp-responsive-deactivate.wp-responsive', function() {
self.deactivate();
+ self.toggleAriaHasPopup( 'remove' );
});
$( '#wp-admin-bar-menu-toggle a' ).attr( 'aria-expanded', 'false' );
@@ -1748,7 +1729,7 @@ $( function() {
setTimeout( function() {
var focusIsInToggle = $.contains( toggleButton, focusedElement );
var focusIsInSidebar = $.contains( sidebar, focusedElement );
-
+
if ( ! focusIsInToggle && ! focusIsInSidebar ) {
$( toggleButton ).trigger( 'click.wp-responsive' );
}
@@ -1762,8 +1743,9 @@ $( function() {
if ( ! $adminmenu.data('wp-responsive') ) {
return;
}
-
+ let state = ( 'false' === $( this ).attr( 'aria-expanded' ) ) ? 'true' : 'false';
$( this ).parent( 'li' ).toggleClass( 'selected' );
+ $( this ).attr( 'aria-expanded', state );
$( this ).trigger( 'focus' );
event.preventDefault();
});
@@ -1838,6 +1820,34 @@ $( function() {
},
/**
+ * Toggles the aria-haspopup attribute for the responsive admin menu.
+ *
+ * The aria-haspopup attribute is only necessary for the responsive menu.
+ * See ticket https://core.trac.wordpress.org/ticket/43095
+ *
+ * @since 6.6.0
+ *
+ * @param {string} action Whether to add or remove the aria-haspopup attribute.
+ *
+ * @return {void}
+ */
+ toggleAriaHasPopup: function( action ) {
+ var elements = $adminmenu.find( '[data-ariahaspopup]' );
+
+ if ( action === 'add' ) {
+ elements.each( function() {
+ $( this ).attr( 'aria-haspopup', 'menu' ).attr( 'aria-expanded', 'false' );
+ } );
+
+ return;
+ }
+
+ elements.each( function() {
+ $( this ).removeAttr( 'aria-haspopup' ).removeAttr( 'aria-expanded' );
+ } );
+ },
+
+ /**
* Sets the responsiveness and enables the overlay based on the viewport width.
*
* @since 3.8.0
@@ -2034,7 +2044,6 @@ $( function() {
window.wpResponsive.init();
setPinMenu();
setMenuState();
- currentMenuItemHasPopup();
makeNoticesDismissible();
aria_button_if_js();