@sofidevo/astro-dynamic-header 1.0.13 → 1.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/Header.astro +0 -31
- package/src/hamburger.ts +3 -91
- package/src/index.ts +1 -1
package/package.json
CHANGED
package/src/Header.astro
CHANGED
|
@@ -39,37 +39,6 @@ const {
|
|
|
39
39
|
} = Astro.props;
|
|
40
40
|
---
|
|
41
41
|
|
|
42
|
-
<script>
|
|
43
|
-
class HamburgerController {
|
|
44
|
-
private hamburgerBtn: HTMLElement | null;
|
|
45
|
-
private mobileMenu: HTMLElement | null;
|
|
46
|
-
|
|
47
|
-
constructor() {
|
|
48
|
-
this.hamburgerBtn = document.getElementById("hamburger-btn");
|
|
49
|
-
this.mobileMenu = document.getElementById("mobile-header-menu");
|
|
50
|
-
this.init();
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
private init(): void {
|
|
54
|
-
if (this.hamburgerBtn && this.mobileMenu) {
|
|
55
|
-
this.hamburgerBtn.addEventListener("click", () => this.toggleMenu());
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
private toggleMenu(): void {
|
|
60
|
-
if (this.hamburgerBtn && this.mobileMenu) {
|
|
61
|
-
this.hamburgerBtn.classList.toggle("is-active");
|
|
62
|
-
this.mobileMenu.classList.toggle("is-active");
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// Auto-initialize when DOM is ready
|
|
68
|
-
document.addEventListener('DOMContentLoaded', () => {
|
|
69
|
-
new HamburgerController();
|
|
70
|
-
});
|
|
71
|
-
</script>
|
|
72
|
-
|
|
73
42
|
<div class={`header__container header__container--${headerType}`} style={`z-index: ${zIndex};`}>
|
|
74
43
|
<header class={`header header--${headerType}`} style={`background-color: ${backgroundColor}; backdrop-filter: ${backdropBlur}; -webkit-backdrop-filter: ${backdropBlur};`}>
|
|
75
44
|
<a href={homeUrl}>
|
package/src/hamburger.ts
CHANGED
|
@@ -1,19 +1,5 @@
|
|
|
1
1
|
const d = document;
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Adds a global click event listener to handle hamburger menu button and menu link interactions.
|
|
6
|
-
*
|
|
7
|
-
* When the hamburger button or any of its child elements are clicked, toggles the "is-active" class
|
|
8
|
-
* on both the hamburger button and the associated panel. When a menu link is clicked, removes the
|
|
9
|
-
* "is-active" class from both the hamburger button and the panel.
|
|
10
|
-
*
|
|
11
|
-
* @param hamburgerBtn - A CSS selector string for the hamburger button element.
|
|
12
|
-
* @param panel - A CSS selector string for the panel element to show/hide.
|
|
13
|
-
* @param menuLink - A CSS selector string for the menu link elements that close the menu when clicked.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
|
|
17
3
|
export function HamburgerButton(hamburgerBtn: string, panel: string, menuLink: string): void {
|
|
18
4
|
d.addEventListener("click", (e: Event) => {
|
|
19
5
|
const target = e.target as Element;
|
|
@@ -32,80 +18,6 @@ export function HamburgerButton(hamburgerBtn: string, panel: string, menuLink: s
|
|
|
32
18
|
});
|
|
33
19
|
}
|
|
34
20
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
private mobileMenu: HTMLElement | null;
|
|
39
|
-
|
|
40
|
-
constructor() {
|
|
41
|
-
this.hamburgerBtn = document.getElementById("hamburger-btn");
|
|
42
|
-
this.mobileMenu = document.getElementById("mobile-header-menu");
|
|
43
|
-
this.init();
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
private init(): void {
|
|
47
|
-
if (this.hamburgerBtn && this.mobileMenu) {
|
|
48
|
-
this.hamburgerBtn.addEventListener("click", () => this.toggleMenu());
|
|
49
|
-
// Add event listener for closing menu when clicking on menu links
|
|
50
|
-
this.addMenuLinkListeners();
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
private addMenuLinkListeners(): void {
|
|
55
|
-
// Listen for clicks on the document to handle menu link clicks
|
|
56
|
-
document.addEventListener("click", (e: Event) => {
|
|
57
|
-
const target = e.target as Element;
|
|
58
|
-
|
|
59
|
-
// Check if the clicked element is within the mobile menu
|
|
60
|
-
const mobileMenu = document.getElementById("mobile-header-menu");
|
|
61
|
-
if (!mobileMenu || !mobileMenu.contains(target)) {
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// Check if the clicked element is a menu link (including all mobile menu variants)
|
|
66
|
-
const isMenuLink = target?.matches?.(".mobile-menu__link") ||
|
|
67
|
-
target?.matches?.(".menu__link.mobile-menu__link") ||
|
|
68
|
-
target?.closest?.(".mobile-menu__link") ||
|
|
69
|
-
target?.closest?.(".menu__link.mobile-menu__link");
|
|
70
|
-
|
|
71
|
-
if (isMenuLink) {
|
|
72
|
-
// Only close if it's an actual link with a valid href (not a summary/dropdown toggle)
|
|
73
|
-
const closestLink = target.closest("a");
|
|
74
|
-
if (closestLink && closestLink.href &&
|
|
75
|
-
closestLink.href !== "#" &&
|
|
76
|
-
!closestLink.href.endsWith("#")) {
|
|
77
|
-
this.closeMenu();
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
private toggleMenu(): void {
|
|
84
|
-
if (this.hamburgerBtn && this.mobileMenu) {
|
|
85
|
-
this.hamburgerBtn.classList.toggle("is-active");
|
|
86
|
-
this.mobileMenu.classList.toggle("is-active");
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
public openMenu(): void {
|
|
91
|
-
if (this.hamburgerBtn && this.mobileMenu) {
|
|
92
|
-
this.hamburgerBtn.classList.add("is-active");
|
|
93
|
-
this.mobileMenu.classList.add("is-active");
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
public closeMenu(): void {
|
|
98
|
-
if (this.hamburgerBtn && this.mobileMenu) {
|
|
99
|
-
this.hamburgerBtn.classList.remove("is-active");
|
|
100
|
-
this.mobileMenu.classList.remove("is-active");
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// Auto-initialize when DOM is ready (using only the class-based approach)
|
|
106
|
-
if (typeof document !== 'undefined') {
|
|
107
|
-
document.addEventListener('DOMContentLoaded', () => {
|
|
108
|
-
// Initialize only the class-based controller to avoid conflicts
|
|
109
|
-
new HamburgerButtonController();
|
|
110
|
-
});
|
|
111
|
-
}
|
|
21
|
+
d.addEventListener("DOMContentLoaded", () => {
|
|
22
|
+
HamburgerButton(".hamburger", ".mobile-header__menu", ".mobile-menu__link, .menu__link.mobile-menu__link");
|
|
23
|
+
});
|
package/src/index.ts
CHANGED