App Sidebar With Nested Groups and Active Item
The main navigation column for a building contractor's project app. Top-level links sit beside groups such as Projects, Team and Billing that open and close with a chevron, and their pages hang off a thin guide line. Clicking any page marks it current in soft emerald and updates the heading beside the sidebar, so you can try it straight away. An unread count rides on Inbox.
Code
import { useState } from "react";
// Navigation tree. Groups with `items` open and close; `href` items are plain links.
const nav = [
{ label: "Inbox", badge: 12 },
{ label: "Projects", items: ["Harbour Wall survey", "Mill Lane extension", "Riverside flats"] },
{ label: "Team", items: ["People", "Rotas", "Time off"] },
{ label: "Billing", items: ["Invoices", "Purchase orders", "Suppliers"] },
{ label: "Settings" },
];
export default function AppSidebarWithNestedGroupsAndActiveItem() {
const [openGroups, setOpenGroups] = useState<string[]>(["Projects", "Billing"]);
const [current, setCurrent] = useState("Mill Lane extension");
const toggle = (g: string) =>
setOpenGroups((list) => (list.includes(g) ? list.filter((x) => x !== g) : [...list, g]));
const linkBase = "flex h-10 w-full items-center rounded-lg px-3 text-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-emerald-600";
return (
<div className="bg-emerald-50/60 px-4 py-10 sm:px-8">
<div className="mx-auto flex h-[540px] max-w-4xl overflow-hidden rounded-2xl bg-white ring-1 ring-slate-200">
<aside className="flex w-full shrink-0 flex-col border-slate-200 sm:w-64 sm:border-r">
<div className="flex h-16 items-center gap-2.5 border-b border-slate-100 px-4">
<svg width="26" height="26" viewBox="0 0 24 24" aria-hidden="true"><path d="M3 20L12 4l9 16z" className="fill-emerald-600" /><path d="M8.5 20L12 13.5 15.5 20z" fill="#fff" /></svg>
<span className="text-sm font-semibold text-slate-900">Brockway Builders</span>
</div>
<nav aria-label="Main" className="flex-1 overflow-y-auto p-3">
<ul className="space-y-0.5">
{nav.map((n) =>
n.items ? (
<li key={n.label}>
<button
type="button"
aria-expanded={openGroups.includes(n.label)}
aria-controls={`asg-${n.label}`}
onClick={() => toggle(n.label)}
className={`${linkBase} justify-between font-medium text-slate-800 hover:bg-slate-100`}
>
{n.label}
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" className={`text-slate-500 transition-transform duration-200 motion-reduce:transition-none ${openGroups.includes(n.label) ? "rotate-90" : ""}`} aria-hidden="true"><path d="M9 6l6 6-6 6" /></svg>
</button>
<ul id={`asg-${n.label}`} hidden={!openGroups.includes(n.label)} className="ml-4 mt-0.5 space-y-0.5 border-l border-slate-200 pl-2">
{n.items.map((it) => (
<li key={it}>
<a
href="#"
aria-current={current === it ? "page" : undefined}
onClick={(e) => { e.preventDefault(); setCurrent(it); }}
className={`${linkBase} ${current === it ? "bg-emerald-50 font-semibold text-emerald-800" : "text-slate-600 hover:bg-slate-100 hover:text-slate-900"}`}
>
<span className="truncate">{it}</span>
</a>
</li>
))}
</ul>
</li>
) : (
<li key={n.label}>
<a
href="#"
aria-current={current === n.label ? "page" : undefined}
onClick={(e) => { e.preventDefault(); setCurrent(n.label); }}
className={`${linkBase} justify-between font-medium ${current === n.label ? "bg-emerald-50 text-emerald-800" : "text-slate-800 hover:bg-slate-100"}`}
>
{n.label}
{n.badge ? <span className="rounded-full bg-emerald-600 px-2 py-0.5 text-xs font-semibold text-white">{n.badge}<span className="sr-only"> unread</span></span> : null}
</a>
</li>
),
)}
</ul>
</nav>
</aside>
<main className="hidden min-w-0 flex-1 p-6 sm:block">
<p className="text-xs font-semibold uppercase tracking-wider text-emerald-700">Projects</p>
<h2 className="mt-1 text-xl font-semibold text-slate-900">{current}</h2>
<div className="mt-6 space-y-3" aria-hidden="true">
<div className="h-3 w-3/4 rounded bg-slate-100" />
<div className="h-3 w-2/3 rounded bg-slate-100" />
<div className="h-24 rounded-xl bg-slate-50 ring-1 ring-slate-200" />
</div>
</main>
</div>
</div>
);
}
Install with one command: npx shadcn@latest add https://we.inc/r/app-sidebar-with-nested-groups-and-active-item.json
When to use it
- Your app has more than eight pages and they fall naturally into a few named groups.
- People switch between projects or accounts often and need to see which one is open at a glance.
- You want a sidebar that stays readable without icons, because the labels themselves are specific.
- The left column must scroll on its own while the main area keeps its position.
How to customize it
- Edit the `nav` array: give an entry `items` to make it a group, a `badge` number to show a count, or neither for a plain link.
- Choose which groups start open with the `useState(["Projects", "Billing"])` list.
- Swap the emerald current style, `bg-emerald-50 text-emerald-800`, for your brand colour; keep the text dark enough to pass 4.5 to 1 on the tint.
- Widen the column from `w-60 sm:w-64` if your page names are long, since they truncate rather than wrap.
Accessibility
- Group headers are buttons with aria-expanded and aria-controls, and closed groups use the hidden attribute so their links leave the tab order.
- The selected page carries aria-current="page", and the badge adds a hidden word so it is read as twelve unread rather than a bare number.
- Every link and button is at least 40px tall with a visible emerald focus ring.
Questions
How do I connect this sidebar to React Router or Next.js?
Replace the anchors with your router's Link component and work out the current item from the location, for example by comparing each item's path to the pathname. Drop the local `current` state; the URL becomes the single source of truth.
Should the group containing the current page open automatically?
Yes. On first render, add the parent group of the current path to the open list so people never land on a page whose link is hidden inside a closed group.
Why are group headers buttons and not links?
They only show and hide a list; they do not go anywhere. If a group also has its own overview page, add that page as the first item inside the group instead of making the header do two jobs.
How deep can the nesting go?
Two levels is the comfortable limit in a sidebar. A third level gets cramped and hard to scan, so move deeper structure into tabs or a secondary list on the page itself.
Related: Sidebar Navigation Layouts for React and Tailwind, Collapsible Icon Rail Sidebar That Expands, Underline Tabs With Sliding Indicator.
More tabs and navigation or all free UI components. Build your site free to have a full site built around it.
More in Navigation
- Brass Lift Floor Indicator Navigation with an Arrow That Sweeps an Enamel Dial Between Sections
- Cast Iron Finger Post Navigation with Painted Arms That Swing Round the Post to Point at the Chosen Page
- Cloth Bound Book Spine Chapter Menu Where the Chosen Volume Leaves the Shelf and Opens at Its Chapter
- CSS Tabs Collection with Radio, :target and Accessible ARIA Tabs in Six Styles
- Enamel Platform Sign Breadcrumbs Hanging from Rail Trolleys That Roll Away Swinging When You Step Back Up the Trail
- Glazed Ceramic Tile Tabs in WebGL Leaning on a Sunlit Plaster Shelf Where the Chosen Tile Rocks Upright into the Window Light
- Manila File Folder Tabs in a Steel Desk Rack That Tip Forward like Records in a Crate to Show the Chosen File
- Pagination Components for React, Tailwind and HTML
Product
Who It's For
- For Agencies
- For Freelancers
- For Hosting Providers
- For SaaS Platforms
- For Startups
- For Financial Services
- For Healthcare
Features
Resources
- Blog
- Help Center
- Free Business Tools
- Free UI Components
- Website Templates
- Template Collections
- Website Examples
- Guides
- Comparisons
- Alternatives
- Best Lists
- Reviews
- Pricing Guides
- Use Cases
- Glossary & Guides
- Integrations
- Developers
- Benchmark
- Windows App