Skip to content

Commit 49b1cd1

Browse files
authored
feat: hide navbar on mobile when heading links clicked (#584)
1 parent b2e6c30 commit 49b1cd1

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

Diff for: assets/js/menu.js

+25-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ document.addEventListener('DOMContentLoaded', function () {
2323
document.body.classList.toggle('md:hx-overflow-auto');
2424
}
2525

26+
function hideOverlay() {
27+
// Hide the overlay
28+
overlay.classList.remove(...overlayClasses);
29+
overlay.classList.add('hx-bg-transparent');
30+
}
31+
2632
menu.addEventListener('click', (e) => {
2733
e.preventDefault();
2834
toggleMenu();
@@ -33,8 +39,7 @@ document.addEventListener('DOMContentLoaded', function () {
3339
overlay.classList.remove('hx-bg-transparent');
3440
} else {
3541
// Hide the overlay
36-
overlay.classList.remove(...overlayClasses);
37-
overlay.classList.add('hx-bg-transparent');
42+
hideOverlay();
3843
}
3944
});
4045

@@ -43,7 +48,23 @@ document.addEventListener('DOMContentLoaded', function () {
4348
toggleMenu();
4449

4550
// Hide the overlay
46-
overlay.classList.remove(...overlayClasses);
47-
overlay.classList.add('hx-bg-transparent');
51+
hideOverlay();
52+
});
53+
54+
// Select all anchor tags in the sidebar container
55+
const sidebarLinks = sidebarContainer.querySelectorAll('a');
56+
57+
// Add click event listener to each anchor tag
58+
sidebarLinks.forEach(link => {
59+
link.addEventListener('click', (e) => {
60+
// Check if the href attribute contains a hash symbol (links to a heading)
61+
if (link.getAttribute('href') && link.getAttribute('href').startsWith('#')) {
62+
// Only dismiss overlay on mobile view
63+
if (window.innerWidth < 768) {
64+
toggleMenu();
65+
hideOverlay();
66+
}
67+
}
68+
});
4869
});
4970
});

0 commit comments

Comments
 (0)