Files
epolicy-dash-front/src/components/layout/KriSidebar.tsx

127 lines
5.0 KiB
TypeScript

'use client'
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
footer?: React.ReactNode
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,
footer,
onNavigate,
className,
contentClassName,
}: KriSidebarProps) {
return (
<aside
className={cn(
'fixed top-20 left-0 z-30 flex h-[calc(100vh-5rem)] flex-col overflow-hidden border-r bg-background transition-all duration-300 lg:static lg:top-0 lg:h-full lg:translate-x-0',
isOpen ? 'w-130 translate-x-0' : 'w-130 -translate-x-full lg:w-0 lg:border-r-0',
className,
)}
>
<div className={cn('min-h-0 flex-1 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 = hasChildren && active
const Icon = item.icon
if (!hasChildren) {
return (
<Button
key={item.id}
type="button"
variant={active ? 'secondary' : 'ghost'}
className={cn(
'h-auto w-full justify-start rounded-md py-2 text-left whitespace-normal break-keep wrap-break-words',
active &&
'border-l-2 border-primary bg-primary/10 text-primary font-medium hover:bg-primary/15 hover:text-primary',
)}
disabled={item.disabled || !item.path}
onClick={() => item.path && onNavigate?.(item.path, item)}
>
{Icon ? <Icon className="size-4 shrink-0" /> : null}
<span>{item.label}</span>
</Button>
)
}
const questionCount = item.children?.length ?? 0
return (
<div key={item.id} className="space-y-1">
<Button
type="button"
variant={active ? 'secondary' : 'ghost'}
className={cn(
'h-auto w-full justify-start gap-1.5 rounded-md px-3 py-2 text-left whitespace-normal break-keep break-words',
active &&
'border-l-2 border-primary bg-primary/10 text-primary font-medium hover:bg-primary/15 hover:text-primary',
)}
disabled={item.disabled}
onClick={() => item.path && onNavigate?.(item.path, item)}
>
{Icon ? <Icon className="size-4 shrink-0" /> : null}
<span className="min-w-0 flex-1 whitespace-normal break-keep break-words">{item.label}</span>
<span className="shrink-0 text-xs whitespace-nowrap text-muted-foreground">{questionCount}문항</span>
<ChevronDown className={cn('size-4 shrink-0 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={cn(
'h-auto w-full justify-start rounded-md py-2 text-left whitespace-normal break-keep break-words',
childActive &&
'border-l-2 border-primary bg-primary/10 text-primary font-medium hover:bg-primary/15 hover:text-primary',
)}
disabled={child.disabled || !child.path}
onClick={() => child.path && onNavigate?.(child.path, child)}
>
{ChildIcon ? <ChildIcon className="size-4 shrink-0" /> : null}
<span>{child.label}</span>
</Button>
)
})}
</div>
) : null}
</div>
)
})}
</nav>
</div>
{footer ? <div className="shrink-0 border-t p-4">{footer}</div> : null}
</aside>
)
}