fix: nested menus in Tailwind 4 upgrade

This commit is contained in:
George Cushen committed 2025-08-20 20:01:56 +01:00
1 parent ee50bbf7e8
commit 87a3cb06f4
3 files changed
+44 -11

No files matched your search

@@ -54,20 +54,28 @@ input#nav-toggle:checked ~ #nav-menu {
.nav-dropdown {
@apply mr-0;
}
& > svg {
@apply pointer-events-none;
}
.nav-dropdown > svg {
@apply pointer-events-none;
}
&.active {
.nav-dropdown-list {
@apply block;
}
}
.nav-dropdown.active .nav-dropdown-list {
@apply block lg:visible lg:opacity-100;
}
.nav-dropdown-list {
@apply bg-white dark:bg-slate-900 z-10 min-w-[180px] rounded py-4 shadow hidden lg:invisible lg:absolute lg:block lg:opacity-0;
/* Ensure dropdown appears above content and under header */
top: 100%;
left: 0;
}
/* Ensure hover opens dropdown in desktop (CSS-driven) */
@media (min-width: 1024px) {
.nav-dropdown:hover > .nav-dropdown-list {
@apply visible opacity-100;
}
}
.nav-dropdown-item {
+23 -2
View File
@@ -10,12 +10,33 @@ const applyScrollPadding = () => {
window.addEventListener("DOMContentLoaded", () => {
const dropdownMenus = document.querySelectorAll(
".nav-dropdown > .nav-link",
".nav-dropdown > .nav-link[role='button']",
);
dropdownMenus.forEach((toggler) => {
const toggle = (el) => {
const parent = el.closest(".nav-dropdown");
const willActivate = !parent.classList.contains("active");
parent.classList.toggle("active", willActivate);
el.setAttribute("aria-expanded", willActivate ? "true" : "false");
};
toggler?.addEventListener("click", (e) => {
e.target.parentElement.classList.toggle("active");
e.preventDefault();
toggle(e.currentTarget);
});
// Keyboard support
toggler?.addEventListener("keydown", (e) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
toggle(e.currentTarget);
}
if (e.key === "Escape") {
const parent = e.currentTarget.closest(".nav-dropdown");
parent?.classList.remove("active");
e.currentTarget.setAttribute("aria-expanded", "false");
}
});
});
@@ -60,7 +60,11 @@
{{ if .HasChildren }}
<li class="nav-item nav-dropdown group relative">
<span
class="nav-link {{ range .Children }}
role="button"
tabindex="0"
aria-haspopup="true"
aria-expanded="false"
class="nav-link cursor-pointer select-none {{ range .Children }}
{{ $childURL := .URL | absLangURL }}
{{ $active := eq $childURL $pageURL }}
{{ if $active }}active{{ end }}