fix: close burger menu not closing when clicking outside the nav or on a link

This commit is contained in:
Corentin 2024-05-23 00:30:56 +02:00
parent a3b10fd52e
commit 1c8a92bddf
1 changed files with 14 additions and 0 deletions

View File

@ -4,6 +4,8 @@ class NavBar extends HTMLElement {
connectedCallback() { connectedCallback() {
document.addEventListener("scroll", this.onScroll.bind(this)) document.addEventListener("scroll", this.onScroll.bind(this))
this.addEventListener('transitionend', this.onTransitionEnd.bind(this)) this.addEventListener('transitionend', this.onTransitionEnd.bind(this))
this.addEventListener('click', this.onClick.bind(this))
document.addEventListener('click', this.onDocumentClick.bind(this))
this.onScroll() this.onScroll()
} }
@ -25,6 +27,18 @@ class NavBar extends HTMLElement {
this.classList.remove("nav-bar__docking") this.classList.remove("nav-bar__docking")
} }
} }
onClick(evt) {
if(evt.target.matches('a')) {
this.classList.remove('nav-bar__open')
}
}
onDocumentClick(evt) {
if(!this.contains(evt.target)) {
this.classList.remove('nav-bar__open')
}
}
} }
class ToggleNavBarButton extends HTMLElement { class ToggleNavBarButton extends HTMLElement {