import { ChevronRight } from 'lucide-react'
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@mdxui/primitives/collapsible'
import {
SidebarGroup,
SidebarGroupLabel,
SidebarMenu,
SidebarMenuAction,
SidebarMenuButton,
SidebarMenuItem,
SidebarMenuSub,
SidebarMenuSubButton,
SidebarMenuSubItem,
} from '@mdxui/primitives/sidebar'
import { useNavigation } from '../context/navigation-context'
import type { NavGroup, NavItem } from '../types'
export interface NavMainProps {
/** Navigation groups to render */
groups?: NavGroup[]
/** Single group of items (shorthand when only one group needed) */
items?: NavItem[]
/** Label for single group */
label?: string
/** Current path for active state detection */
currentPath?: string
}
/**
* NavMain renders the main navigation in the sidebar.
* Supports collapsible groups with nested items.
*
* @example
* ```tsx
* // Single group
*
*
* // Multiple groups
*
* ```
*/
export function NavMain({
groups,
items,
label = 'Navigation',
currentPath: currentPathProp,
}: NavMainProps) {
const { getCurrentPath, Link } = useNavigation()
const currentPath = currentPathProp ?? getCurrentPath()
// Convert single items array to groups format
const navGroups: NavGroup[] = groups ?? (items ? [{ label, items }] : [])
if (navGroups.length === 0) {
return null
}
return (
<>
{navGroups.map((group) => (
{group.label}
{group.items.map((item) => (
))}
))}
>
)
}
interface NavMainItemProps {
item: NavItem
currentPath: string
Link: React.ComponentType<{ href: string; children: React.ReactNode; className?: string }>
}
function NavMainItem({ item, currentPath, Link }: NavMainItemProps) {
const isActive = currentPath === item.url || (item.url !== '/' && currentPath.startsWith(item.url))
const hasSubItems = item.items && item.items.length > 0
if (hasSubItems) {
return (
{item.icon && }
{item.title}
Toggle
{item.items!.map((subItem) => {
const subIsActive =
currentPath === subItem.url ||
(subItem.url !== '/' && currentPath.startsWith(subItem.url))
return (
{subItem.title}
)
})}
)
}
return (
{item.icon && }
{item.title}
)
}