Make WordPress Core

Changeset 59916


Ignore:
Timestamp:
03/03/2025 09:05:20 PM (3 months ago)
Author:
joedolson
Message:

Bundled Themes: Twenty Fifteen: Fix ARIA expanded in primary menu.

Set aria-expanded to true when automatically expanding active child menus in the primary menu. Add aria-controls to explicitly define controlled content in menus.

Props bschneidewind, joedolson, dhruvang21, pooja1210, shailu25.
Fixes #62936.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-content/themes/twentyfifteen/js/functions.js

    r53247 r59916  
    1515
    1616        // Toggle buttons and submenu items with active children menu items.
    17         container.find( '.current-menu-ancestor > button' ).addClass( 'toggle-on' );
     17        container.find( '.current-menu-ancestor > button' ).addClass( 'toggle-on' ).attr( 'aria-expanded', 'true' );
    1818        container.find( '.current-menu-ancestor > .sub-menu' ).addClass( 'toggled-on' );
    1919
     
    2323            _this.toggleClass( 'toggle-on' );
    2424            _this.next( '.children, .sub-menu' ).toggleClass( 'toggled-on' );
    25             _this.attr( 'aria-expanded', _this.attr( 'aria-expanded' ) === 'false' ? 'true' : 'false' );
     25            _this.attr( 'aria-expanded', _this.hasClass( 'toggle-on' ) ? 'true' : 'false' );
    2626            _this.html( _this.html() === screenReaderText.expand ? screenReaderText.collapse : screenReaderText.expand );
    2727        } );
    2828    }
    2929    initMainNavigation( $( '.main-navigation' ) );
     30
     31    // Add unique ID to each .sub-menu and aria-controls to parent links
     32    function addUniqueIDToSubMenus() {
     33        var subMenus = document.querySelectorAll( '.main-navigation .sub-menu' );
     34        subMenus.forEach( function( subMenu, index ) {
     35            var parentLi = subMenu.closest( 'li.menu-item-has-children' );
     36            subMenu.id = 'sub-menu-' + (index + 1);
     37            if ( parentLi ) {
     38                var parentLink = parentLi.querySelector( 'button' );
     39                if ( parentLink ) {
     40                    parentLink.setAttribute( 'aria-controls', subMenu.id );
     41                }
     42            }
     43        } );
     44    }
     45
     46    addUniqueIDToSubMenus();
    3047
    3148    // Re-initialize the main navigation when it is updated, persisting any existing submenu expanded states.
Note: See TracChangeset for help on using the changeset viewer.