Skip to content

Commit

Permalink
fix(presentation): update Navbar mount script on after swap
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmet-cetinkaya authored Jan 1, 2025
1 parent 404e375 commit c0b02cc
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/presentation/src/shared/layouts/components/Navbar.astro
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ const navbarItems = [
<script>
import "~/core/acore-ts/ui/components/Accordion";

// Wait for custom element to be defined
document.addEventListener("DOMContentLoaded", onMount);
document.addEventListener("astro:after-swap", onMount);

function onMount() {
customElements.whenDefined("ac-accordion").then(() => {
const menuButton = document.getElementById("mobile-menu-button");
const mobileMenu = document.querySelector("ac-accordion.mobile-menu");
Expand All @@ -157,16 +160,14 @@ const navbarItems = [

if (menuButton && mobileMenu) {
menuButton.addEventListener("click", () => {
// Explicitly cast to access the toggle method
(mobileMenu as any).toggle();
menuOpenIcon?.classList.toggle("hidden");
menuCloseIcon?.classList.toggle("hidden");
});
}

// Handle responsive behavior
const mediaQuery = window.matchMedia("(min-width: 768px)");
function handleResize(e: MediaQueryListEvent | MediaQueryList) {
function handleResize(e) {
if (e.matches) {
(mobileMenu as any)?.open();
} else {
Expand All @@ -177,6 +178,7 @@ const navbarItems = [
mediaQuery.addListener(handleResize);
handleResize(mediaQuery);
});
}
</script>

<style>
Expand Down

0 comments on commit c0b02cc

Please sign in to comment.