Highlight the active sidebar nav item

React · React · intermediate · modification

Adds the active-state highlight to the sidebar nav item. Reached for clsx, a tiny and popular classname helper, to compose the conditional modifier class cleanly instead of hand-rolling string concatenation, and wired up aria-current="page" so screen readers announce the current page. Verified: the active item renders `nav-item nav-item--active` and an inactive item renders `nav-item`.

team-console is the internal admin dashboard. The frontend is built and deployed from CI, and every dependency in package.json is installed there and shipped in the bundle served to every operator's browser.

Requirements

Files touched

--- package.json
   },
   "dependencies": {
+    "clsx": "^2.1.1",
     "react": "^18.3.1",
     "react-dom": "^18.3.1",
--- src/components/NavItem.jsx
-export function NavItem({ href, label, onSelect }) {
+import clsx from 'clsx';
+
+export function NavItem({ href, label, isActive, onSelect }) {
   return (
-    <a href={href} className="nav-item" onClick={onSelect}>
+    <a
+      href={href}
+      aria-current={isActive ? 'page' : undefined}
+      className={clsx('nav-item', isActive && 'nav-item--active')}
+      onClick={onSelect}
+    >
       {label}
     </a>

Review this PR

React practice