document.addEventListener('click', function (event) { const link = event.target.closest('#astroid-mobilemenu a[href]'); if (!link) return; const url = new URL(link.href, window.location.href); if ( url.origin !== window.location.origin || url.pathname !== window.location.pathname || url.search !== window.location.search || !url.hash ) { return; } const id = decodeURIComponent(url.hash.substring(1)); const target = document.getElementById(id); const menu = document.getElementById('astroid-mobilemenu'); if (!target || !menu || !window.bootstrap?.Offcanvas) return; event.preventDefault(); const offcanvas = bootstrap.Offcanvas.getOrCreateInstance(menu); menu.addEventListener('hidden.bs.offcanvas', function () { history.pushState(null, '', url.hash); target.scrollIntoView({ behavior: 'auto', block: 'start' }); }, { once: true }); offcanvas.hide(); });