website/public/assets/js/snippets/HTMLNavMenuElement.mjs
vlw 7a8fc36ec0 feat: new website design and update to Vegvisir 3.1 (#2)
Brand new design for the website following the new design language I used for [version 2.0 of my personal website](https://codeberg.org/vlw/vlw.se/releases/tag/2.0.0).

Reviewed-on: https://codeberg.org/vegvisir/website/pulls/2
Co-authored-by: vlw <victor@vlw.se>
Co-committed-by: vlw <victor@vlw.se>
2025-03-05 11:16:54 +00:00

36 lines
No EOL
871 B
JavaScript

import { CustomElement } from "../CustomElement.mjs";
export const TAG_NAME = "nav-menu";
class HTMLNavMenuElement extends CustomElement {
constructor() {
super();
this.importElementStylesheet(this.constructor.name);
}
open() {
document.body.classList.add("menu-open");
this.classList.add("active");
}
close() {
this.classList.remove("active");
// Remove classname from body tag if this was the last menu that was open
if (!document.querySelector(TAG_NAME + ".active")) {
document.body.classList.remove("menu-open");
}
}
toggle() {
this.classList.contains("active") ? this.close() : this.open();
}
connected() {
// Close menu when a link is clicked
[...document.querySelectorAll("a")].forEach(element => element.addEventListener("click", () => this.close()));
}
}
globalThis.customElements.define(TAG_NAME, HTMLNavMenuElement);