Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit b038bfa

Browse files
authored
ensure deeply nested collapsible nav items are active on init (#645)
1 parent 389aefa commit b038bfa

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/components/nav/CollapsibleNavItem.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,27 @@ import { cn } from '@/lib/utils'
77
import { NavLink } from './NavLink'
88
import { usePathname } from 'next/navigation'
99
import { Button } from '../ui/button'
10-
import { NavGroup } from '@/config/types'
10+
import { NavEntry, NavGroup } from '@/config/types'
1111

1212
interface Props {
1313
group: NavGroup
1414
className?: string
1515
}
1616

17+
const checkIfActive = (items: NavEntry[], pathname: string): boolean => {
18+
return items.some((link) => {
19+
if ('href' in link && link.href === pathname) {
20+
return true
21+
}
22+
23+
if ('items' in link && Array.isArray(link.items)) {
24+
return checkIfActive(link.items, pathname)
25+
}
26+
27+
return false
28+
})
29+
}
30+
1731
const CollapsibleNavItem: React.FC<Props> = ({ group, className }) => {
1832
const pathname = usePathname()
1933

@@ -22,9 +36,8 @@ const CollapsibleNavItem: React.FC<Props> = ({ group, className }) => {
2236

2337
const { title, items, icon: Icon } = group
2438

25-
const isActive = items.some(
26-
(link) => 'href' in link && link.href === pathname,
27-
)
39+
const isActive = checkIfActive(items, pathname)
40+
2841
const [isOpen, setIsOpen] = React.useState(isActive)
2942

3043
return (

0 commit comments

Comments
 (0)