Make WordPress Core

Changeset 58449


Ignore:
Timestamp:
06/20/2024 07:39:50 PM (2 years ago)
Author:
joedolson
Message:

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.

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/js/_enqueues/admin/common.js

    r58375 r58449  
    904904
    905905        /**
    906          * Handles the `aria-haspopup` attribute on the current menu item when it has a submenu.
    907          *
    908          * @since 4.4.0
    909          *
    910          * @return {void}
    911          */
    912         function currentMenuItemHasPopup() {
    913                 var $current = $( 'a.wp-has-current-submenu' );
    914 
    915                 if ( 'folded' === menuState ) {
    916                         // When folded or auto-folded and not responsive view, the current menu item does have a fly-out sub-menu.
    917                         $current.attr( 'aria-haspopup', 'true' );
    918                 } else {
    919                         // When expanded or in responsive view, reset aria-haspopup.
    920                         $current.attr( 'aria-haspopup', 'false' );
    921                 }
    922         }
    923 
    924         $document.on( 'wp-menu-state-set wp-collapse-menu wp-responsive-activate wp-responsive-deactivate', currentMenuItemHasPopup );
    925 
    926         /**
    927906         * Ensures an admin submenu is within the visual viewport.
    928907         *
     
    16961675                        $document.on( 'wp-responsive-activate.wp-responsive', function() {
    16971676                                self.activate();
     1677                                self.toggleAriaHasPopup( 'add' );
    16981678                        }).on( 'wp-responsive-deactivate.wp-responsive', function() {
    16991679                                self.deactivate();
     1680                                self.toggleAriaHasPopup( 'remove' );
    17001681                        });
    17011682
     
    17491730                                                        var focusIsInToggle  = $.contains( toggleButton, focusedElement );
    17501731                                                        var focusIsInSidebar = $.contains( sidebar, focusedElement );
    1751                                                        
     1732
    17521733                                                        if ( ! focusIsInToggle && ! focusIsInSidebar ) {
    17531734                                                                $( toggleButton ).trigger( 'click.wp-responsive' );
     
    17631744                                        return;
    17641745                                }
    1765 
     1746                                let state = ( 'false' === $( this ).attr( 'aria-expanded' ) ) ? 'true' : 'false';
    17661747                                $( this ).parent( 'li' ).toggleClass( 'selected' );
     1748                                $( this ).attr( 'aria-expanded', state );
    17671749                                $( this ).trigger( 'focus' );
    17681750                                event.preventDefault();
     
    18361818
    18371819                        this.maybeDisableSortables();
     1820                },
     1821
     1822                /**
     1823                 * Toggles the aria-haspopup attribute for the responsive admin menu.
     1824                 *
     1825                 * The aria-haspopup attribute is only necessary for the responsive menu.
     1826                 * See ticket https://core.trac.wordpress.org/ticket/43095
     1827                 *
     1828                 * @since 6.6.0
     1829                 *
     1830                 * @param {string} action Whether to add or remove the aria-haspopup attribute.
     1831                 *
     1832                 * @return {void}
     1833                 */
     1834                toggleAriaHasPopup: function( action ) {
     1835                        var elements = $adminmenu.find( '[data-ariahaspopup]' );
     1836
     1837                        if ( action === 'add' ) {
     1838                                elements.each( function() {
     1839                                        $( this ).attr( 'aria-haspopup', 'menu' ).attr( 'aria-expanded', 'false' );
     1840                                } );
     1841
     1842                                return;
     1843                        }
     1844
     1845                        elements.each( function() {
     1846                                $( this ).removeAttr( 'aria-haspopup' ).removeAttr( 'aria-expanded' );
     1847                        } );
    18381848                },
    18391849
     
    20352045        setPinMenu();
    20362046        setMenuState();
    2037         currentMenuItemHasPopup();
    20382047        makeNoticesDismissible();
    20392048        aria_button_if_js();
  • trunk/src/wp-admin/menu-header.php

    r56175 r58449  
    9999                        $class[] = 'wp-not-current-submenu';
    100100                        if ( ! empty( $submenu_items ) ) {
    101                                 $aria_attributes .= 'aria-haspopup="true"';
     101                                $aria_attributes .= 'data-ariahaspopup';
    102102                        }
    103103                }
Note: See TracChangeset for help on using the changeset viewer.