"use client"; import { useState, useCallback, useEffect, useRef, type ReactNode } from "react"; import { cn } from "@/lib/utils"; import { ChevronLeft, ChevronRight } from "lucide-react"; import { Button } from "@/components/ui/button"; interface ResizableSidebarProps { children: ReactNode; defaultWidth?: number; minWidth?: number; maxWidth?: number; } export function ResizableSidebar({ children, defaultWidth = 250, minWidth = 200, maxWidth = 400, }: ResizableSidebarProps) { const [width, setWidth] = useState(defaultWidth); const [isCollapsed, setIsCollapsed] = useState(false); const [isResizing, setIsResizing] = useState(false); const [showPeek, setShowPeek] = useState(false); const sidebarRef = useRef(null); const initialXRef = useRef(0); const initialWidthRef = useRef(defaultWidth); const startResizing = useCallback((e: React.MouseEvent) => { setIsResizing(true); initialXRef.current = e.clientX; initialWidthRef.current = width; e.preventDefault(); }, [width]); const stopResizing = useCallback(() => { setIsResizing(false); }, []); const resize = useCallback( (e: MouseEvent) => { if (isResizing) { const deltaX = e.clientX - initialXRef.current; const newWidth = initialWidthRef.current + deltaX; const clampedWidth = Math.min(Math.max(newWidth, minWidth), maxWidth); setWidth(clampedWidth); } }, [isResizing, minWidth, maxWidth] ); useEffect(() => { if (isResizing) { window.addEventListener("mousemove", resize); window.addEventListener("mouseup", stopResizing); document.body.style.cursor = "col-resize"; document.body.style.userSelect = "none"; } return () => { window.removeEventListener("mousemove", resize); window.removeEventListener("mouseup", stopResizing); document.body.style.cursor = ""; document.body.style.userSelect = ""; }; }, [isResizing, resize, stopResizing]); const handleMouseEnter = () => { if (isCollapsed) { setShowPeek(true); } }; const handleMouseLeave = () => { if (isCollapsed) { setTimeout(() => setShowPeek(false), 300); } }; return ( <> {/* Peek trigger when collapsed */} {isCollapsed && (
)} {/* Peek Sidebar (when collapsed) */} {isCollapsed && showPeek && (
{children}
)} ); }