Skip to content

Commit 1914333

Browse files
committed
feat: avoid dupe topnav matches
1 parent 2376b95 commit 1914333

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

.changeset/fuzzy-pans-talk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"vocs": patch
3+
---
4+
5+
Enhanced sidebar active match mechanism to avoid duplicate topnav matches.

src/app/hooks/useActiveNavIds.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,21 @@ function getActiveNavIds({
99
pathname: string
1010
}): number[] {
1111
const path = pathname.replace(/\.html$/, '')
12-
const activeIds = []
12+
const matches: { id: number; children: number[] }[] = []
13+
1314
for (const item of items) {
14-
if (item.link && path.startsWith(item.match || item.link)) activeIds.push(item.id)
15+
if (item.link && path.startsWith(item.match || item.link))
16+
matches.push({ id: item.id, children: [] })
1517
else if (item.items) {
1618
const activeChildItems = getActiveNavIds({ items: item.items, pathname })
17-
if (activeChildItems.length > 0) activeIds.push(item.id)
19+
if (activeChildItems.length > 0) matches.push({ id: item.id, children: activeChildItems })
1820
}
1921
}
20-
return activeIds
22+
23+
// Return only the last match and its children (prefer most specific/last match)
24+
if (matches.length === 0) return []
25+
const lastMatch = matches[matches.length - 1]
26+
return [lastMatch.id, ...lastMatch.children]
2127
}
2228

2329
export function useActiveNavIds({

0 commit comments

Comments
 (0)