Product sub-category menus with keyboard focus and ARIA controls

Product Sub-category Menus with Keyboard Focus and ARIA Controls #

Description:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Accessible Product Sub-Category Menu</title>
    <style>
        /* Basic styling for menu and submenu */
        .menu, .submenu {
            list-style: none;
            padding: 0;
            margin: 0;
        }
        .menu > li {
            display: inline-block;
            position: relative;
            margin-right: 20px;
        }
        /* Submenu styling */
        .submenu {
            position: absolute;
            top: 100%;
            left: 0;
            background-color: #fff;
            border: 1px solid #ddd;
            padding: 10px;
            width: 200px;
            display: none;
        }
        /* Show submenu when expanded */
        .submenu[aria-hidden="false"] {
            display: block;
        }
        /* Focus indicator */
        .menu-item:focus, .submenu-item:focus {
            outline: 2px solid #005fcc;
            background-color: #e0e0e0;
        }
    </style>
</head>
<body>
    <!-- Main Navigation -->
    <nav aria-label="Product Categories">
        <ul class="menu" role="menubar">
            <!-- Category: Men's Clothing -->
            <li role="none">
                <button class="menu-item" aria-haspopup="true" aria-expanded="false" aria-controls="mens-clothing-submenu" id="mens-clothing">
                    Men's Clothing
                </button>
                <ul class="submenu" id="mens-clothing-submenu" role="menu" aria-labelledby="mens-clothing" aria-hidden="true">
                    <li role="none"><a href="#" role="menuitem" class="submenu-item">T-Shirts</a></li>
                    <li role="none"><a href="#" role="menuitem" class="submenu-item">Jeans</a></li>
                    <li role="none"><a href="#" role="menuitem" class="submenu-item">Jackets</a></li>
                </ul>
            </li>
            <!-- Category: Women's Clothing -->
            <li role="none">
                <button class="menu-item" aria-haspopup="true" aria-expanded="false" aria-controls="womens-clothing-submenu" id="womens-clothing">
                    Women's Clothing
                </button>
                <ul class="submenu" id="womens-clothing-submenu" role="menu" aria-labelledby="womens-clothing" aria-hidden="true">
                    <li role="none"><a href="#" role="menuitem" class="submenu-item">Dresses</a></li>
                    <li role="none"><a href="#" role="menuitem" class="submenu-item">Blouses</a></li>
                    <li role="none"><a href="#" role="menuitem" class="submenu-item">Skirts</a></li>
                </ul>
            </li>
            <!-- Category: Kids' Clothing -->
            <li role="none">
                <button class="menu-item" aria-haspopup="true" aria-expanded="false" aria-controls="kids-clothing-submenu" id="kids-clothing">
                    Kids' Clothing
                </button>
                <ul class="submenu" id="kids-clothing-submenu" role="menu" aria-labelledby="kids-clothing" aria-hidden="true">
                    <li role="none"><a href="#" role="menuitem" class="submenu-item">T-Shirts</a></li>
                    <li role="none"><a href="#" role="menuitem" class="submenu-item">Pajamas</a></li>
                    <li role="none"><a href="#" role="menuitem" class="submenu-item">Shorts</a></li>
                </ul>
            </li>
        </ul>
    </nav>
    <script>
        // Toggle submenu visibility and update accessibility attributes
        document.querySelectorAll('.menu-item').forEach(button => {
            button.addEventListener('click', () => {
                const expanded = button.getAttribute('aria-expanded') === 'true';
                button.setAttribute('aria-expanded', !expanded);
                document.getElementById(button.getAttribute('aria-controls')).setAttribute('aria-hidden', expanded);
            });
            button.addEventListener('keydown', (e) => {
                // Open submenu with Enter or Space keys
                if (e.key === 'Enter' || e.key === ' ') {
                    e.preventDefault();
                    const expanded = button.getAttribute('aria-expanded') === 'true';
                    button.setAttribute('aria-expanded', !expanded);
                    document.getElementById(button.getAttribute('aria-controls')).setAttribute('aria-hidden', expanded);
                }
                // Move focus to the first item in the submenu with Arrow Down
                if (e.key === 'ArrowDown') {
                    e.preventDefault();
                    const submenu = document.getElementById(button.getAttribute('aria-controls'));
                    if (submenu) {
                        const firstSubmenuItem = submenu.querySelector('[role="menuitem"]');
                        if (firstSubmenuItem) {
                            firstSubmenuItem.focus();
                        }
                    }
                }
            });
        });
        // Close submenu on Escape key and return focus to the menu button
        document.querySelectorAll('.submenu-item').forEach(item => {
            item.addEventListener('keydown', (e) => {
                if (e.key === 'Escape') {
                    const menuButton = document.getElementById(item.closest('.submenu').getAttribute('aria-labelledby'));
                    menuButton.focus();
                    menuButton.setAttribute('aria-expanded', 'false');
                    item.closest('.submenu').setAttribute('aria-hidden', 'true');
                }
            });
        });
    </script>
</body>
</html>

Explanation #

  1. ARIA Roles and Attributes:
    • role="menubar" on the main menu and role="menu" on each submenu provide structure for screen readers.
    • aria-haspopup="true" on each main category button indicates that it has a submenu.
    • aria-expanded toggles between true and false based on the submenu’s open/closed state, keeping screen readers informed.
    • aria-controls links each button with its corresponding submenu, while aria-labelledby and aria-hidden clarify which submenu is open or hidden.
  2. Keyboard Navigation:
    • Enter or Space opens the submenu for the selected category.
    • Arrow Down moves focus to the first item in the submenu, making it easier to access sub-category links.
    • Escape closes the submenu and returns focus to the main category button, creating a predictable navigation experience.
  3. Focus Management:
    • When a submenu opens, focus moves to the first submenu item for smoother keyboard navigation.
    • When the submenu closes, focus returns to the main category button, ensuring that users don’t lose their place in the navigation structure.
  4. CSS Styling for Visibility:
    • Submenus are hidden by default and only displayed when aria-hidden="false".
    • Clear focus and hover styles highlight active elements, providing visual feedback for keyboard users.

Key Takeaways #

  • Use ARIA Attributes to Enhance Accessibility: Implement aria-haspopup, aria-expanded, aria-controls, and aria-labelledby to ensure screen readers can understand the menu structure and state.
  • Enable Keyboard Navigation: Provide intuitive keyboard interactions like Enter/Space to open submenus and Escape to close them.
  • Manage Focus for Smooth Navigation: Move focus into the submenu when opened and return it to the category button when closed, ensuring logical focus flow.


Screen reader: NVDA
Video:

What are your feelings

Updated on March 6, 2025