Skip to content

Commit 6708307

Browse files
committed
🩹 fix mobile nav opening/closing twice
1 parent ab4093c commit 6708307

File tree

2 files changed

+44
-34
lines changed

2 files changed

+44
-34
lines changed

‎devsetup.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ setup() {
8888
git clone [email protected]:okfde/$MAIN.git
8989
else
9090
pushd $MAIN
91-
git pull origin "$(git branch --show-current)"
91+
#git pull origin "$(git branch --show-current)"
9292
popd
9393
fi
9494
pip install -U pip-tools
@@ -103,7 +103,7 @@ setup() {
103103
git clone [email protected]:okfde/$name.git
104104
else
105105
pushd $name
106-
git pull origin "$(git branch --show-current)"
106+
git pull #origin "$(git branch --show-current)"
107107
popd
108108
fi
109109
pip uninstall -y $name

‎frontend/javascript/navbar.ts

+42-32
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,74 @@
11
import transitionDone from './misc/await-transition'
22

33
const header = document.querySelector('#header')
4-
let counter = 0
4+
let open: HTMLElement | undefined = undefined
5+
let hide: () => void = () => {}
56

6-
header?.querySelectorAll<HTMLElement>('.nav-toggle-menu').forEach((el) =>
7-
el.addEventListener('click', async () => {
8-
counter++
7+
const menuToggles = [
8+
...(header?.querySelectorAll<HTMLElement>('.nav-toggle-menu') ?? [])
9+
]
910

11+
menuToggles.forEach((el) =>
12+
el.addEventListener('click', async () => {
1013
const targetName = el.dataset.target
1114
if (targetName == null) return
1215

13-
const target = header.querySelector<HTMLElement>(`#menu-${targetName}`)
16+
const target = header!.querySelector<HTMLElement>(`#menu-${targetName}`)
1417
if (target == null) return
1518

1619
// hide other navs
17-
const otherNavs = header.querySelectorAll('.nav-menu')
20+
const otherNavs = header!.querySelectorAll('.nav-menu')
1821
otherNavs.forEach((el) => el !== target && el.classList.remove('show'))
1922
await transitionDone(otherNavs)
2023

21-
const otherTriggers = header.querySelectorAll('a[data-target]')
24+
const otherTriggers = header!.querySelectorAll('a[data-target]')
2225
otherTriggers.forEach((el) => el.setAttribute('aria-expanded', 'false'))
2326

2427
updateDropdowns()
2528
if (window.innerWidth >= 992) return
2629

27-
target.classList.toggle('show')
30+
// hide if it is already open
31+
if (open === target) return hide()
32+
33+
const show = !target.classList.contains('show')
34+
open = target
35+
2836
target.classList.remove('d-none')
2937

30-
el.setAttribute(
31-
'aria-expanded',
32-
target.classList.contains('show') ? 'true' : 'false'
33-
)
38+
el.setAttribute('aria-expanded', show ? 'true' : 'false')
3439

35-
if (target.classList.contains('show')) {
40+
if (show) {
3641
target.querySelector('a')?.focus()
42+
target.classList.add('show')
43+
} else {
44+
target.classList.remove('show')
3745
}
3846

39-
let id = counter
40-
41-
const hide = () => {
42-
if (id === counter) {
43-
target.classList.remove('show')
44-
el.setAttribute('aria-expanded', 'false')
45-
}
47+
hide = () => {
48+
target.classList.remove('show')
49+
el.setAttribute('aria-expanded', 'false')
50+
open = undefined
4651
}
47-
window.addEventListener('resize', hide, { once: true })
48-
49-
window.requestAnimationFrame(() => {
50-
window.addEventListener('click', (e) => {
51-
if (!target.contains(e.target as Element) && id === counter) hide()
52-
})
53-
})
54-
55-
const height = target.clientHeight
56-
window.addEventListener('scroll', () => {
57-
if (window.scrollY > height) hide()
58-
})
5952
})
6053
)
6154

55+
window.addEventListener('resize', () => hide())
56+
57+
window.addEventListener('click', (e) => {
58+
if (
59+
open !== undefined &&
60+
header?.contains(e.target as HTMLElement) === false
61+
) {
62+
hide()
63+
}
64+
})
65+
66+
window.addEventListener('scroll', () => {
67+
if (open && open.clientHeight * 2 < window.scrollY) {
68+
hide()
69+
}
70+
})
71+
6272
function updateDropdowns(): void {
6373
document.querySelectorAll('.nav-dropdown-trigger').forEach((trigger) => {
6474
const target = trigger.nextElementSibling!

0 commit comments

Comments
 (0)