From bedc70a3a558c6c67d6bc89a7b7ac3b6e6a375d1 Mon Sep 17 00:00:00 2001 From: Paolo Scattolin Date: Wed, 4 Nov 2026 15:59:55 +0100 Subject: [PATCH] Make the app sidebar collapsible --- src/components/Sidebar.tsx | 149 ++++++++++++++++++------------- src/pages/main-page/MainPage.tsx | 107 ++++++++++++++-------- 2 files changed, 155 insertions(+), 101 deletions(-) diff --git a/src/components/Sidebar.tsx b/src/components/Sidebar.tsx index f73212e4..ae4baddc 100644 --- a/src/components/Sidebar.tsx +++ b/src/components/Sidebar.tsx @@ -1,48 +1,46 @@ -import styled from "styled-components"; +import styled, { css } from "styled-components"; -type SidebarWidth = "compact" | "normal" | "wide"; - -const sidebarWidths: Record = { - compact: "200px", - normal: "280px", - wide: "350px", -}; +import { Button } from "./Button"; export interface SidebarProps extends React.HTMLAttributes { $isOpen: boolean; - $maxWidth?: SidebarWidth; + $collapsed?: boolean; } /** - * Sidebar component with responsive behavior and customizable max-width. - * The sidebar will always attempt to fit its content but will not exceed the specified max-width. + * App sidebar. On desktop it can collapse to an icon rail. The icons keep their + * place in both states, so only the width animates and the labels get clipped. * @param $isOpen - Controls sidebar visibility on mobile (default: false) - * @param $maxWidth - Sidebar max-width: "compact" | "normal" | "wide" (default: "normal") + * @param $collapsed - Shows only icons (default: false) */ export const Sidebar = styled.aside` - overflow-y: auto; + overflow: hidden auto; display: flex; flex-direction: column; - - width: auto; - max-width: ${({ $maxWidth = "normal" }) => sidebarWidths[$maxWidth]}; - + flex-shrink: 0; gap: ${({ theme }) => theme.spacing.lg}; + + width: ${({ $collapsed }) => ($collapsed ? "4.5rem" : "15rem")}; padding: ${({ theme }) => theme.spacing.lg}; + border-right: ${({ theme, $collapsed }) => + $collapsed + ? `2px solid ${theme.colors.primary.main}` + : `1px solid ${theme.colors.border.light}`}; - border-right: 1px solid ${({ theme }) => theme.colors.border.light}; + white-space: nowrap; background-color: ${({ theme }) => theme.colors.background.primary}; - transition: width ${({ theme }) => theme.transitions.slow}; + transition: width ${({ theme }) => theme.transitions.base}; @media (max-width: 768px) { position: fixed; - left: 0; + z-index: ${({ theme }) => theme.zIndex.sticky}; top: 0; bottom: 0; - z-index: ${({ theme }) => theme.zIndex.sticky}; + left: 0; transform: translateX(${({ $isOpen }) => ($isOpen ? "0" : "-100%")}); + transition: transform ${({ theme }) => theme.transitions.slow}; } `; @@ -50,27 +48,65 @@ export const Sidebar = styled.aside` export const SidebarHeader = styled.div` display: flex; flex-direction: column; - gap: ${({ theme }) => theme.spacing.lg}; + padding-top: ${({ theme }) => theme.spacing.lg}; padding-bottom: ${({ theme }) => theme.spacing.xl}; - border-bottom: 1px solid ${({ theme }) => theme.colors.border.light}; `; export const SidebarNav = styled.nav` display: flex; flex-direction: column; - gap: ${({ theme }) => theme.spacing.md}; `; -export const SidebarLogoContainer = styled.div` - display: flex; - align-items: center; - justify-content: center; +const fade = css<{ $hidden: boolean }>` + opacity: ${({ $hidden }) => ($hidden ? 0 : 1)}; + transition: opacity ${({ theme }) => theme.transitions.base}; +`; + +// Both logos stay mounted and cross-fade. The small one is centered on the icon column. +export const SidebarLogos = styled.div` + position: relative; + height: 2rem; +`; + +export const SidebarLogo = styled.img<{ $hidden: boolean; $small?: boolean }>` + position: absolute; + top: 0; + left: ${({ theme, $small }) => ($small ? "0.25rem" : theme.spacing.md)}; + + width: ${({ $small }) => ($small ? "2rem" : "11.5rem")}; + height: 100%; + + ${fade} +`; + +export const SidebarLabel = styled.span<{ $hidden: boolean }>` + ${fade} +`; + +/** + * The primary action at the top. Its label shrinks to nothing when collapsed, + * so the centered icon slides along with the width instead of jumping. + */ +export const SidebarAction = styled(Button)` + gap: 0; +`; + +export const SidebarActionLabel = styled.span<{ $hidden: boolean }>` + overflow: hidden; + + max-width: ${({ $hidden }) => ($hidden ? 0 : "10rem")}; + margin-left: ${({ theme, $hidden }) => ($hidden ? 0 : theme.spacing.sm)}; + + opacity: ${({ $hidden }) => ($hidden ? 0 : 1)}; - padding: ${({ theme }) => theme.spacing.md}; + transition: + max-width ${({ theme }) => theme.transitions.base}, + margin ${({ theme }) => theme.transitions.base}, + opacity ${({ theme }) => theme.transitions.base}; `; /** @@ -79,22 +115,24 @@ export const SidebarLogoContainer = styled.div` * @param $isActive - Whether the nav item is currently active (default: false) */ export const NavItem = styled.a<{ $isActive?: boolean }>` + cursor: pointer; + display: flex; + gap: ${({ theme }) => theme.spacing.md}; align-items: center; + width: 100%; + padding: ${({ theme }) => `${theme.spacing.sm} ${theme.spacing.md}`}; + border-radius: ${({ theme }) => theme.radius.md}; + font: ${({ theme }) => theme.typography.bodyMedium.font}; + color: ${({ theme }) => theme.colors.text.secondary}; + text-decoration: none; - cursor: pointer; transition: color ${({ theme }) => theme.transitions.base}, background-color ${({ theme }) => theme.transitions.base}; - border-radius: ${({ theme }) => theme.radius.md}; - color: ${({ theme }) => theme.colors.text.secondary}; - - padding: ${({ theme }) => `${theme.spacing.sm} ${theme.spacing.lg}`}; - gap: ${({ theme }) => theme.spacing.md}; - &:hover { color: ${({ theme }) => theme.colors.primary.main}; background-color: ${({ theme }) => `${theme.colors.primary.main}26`}; @@ -108,37 +146,24 @@ export const NavItem = styled.a<{ $isActive?: boolean }>` : ""} `; +// Icon-only items in a centered row that wraps into a column in the collapsed rail. export const SidebarFooter = styled.div` display: flex; - align-items: center; + flex-wrap: wrap; + gap: ${({ theme }) => theme.spacing.xs}; justify-content: center; - padding: ${({ theme }) => theme.spacing.md} ${({ theme }) => theme.spacing.md} - 0 ${({ theme }) => theme.spacing.md}; - - border-top: 1px solid ${({ theme }) => theme.colors.border.light}; margin-top: auto; -`; - -export const SidebarFooterLink = styled.a` - display: flex; - align-items: center; - - gap: ${({ theme }) => theme.spacing.md}; - padding: ${({ theme }) => `${theme.spacing.sm} ${theme.spacing.lg}`}; - - font: ${({ theme }) => theme.typography.bodyMedium.font}; - cursor: pointer; - border-radius: ${({ theme }) => theme.radius.md}; - color: ${({ theme }) => theme.colors.text.secondary}; - text-decoration: none; + padding-top: ${({ theme }) => theme.spacing.md}; + border-top: 1px solid ${({ theme }) => theme.colors.border.light}; - transition: - color ${({ theme }) => theme.transitions.base}, - background-color ${({ theme }) => theme.transitions.base}; + & > ${NavItem} { + width: auto; + } - &:hover { - color: ${({ theme }) => theme.colors.primary.main}; - background-color: ${({ theme }) => `${theme.colors.primary.main}26`}; + @media (max-width: 768px) { + & > button { + display: none; + } } -`; +`; \ No newline at end of file diff --git a/src/pages/main-page/MainPage.tsx b/src/pages/main-page/MainPage.tsx index 6cdccf2a..e030af87 100644 --- a/src/pages/main-page/MainPage.tsx +++ b/src/pages/main-page/MainPage.tsx @@ -1,10 +1,12 @@ import { IconActivity, IconBook, + IconChevronLeft, + IconChevronRight, IconFilePlus, IconHome, - IconLayoutDashboard, IconMenu2, + IconNotebook, IconSettings, IconX, type TablerIcon, @@ -24,14 +26,16 @@ import { PATHS, SOARCA_DOC_URL } from "@/utils"; import { Button, Icon, - ImageContainer, - Link, NavItem, Popover, Sidebar, SidebarFooter, SidebarHeader, - SidebarLogoContainer, + SidebarAction, + SidebarActionLabel, + SidebarLabel, + SidebarLogo, + SidebarLogos, SidebarNav, Spacer, ThemeSize, @@ -61,25 +65,27 @@ interface NavRoute { permission?: Permission; } -const NAV_ROUTES: NavRoute[] = [ - { path: PATHS.DASHBOARD, label: "Dashboard (Soon™)", icon: IconHome }, - { - path: PATHS.PLAYBOOKS.BASE, - label: "Playbooks (Beta)", - icon: IconLayoutDashboard, - permission: Permissions.PlaybookRead, - }, - { - path: PATHS.MONITORING.BASE, - label: "Monitoring", - icon: IconActivity, - permission: Permissions.PlaybookRead, - }, - { path: PATHS.SETTINGS, label: "Settings (Beta)", icon: IconSettings }, -]; - -const initial = (user: SessionUser) => - (user.name?.trim() || user.email || "?").charAt(0).toLocaleUpperCase(); +const NAV_ROUTES: NavRoute[] = [ + { path: PATHS.DASHBOARD, label: "Dashboard", icon: IconHome }, + { + path: PATHS.PLAYBOOKS.BASE, + label: "Playbooks", + icon: IconNotebook, + permission: Permissions.PlaybookRead, + }, + { + path: PATHS.MONITORING.BASE, + label: "Monitoring", + icon: IconActivity, + permission: Permissions.PlaybookRead, + }, + { path: PATHS.SETTINGS, label: "Settings", icon: IconSettings }, +]; + +const initial = (user: SessionUser) => + (user.name?.trim() || user.email || "?").charAt(0).toLocaleUpperCase(); + +const COLLAPSED_KEY = "soarca-sidebar-collapsed"; export const MainPage: React.FC = () => { const auth = useAuth(); @@ -87,6 +93,14 @@ export const MainPage: React.FC = () => { const { resolved } = useThemeMode(); const location = useLocation(); const [sidebarOpen, setSidebarOpen] = useState(false); + const [collapsed, setCollapsed] = useState( + () => localStorage.getItem(COLLAPSED_KEY) === "true", + ); + + const toggleCollapsed = () => { + localStorage.setItem(COLLAPSED_KEY, String(!collapsed)); + setCollapsed(!collapsed); + }; const openSidebar = () => setSidebarOpen(true); const closeSidebar = () => setSidebarOpen(false); @@ -99,25 +113,32 @@ export const MainPage: React.FC = () => { return ( - + - - + - - - - + New playbook + + {NAV_ROUTES.filter( @@ -132,23 +153,31 @@ export const MainPage: React.FC = () => { (route.path === PATHS.PLAYBOOKS.BASE && location.pathname.includes(PATHS.PLAYBOOKS.BASE)) } + title={collapsed ? route.label : undefined} onClick={() => handleNavigation(route.path)} > - {route.label} + {route.label} ))} - - Docs - + + + +