From 2fd6726f08e98d370cf5827a6d93d735f6fb40f9 Mon Sep 17 00:00:00 2001 From: Saeed Abadiyan Date: Mon, 11 Aug 2025 03:14:04 +0330 Subject: [PATCH] update and fix the styles in sidebard --- app/components/dashboard/sidebar.tsx | 200 +++++++++++++++++++-------- 1 file changed, 145 insertions(+), 55 deletions(-) diff --git a/app/components/dashboard/sidebar.tsx b/app/components/dashboard/sidebar.tsx index 9431ea4..44e887b 100644 --- a/app/components/dashboard/sidebar.tsx +++ b/app/components/dashboard/sidebar.tsx @@ -145,12 +145,47 @@ export function Sidebar({ const [expandedItems, setExpandedItems] = useState([]); const { logout } = useAuth(); + // Auto-expand parent sections when their children are active + React.useEffect(() => { + const autoExpandParents = () => { + const newExpandedItems: string[] = []; + + menuItems.forEach((item) => { + if (item.children) { + const hasActiveChild = item.children.some( + (child) => child.href && location.pathname === child.href + ); + if (hasActiveChild) { + newExpandedItems.push(item.id); + } + } + }); + + setExpandedItems(newExpandedItems); + }; + + autoExpandParents(); + }, [location.pathname]); + const toggleExpanded = (itemId: string) => { - setExpandedItems((prev) => - prev.includes(itemId) - ? prev.filter((id) => id !== itemId) - : [...prev, itemId], - ); + setExpandedItems((prev) => { + // If trying to collapse, check if any child is active + if (prev.includes(itemId)) { + const item = menuItems.find(menuItem => menuItem.id === itemId); + if (item?.children) { + const hasActiveChild = item.children.some( + (child) => child.href && location.pathname === child.href + ); + // Don't collapse if a child is active + if (hasActiveChild) { + return prev; + } + } + return prev.filter((id) => id !== itemId); + } else { + return [...prev, itemId]; + } + }); }; const isActiveRoute = (href?: string, children?: MenuItem[]) => { @@ -165,7 +200,10 @@ export function Sidebar({ const renderMenuItem = (item: MenuItem, level = 0) => { const isActive = isActiveRoute(item.href, item.children); - const isExpanded = expandedItems.includes(item.id); + const isExpanded = expandedItems.includes(item.id) || + (item.children && item.children.some(child => + child.href && location.pathname === child.href + )); const hasChildren = item.children && item.children.length > 0; const ItemIcon = item.icon; @@ -178,61 +216,113 @@ export function Sidebar({ } }; - const menuItemContent = ( -
-
- - {!isCollapsed && ( - - {item.label} - - )} -
- - {!isCollapsed && ( -
- {item.badge && ( - - {item.badge} - - )} - {hasChildren && ( - - )} -
- )} -
- ); - return (
{item.href && item.id !== "logout" ? ( - {menuItemContent} +
+
+ + {!isCollapsed && ( + + {item.label} + + )} +
+ + {!isCollapsed && ( +
+ {item.badge && ( + + {item.badge} + + )} + {hasChildren && ( + + )} +
+ )} +
) : ( - + )} {/* Submenu */}