Skip to content

Commit 19e3549

Browse files
committed
prevent duplicate event listeners
1 parent 1e3b9ee commit 19e3549

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

docs/src/components/CustomHeader.astro

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
---
2-
import type { Props } from '@astrojs/starlight/props';
32
import Default from '@astrojs/starlight/components/SocialIcons.astro';
43
const base = import.meta.env.BASE_URL;
54
---
@@ -129,7 +128,13 @@ const base = import.meta.env.BASE_URL;
129128
</style>
130129

131130
<script>
131+
let hamburgerAbort: AbortController | undefined;
132+
132133
function initHamburgerMenu() {
134+
hamburgerAbort?.abort();
135+
hamburgerAbort = new AbortController();
136+
const { signal } = hamburgerAbort;
137+
133138
const hamburgerBtn = document.querySelector<HTMLButtonElement>('.hamburger-btn');
134139
const tabletDropdown = document.querySelector<HTMLElement>('.tablet-dropdown');
135140

@@ -144,22 +149,22 @@ const base = import.meta.env.BASE_URL;
144149
const firstLink = tabletDropdown.querySelector<HTMLAnchorElement>('.dropdown-link');
145150
firstLink?.focus();
146151
}
147-
});
152+
}, { signal });
148153

149154
document.addEventListener('click', (e) => {
150155
if (!hamburgerBtn.contains(e.target as Node) && !tabletDropdown.contains(e.target as Node)) {
151156
hamburgerBtn.setAttribute('aria-expanded', 'false');
152157
tabletDropdown.hidden = true;
153158
}
154-
});
159+
}, { signal });
155160

156161
document.addEventListener('keydown', (e) => {
157162
if (e.key === 'Escape' && hamburgerBtn.getAttribute('aria-expanded') === 'true') {
158163
hamburgerBtn.setAttribute('aria-expanded', 'false');
159164
tabletDropdown.hidden = true;
160165
hamburgerBtn.focus();
161166
}
162-
});
167+
}, { signal });
163168
}
164169

165170
initHamburgerMenu();

0 commit comments

Comments
 (0)