Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Nested Collapsible Sidebar Block for React

A 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.

Framework
UI library

Preview

Preview width

Installation

Part of Pro
Pro unlocks the code and one-line install of every Pro button and text effect, plus the MCP for your AI editor.
Have Pro already? Sign in to open it.Opens here as soon as you're Pro.

Overview

Keep a deep help centre navigable with this nested collapsible sidebar for React with Base UI. A window framed by a hairline with a plus mark on each corner holds a wide Sidebar three levels deep: four categories hold topics, and topics hold articles, so Account holds Security, and Security holds Two-factor sign-in. Every category and every topic is its own Collapsible, and which of the dozen folds are open is one list in the block's state, counted beside the buttons. The page is the chosen article in typed text, with its steps and links to related articles that can sit inside folds that are shut. That is what the ghost Button is for: Reveal active opens only the folds above the open article, leaves the others as they were, and scrolls the article to the middle of the rail. Folded to icons the rail shows the four category icons, and pressing one unfolds the rail onto it; below the medium breakpoint it is a sheet. It's built from Sidebar, Collapsible, Button, Badge and Guides, with a CSS entrance only. For help centres, knowledge bases and any menu deeper than two levels.

FAQ

How are three levels built?

The first level is a SidebarMenuItem whose button is the Collapsible trigger. Its content is a SidebarMenuSub, and each topic in it is a SidebarMenuSubItem holding another Collapsible, whose trigger is a real button styled like a sub-menu row and whose content is a second SidebarMenuSub of article links. The folds are independent: opening a category leaves its topics as they were, so a topic you opened before stays open when you come back.

What does Reveal active do, and when is it available?

It opens only the two folds above the open article, the category and the topic, and leaves every other fold alone, then scrolls the article's link to the middle of the rail. It is useful after you follow a related link on the page into a fold that is shut: the status line says so, and the button wakes up. When the article is already showing, the button is dimmed with aria-disabled but stays in place, and pressing it says "already showing" instead of doing nothing silently.

How is the open state kept?

As a list of fold ids, open, in the block's state. Each fold is controlled by isOpen(open, id), the count beside the button is its length, and Reveal active is a pure step from one list to another. Opening and closing animates with the catalog's own motion, which reduced motion switches off. The block does not store anything outside the page.

What happens when the sidebar folds to icons?

The menu button or the rail along the edge folds it to the four category icons with tooltips. The topics and articles are hidden there, so pressing a category icon unfolds the rail and opens that category. 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 sixteen articles are typed in CATEGORIES 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 article instead of navigating. The category rows are SidebarMenuButtons that flip their own fold: each carries its own aria-expanded and a press handler, onClick, over a controlled Collapsible, rather than being the Collapsible's trigger, because a row's tooltip and a trigger would both claim the button's id. The topic rows are real CollapsibleTriggers. The article links are SidebarMenuSubButtons with an href. Point them 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 folds and Reveal active work inside it, and choosing an article closes the sheet and puts focus back on the button. The status line under the page says what opened and how many folds are open.

  • Pro

    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.

  • Pro

    A 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

    A 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

    A grocery app in a fixed-height window whose inset sidebar drills down one level at a time, panel by panel up to four levels deep, with a back button, Escape and a breadcrumb that all go up.

  • Pro

    Sub-Menus Sidebar

    Folds and trees

    A 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

    File Tree Sidebar

    Folds and trees

    A 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.