|
1 | 1 | /** |
2 | 2 | * Mobile Navigation Menu |
3 | | - * Handles touch/click events for mobile devices where hover doesn't work properly |
| 3 | + * Enhanced mobile navigation with improved touch interactions and accessibility |
4 | 4 | */ |
5 | 5 | (function () { |
6 | 6 | 'use strict'; |
|
9 | 9 | // Find the mobile dropdown toggle |
10 | 10 | const mobileDropdown = document.querySelector('.resp-nav-dropdown'); |
11 | 11 | const dropdownContent = document.querySelector('.resp-dropdown-content'); |
| 12 | + const menuButton = mobileDropdown?.querySelector('p'); |
12 | 13 |
|
13 | 14 | if (!mobileDropdown || !dropdownContent) { |
14 | 15 | return; |
15 | 16 | } |
16 | 17 |
|
17 | | - // Add click event listener to toggle mobile menu |
18 | | - mobileDropdown.addEventListener('click', (e) => { |
| 18 | + // Add ARIA attributes for accessibility |
| 19 | + mobileDropdown.setAttribute('role', 'button'); |
| 20 | + mobileDropdown.setAttribute('aria-expanded', 'false'); |
| 21 | + mobileDropdown.setAttribute('aria-haspopup', 'true'); |
| 22 | + mobileDropdown.setAttribute('tabindex', '0'); |
| 23 | + dropdownContent.setAttribute('role', 'menu'); |
| 24 | + |
| 25 | + // Update menu button text and ARIA state |
| 26 | + function updateMenuState(isOpen) { |
| 27 | + const isExpanded = isOpen ? 'true' : 'false'; |
| 28 | + mobileDropdown.setAttribute('aria-expanded', isExpanded); |
| 29 | + |
| 30 | + if (menuButton) { |
| 31 | + menuButton.textContent = isOpen ? 'CLOSE' : 'MENU'; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // Toggle mobile menu |
| 36 | + function toggleMenu(e) { |
19 | 37 | e.preventDefault(); |
20 | 38 | e.stopPropagation(); |
21 | 39 |
|
22 | | - // Toggle the show class |
23 | | - dropdownContent.classList.toggle('show'); |
| 40 | + const isOpen = dropdownContent.classList.toggle('show'); |
| 41 | + updateMenuState(isOpen); |
| 42 | + |
| 43 | + // Add/remove body scroll lock on mobile |
| 44 | + if (isOpen) { |
| 45 | + document.body.style.overflow = 'hidden'; |
| 46 | + } else { |
| 47 | + document.body.style.overflow = ''; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // Close menu |
| 52 | + function closeMenu() { |
| 53 | + dropdownContent.classList.remove('show'); |
| 54 | + updateMenuState(false); |
| 55 | + document.body.style.overflow = ''; |
| 56 | + } |
| 57 | + |
| 58 | + // Enhanced event listeners |
| 59 | + mobileDropdown.addEventListener('click', toggleMenu); |
| 60 | + mobileDropdown.addEventListener('touchstart', (e) => { |
| 61 | + // Prevent double-tap zoom on iOS |
| 62 | + e.preventDefault(); |
| 63 | + }); |
| 64 | + |
| 65 | + // Keyboard support |
| 66 | + mobileDropdown.addEventListener('keydown', (e) => { |
| 67 | + if (e.key === 'Enter' || e.key === ' ') { |
| 68 | + toggleMenu(e); |
| 69 | + } |
24 | 70 | }); |
25 | 71 |
|
26 | 72 | // Close menu when clicking outside |
27 | 73 | document.addEventListener('click', (e) => { |
28 | | - if (!mobileDropdown.contains(e.target)) { |
29 | | - dropdownContent.classList.remove('show'); |
| 74 | + if (!mobileDropdown.contains(e.target) && !dropdownContent.contains(e.target)) { |
| 75 | + closeMenu(); |
30 | 76 | } |
31 | 77 | }); |
32 | 78 |
|
33 | 79 | // Close menu when pressing escape key |
34 | 80 | document.addEventListener('keydown', (e) => { |
35 | 81 | if (e.key === 'Escape') { |
36 | | - dropdownContent.classList.remove('show'); |
| 82 | + closeMenu(); |
37 | 83 | } |
38 | 84 | }); |
39 | 85 |
|
40 | 86 | // Close menu when window is resized to desktop size |
41 | 87 | window.addEventListener('resize', () => { |
42 | 88 | if (window.innerWidth > 650) { |
43 | | - dropdownContent.classList.remove('show'); |
| 89 | + closeMenu(); |
44 | 90 | } |
45 | 91 | }); |
| 92 | + |
| 93 | + // Enhanced touch handling for mobile menu items |
| 94 | + const menuItems = dropdownContent.querySelectorAll('.nav-item, .lang'); |
| 95 | + menuItems.forEach(item => { |
| 96 | + item.addEventListener('touchstart', function() { |
| 97 | + this.style.backgroundColor = 'rgba(255,255,255,0.1)'; |
| 98 | + }); |
| 99 | + |
| 100 | + item.addEventListener('touchend', function() { |
| 101 | + setTimeout(() => { |
| 102 | + this.style.backgroundColor = ''; |
| 103 | + }, 150); |
| 104 | + }); |
| 105 | + }); |
46 | 106 | } |
47 | 107 |
|
48 | 108 | // Initialize when DOM is ready |
|
0 commit comments