fix(site): close mobile menu when a nav item is tapped
The CSS-only hamburger (checkbox toggle) only reset on astro:page-load, which fires for real navigations (Blog) but not for same-page anchor links (How it works / Open & yours / Join the waitlist) that scroll without navigating — so the menu stayed open after tapping them. Add a one-time delegated click handler on the nav that unchecks the toggle on any link tap. Items stay identical to desktop; scrolling unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
37a1df2315
commit
c8c09cc3d7
1 changed files with 18 additions and 6 deletions
|
|
@ -155,11 +155,23 @@ const isActive = (href: string, external?: boolean) =>
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
// ClientRouter persists the header (transition:persist), so reset the mobile
|
// ClientRouter persists the header (transition:persist). Two jobs:
|
||||||
// menu toggle after every navigation to avoid it staying open.
|
// 1. Reset the mobile menu toggle after every navigation (was already here).
|
||||||
function resetNav() {
|
// 2. Close the menu when ANY nav link is tapped — including same-page anchor
|
||||||
const t = document.getElementById('nav-toggle') as HTMLInputElement | null;
|
// links (How it works / Open & yours / Join the waitlist) that scroll but
|
||||||
if (t) t.checked = false;
|
// don't navigate, so astro:page-load never fires for them and the CSS-only
|
||||||
|
// checkbox would otherwise stay checked (menu stuck open).
|
||||||
|
function initMobileNav() {
|
||||||
|
const toggle = document.getElementById('nav-toggle') as HTMLInputElement | null;
|
||||||
|
if (!toggle) return;
|
||||||
|
toggle.checked = false;
|
||||||
|
|
||||||
|
const nav = document.querySelector<HTMLElement>('.nav');
|
||||||
|
if (!nav || nav.dataset.bound === '1') return; // header persists — bind once
|
||||||
|
nav.dataset.bound = '1';
|
||||||
|
nav.addEventListener('click', (e) => {
|
||||||
|
if ((e.target as HTMLElement).closest('a')) toggle.checked = false;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
document.addEventListener('astro:page-load', resetNav);
|
document.addEventListener('astro:page-load', initMobileNav);
|
||||||
</script>
|
</script>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue