-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(header): add mobile menu functionality
Implemented a toggle function for the mobile menu, allowing it to open and close with button clicks. Added event listeners to close the menu when clicking outside of it or pressing the Escape key. Updated header layout to include a mobile menu button and icons for open/closed states.
- Loading branch information
Showing
3 changed files
with
88 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,42 @@ | ||
console.log('This site was generated by Hugo.'); | ||
|
||
document.addEventListener('DOMContentLoaded', () => { | ||
// Mobile menu functionality | ||
const mobileMenuButton = document.getElementById('mobile-menu-button'); | ||
if (mobileMenuButton) { | ||
mobileMenuButton.addEventListener('click', toggleMobileMenu); | ||
} | ||
|
||
function toggleMobileMenu() { | ||
const menu = document.getElementById('mobile-menu'); | ||
const button = document.getElementById('mobile-menu-button'); | ||
const closedIcon = document.getElementById('menu-closed-icon'); | ||
const openIcon = document.getElementById('menu-open-icon'); | ||
|
||
const isExpanded = button.getAttribute('aria-expanded') === 'true'; | ||
|
||
button.setAttribute('aria-expanded', !isExpanded); | ||
menu.classList.toggle('hidden'); | ||
closedIcon.classList.toggle('hidden'); | ||
openIcon.classList.toggle('block'); | ||
openIcon.classList.toggle('hidden'); | ||
closedIcon.classList.toggle('block'); | ||
} | ||
|
||
// Close mobile menu when clicking outside | ||
document.addEventListener('click', (event) => { | ||
const menu = document.getElementById('mobile-menu'); | ||
const button = document.getElementById('mobile-menu-button'); | ||
|
||
if (!menu.contains(event.target) && !button.contains(event.target) && !menu.classList.contains('hidden')) { | ||
toggleMobileMenu(); | ||
} | ||
}); | ||
|
||
// Close mobile menu when pressing Escape key | ||
document.addEventListener('keydown', (event) => { | ||
if (event.key === 'Escape' && !document.getElementById('mobile-menu').classList.contains('hidden')) { | ||
toggleMobileMenu(); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters