shadcn.io is not affiliated with official shadcn/ui
Collapsible Items Sidebar Block for React
A design system's docs in a fixed-height window whose sidebar rows are split: a label that opens a section's overview and a separate chevron that opens its sub-menu, with a count of pages on each row.
Installation
Overview
Separate going somewhere from opening a list with this collapsible items sidebar for React with Base UI. A window framed by a hairline with a plus mark on each corner holds the Sidebar, and each of its four sections, Components, Layout, Theming and Documentation, is two controls side by side. The label is a link that opens the section's overview page; the chevron beside it is its own button that opens and closes the pages under it as a sub-menu, so choosing a section never has to fold it and folding it never changes the page. A count says how many pages each section holds. The page is the chosen component's page in typed text, a sentence with a table of its props and a note, or the section's overview as a list of its pages. It's built from Sidebar, Collapsible, Avatar and Guides, with a CSS entrance only. Folded to icons the rail keeps the four section icons, which open the overviews; below the medium breakpoint it is a sheet. For design systems, component libraries and any docs where a section is both a page and a list.
FAQ
Why is the row split in two?
A section is both a place and a list. If the whole row only toggled, you could never open the section's own page; if the whole row only navigated, you could never fold the list without leaving where you are. So the label is a SidebarMenuButton link and the chevron is a SidebarMenuAction that is the Collapsible's trigger. Each has its own name: "Components, 4 pages" for the link and "Components pages" for the button, which also reports aria-expanded and aria-controls for the sub-menu it owns. Choosing a label opens its section if it was closed, since you will want to see the pages.
Where does the count come from?
It is the length of the section's pages array, never typed twice. It sits between the label and the chevron as a SidebarMenuBadge, hidden from assistive technology because the link's own name already says "4 pages". The label has padding at its end so a long name is cut with an ellipsis before it reaches the count.
What happens when the sidebar folds to icons?
The menu button or the rail along the edge folds it to the four section icons with tooltips. The sub-menus, counts and chevrons are hidden there, and each icon is still the section's overview link, so a folded rail can still reach every overview. Unfold it and the sections you left open are as you left them. The provider is set with persist={false} and shortcut={false}, so the block writes no cookie and leaves Ctrl and Command with B to the page it sits on.
What stays in the page, and how do I connect it?
Everything. The fourteen pages and four overviews are typed in SECTIONS at the top of the file, and choosing one swaps the page's text in the block's own state; nothing is fetched. The links go to # whose handler shows the page instead of navigating. The section labels are SidebarMenuButtons with render set to an a, and the chevron is a CollapsibleTrigger with render set to a SidebarMenuAction. Point the links at your routes and mark the current one with isActive and aria-current.
How does it work on a phone?
Below the medium breakpoint the sidebar is a sheet that the menu button in the top bar opens from the start edge, the right in a right-to-left layout. The chevrons work inside it, and choosing a label or a page closes the sheet and puts focus back on the button. The status line under the page says what opened.
Is it accessible?
The sidebar is a navigation landmark named after the product, its list named by the group label, and the open page, or the open overview, is marked as the current page. The chevron is a real button with a name and an expanded state, reachable by Tab right after its label. The page is a scrollable region named by its heading, the props are a table with column and row headings, and one polite status line reports every choice and every fold.
Related
- Pro
Collapsible Groups Sidebar
Folds and treesA framework's docs in a fixed-height window whose slim sidebar has five groups that fold from their labels, with Expand all and Collapse all, a count of open groups and a dot on a folded group that holds the current page.
- Pro
Nested Collapsible Sidebar
Folds and treesA help centre in a fixed-height window whose wide sidebar folds three levels deep, categories that hold topics that hold articles, with a Reveal active button that opens only the folds above the open article.
- Pro
Sub-Menus Sidebar
Folds and treesA recipe manager in a fixed-height window whose floating sidebar has sections with sub-menus, a switch between One at a time and Keep open, and arrow keys that open a sub-menu toward the end of the line and close it toward the start.
- Pro
Documentation Tree Sidebar
Folds and treesA docs host in a fixed-height window whose sidebar is a numbered outline that opens the folds above the page you choose, an Auto-collapse switch that closes the rest, a breadcrumb over the page and Previous and Next buttons in the top bar.
- Pro
Floating Variant Sidebar
VariantsA CSS toolkit's docs in a fixed-height window with a floating sidebar: a rounded card inset from the window over a tinted shell, five collapsing sections of pages, and a switch to the flush rail for comparison.
- Pro
File Tree Sidebar
Folds and treesA code editor in a fixed-height window whose slim sidebar is a file tree with file-type icons, arrow-key, Home, End, asterisk and type-ahead navigation, a Collapse all button, and the chosen file's lines and path on the page.