Files
KART-FRONT-NEXT/src/components/layout/KriSidebar.tsx

130 lines
4.4 KiB
TypeScript

'use client'
import * as React from 'react'
import { ChevronDown } from 'lucide-react'
import { Button } from '@/components/ui/button'
import { cn } from '@/lib/utils'
import type { KriNavigationItem } from './types'
export interface KriSidebarProps {
items: KriNavigationItem[]
isOpen: boolean
pathname?: string
header?: React.ReactNode
openItemIds?: string[]
defaultOpenItemIds?: string[]
onOpenItemIdsChange?: (ids: string[]) => void
onNavigate?: (path: string, item: KriNavigationItem) => void
className?: string
contentClassName?: string
}
function isActive(item: KriNavigationItem, pathname: string): boolean {
if (item.match) return item.match(pathname)
if (item.path && (pathname === item.path || pathname.startsWith(`${item.path}/`))) return true
return item.children?.some((child) => isActive(child, pathname)) ?? false
}
export function KriSidebar({
items,
isOpen,
pathname = '',
header,
openItemIds,
defaultOpenItemIds = [],
onOpenItemIdsChange,
onNavigate,
className,
contentClassName,
}: KriSidebarProps) {
const [internalOpenIds, setInternalOpenIds] = React.useState(defaultOpenItemIds)
const currentOpenIds = openItemIds ?? internalOpenIds
const setOpenIds = (next: string[]) => {
if (openItemIds === undefined) setInternalOpenIds(next)
onOpenItemIdsChange?.(next)
}
const toggleItem = (id: string) => {
setOpenIds(currentOpenIds.includes(id) ? currentOpenIds.filter((value) => value !== id) : [...currentOpenIds, id])
}
return (
<aside
className={cn(
'fixed top-16 left-0 z-30 h-[calc(100vh-4rem)] overflow-y-auto border-r bg-background transition-all duration-300 lg:static lg:top-0 lg:h-auto lg:translate-x-0',
isOpen ? 'w-64 translate-x-0' : 'w-64 -translate-x-full lg:w-0 lg:border-r-0',
className,
)}
>
<div className={cn('h-full space-y-4 overflow-y-auto p-4', contentClassName)}>
{header}
<nav className="space-y-1">
{items.map((item) => {
const active = isActive(item, pathname)
const hasChildren = Boolean(item.children?.length)
const opened = currentOpenIds.includes(item.id)
const Icon = item.icon
if (!hasChildren) {
return (
<Button
key={item.id}
type="button"
variant={active ? 'secondary' : 'ghost'}
className="w-full justify-start"
disabled={item.disabled || !item.path}
onClick={() => item.path && onNavigate?.(item.path, item)}
>
{Icon ? <Icon className="size-4" /> : null}
<span>{item.label}</span>
</Button>
)
}
return (
<div key={item.id} className="space-y-1">
<Button
type="button"
variant={active ? 'secondary' : 'ghost'}
className="w-full justify-between"
disabled={item.disabled}
onClick={() => toggleItem(item.id)}
>
<span className="flex items-center gap-2">
{Icon ? <Icon className="size-4" /> : null}
<span>{item.label}</span>
</span>
<ChevronDown className={cn('size-4 transition-transform', opened && 'rotate-180')} />
</Button>
{opened ? (
<div className="ml-6 space-y-1">
{item.children?.map((child) => {
const childActive = isActive(child, pathname)
const ChildIcon = child.icon
return (
<Button
key={child.id}
type="button"
variant={childActive ? 'secondary' : 'ghost'}
className="w-full justify-start"
disabled={child.disabled || !child.path}
onClick={() => child.path && onNavigate?.(child.path, child)}
>
{ChildIcon ? <ChildIcon className="size-4" /> : null}
<span>{child.label}</span>
</Button>
)
})}
</div>
) : null}
</div>
)
})}
</nav>
</div>
</aside>
)
}