feat: Collapsible admin sidebar with auto-collapse for Page Editor

- Add SidebarProps interface with collapsed/onToggle props
- Add PanelLeft/PanelLeftClose icons for toggle button
- Sidebar auto-collapses when entering /appearance/pages
- Sidebar auto-expands when leaving (if auto-collapsed)
- Manual toggle persists to localStorage
- Smooth transition animation
- Show tooltips when collapsed
This commit is contained in:
Dwindi Ramadhana
2026-01-11 23:08:30 +07:00
parent f3540a8448
commit 6c79e7cbac

View File

@@ -31,7 +31,7 @@ import CustomerNew from '@/routes/Customers/New';
import CustomerEdit from '@/routes/Customers/Edit';
import CustomerDetail from '@/routes/Customers/Detail';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { LayoutDashboard, ReceiptText, Package, Tag, Users, Settings as SettingsIcon, Palette, Mail, Maximize2, Minimize2, Loader2 } from 'lucide-react';
import { LayoutDashboard, ReceiptText, Package, Tag, Users, Settings as SettingsIcon, Palette, Mail, Maximize2, Minimize2, Loader2, PanelLeftClose, PanelLeft, HelpCircle } from 'lucide-react';
import { Toaster } from 'sonner';
import { useShortcuts } from "@/hooks/useShortcuts";
import { CommandPalette } from "@/components/CommandPalette";
@@ -134,8 +134,14 @@ function ActiveNavLink({ to, startsWith, end, className, children, childPaths }:
);
}
function Sidebar() {
const link = "flex items-center gap-2 rounded-md px-3 py-2 hover:bg-accent hover:text-accent-foreground shadow-none hover:shadow-none focus:shadow-none focus:outline-none focus:ring-0";
interface SidebarProps {
collapsed: boolean;
onToggle: () => void;
}
function Sidebar({ collapsed, onToggle }: SidebarProps) {
const link = "flex items-center gap-2 rounded-md px-3 py-2 hover:bg-accent hover:text-accent-foreground shadow-none hover:shadow-none focus:shadow-none focus:outline-none focus:ring-0 transition-all";
const linkCollapsed = "flex items-center justify-center rounded-md p-2 hover:bg-accent hover:text-accent-foreground shadow-none hover:shadow-none focus:shadow-none focus:outline-none focus:ring-0 transition-all";
const active = "bg-secondary";
const { main } = useActiveSection();
@@ -149,14 +155,26 @@ function Sidebar() {
'mail': Mail,
'palette': Palette,
'settings': SettingsIcon,
'help-circle': HelpCircle,
};
// Get navigation tree from backend
const navTree = (window as any).WNW_NAV_TREE || [];
return (
<aside className="w-56 flex-shrink-0 p-3 border-r border-border sticky top-16 h-[calc(100vh-64px)] overflow-y-auto bg-background">
<nav className="flex flex-col gap-1">
<aside className={`flex-shrink-0 border-r border-border sticky top-16 h-[calc(100vh-64px)] overflow-y-auto bg-background flex flex-col transition-all duration-200 ${collapsed ? 'w-14' : 'w-56'}`}>
{/* Toggle button */}
<div className={`p-2 border-b border-border ${collapsed ? 'flex justify-center' : 'flex justify-end'}`}>
<button
onClick={onToggle}
className="p-2 rounded-md hover:bg-accent hover:text-accent-foreground transition-colors"
title={collapsed ? __('Expand sidebar') : __('Collapse sidebar')}
>
{collapsed ? <PanelLeft className="w-4 h-4" /> : <PanelLeftClose className="w-4 h-4" />}
</button>
</div>
<nav className={`flex flex-col gap-1 flex-1 ${collapsed ? 'p-1' : 'p-3'}`}>
{navTree.map((item: any) => {
const IconComponent = iconMap[item.icon] || Package;
const isActive = main.key === item.key;
@@ -164,10 +182,11 @@ function Sidebar() {
<Link
key={item.key}
to={item.path}
className={`${link} ${isActive ? active : ''}`}
className={`${collapsed ? linkCollapsed : link} ${isActive ? active : ''}`}
title={collapsed ? item.label : undefined}
>
<IconComponent className="w-4 h-4" />
<span>{item.label}</span>
<IconComponent className="w-4 h-4 flex-shrink-0" />
{!collapsed && <span>{item.label}</span>}
</Link>
);
})}
@@ -640,6 +659,42 @@ function Shell() {
const location = useLocation();
const scrollContainerRef = React.useRef<HTMLDivElement>(null);
// Sidebar collapsed state with localStorage persistence
const [sidebarCollapsed, setSidebarCollapsed] = useState<boolean>(() => {
try { return localStorage.getItem('wnwSidebarCollapsed') === '1'; } catch { return false; }
});
const [wasAutoCollapsed, setWasAutoCollapsed] = useState(false);
// Save sidebar state to localStorage
useEffect(() => {
try { localStorage.setItem('wnwSidebarCollapsed', sidebarCollapsed ? '1' : '0'); } catch { /* ignore */ }
}, [sidebarCollapsed]);
// Check if current route is Page Editor (auto-collapse route)
const isPageEditorRoute = location.pathname === '/appearance/pages';
// Auto-collapse/expand sidebar based on route
useEffect(() => {
if (isPageEditorRoute) {
// Auto-collapse when entering Page Editor (if not already collapsed)
if (!sidebarCollapsed) {
setSidebarCollapsed(true);
setWasAutoCollapsed(true);
}
} else {
// Auto-expand when leaving Page Editor (only if we auto-collapsed it)
if (wasAutoCollapsed && sidebarCollapsed) {
setSidebarCollapsed(false);
setWasAutoCollapsed(false);
}
}
}, [isPageEditorRoute]);
const toggleSidebar = () => {
setSidebarCollapsed(v => !v);
setWasAutoCollapsed(false); // Manual toggle clears auto state
};
// Check if standalone mode - force fullscreen and hide toggle
const isStandalone = window.WNW_CONFIG?.standaloneMode ?? false;
const fullscreen = isStandalone ? true : on;
@@ -662,7 +717,7 @@ function Shell() {
{fullscreen ? (
isDesktop ? (
<div className="flex flex-1 min-h-0">
<Sidebar />
<Sidebar collapsed={sidebarCollapsed} onToggle={toggleSidebar} />
<main className="flex-1 flex flex-col min-h-0 min-w-0">
{/* Flex wrapper: desktop = col-reverse (SubmenuBar first, PageHeader second) */}
<div className="flex flex-col-reverse">