/** * Script for keyboard navigation improvements * * Enhances keyboard accessibility for dropdown menus and other interactive elements. */ (function() { // Get all dropdown menu items const dropdowns = document.querySelectorAll('.menu-item-has-children, .page_item_has_children'); // Add keyboard support for dropdown menus dropdowns.forEach(function(dropdown) { const link = dropdown.querySelector('a'); const subMenu = dropdown.querySelector('.sub-menu'); if (link && subMenu) { // Add ARIA attributes link.setAttribute('aria-haspopup', 'true'); link.setAttribute('aria-expanded', 'false'); // Handle focus events link.addEventListener('focus', function() { // Close other open dropdowns dropdowns.forEach(function(otherDropdown) { if (otherDropdown !== dropdown) { otherDropdown.classList.remove('focus'); const otherLink = otherDropdown.querySelector('a'); if (otherLink) { otherLink.setAttribute('aria-expanded', 'false'); } } }); // Open this dropdown dropdown.classList.add('focus'); link.setAttribute('aria-expanded', 'true'); }); // Handle keyboard events link.addEventListener('keydown', function(e) { if (e.key === 'ArrowDown') { e.preventDefault(); const firstSubItem = subMenu.querySelector('a'); if (firstSubItem) { firstSubItem.focus(); } } if (e.key === 'Escape') { dropdown.classList.remove('focus'); link.setAttribute('aria-expanded', 'false'); link.focus(); } }); // Handle submenu items const subItems = subMenu.querySelectorAll('a'); subItems.forEach(function(subItem, index) { subItem.addEventListener('keydown', function(e) { if (e.key === 'ArrowUp') { e.preventDefault(); if (index > 0) { subItems[index - 1].focus(); } else { link.focus(); } } if (e.key === 'ArrowDown') { e.preventDefault(); if (index < subItems.length - 1) { subItems[index + 1].focus(); } } if (e.key === 'Escape') { dropdown.classList.remove('focus'); link.setAttribute('aria-expanded', 'false'); link.focus(); } }); }); } }); // Handle Escape key to close all dropdowns document.addEventListener('keydown', function(e) { if (e.key === 'Escape') { dropdowns.forEach(function(dropdown) { dropdown.classList.remove('focus'); const link = dropdown.querySelector('a'); if (link) { link.setAttribute('aria-expanded', 'false'); } }); } }); // Close dropdowns when clicking outside document.addEventListener('click', function(e) { if (!e.target.closest('.menu-item-has-children, .page_item_has_children')) { dropdowns.forEach(function(dropdown) { dropdown.classList.remove('focus'); const link = dropdown.querySelector('a'); if (link) { link.setAttribute('aria-expanded', 'false'); } }); } }); })();