React Sidebar Components With Tailwind
Updated 2026-10-28. 5 sidebars.
A React sidebar is a vertical navigation column, usually inside an aside with a nav, that holds the main sections of an app or docs site. The ones below keep state in useState, mark the current page with aria-current, collapse to an icon rail or slide in on phones, and copy as React or plain HTML.
Match the sidebar to how deep your navigation goes. A product with six or seven top areas suits the icon rail, which saves width for tables and charts and expands when someone needs the labels. An admin or SaaS app with sections inside sections wants the nested version, where groups open and close and the active page is always visible.
Documentation is different: readers scan headings, so the docs sidebar uses quiet section titles and a thin line that marks the page you are on. Team tools that juggle several workspaces need the dark variant with a switcher at the top and the signed-in person at the bottom. On phones, any of them can live in the off-canvas pattern.
Compare them
| Variant | Built with | Size (gzipped) | Running cost | Browser support |
|---|---|---|---|---|
| App Sidebar With Nested Groups and Active Item | CSS only | 1.7 KB | Opening a group shows an existing list; the only animation is a 200ms chevron rotation. | All current browsers |
| Collapsible Icon Rail Sidebar That Expands | CSS only | 1.7 KB | Width changes once per click and re-lays out the content beside it; nothing runs at rest. | All current browsers |
| Dark Sidebar With Workspace Switcher and User Footer | CSS only | 1.7 KB | No transitions; switching workspace re-renders a few labels and colours only. | All current browsers |
| Docs Sidebar With Section Headings | CSS only | 1.6 KB | No animation at all; filtering hides list items on each keystroke, which is trivial for a few dozen pages. | All current browsers |
| Mobile Off-Canvas Sidebar With Overlay | CSS only | 1.8 KB | A translateX slide and an overlay fade, 300ms each, only when opened or closed. | All current browsers |
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.
<div class="asg">
<aside class="asg-side">
<div class="asg-brand"><svg width="26" height="26" viewBox="0 0 24 24" aria-hidden="true"><path d="M3 20L12 4l9 16z" fill="#059669"/><path d="M8.5 20L12 13.5 15.5 20z" fill="#fff"/></svg>Brockway Builders</div>
<nav aria-label="Main">
<ul>
<li><a href="#" class="asg-top">Inbox <span class="asg-badge">12<span class="asg-sr"> unread</span></span></a></li>
<li>
<button type="button" class="asg-group" aria-expanded="true" aria-controls="asg-projects">Projects <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M9 6l6 6-6 6"/></svg></button>
<ul id="asg-projects" class="asg-sub"><li><a href="#">Harbour Wall survey</a></li><li><a href="#" aria-current="page">Mill Lane extension</a></li><li><a href="#">Riverside flats</a></li></ul>
</li>
<li>
<button type="button" class="asg-group" aria-expanded="false" aria-controls="asg-team">Team <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M9 6l6 6-6 6"/></svg></button>
<ul id="asg-team" class="asg-sub" hidden><li><a href="#">People</a></li><li><a href="#">Rotas</a></li><li><a href="#">Time off</a></li></ul>
</li>
<li>
<button type="button" class="asg-group" aria-expanded="true" aria-controls="asg-billing">Billing <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M9 6l6 6-6 6"/></svg></button>
<ul id="asg-billing" class="asg-sub"><li><a href="#">Invoices</a></li><li><a href="#">Purchase orders</a></li><li><a href="#">Suppliers</a></li></ul>
</li>
<li><a href="#" class="asg-top">Settings</a></li>
</ul>
</nav>
</aside>
<main class="asg-main"><p>Projects</p><h2 class="asg-title">Mill Lane extension</h2></main>
</div>
<style>
.asg { --asg-accent: #047857; --asg-soft: #ecfdf5; }
.asg { display: flex; height: 540px; width: calc(100% - 32px); max-width: 56rem; margin: 40px auto; overflow: hidden; border: 1px solid #e2e8f0; border-radius: 16px; background: #fff; font-family: ui-sans-serif, system-ui, sans-serif; color: #0f172a; }
.asg :focus-visible { outline: 2px solid var(--asg-accent); outline-offset: 2px; }
.asg-side { flex-shrink: 0; width: 100%; overflow-y: auto; }
@media (min-width: 640px) { .asg-side { width: 256px; border-right: 1px solid #e2e8f0; } }
@media (max-width: 639px) { .asg-main { display: none; } }
.asg-brand { display: flex; align-items: center; gap: 10px; height: 64px; padding: 0 16px; border-bottom: 1px solid #f1f5f9; font-size: 14px; font-weight: 600; }
.asg nav { padding: 12px; }
.asg ul { margin: 0; padding: 0; list-style: none; }
.asg nav a, .asg-group { display: flex; align-items: center; justify-content: space-between; width: 100%; height: 40px; margin-bottom: 2px; padding: 0 12px; box-sizing: border-box; border: 0; border-radius: 8px; background: none; color: #1e293b; font: 500 14px ui-sans-serif, system-ui, sans-serif; text-decoration: none; cursor: pointer; }
.asg nav a:hover, .asg-group:hover { background: #f1f5f9; }
.asg-group svg { width: 16px; height: 16px; fill: none; stroke: #64748b; stroke-width: 2; stroke-linecap: round; transition: transform .2s; }
.asg-group[aria-expanded="true"] svg { transform: rotate(90deg); }
.asg-sub { margin-left: 16px !important; padding-left: 8px !important; border-left: 1px solid #e2e8f0; }
.asg-sub a { font-weight: 400 !important; color: #475569 !important; }
.asg nav a[aria-current="page"] { background: var(--asg-soft); color: var(--asg-accent) !important; font-weight: 600 !important; }
.asg-badge { padding: 2px 8px; border-radius: 999px; background: #059669; color: #fff; font-size: 12px; font-weight: 600; }
.asg-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.asg-main { flex: 1; min-width: 0; padding: 24px; }
.asg-main p { margin: 0; font-size: 12px; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; color: var(--asg-accent); }
.asg-main h2 { margin: 4px 0 0; font-size: 20px; }
@media (prefers-reduced-motion: reduce) { .asg-group svg { transition: none; } }
</style>
<script>
(function () {
document.querySelectorAll(".asg").forEach(function (root) {
root.querySelectorAll(".asg-group").forEach(function (btn) {
btn.addEventListener("click", function () {
var open = btn.getAttribute("aria-expanded") !== "true";
btn.setAttribute("aria-expanded", open);
root.querySelector("#" + btn.getAttribute("aria-controls")).hidden = !open;
});
});
root.querySelectorAll("nav a").forEach(function (a) {
a.addEventListener("click", function (e) {
e.preventDefault();
root.querySelectorAll("nav a[aria-current]").forEach(function (x) { x.removeAttribute("aria-current"); });
a.setAttribute("aria-current", "page");
root.querySelector(".asg-title").textContent = a.firstChild.textContent.trim();
});
});
});
})();
</script>
With reduced motion: Chevrons point the new way at once instead of turning.
Running cost: Opening a group shows an existing list; the only animation is a 200ms chevron rotation.
- 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.
Full page for App Sidebar With Nested Groups and Active Item
Collapsible Icon Rail Sidebar That Expands
A wholesale dashboard with a light sidebar that folds down to a 68px column of icons. Labels and the brand name drop out when it is narrow, each link keeps its name as a tooltip and accessible label, and the active Overview link stays filled in indigo either way. A Collapse button at the foot flips its arrow to show which way it will go.
<div class="sbr">
<aside class="sbr-side">
<div class="sbr-brand"><span class="sbr-logo" aria-hidden="true">K</span><span class="sbr-text">Kestrel Supply</span></div>
<nav aria-label="Main">
<ul>
<li><a href="#" aria-current="page" title="Overview"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4 13h6V4H4zM14 20h6v-9h-6zM14 4v4h6V4zM4 20h6v-3H4z"/></svg><span class="sbr-text">Overview</span></a></li>
<li><a href="#" title="Orders"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M5 7h14l-1.5 12h-11zM9 7a3 3 0 016 0"/></svg><span class="sbr-text">Orders</span></a></li>
<li><a href="#" title="Customers"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M9 11a3.5 3.5 0 100-7 3.5 3.5 0 000 7zM3 20a6 6 0 0112 0"/></svg><span class="sbr-text">Customers</span></a></li>
<li><a href="#" title="Products"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3l8 4.5v9L12 21l-8-4.5v-9zM4 7.5l8 4.5 8-4.5M12 12v9"/></svg><span class="sbr-text">Products</span></a></li>
<li><a href="#" title="Reports"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4 20V10M10 20V4M16 20v-7M22 20H2"/></svg><span class="sbr-text">Reports</span></a></li>
</ul>
</nav>
<button type="button" class="sbr-toggle" aria-expanded="true" aria-label="Collapse sidebar"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M15 6l-6 6 6 6"/></svg><span class="sbr-text">Collapse</span></button>
</aside>
<main class="sbr-main">
<h2>Overview</h2><p>Week of 20 October</p>
<div class="sbr-stats"><div><small>Orders</small><b>184</b></div><div><small>Revenue</small><b>$12,480</b></div><div><small>Returns</small><b>3</b></div></div>
</main>
</div>
<style>
.sbr { --sbr-accent: #4f46e5; --sbr-wide: 208px; --sbr-narrow: 68px; }
.sbr { display: flex; height: 460px; width: calc(100% - 32px); max-width: 56rem; margin: 40px auto; overflow: hidden; border: 1px solid #e2e8f0; border-radius: 16px; background: #fff; font-family: ui-sans-serif, system-ui, sans-serif; color: #0f172a; }
.sbr :focus-visible { outline: 2px solid var(--sbr-accent); outline-offset: 2px; }
.sbr-side { display: flex; flex-direction: column; flex-shrink: 0; width: var(--sbr-wide); box-sizing: border-box; padding: 12px; border-right: 1px solid #e2e8f0; background: #f8fafc; transition: width .2s ease-out; }
.sbr[data-collapsed] .sbr-side { width: var(--sbr-narrow); }
.sbr[data-collapsed] .sbr-text { display: none; }
.sbr-brand { display: flex; align-items: center; gap: 8px; height: 40px; padding: 0 8px; font-size: 14px; font-weight: 600; white-space: nowrap; }
.sbr-logo { display: grid; place-items: center; flex-shrink: 0; width: 28px; height: 28px; border-radius: 8px; background: var(--sbr-accent); color: #fff; font-size: 12px; font-weight: 700; }
.sbr nav { flex: 1; margin-top: 16px; }
.sbr ul { margin: 0; padding: 0; list-style: none; }
.sbr nav a, .sbr-toggle { display: flex; align-items: center; gap: 12px; width: 100%; height: 40px; margin-bottom: 4px; padding: 0 12px; box-sizing: border-box; border: 0; border-radius: 8px; background: none; color: #334155; font: 500 14px ui-sans-serif, system-ui, sans-serif; text-decoration: none; white-space: nowrap; cursor: pointer; }
.sbr nav a:hover, .sbr-toggle:hover { background: #e2e8f0; }
.sbr nav a[aria-current="page"] { background: var(--sbr-accent); color: #fff; }
.sbr svg { flex-shrink: 0; width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.sbr-toggle svg { transition: transform .2s; }
.sbr[data-collapsed] .sbr-toggle svg { transform: rotate(180deg); }
.sbr-main { flex: 1; min-width: 0; padding: 16px; }
.sbr-main h2 { margin: 0; font-size: 18px; }
.sbr-main > p { margin: 4px 0 0; font-size: 14px; color: #475569; }
.sbr-stats { display: grid; gap: 16px; margin-top: 24px; }
@media (min-width: 640px) { .sbr-stats { grid-template-columns: repeat(3, 1fr); } }
.sbr-stats div { padding: 16px; border: 1px solid #e2e8f0; border-radius: 12px; }
.sbr-stats small { display: block; font-size: 14px; color: #475569; }
.sbr-stats b { font-size: 20px; font-variant-numeric: tabular-nums; }
@media (prefers-reduced-motion: reduce) { .sbr-side, .sbr-toggle svg { transition: none; } }
</style>
<script>
(function () {
document.querySelectorAll(".sbr").forEach(function (root) {
var btn = root.querySelector(".sbr-toggle");
btn.addEventListener("click", function () {
var open = btn.getAttribute("aria-expanded") !== "true";
btn.setAttribute("aria-expanded", open);
btn.setAttribute("aria-label", open ? "Collapse sidebar" : "Expand sidebar");
root.toggleAttribute("data-collapsed", !open);
});
});
})();
</script>
With reduced motion: The sidebar snaps between its wide and narrow widths without easing.
Running cost: Width changes once per click and re-lays out the content beside it; nothing runs at rest.
- Change the two widths with `w-52` and `w-[68px]`, or `--sbr-wide` and `--sbr-narrow` in the plain version.
- Mark a different page as current by moving `current: true` in `links`, or `aria-current` in the HTML.
- Start it collapsed by setting `useState(false)`, which suits data-heavy screens.
Dark Sidebar With Workspace Switcher and User Footer
A charcoal sidebar for a small record label's back office. The top row names the open workspace and its plan; pressing it lists the other workspaces with a coloured dot and a tick on the current one, plus a link to create a new one. Five page links fill the middle, and the signed-in person with an account menu sits at the foot.
<div class="dsw">
<aside class="dsw-side">
<button type="button" class="dsw-trigger" aria-expanded="true" aria-controls="dsw-list">
<span class="dsw-mark" aria-hidden="true">L</span>
<span class="dsw-who"><b class="dsw-name">Lumen Records</b><small>Team plan</small></span>
<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M8 9l4-4 4 4M8 15l4 4 4-4"/></svg>
</button>
<div class="dsw-list" id="dsw-list">
<p>Switch workspace</p>
<ul>
<li><button type="button" aria-pressed="true" data-color="#8b5cf6">Lumen Records</button></li>
<li><button type="button" aria-pressed="false" data-color="#f59e0b">Fieldnote Press</button></li>
<li><button type="button" aria-pressed="false" data-color="#0ea5e9">Saltmarsh Radio</button></li>
</ul>
</div>
<nav aria-label="Main">
<ul>
<li><a href="#" aria-current="page">Releases</a></li><li><a href="#">Artists</a></li><li><a href="#">Royalties</a></li><li><a href="#">Calendar</a></li><li><a href="#">Files</a></li>
</ul>
</nav>
<div class="dsw-user">
<span class="dsw-avatar" aria-hidden="true">IM</span>
<span class="dsw-who"><b>Imani Mensah</b><small>[email protected]</small></span>
</div>
</aside>
</div>
<style>
.dsw { --dsw-accent: #a78bfa; display: flex; height: 540px; width: calc(100% - 32px); max-width: 20rem; margin: 40px auto; overflow: hidden; border-radius: 16px; font-family: ui-sans-serif, system-ui, sans-serif; }
.dsw :focus-visible { outline: 2px solid var(--dsw-accent); outline-offset: 2px; }
.dsw-side { position: relative; display: flex; flex-direction: column; width: 100%; padding: 12px; background: #18181b; color: #f4f4f5; }
.dsw-trigger { display: flex; align-items: center; gap: 12px; width: 100%; height: 56px; padding: 0 8px; border: 0; border-radius: 12px; background: none; color: inherit; text-align: left; cursor: pointer; font: inherit; }
.dsw-trigger:hover { background: #27272a; }
.dsw-trigger svg { width: 16px; height: 16px; fill: none; stroke: #a1a1aa; stroke-width: 2; stroke-linecap: round; }
.dsw-mark { display: grid; place-items: center; flex-shrink: 0; width: 36px; height: 36px; border-radius: 8px; background: #8b5cf6; color: #09090b; font-weight: 700; font-size: 14px; }
.dsw-who { flex: 1; min-width: 0; }
.dsw-who b, .dsw-who small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dsw-who b { font-size: 14px; }
.dsw-who small { font-size: 12px; color: #a1a1aa; }
.dsw-list { margin-top: 8px; padding: 6px; border-radius: 12px; background: #27272a; box-shadow: 0 20px 40px rgba(0,0,0,.4), 0 0 0 1px rgba(255,255,255,.1); }
.dsw-list p { margin: 0; padding: 6px 10px; font-size: 12px; color: #a1a1aa; }
.dsw ul { margin: 0; padding: 0; list-style: none; }
.dsw-list button { display: flex; align-items: center; gap: 10px; width: 100%; height: 40px; padding: 0 10px; border: 0; border-radius: 8px; background: none; color: inherit; font: 14px ui-sans-serif, system-ui, sans-serif; cursor: pointer; }
.dsw-list button:hover { background: #3f3f46; }
.dsw-list button::before { content: ""; width: 10px; height: 10px; border-radius: 50%; background: var(--dot, #8b5cf6); }
.dsw-list button[aria-pressed="true"]::after { content: "Current"; margin-left: auto; font-size: 12px; color: var(--dsw-accent); }
.dsw nav { flex: 1; margin-top: 16px; }
.dsw nav a { display: flex; align-items: center; height: 40px; margin-bottom: 2px; padding: 0 12px; border-radius: 8px; color: #d4d4d8; font-size: 14px; text-decoration: none; }
.dsw nav a:hover { background: rgba(255,255,255,.05); color: #fff; }
.dsw nav a[aria-current="page"] { background: rgba(255,255,255,.1); color: #fff; font-weight: 500; }
.dsw-user { display: flex; align-items: center; gap: 12px; padding: 12px 8px 0; border-top: 1px solid rgba(255,255,255,.1); }
.dsw-avatar { display: grid; place-items: center; width: 36px; height: 36px; border-radius: 50%; background: linear-gradient(135deg, #fb7185, #fdba74); color: #18181b; font-size: 14px; font-weight: 600; }
</style>
<script>
(function () {
document.querySelectorAll(".dsw").forEach(function (root) {
var trigger = root.querySelector(".dsw-trigger"), list = root.querySelector(".dsw-list");
function set(open) { trigger.setAttribute("aria-expanded", open); list.hidden = !open; }
list.querySelectorAll("button").forEach(function (b) {
b.style.setProperty("--dot", b.dataset.color);
b.addEventListener("click", function () {
list.querySelectorAll("button").forEach(function (x) { x.setAttribute("aria-pressed", x === b); });
root.querySelector(".dsw-name").textContent = b.textContent;
root.querySelector(".dsw-mark").textContent = b.textContent[0];
root.querySelector(".dsw-mark").style.background = b.dataset.color;
set(false); trigger.focus();
});
});
trigger.addEventListener("click", function () { set(list.hidden); });
document.addEventListener("keydown", function (e) { if (e.key === "Escape" && !list.hidden) { set(false); trigger.focus(); } });
root.querySelectorAll("nav a").forEach(function (a) {
a.addEventListener("click", function (e) {
e.preventDefault();
root.querySelectorAll("nav [aria-current]").forEach(function (x) { x.removeAttribute("aria-current"); });
a.setAttribute("aria-current", "page");
});
});
});
})();
</script>
With reduced motion: Nothing animates; the switcher list simply appears and hides.
Running cost: No transitions; switching workspace re-renders a few labels and colours only.
- Add workspaces to `workspaces`; `color` sets both the tile behind the initial and the dot in the list.
- Lighten the column by swapping `bg-zinc-900` for `bg-zinc-800`, keeping link text at `text-zinc-300` or lighter for contrast.
- Replace the gradient initials circle with an `<img>` avatar of the same `h-9 w-9 rounded-full` size.
Docs Sidebar With Section Headings
A documentation index for a job-scheduling tool: small uppercase section headings, pages listed against a hairline, and a sky blue bar marking the page being read. A filter box above narrows the list as you type and says so when nothing matches. On phones the sidebar takes the full width and the article is left to its own page.
<div class="dss">
<aside class="dss-side">
<p class="dss-logo">tidepool<span>/docs</span></p>
<label for="dss-q" class="dss-sr">Filter pages</label>
<input id="dss-q" class="dss-q" type="search" placeholder="Filter pages">
<nav aria-label="Documentation">
<div class="dss-sec"><h3>Getting started</h3><ul><li><a href="#">Introduction</a></li><li><a href="#">Install the CLI</a></li><li><a href="#">Your first sync</a></li></ul></div>
<div class="dss-sec"><h3>Guides</h3><ul><li><a href="#">Scheduling jobs</a></li><li><a href="#" aria-current="page">Retries and backoff</a></li><li><a href="#">Webhooks</a></li><li><a href="#">Secrets</a></li></ul></div>
<div class="dss-sec"><h3>Reference</h3><ul><li><a href="#">Config file</a></li><li><a href="#">CLI commands</a></li><li><a href="#">REST API</a></li></ul></div>
<p class="dss-none" hidden>No pages match.</p>
</nav>
</aside>
<article class="dss-doc">
<p>Guides</p>
<h2 class="dss-title">Retries and backoff</h2>
<p>A failed job is tried again up to five times, waiting longer after each attempt. Set the limit and the first delay in your config file.</p>
</article>
</div>
<style>
.dss { --dss-accent: #0284c7; }
.dss { display: flex; height: 560px; width: calc(100% - 32px); max-width: 64rem; margin: 40px auto; overflow: hidden; border: 1px solid #e2e8f0; border-radius: 12px; background: #fff; font-family: ui-sans-serif, system-ui, sans-serif; color: #0f172a; }
.dss :focus-visible { outline: 2px solid var(--dss-accent); outline-offset: 2px; }
.dss-side { flex-shrink: 0; width: 100%; box-sizing: border-box; padding: 20px 16px; overflow-y: auto; }
@media (min-width: 640px) { .dss-side { width: 256px; border-right: 1px solid #e2e8f0; } }
.dss-logo { margin: 0; padding: 0 8px; font: 600 14px ui-monospace, Menlo, monospace; }
.dss-logo span { color: var(--dss-accent); }
.dss-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.dss-q { width: 100%; height: 40px; margin-top: 16px; padding: 0 12px; box-sizing: border-box; border: 1px solid #cbd5e1; border-radius: 6px; background: #f8fafc; font: 14px ui-sans-serif, system-ui, sans-serif; }
.dss-sec { margin-top: 24px; }
.dss-sec h3 { margin: 0; padding: 0 8px; font-size: 12px; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; color: #64748b; }
.dss-sec ul { margin: 8px 0 0; padding: 0; list-style: none; border-left: 1px solid #e2e8f0; }
.dss-sec a { display: flex; align-items: center; min-height: 40px; margin-left: -1px; padding-left: 16px; border-left: 2px solid transparent; color: #475569; font-size: 14px; text-decoration: none; }
.dss-sec a:hover { border-color: #94a3b8; color: #0f172a; }
.dss-sec a[aria-current="page"] { border-color: var(--dss-accent); color: #0369a1; font-weight: 500; }
.dss-none { padding: 0 8px; font-size: 14px; color: #475569; }
.dss-doc { display: none; flex: 1; min-width: 0; padding: 32px 40px; }
@media (min-width: 640px) { .dss-doc { display: block; } }
.dss-doc p { color: #475569; line-height: 1.75; }
.dss-doc p:first-child { margin: 0; font-size: 14px; }
.dss-doc h2 { margin: 4px 0 0; font-size: 30px; letter-spacing: -.02em; }
</style>
<script>
(function () {
document.querySelectorAll(".dss").forEach(function (root) {
var q = root.querySelector(".dss-q"), none = root.querySelector(".dss-none");
root.querySelectorAll(".dss-sec a").forEach(function (a) {
a.addEventListener("click", function (e) {
e.preventDefault();
root.querySelectorAll("[aria-current]").forEach(function (x) { x.removeAttribute("aria-current"); });
a.setAttribute("aria-current", "page");
root.querySelector(".dss-title").textContent = a.textContent;
});
});
q.addEventListener("input", function () {
var term = q.value.trim().toLowerCase(), any = false;
root.querySelectorAll(".dss-sec").forEach(function (sec) {
var shown = 0;
sec.querySelectorAll("li").forEach(function (li) {
var hit = li.textContent.toLowerCase().indexOf(term) !== -1;
li.hidden = !hit; if (hit) shown++;
});
sec.hidden = !shown; if (shown) any = true;
});
none.hidden = any;
});
});
})();
</script>
With reduced motion: Nothing animates; the current-page line and focus rings still show.
Running cost: No animation at all; filtering hides list items on each keystroke, which is trivial for a few dozen pages.
- Edit `sections` to match your docs; headings stay as plain text and only pages are links.
- Recolour the current-page bar through `border-sky-600 text-sky-700` or `--dss-accent`.
- Remove the filter input and its state if your docs have fewer than fifteen pages.
Mobile Off-Canvas Sidebar With Overlay
A meal-planning app on a phone screen whose sidebar slides in from the left over a darkened page. Links show item counts, the current list is filled in orange, and choosing a link updates the page title and closes the sidebar. The close button sits where the thumb already is, and a short tip fills the bottom.
<div class="moc-stage">
<div class="moc">
<header class="moc-head">
<button type="button" class="moc-open" aria-expanded="true" aria-controls="moc-side" aria-label="Open sidebar">
<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4 6h16M4 12h16M4 18h16"/></svg>
</button>
<h2 class="moc-title">Upcoming</h2>
</header>
<ul class="moc-meals" aria-label="Meals">
<li><small>Mon</small>Miso salmon with greens</li>
<li><small>Tue</small>Lentil and squash dal</li>
<li><small>Wed</small>Lemon orzo with peas</li>
</ul>
<div class="moc-scrim" aria-hidden="true"></div>
<aside class="moc-side" id="moc-side" role="dialog" aria-modal="true" aria-label="Sidebar">
<div class="moc-top">
<span>Sunday Prep</span>
<button type="button" class="moc-close" aria-label="Close sidebar"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M15 6l-6 6 6 6"/></svg></button>
</div>
<nav aria-label="Main">
<ul>
<li><a href="#">Today <small>4</small></a></li>
<li><a href="#" aria-current="page">Upcoming <small>11</small></a></li>
<li><a href="#">Recipes</a></li>
<li><a href="#">Shopping list <small>7</small></a></li>
<li><a href="#">Pantry</a></li>
</ul>
</nav>
<p class="moc-tip">Plan five dinners and the shopping list fills itself.</p>
</aside>
</div>
</div>
<style>
.moc-stage { display: flex; justify-content: center; padding: 40px 16px; background: #fff7ed; font-family: ui-sans-serif, system-ui, sans-serif; }
.moc { --moc-accent: #ea580c; position: relative; width: 100%; max-width: 360px; height: 580px; overflow: hidden; border: 1px solid #fed7aa; border-radius: 32px; background: #fff; color: #1c1917; }
.moc :focus-visible { outline: 2px solid var(--moc-accent); outline-offset: 2px; }
.moc svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.moc-head { display: flex; align-items: center; gap: 8px; height: 56px; padding: 0 12px; border-bottom: 1px solid #ffedd5; }
.moc-head h2 { margin: 0; font-size: 16px; }
.moc-open, .moc-close { display: grid; place-items: center; width: 44px; height: 44px; border: 0; border-radius: 12px; background: none; color: inherit; cursor: pointer; }
.moc-meals { margin: 0; padding: 16px; list-style: none; }
.moc-meals li { display: flex; align-items: center; gap: 12px; margin-bottom: 8px; padding: 12px; border-radius: 12px; background: #fff7ed; font-size: 14px; }
.moc-meals small { font-size: 12px; font-weight: 600; color: #9a3412; }
.moc-scrim { position: absolute; inset: 0; background: rgba(28,25,23,.5); transition: opacity .3s; }
.moc-side { position: absolute; top: 0; bottom: 0; left: 0; display: flex; flex-direction: column; width: 78%; background: #fafaf9; transition: transform .3s ease-out, visibility .3s; }
.moc:not([data-open]) .moc-scrim { opacity: 0; pointer-events: none; }
.moc:not([data-open]) .moc-side { transform: translateX(-100%); visibility: hidden; }
.moc-top { display: flex; align-items: center; justify-content: space-between; height: 56px; padding: 0 12px 0 16px; font-weight: 700; color: #c2410c; }
.moc-close { color: #57534e; }
.moc nav { flex: 1; padding: 0 12px; }
.moc nav ul { margin: 0; padding: 0; list-style: none; }
.moc nav a { display: flex; align-items: center; justify-content: space-between; height: 44px; margin-bottom: 4px; padding: 0 12px; border-radius: 12px; color: #292524; font-size: 15px; text-decoration: none; }
.moc nav a:hover { background: #e7e5e4; }
.moc nav a[aria-current="page"] { background: var(--moc-accent); color: #fff; font-weight: 500; }
.moc nav small { font-size: 12px; color: inherit; opacity: .85; }
.moc-tip { margin: 12px; padding: 12px; border-radius: 12px; background: #ffedd5; color: #7c2d12; font-size: 12px; line-height: 1.6; }
@media (prefers-reduced-motion: reduce) { .moc-scrim, .moc-side { transition: none; } }
</style>
<script>
(function () {
document.querySelectorAll(".moc").forEach(function (root) {
var opener = root.querySelector(".moc-open");
function set(open) {
root.toggleAttribute("data-open", open);
opener.setAttribute("aria-expanded", open);
if (open) root.querySelector(".moc-close").focus(); else opener.focus();
}
root.toggleAttribute("data-open", true);
opener.addEventListener("click", function () { set(true); });
root.querySelector(".moc-close").addEventListener("click", function () { set(false); });
root.querySelector(".moc-scrim").addEventListener("click", function () { set(false); });
document.addEventListener("keydown", function (e) { if (e.key === "Escape" && root.hasAttribute("data-open")) set(false); });
root.querySelectorAll("nav a").forEach(function (a) {
a.addEventListener("click", function (e) {
e.preventDefault();
root.querySelectorAll("nav [aria-current]").forEach(function (x) { x.removeAttribute("aria-current"); });
a.setAttribute("aria-current", "page");
root.querySelector(".moc-title").textContent = a.firstChild.textContent.trim();
set(false);
});
});
});
})();
</script>
With reduced motion: The sidebar and its overlay appear in place without sliding or fading.
Running cost: A translateX slide and an overlay fade, 300ms each, only when opened or closed.
- Set how much page stays visible with `w-[78%]`; leave at least a fifth so people see there is a page behind.
- Change the accent from `bg-orange-600` or `--moc-accent`; counts on the active row use `text-orange-50`.
- Delete the `close()` call in the link click handler if you want the sidebar to stay open after choosing a page.
Performance notes
Collapsing a sidebar changes its width, which does make the browser lay out the content next to it again. That happens once per click, not on every frame, and the rail version keeps the transition short so it never competes with scrolling. If your main area is heavy, animate a transform on the sidebar and leave the content width fixed instead.
Nested groups render all their links up front and only hide the closed ones, so opening a group never waits on data. The mobile drawer slides with translateX over a fading backdrop, both composited, and its Escape listener exists only while it is open.
Questions
How do I make a collapsible sidebar in React?
Hold a boolean in useState, switch the aside between a wide and a narrow width class, and hide the text labels when it is narrow while keeping them as aria-label or title on each link. Put the toggle in a button with aria-expanded so the state is announced.
Should a sidebar use nav, aside or both?
Use a nav with an aria-label such as Main for the links themselves. Wrapping it in an aside is fine when the column also holds other things, like a workspace switcher or a profile, that are not navigation.
How do I mark the current page in a sidebar?
Put aria-current="page" on the active link and style from it, so screen reader users hear which page they are on and the highlight cannot drift from the real state. In a router, set it from the current path.
Can I build this as a shadcn-style sidebar?
The structure is close: a provider-free component with a header, scrollable groups and a footer. These versions have no dependency on any component library, so you can paste them into a project that uses shadcn/ui or one that uses nothing at all.
What should happen to the sidebar on mobile?
Hide it off screen and open it from a menu button as a drawer over the page, with a backdrop, Escape to close and focus moved inside. A permanently visible sidebar on a 375 pixel screen leaves too little room for content.
Also in the gallery: Sidebar Navigation Layouts for React and Tailwind.
More collections: Hamburger Menu in HTML and CSS: 5 Menus to Copy, React Tab Components: 5 Accessible Tab Styles, Stepper Components for Multi-Step Forms. All tabs and navigation. Build your site free to have a full site built around them.
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