-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: scroll bug due to incorrect toggle state (#186)
* fix: add mobile menu overlay to toggle body scroll * chore: update styles
- Loading branch information
Showing
4 changed files
with
68 additions
and
12 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
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,19 +1,49 @@ | ||
// Hamburger menu for mobile navigation | ||
|
||
const menu = document.querySelector('.hamburger-menu'); | ||
|
||
menu.addEventListener('click', (e) => { | ||
e.preventDefault(); | ||
document.addEventListener('DOMContentLoaded', function () { | ||
const menu = document.querySelector('.hamburger-menu'); | ||
const overlay = document.querySelector('.mobile-menu-overlay'); | ||
const sidebarContainer = document.querySelector('.sidebar-container'); | ||
|
||
// Toggle the hamburger menu | ||
menu.querySelector('svg').classList.toggle('open'); | ||
// Initialize the overlay | ||
const overlayClasses = ['fixed', 'inset-0', 'z-10', 'bg-black/80', 'dark:bg-black/60']; | ||
overlay.classList.add('bg-transparent'); | ||
overlay.classList.remove("hidden", ...overlayClasses); | ||
|
||
function toggleMenu() { | ||
// Toggle the hamburger menu | ||
menu.querySelector('svg').classList.toggle('open'); | ||
|
||
// When the menu is open, we want to show the navigation sidebar | ||
sidebarContainer.classList.toggle('max-md:[transform:translate3d(0,-100%,0)]'); | ||
sidebarContainer.classList.toggle('max-md:[transform:translate3d(0,0,0)]'); | ||
|
||
// When the menu is open, we want to prevent the body from scrolling | ||
document.body.classList.toggle('overflow-hidden'); | ||
document.body.classList.toggle('md:overflow-auto'); | ||
} | ||
|
||
menu.addEventListener('click', (e) => { | ||
e.preventDefault(); | ||
toggleMenu(); | ||
|
||
if (overlay.classList.contains('bg-transparent')) { | ||
// Show the overlay | ||
overlay.classList.add(...overlayClasses); | ||
overlay.classList.remove('bg-transparent'); | ||
} else { | ||
// Hide the overlay | ||
overlay.classList.remove(...overlayClasses); | ||
overlay.classList.add('bg-transparent'); | ||
} | ||
}); | ||
|
||
// When the menu is open, we want to show the navigation sidebar | ||
sidebarContainer.classList.toggle('max-md:[transform:translate3d(0,-100%,0)]'); | ||
sidebarContainer.classList.toggle('max-md:[transform:translate3d(0,0,0)]'); | ||
overlay.addEventListener('click', (e) => { | ||
e.preventDefault(); | ||
toggleMenu(); | ||
|
||
// When the menu is open, we want to prevent the body from scrolling | ||
document.body.classList.toggle('overflow-hidden'); | ||
document.body.classList.toggle('md:overflow-auto'); | ||
// Hide the overlay | ||
overlay.classList.remove(...overlayClasses); | ||
overlay.classList.add('bg-transparent'); | ||
}); | ||
}); |
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