From 6708307c8d26d39e2dc4c7b6bd071d9c18287f4d Mon Sep 17 00:00:00 2001 From: krmax44 Date: Mon, 19 Feb 2024 18:09:18 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A9=B9=20fix=20mobile=20nav=20opening/clo?= =?UTF-8?q?sing=20twice?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- devsetup.sh | 4 +- frontend/javascript/navbar.ts | 74 ++++++++++++++++++++--------------- 2 files changed, 44 insertions(+), 34 deletions(-) diff --git a/devsetup.sh b/devsetup.sh index 2c2cd2185..a6892001c 100755 --- a/devsetup.sh +++ b/devsetup.sh @@ -88,7 +88,7 @@ setup() { git clone git@github.com:okfde/$MAIN.git else pushd $MAIN - git pull origin "$(git branch --show-current)" + #git pull origin "$(git branch --show-current)" popd fi pip install -U pip-tools @@ -103,7 +103,7 @@ setup() { git clone git@github.com:okfde/$name.git else pushd $name - git pull origin "$(git branch --show-current)" + git pull #origin "$(git branch --show-current)" popd fi pip uninstall -y $name diff --git a/frontend/javascript/navbar.ts b/frontend/javascript/navbar.ts index 36c22a08c..d54dfdbe3 100644 --- a/frontend/javascript/navbar.ts +++ b/frontend/javascript/navbar.ts @@ -1,64 +1,74 @@ import transitionDone from './misc/await-transition' const header = document.querySelector('#header') -let counter = 0 +let open: HTMLElement | undefined = undefined +let hide: () => void = () => {} -header?.querySelectorAll('.nav-toggle-menu').forEach((el) => - el.addEventListener('click', async () => { - counter++ +const menuToggles = [ + ...(header?.querySelectorAll('.nav-toggle-menu') ?? []) +] +menuToggles.forEach((el) => + el.addEventListener('click', async () => { const targetName = el.dataset.target if (targetName == null) return - const target = header.querySelector(`#menu-${targetName}`) + const target = header!.querySelector(`#menu-${targetName}`) if (target == null) return // hide other navs - const otherNavs = header.querySelectorAll('.nav-menu') + const otherNavs = header!.querySelectorAll('.nav-menu') otherNavs.forEach((el) => el !== target && el.classList.remove('show')) await transitionDone(otherNavs) - const otherTriggers = header.querySelectorAll('a[data-target]') + const otherTriggers = header!.querySelectorAll('a[data-target]') otherTriggers.forEach((el) => el.setAttribute('aria-expanded', 'false')) updateDropdowns() if (window.innerWidth >= 992) return - target.classList.toggle('show') + // hide if it is already open + if (open === target) return hide() + + const show = !target.classList.contains('show') + open = target + target.classList.remove('d-none') - el.setAttribute( - 'aria-expanded', - target.classList.contains('show') ? 'true' : 'false' - ) + el.setAttribute('aria-expanded', show ? 'true' : 'false') - if (target.classList.contains('show')) { + if (show) { target.querySelector('a')?.focus() + target.classList.add('show') + } else { + target.classList.remove('show') } - let id = counter - - const hide = () => { - if (id === counter) { - target.classList.remove('show') - el.setAttribute('aria-expanded', 'false') - } + hide = () => { + target.classList.remove('show') + el.setAttribute('aria-expanded', 'false') + open = undefined } - window.addEventListener('resize', hide, { once: true }) - - window.requestAnimationFrame(() => { - window.addEventListener('click', (e) => { - if (!target.contains(e.target as Element) && id === counter) hide() - }) - }) - - const height = target.clientHeight - window.addEventListener('scroll', () => { - if (window.scrollY > height) hide() - }) }) ) +window.addEventListener('resize', () => hide()) + +window.addEventListener('click', (e) => { + if ( + open !== undefined && + header?.contains(e.target as HTMLElement) === false + ) { + hide() + } +}) + +window.addEventListener('scroll', () => { + if (open && open.clientHeight * 2 < window.scrollY) { + hide() + } +}) + function updateDropdowns(): void { document.querySelectorAll('.nav-dropdown-trigger').forEach((trigger) => { const target = trigger.nextElementSibling!