NvSidebar Component Guide for React
Introduction
The nv-sidebar component provides a flexible and responsive navigation sidebar for React applications. Built on top of Nova's design tokens, it offers collapsible navigation with support for nested items, notifications, and seamless integration with React Router.
Key Features:
- Collapsible and static sidebar types
- Nested navigation with sub-items
- Notification badges following Nova's design tokens
- Active state management
- React Router integration
- Responsive design
Installation
npm install @nova-design-system/nova-react
Basic Usage
Static Sidebar
import { NvSidebar, NvSidebarlogo, NvSidebarcontent, NvSidebargroup, NvSidebarnavitem } from '@nova-design-system/nova-react';
function AppLayout() {
return (
<NvSidebar type="static">
<NvSidebarlogo label="My App" />
<NvSidebarcontent>
<NvSidebargroup label="Navigation">
<NvSidebarnavitem icon="home">
<a href="/dashboard">Dashboard</a>
</NvSidebarnavitem>
<NvSidebarnavitem icon="user">
<a href="/profile">Profile</a>
</NvSidebarnavitem>
</NvSidebargroup>
</NvSidebarcontent>
</NvSidebar>
);
}
Collapsible Sidebar
import { useState } from 'react';
import { NvSidebar, NvSidebarlogo, NvSidebarcontent, NvSidebargroup, NvSidebarnavitem } from '@nova-design-system/nova-react';
function CollapsibleSidebar() {
const [isOpen, setIsOpen] = useState(true);
return (
<NvSidebar
type="collapsible"
open={isOpen}
onOpenChanged={(e) => setIsOpen(e.detail)}
>
<NvSidebarlogo label="My App" />
<NvSidebarcontent>
<NvSidebargroup label="Main">
<NvSidebarnavitem icon="home">
<a href="/dashboard">Dashboard</a>
</NvSidebarnavitem>
</NvSidebargroup>
</NvSidebarcontent>
</NvSidebar>
);
}
React Router Integration
With NavLink
import { NavLink, useLocation } from 'react-router-dom';
import {
NvSidebar,
NvSidebarlogo,
NvSidebarcontent,
NvSidebargroup,
NvSidebarnavitem,
NvSidebarnavsubitem,
NvSidebardivider,
NvSidebarfooter,
} from '@nova-design-system/nova-react';
function Sidebar() {
const location = useLocation();
return (
<NvSidebar
type="collapsible"
open={true}
activePath={location.pathname}
notificationIntention="brand"
notificationEmphasis="high"
>
<NvSidebarlogo label="nova React" />
<NvSidebarcontent>
<NvSidebargroup label="Main">
<NvSidebarnavitem icon="home">
<NavLink to="/dashboard" className={({ isActive }) => isActive ? 'active' : ''}>
Dashboard
</NavLink>
</NvSidebarnavitem>
<NvSidebarnavitem icon="chart-bar" collapsible>
<span>Analytics</span>
<NvSidebarnavsubitem>
<NavLink to="/analytics/reports" className={({ isActive }) => isActive ? 'active' : ''}>
Reports
</NavLink>
</NvSidebarnavsubitem>
<NvSidebarnavsubitem>
<NavLink to="/analytics/metrics" className={({ isActive }) => isActive ? 'active' : ''}>
Metrics
</NavLink>
</NvSidebarnavsubitem>
</NvSidebarnavitem>
<NvSidebarnavitem icon="settings" collapsible notificationCount={5}>
<span>Settings</span>
<NvSidebarnavsubitem>
<NavLink to="/settings/profile" className={({ isActive }) => isActive ? 'active' : ''}>
Profile
</NavLink>
</NvSidebarnavsubitem>
<NvSidebarnavsubitem>
<NavLink to="/settings/security" className={({ isActive }) => isActive ? 'active' : ''}>
Security
</NavLink>
</NvSidebarnavsubitem>
</NvSidebarnavitem>
</NvSidebargroup>
<NvSidebardivider />
<NvSidebargroup label="Other">
<NvSidebarnavitem icon="user" notificationCount={12}>
<NavLink to="/users" className={({ isActive }) => isActive ? 'active' : ''}>
Users
</NavLink>
</NvSidebarnavitem>
</NvSidebargroup>
</NvSidebarcontent>
<NvSidebarfooter>
<NvSidebarnavitem icon="bell" notificationCount={170}>
<NavLink to="/notifications" className={({ isActive }) => isActive ? 'active' : ''}>
Notifications
</NavLink>
</NvSidebarnavitem>
</NvSidebarfooter>
</NvSidebar>
);
}
Component Structure
NvSidebar
The main container component with the following props:
| Prop | Type | Default | Description |
|---|---|---|---|
type | 'static' | 'collapsible' | 'static' | Sidebar behavior type |
open | boolean | true | Controls open/closed state (collapsible only) |
activePath | string | undefined | Current active path for automatic highlighting |
notificationIntention | string | 'brand' | Default notification badge intention |
notificationEmphasis | string | 'high' | Default notification badge emphasis |
Events:
onOpenChanged: Fired when sidebar open state changes
NvSidebarlogo
Displays the application logo/brand.
| Prop | Type | Default | Description |
|---|---|---|---|
label | string | undefined | Logo text label |
NvSidebarcontent
Container for navigation groups and dividers.
NvSidebargroup
Groups related navigation items.
| Prop | Type | Default | Description |
|---|---|---|---|
label | string | undefined | Group title |
NvSidebarnavitem
Individual navigation item with optional sub-items.
| Prop | Type | Default | Description |
|---|---|---|---|
icon | string | undefined | Icon name from Nova icon set |
collapsible | boolean | false | Makes item expandable with sub-items |
notificationCount | number | undefined | Number to display in notification badge |
active | boolean | false | Forces active state |
NvSidebarnavsubitem
Sub-navigation item (child of NvSidebarnavitem).
NvSidebardivider
Visual separator between groups.
NvSidebarfooter
Container for footer navigation items.
Advanced Usage
Custom Active State Logic
import { useLocation } from 'react-router-dom';
function CustomActiveSidebar() {
const location = useLocation();
// Custom logic for determining active state
const isActive = (path: string) => {
return location.pathname.startsWith(path);
};
return (
<NvSidebar activePath={location.pathname}>
<NvSidebarcontent>
<NvSidebargroup>
<NvSidebarnavitem icon="folder" active={isActive('/projects')}>
<NavLink to="/projects">Projects</NavLink>
</NvSidebarnavitem>
</NvSidebargroup>
</NvSidebarcontent>
</NvSidebar>
);
}
Dynamic Navigation Items
import { useState, useEffect } from 'react';
function DynamicSidebar() {
const [menuItems, setMenuItems] = useState([]);
useEffect(() => {
// Fetch menu items from API
fetch('/api/navigation')
.then(res => res.json())
.then(setMenuItems);
}, []);
return (
<NvSidebar>
<NvSidebarcontent>
{menuItems.map(item => (
<NvSidebargroup key={item.id} label={item.label}>
{item.children.map(child => (
<NvSidebarnavitem
key={child.id}
icon={child.icon}
notificationCount={child.notifications}
>
<NavLink to={child.path}>
{child.label}
</NavLink>
</NvSidebarnavitem>
))}
</NvSidebargroup>
))}
</NvSidebarcontent>
</NvSidebar>
);
}
React-Specific Notes
Event Handling
- Use
onOpenChangedevent to manage sidebar state withuseState - Event detail contains the new open state as
e.detail
Navigation Integration
- Important:
stopPropagation()is not used on sub-item clicks to prevent issues with React Router and other SPA routers due to event delegation. The parentnv-sidebarnavitemcomponent handles click detection from sub-items instead
Active Link Styling
Use React Router's NavLink with conditional className:
<NavLink
to="/dashboard"
className={({ isActive }) => isActive ? 'active' : ''}
>
Dashboard
</NavLink>
Parent Items with Sub-items
When a navigation item has sub-items, use a <span> as the label instead of a NavLink:
<NvSidebarnavitem icon="settings" collapsible>
<span>Settings</span> {/* Not a NavLink */}
<NvSidebarnavsubitem>
<NavLink to="/settings/profile">Profile</NavLink>
</NvSidebarnavsubitem>
</NvSidebarnavitem>
Styling
The sidebar follows Nova's design system and uses CSS variables for consistent theming. To maintain design consistency, override these variables only when integrating with your established design system:
:root {
--nv-sidebar-width: 280px;
--nv-sidebar-bg: var(--nv-color-surface-primary);
--nv-sidebar-border: var(--nv-color-border-primary);
}
Accessibility
- Keyboard navigation support
- ARIA labels and roles
- Focus management for collapsible items
- Screen reader friendly notification counts
Common Patterns
Admin Dashboard Layout
function AdminLayout({ children }) {
const [sidebarOpen, setSidebarOpen] = useState(true);
return (
<div className="admin-layout">
<Sidebar
open={sidebarOpen}
onToggle={() => setSidebarOpen(!sidebarOpen)}
/>
<main className="main-content">
{children}
</main>
</div>
);
}
Responsive Design
Note: Responsive design features are not currently implemented in the NvSidebar component. Mobile-first responsive behavior will be considered for inclusion in a future v2 release, potentially including:
- Automatic mobile/desktop mode switching
- Touch-friendly interactions
- Collapsible behavior based on screen size
- Overlay modes for mobile devices
For now, implement responsive behavior at the application level using CSS media queries and React's state management.