260813_01_FST: First Commit
This commit is contained in:
123
src/components/layout/KriSidebar.tsx
Normal file
123
src/components/layout/KriSidebar.tsx
Normal file
@@ -0,0 +1,123 @@
|
||||
'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
|
||||
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,
|
||||
onNavigate,
|
||||
className,
|
||||
contentClassName,
|
||||
}: KriSidebarProps) {
|
||||
return (
|
||||
<aside
|
||||
className={cn(
|
||||
'fixed top-20 left-0 z-30 h-[calc(100vh-5rem)] overflow-y-auto border-r bg-background transition-all duration-300 lg:static lg:top-0 lg:h-full lg:translate-x-0',
|
||||
isOpen ? 'w-80 translate-x-0' : 'w-80 -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 = 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-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-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">{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-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>
|
||||
</aside>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user