From 681d932b6d205f0a75658270e8de296f6c48189b Mon Sep 17 00:00:00 2001 From: Koorosh Aslansefat Date: Thu, 24 Sep 2026 07:56:45 -0700 Subject: [PATCH] fix: highlight closest matching research menu item --- docs/assets/javascripts/xwhy-nav.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/docs/assets/javascripts/xwhy-nav.js b/docs/assets/javascripts/xwhy-nav.js index e304d93..a3c8fe2 100644 --- a/docs/assets/javascripts/xwhy-nav.js +++ b/docs/assets/javascripts/xwhy-nav.js @@ -124,6 +124,21 @@ } function updateActiveState(nav) { + const activeDropdownLinks = new Set(); + + nav.querySelectorAll(".xwhy-topnav__dropdown").forEach(function (submenu) { + const matchingLinks = Array.from(submenu.querySelectorAll("a[href]")).filter(function (anchor) { + return isActive(new URL(anchor.href).pathname); + }); + const closestMatch = matchingLinks.reduce(function (best, anchor) { + return !best || anchor.pathname.length > best.pathname.length ? anchor : best; + }, null); + + if (closestMatch) { + activeDropdownLinks.add(closestMatch); + } + }); + nav.querySelectorAll("a[href]").forEach(function (anchor) { const href = new URL(anchor.href).pathname; const active = isActive(href); @@ -134,7 +149,7 @@ } if (anchor.classList.contains("xwhy-topnav__dropdown-link")) { - anchor.classList.toggle("xwhy-topnav__dropdown-link--active", active); + anchor.classList.toggle("xwhy-topnav__dropdown-link--active", activeDropdownLinks.has(anchor)); } if (exact) {