Great! Let's create a configuration file for the menu bar and sidebar, and then implement the logic to highlight the active menu bar and select the first menu item of the sidebar based on the current path.
Step 1: Create Configuration Files
menuBarConfig.js
This file will define the menu bar items and their corresponding sidebars.
// src/config/menuBarConfig.js
export const menuBarItems = [
{
name: 'Analysis',
path: '/analysis',
sidebarItems: [
{ name: 'Dashboard', path: '/analysis/dashboard' },
{ name: 'Performance', path: '/analysis/performance' },
// Add more sidebar items for Analysis here
],
},
{
name: 'Storage',
path: '/storage',
sidebarItems: [
{ name: 'Summary', path: '/storage/summary' },
{ name: 'File System', path: '/storage/file-system' },
{ name: 'Block', path: '/storage/block' },
{ name: 'Object', path: '/storage/object' },
// Add more sidebar items for Storage here
],
},
// Add more menu bar items here
];
Step 2: Create a MenuBar Component
This component will render the menu bar items and highlight the active one.
// src/components/MenuBar.js
import React from 'react';
import { useLocation } from 'react-router-dom';
import { menuBarItems } from '../config/menuBarConfig';
const MenuBar = () => {
const location = useLocation();
const pathname = location.pathname;
return (
<nav>
{menuBarItems.map((item) => (
<a
key={item.name}
href={item.path}
className={pathname.startsWith(item.path) ? 'active' : ''}
>
{item.name}
</a>
))}
</nav>
);
};
export default MenuBar;
Step 3: Create a Sidebar Component
This component will render the sidebar items based on the active menu bar item.
// src/components/Sidebar.js
import React from 'react';
import { useLocation } from 'react-router-dom';
import { menuBarItems } from '../config/menuBarConfig';
const Sidebar = () => {
const location = useLocation();
const pathname = location.pathname;
// Find the active menu bar item
const activeMenuBarItem = menuBarItems.find((item) =>
pathname.startsWith(item.path)
);
if (!activeMenuBarItem) {
return null; // No matching menu bar item found
}
// Get the sidebar items for the active menu bar item
const sidebarItems = activeMenuBarItem.sidebarItems;
return (
<aside>
{sidebarItems.map((item) => (
<a
key={item.name}
href={item.path}
className={pathname === item.path ? 'active' : ''}
>
{item.name}
</a>
))}
</aside>
);
};
export default Sidebar;
Step 4: Use the MenuBar and Sidebar Components in Your App
// src/App.js
import React from 'react';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import MenuBar from './components/MenuBar';
import Sidebar from './components/Sidebar';
import Wrapper from './layout/components/Wrapper';
import Dashboard from './Dashboard';
import PerformancePage from './PerformancePage';
import SummaryPage from './SummaryPage';
import FileSystemPage from './FileSystemPage';
import BlockPage from './BlockPage';
import ObjectPage from './ObjectPage';
const App = () => {
return (
<Router>
<MenuBar />
<Sidebar />
<Routes>
<Route path="/" element={<Wrapper />}>
<Route index element={<Navigate to="analysis/dashboard" replace />} />
<Route path="analysis">
<Route index element={<Navigate to="dashboard" replace />} />
<Route path="dashboard" element={<Dashboard />} />
<Route path="performance" element={<PerformancePage />} />
</Route>
<Route path="storage">
<Route index element={<Navigate to="summary" replace />} />
<Route path="summary" element={<SummaryPage />} />
<Route path="file-system" element={<FileSystemPage />} />
<Route path="block" element={<BlockPage />} />
<Route path="object" element={<ObjectPage />} />
</Route>
<Route path="*" element={<div>404 Not Found</div>} />
</Route>
</Routes>
</Router>
);
};
export default App;
Step 5: Add CSS for Active Classes
/* src/App.css */
nav a.active,
aside a.active {
font-weight: bold;
color: blue;
}
Now, when you navigate to different paths, the corresponding menu bar item and sidebar item will be highlighted. The sidebar will display the items specific to the active menu bar item, and the first item will be selected by default.
Would you like to know more about any specific part of this setup?