Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

SidebarSection.tsx

103 lines2.9 KB

1'use client'
2​
3import { useState, type ReactNode } from 'react'
4import { ChevronDown, ChevronRight } from 'lucide-react'
5import { motion, AnimatePresence } from 'framer-motion'
6​
7interface SidebarSectionProps {
8 title: string
9 count?: number
10 defaultOpen?: boolean
11 collapsible?: boolean
12 className?: string
13 minHeight?: number
14 actions?: ReactNode
15 /** When true, content will grow to fill available space and be scrollable */
16 flexContent?: boolean
17 children: ReactNode
19​
20export function SidebarSection({
21 title,
22 count,
23 defaultOpen = true,
24 collapsible = true,
25 className = '',
26 minHeight,
27 actions,
28 flexContent = false,
29 children,
30}: SidebarSectionProps) {
31 const [isOpen, setIsOpen] = useState(defaultOpen)
32​
33 return (
34 <div
35 className={`flex flex-col border-b border-gray-700/50 ${flexContent ? 'min-h-0' : ''} ${className}`}
36 style={minHeight && isOpen ? { minHeight } : undefined}
37 >
38 {/* Header */}
39 <button
40 onClick={() => collapsible && setIsOpen(!isOpen)}
41 disabled={!collapsible}
42 className={`flex items-center justify-between px-3 py-2.5 text-sm font-medium transition-colors shrink-0 ${
43 collapsible
44 ? 'text-gray-300 hover:bg-gray-800/50 cursor-pointer'
45 : 'text-gray-300 cursor-default'
46 }`}
47 >
48 <div className="flex items-center gap-2">
49 {collapsible && (
50 <span className="text-gray-500">
51 {isOpen ? (
52 <ChevronDown className="w-4 h-4" />
53 ) : (
54 <ChevronRight className="w-4 h-4" />
55 )}
56 </span>
57 )}
58 <span>{title}</span>
59 {count !== undefined && (
60 <span className="px-1.5 py-0.5 text-xs rounded-full bg-gray-700/50 text-gray-400">
61 {count}
62 </span>
63 )}
64 </div>
65 {actions && (
66 <div
67 onClick={(e) => e.stopPropagation()}
68 className="flex items-center gap-1"
69 >
70 {actions}
71 </div>
72 )}
73 </button>
74​
75 {/* Content */}
76 <AnimatePresence initial={false}>
77 {isOpen && (
78 <motion.div
79 initial={{ height: 0, opacity: 0 }}
80 animate={{ height: flexContent ? '100%' : 'auto', opacity: 1 }}
81 exit={{ height: 0, opacity: 0 }}
82 transition={{ duration: 0.2, ease: 'easeInOut' }}
83 className={
84 flexContent
85 ? 'flex-1 min-h-0 flex flex-col'
86 : 'overflow-hidden flex-1'
87 }
88 >
89 <div
90 className={
91 flexContent
92 ? 'px-2 pb-2 flex-1 min-h-0 flex flex-col overflow-hidden'
93 : 'px-2 pb-2'
94 }
95 >
96 {children}
97 </div>
98 </motion.div>
99 )}
100 </AnimatePresence>
101 </div>
102 )