feat: hide navbar on mobile when heading links clicked (#584)

This commit is contained in:
Xin 2025-02-25 18:48:25 +00:00 committed by GitHub
parent b2e6c30c7f
commit 49b1cd11ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -23,6 +23,12 @@ document.addEventListener('DOMContentLoaded', function () {
document.body.classList.toggle('md:hx-overflow-auto');
}
function hideOverlay() {
// Hide the overlay
overlay.classList.remove(...overlayClasses);
overlay.classList.add('hx-bg-transparent');
}
menu.addEventListener('click', (e) => {
e.preventDefault();
toggleMenu();
@ -33,8 +39,7 @@ document.addEventListener('DOMContentLoaded', function () {
overlay.classList.remove('hx-bg-transparent');
} else {
// Hide the overlay
overlay.classList.remove(...overlayClasses);
overlay.classList.add('hx-bg-transparent');
hideOverlay();
}
});
@ -43,7 +48,23 @@ document.addEventListener('DOMContentLoaded', function () {
toggleMenu();
// Hide the overlay
overlay.classList.remove(...overlayClasses);
overlay.classList.add('hx-bg-transparent');
hideOverlay();
});
// Select all anchor tags in the sidebar container
const sidebarLinks = sidebarContainer.querySelectorAll('a');
// Add click event listener to each anchor tag
sidebarLinks.forEach(link => {
link.addEventListener('click', (e) => {
// Check if the href attribute contains a hash symbol (links to a heading)
if (link.getAttribute('href') && link.getAttribute('href').startsWith('#')) {
// Only dismiss overlay on mobile view
if (window.innerWidth < 768) {
toggleMenu();
hideOverlay();
}
}
});
});
});