Collapsible sidebar
An app sidebar that folds from labels to an icon rail, with an expandable project group, unread badges and tooltips when collapsed.
"use client";
import { useState, type ReactNode } from "react";
import "./sidebar-nav.css";
export type SidebarItem = {
id: string;
label: string;
icon: ReactNode;
badge?: number;
/** Sub-links shown in an expandable group. */
children?: { id: string; label: string; color?: string }[];
};
type SidebarNavProps = {
workspace: string;
items: SidebarItem[];
footer?: SidebarItem[];
initial?: string;
theme?: "light" | "dark";
/** Whatever the app shows next to the sidebar. */
children?: ReactNode;
};
/**
* SidebarNav
* - Toggle between a 248px sidebar with labels and a 72px icon rail.
* - Groups (like Projects) expand with a smooth grid-rows animation.
* - In the rail, labels become tooltips on hover and keyboard focus.
* - Under 520px of width it always shows the rail.
*/
export function SidebarNav({
workspace,
items,
footer = [],
initial,
theme = "light",
children,
}: SidebarNavProps) {
const [collapsed, setCollapsed] = useState(false);
const [active, setActive] = useState(initial ?? items[0]?.id);
const [expanded, setExpanded] = useState<string[]>(
items.filter((i) => i.children).map((i) => i.id),
);
const toggleGroup = (id: string) =>
setExpanded((e) =>
e.includes(id) ? e.filter((x) => x !== id) : [...e, id],
);
const renderItem = (item: SidebarItem) => {
const open = expanded.includes(item.id);
return (
<li key={item.id}>
<button
type="button"
className="gu-side__link"
aria-current={active === item.id ? "page" : undefined}
aria-expanded={item.children ? open : undefined}
onClick={() =>
item.children ? toggleGroup(item.id) : setActive(item.id)
}
>
<span className="gu-side__icon" aria-hidden="true">
{item.icon}
</span>
<span className="gu-side__text">{item.label}</span>
{item.badge ? (
<span className="gu-side__badge">{item.badge}</span>
) : null}
{item.children && (
<span className="gu-side__chev" aria-hidden="true" />
)}
<span className="gu-side__tip" aria-hidden="true">
{item.label}
</span>
</button>
{item.children && (
<div className="gu-side__group" data-open={open || undefined}>
<ul>
{item.children.map((c) => (
<li key={c.id}>
<button
type="button"
className="gu-side__sub"
aria-current={active === c.id ? "page" : undefined}
onClick={() => setActive(c.id)}
tabIndex={open ? 0 : -1}
>
<span
className="gu-side__dot"
style={{ background: c.color }}
aria-hidden="true"
/>
<span className="gu-side__text">{c.label}</span>
</button>
</li>
))}
</ul>
</div>
)}
</li>
);
};
return (
<div
className="gu-side"
data-theme={theme}
data-collapsed={collapsed || undefined}
>
<nav className="gu-side__nav" aria-label="App">
<div className="gu-side__head">
<span className="gu-side__logo" aria-hidden="true">
{workspace[0]}
</span>
<span className="gu-side__text gu-side__ws">{workspace}</span>
<button
type="button"
className="gu-side__toggle"
onClick={() => setCollapsed((c) => !c)}
aria-label={collapsed ? "Expand sidebar" : "Collapse sidebar"}
aria-pressed={collapsed}
>
<svg viewBox="0 0 20 20" aria-hidden="true">
<rect
x="2.5"
y="3.5"
width="15"
height="13"
rx="3"
fill="none"
stroke="currentColor"
strokeWidth="1.6"
/>
<path d="M8 4v12" stroke="currentColor" strokeWidth="1.6" />
</svg>
</button>
</div>
<ul className="gu-side__list">{items.map(renderItem)}</ul>
<ul className="gu-side__list gu-side__foot">
{footer.map(renderItem)}
</ul>
</nav>
<div className="gu-side__main">{children}</div>
</div>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Collapsible sidebar
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
An app-style sidebar that collapses from a 248px panel with labels into a 72px icon rail. It has a workspace header, nav items with unread badges, an expandable "Projects" group with colored dots, a settings item pinned to the bottom, and tooltips when collapsed.
## Layout and look
- Frame: rounded 20px app shell, sidebar background #fbf8f2 (dark #18181b) with a 1px divider, app area #f3eee3 (dark #0e0e10).
- Header: 36px orange (#ff5a1f) rounded logo tile with the workspace initial, workspace name (weight 800), and a small panel-toggle icon button on the right.
- Items: 42px tall, radius 12px, 20px line icons, 15px weight 600 labels. Active item: solid #0e0e10 with cream text (inverted in dark). Badges: orange pills.
- Group: chevron that rotates; sub-items indented with 9px colored square dots.
## Behavior and motion
1. The toggle animates the sidebar width (450ms, cubic-bezier(0.16, 1, 0.3, 1)); labels fade to 0 and collapse, icons center, badges move to the icon's corner.
2. Groups expand and collapse by animating grid-template-rows from 0fr to 1fr (400ms).
3. In the rail, hovering or focusing an item shows a dark tooltip to its right (fade + 4px slide).
4. When the component's container is under 520px wide, it always shows the rail.
## Touch
All tap-driven. Every icon has an accessible name, so the rail is usable without tooltips.
## Accessibility
- <nav aria-label="App">; the current item has aria-current="page"; groups use aria-expanded; the toggle has aria-pressed and a changing label.
- Hidden sub-items leave the tab order.
- prefers-reduced-motion: width and group changes snap.
## Props
workspace, items ({ id, label, icon, badge?, children?: { id, label, color? }[] }[]), footer (same shape), initial (active id), theme, children (the app content).
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### SidebarNav.tsx
```tsx
"use client";
import { useState, type ReactNode } from "react";
import "./sidebar-nav.css";
export type SidebarItem = {
id: string;
label: string;
icon: ReactNode;
badge?: number;
/** Sub-links shown in an expandable group. */
children?: { id: string; label: string; color?: string }[];
};
type SidebarNavProps = {
workspace: string;
items: SidebarItem[];
footer?: SidebarItem[];
initial?: string;
theme?: "light" | "dark";
/** Whatever the app shows next to the sidebar. */
children?: ReactNode;
};
/**
* SidebarNav
* - Toggle between a 248px sidebar with labels and a 72px icon rail.
* - Groups (like Projects) expand with a smooth grid-rows animation.
* - In the rail, labels become tooltips on hover and keyboard focus.
* - Under 520px of width it always shows the rail.
*/
export function SidebarNav({
workspace,
items,
footer = [],
initial,
theme = "light",
children,
}: SidebarNavProps) {
const [collapsed, setCollapsed] = useState(false);
const [active, setActive] = useState(initial ?? items[0]?.id);
const [expanded, setExpanded] = useState<string[]>(
items.filter((i) => i.children).map((i) => i.id),
);
const toggleGroup = (id: string) =>
setExpanded((e) =>
e.includes(id) ? e.filter((x) => x !== id) : [...e, id],
);
const renderItem = (item: SidebarItem) => {
const open = expanded.includes(item.id);
return (
<li key={item.id}>
<button
type="button"
className="gu-side__link"
aria-current={active === item.id ? "page" : undefined}
aria-expanded={item.children ? open : undefined}
onClick={() =>
item.children ? toggleGroup(item.id) : setActive(item.id)
}
>
<span className="gu-side__icon" aria-hidden="true">
{item.icon}
</span>
<span className="gu-side__text">{item.label}</span>
{item.badge ? (
<span className="gu-side__badge">{item.badge}</span>
) : null}
{item.children && (
<span className="gu-side__chev" aria-hidden="true" />
)}
<span className="gu-side__tip" aria-hidden="true">
{item.label}
</span>
</button>
{item.children && (
<div className="gu-side__group" data-open={open || undefined}>
<ul>
{item.children.map((c) => (
<li key={c.id}>
<button
type="button"
className="gu-side__sub"
aria-current={active === c.id ? "page" : undefined}
onClick={() => setActive(c.id)}
tabIndex={open ? 0 : -1}
>
<span
className="gu-side__dot"
style={{ background: c.color }}
aria-hidden="true"
/>
<span className="gu-side__text">{c.label}</span>
</button>
</li>
))}
</ul>
</div>
)}
</li>
);
};
return (
<div
className="gu-side"
data-theme={theme}
data-collapsed={collapsed || undefined}
>
<nav className="gu-side__nav" aria-label="App">
<div className="gu-side__head">
<span className="gu-side__logo" aria-hidden="true">
{workspace[0]}
</span>
<span className="gu-side__text gu-side__ws">{workspace}</span>
<button
type="button"
className="gu-side__toggle"
onClick={() => setCollapsed((c) => !c)}
aria-label={collapsed ? "Expand sidebar" : "Collapse sidebar"}
aria-pressed={collapsed}
>
<svg viewBox="0 0 20 20" aria-hidden="true">
<rect
x="2.5"
y="3.5"
width="15"
height="13"
rx="3"
fill="none"
stroke="currentColor"
strokeWidth="1.6"
/>
<path d="M8 4v12" stroke="currentColor" strokeWidth="1.6" />
</svg>
</button>
</div>
<ul className="gu-side__list">{items.map(renderItem)}</ul>
<ul className="gu-side__list gu-side__foot">
{footer.map(renderItem)}
</ul>
</nav>
<div className="gu-side__main">{children}</div>
</div>
);
}
```
### sidebar-nav.css
```css
/* SidebarNav: change these colors to match your brand. */
.gu-side {
--bg: #f3eee3; /* app background */
--rail: #fbf8f2; /* sidebar */
--text: #0e0e10;
--muted: #57534b;
--hover: rgb(14 14 16 / 0.06);
--active: #0e0e10;
--active-text: #f3eee3;
--badge: #ff5a1f;
--badge-text: #0e0e10;
--line: rgb(14 14 16 / 0.1);
--focus: #2b35ff;
--ease: cubic-bezier(0.16, 1, 0.3, 1);
container-type: inline-size;
display: flex;
width: min(100%, 860px);
height: 460px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 20px;
background: var(--bg);
color: var(--text);
font-family: system-ui, sans-serif;
}
.gu-side[data-theme="dark"] {
--bg: #0e0e10;
--rail: #18181b;
--text: #f3eee3;
--muted: #aea89c;
--hover: rgb(243 238 227 / 0.07);
--active: #f3eee3;
--active-text: #0e0e10;
--line: rgb(243 238 227 / 0.1);
--focus: #d7ff3a;
}
.gu-side button {
font: inherit;
color: inherit;
}
.gu-side button:focus-visible {
outline: 2px solid var(--focus);
outline-offset: -2px;
}
.gu-side__nav {
display: flex;
flex: none;
flex-direction: column;
width: 248px;
padding: 12px;
border-right: 1px solid var(--line);
background: var(--rail);
transition: width 0.45s var(--ease);
}
.gu-side[data-collapsed] .gu-side__nav {
width: 72px;
}
.gu-side__head {
display: flex;
align-items: center;
gap: 10px;
min-height: 44px;
margin-bottom: 14px;
padding-left: 4px;
}
.gu-side__logo {
display: grid;
flex: none;
place-items: center;
width: 36px;
height: 36px;
border-radius: 11px;
background: var(--badge);
color: var(--badge-text);
font-weight: 900;
}
.gu-side__ws {
font-weight: 800;
letter-spacing: -0.02em;
}
.gu-side__toggle {
display: grid;
flex: none;
place-items: center;
width: 36px;
height: 36px;
margin-left: auto;
border: 0;
border-radius: 10px;
background: none;
color: var(--muted) !important;
cursor: pointer;
}
.gu-side__toggle:hover {
background: var(--hover);
}
.gu-side__toggle svg {
width: 20px;
height: 20px;
}
/* In the rail the toggle drops under the logo */
.gu-side[data-collapsed] .gu-side__head {
flex-direction: column;
padding-left: 0;
}
.gu-side[data-collapsed] .gu-side__toggle {
margin-left: 0;
}
.gu-side__list {
display: grid;
gap: 2px;
margin: 0;
padding: 0;
list-style: none;
}
.gu-side__foot {
margin-top: auto;
}
.gu-side__link,
.gu-side__sub {
position: relative;
display: flex;
align-items: center;
gap: 12px;
width: 100%;
min-height: 42px;
padding: 0 10px;
border: 0;
border-radius: 12px;
background: none;
font-size: 0.92rem;
font-weight: 600;
text-align: left;
white-space: nowrap;
cursor: pointer;
transition:
background-color 0.2s,
color 0.2s;
}
.gu-side__link:hover,
.gu-side__sub:hover {
background: var(--hover);
}
.gu-side__link[aria-current="page"],
.gu-side__sub[aria-current="page"] {
background: var(--active);
color: var(--active-text);
}
.gu-side__icon {
display: grid;
flex: none;
place-items: center;
width: 28px;
}
.gu-side__icon svg {
width: 20px;
height: 20px;
}
.gu-side__text {
overflow: hidden;
transition: opacity 0.25s;
}
.gu-side__badge {
margin-left: auto;
padding: 2px 7px;
border-radius: 999px;
background: var(--badge);
color: var(--badge-text);
font-size: 0.72rem;
font-weight: 800;
}
.gu-side__chev {
width: 7px;
height: 7px;
margin: -3px 4px 0 auto;
border-right: 2px solid currentColor;
border-bottom: 2px solid currentColor;
transform: rotate(45deg);
transition: transform 0.3s var(--ease);
}
.gu-side__link[aria-expanded="false"] .gu-side__chev {
transform: rotate(-45deg);
}
/* Expandable group: animates height with grid-template-rows */
.gu-side__group {
display: grid;
grid-template-rows: 0fr;
transition: grid-template-rows 0.4s var(--ease);
}
.gu-side__group[data-open] {
grid-template-rows: 1fr;
}
.gu-side__group > ul {
min-height: 0;
overflow: hidden;
margin: 0;
padding: 0 0 0 22px;
list-style: none;
}
.gu-side__sub {
min-height: 36px;
font-weight: 500;
}
.gu-side__dot {
flex: none;
width: 9px;
height: 9px;
margin: 0 5px;
border-radius: 3px;
background: var(--muted);
}
/* Collapsed rail: hide words, center icons, show tooltips */
.gu-side[data-collapsed] .gu-side__text,
.gu-side[data-collapsed] .gu-side__chev {
width: 0;
opacity: 0;
}
.gu-side[data-collapsed] .gu-side__link {
justify-content: center;
padding: 0;
}
.gu-side[data-collapsed] .gu-side__badge {
position: absolute;
top: 4px;
right: 6px;
padding: 0 5px;
font-size: 0.65rem;
}
.gu-side[data-collapsed] .gu-side__group > ul {
padding-left: 0;
}
.gu-side[data-collapsed] .gu-side__sub {
justify-content: center;
padding: 0;
}
.gu-side__tip {
display: none;
}
.gu-side[data-collapsed] .gu-side__tip {
position: absolute;
left: calc(100% + 12px);
z-index: 3;
display: block;
padding: 6px 10px;
border-radius: 8px;
background: var(--active);
color: var(--active-text);
font-size: 0.8rem;
opacity: 0;
transform: translateX(-4px);
pointer-events: none;
transition:
opacity 0.2s,
transform 0.3s var(--ease);
}
.gu-side[data-collapsed] .gu-side__link:hover .gu-side__tip,
.gu-side[data-collapsed] .gu-side__link:focus-visible .gu-side__tip {
opacity: 1;
transform: none;
}
.gu-side__main {
flex: 1;
min-width: 0;
overflow: hidden;
}
/* Narrow boxes always use the rail */
@container (max-width: 520px) {
.gu-side__nav {
width: 72px;
}
.gu-side .gu-side__text,
.gu-side .gu-side__chev,
.gu-side .gu-side__toggle {
display: none;
}
.gu-side .gu-side__head {
justify-content: center;
padding: 0;
}
.gu-side .gu-side__link,
.gu-side .gu-side__sub {
justify-content: center;
padding: 0;
}
.gu-side .gu-side__group > ul {
padding-left: 0;
}
.gu-side .gu-side__badge {
position: absolute;
top: 4px;
right: 6px;
padding: 0 5px;
font-size: 0.65rem;
}
}
@media (prefers-reduced-motion: reduce) {
.gu-side__nav,
.gu-side__group,
.gu-side__text,
.gu-side__chev,
.gu-side__tip {
transition: none;
}
}
```
You might also want
Next piece: Conference pageGlide nav
Components, intermediate
Mega menu
Components, advanced
Announcement bar
Components, beginner