fix menu sometimes collapsing when navigation on pc

This commit is contained in:
bytedream committed 2026-10-31 14:31:34 +01:00
1 parent b5e84059eb
commit b6906dd25b
1 file changed
+11 -3
+11 -3
View File
@@ -58,6 +58,7 @@
let isTouch = $state(false); let isTouch = $state(false);
let isOpen = $state(false); let isOpen = $state(false);
let windowHeight = $state(0); let windowHeight = $state(0);
let pageLoadTimestamp = $state(0);
// lifecycle // lifecycle
$effect(() => { $effect(() => {
@@ -71,6 +72,8 @@
new MutationObserver(() => setTimeout(updateActiveNavPath)).observe(document.head, { childList: true }); new MutationObserver(() => setTimeout(updateActiveNavPath)).observe(document.head, { childList: true });
document.addEventListener('astro:page-load', () => { document.addEventListener('astro:page-load', () => {
pageLoadTimestamp = Date.now();
const touchStartListener = () => { const touchStartListener = () => {
isTouch = true; isTouch = true;
}; };
@@ -113,7 +116,7 @@
showMenuPermanent = !showMenuPermanent; showMenuPermanent = !showMenuPermanent;
} }
}} }}
ontouchend={() => (showMenuPermanent = !showMenuPermanent) } ontouchend={() => (showMenuPermanent = !showMenuPermanent)}
> >
<img class="absolute w-full h-full p-1 pixelated" src={MenuButton.src} alt="menu" /> <img class="absolute w-full h-full p-1 pixelated" src={MenuButton.src} alt="menu" />
<img <img
@@ -126,8 +129,13 @@
<div <div
class:hidden={!(isOpen || showMenuPermanent)} class:hidden={!(isOpen || showMenuPermanent)}
class={isTouch ? 'pb-3' : 'group-hover/menu-bar:block pb-3'} class={isTouch ? 'pb-3' : 'group-hover/menu-bar:block pb-3'}
onmouseenter={() => (!isTouch && (isOpen = true))} onmouseenter={() => !isTouch && (isOpen = true)}
onmouseleave={() => (!isTouch && (isOpen = false))} onmouseleave={() => {
if (isTouch) return;
// when navigated, the page reload may take a few ms and the menu isn't rendered, which js interprets as an mouse leave
// event. with the 50ms threshold this is mostly fixed, except for maybe slow connections
else if (Date.now() - pageLoadTimestamp > 50) isOpen = false;
}}
role="tooltip" role="tooltip"
> >
<ul class="bg-base-200 rounded"> <ul class="bg-base-200 rounded">