Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Sidebar Toggle Navbar Block for React

A navbar with a sidebar toggle for a project tracker: a button, the [ key and a tooltip fold a mini sidebar under the bar between icons only and icons with labels, and on phones the same button opens it as a sheet.

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

A sidebar-toggle navbar for React with Base UI, the bar of an application whose navigation lives in a rail at the side. A 48-pixel bar on the page's rails carries a sidebar button, the Panelfold wordmark, the page's title and an avatar, and under it a framed 96-pixel stage with corner marks holds a mini sidebar of four links (Board, Backlog, Timeline and Reports) beside a greyscale sketch of a page. The button, the [ key and a tooltip that names the key fold the sidebar between icons only and icons with labels as its grid column transitions from 10 rem to 3 rem; the button says whether it's expanded and which sidebar it controls, and the links keep their names while folded. On a phone the stage is gone and the same button opens the four links in a sheet from the start. The block stays within one bar and its stage, and the page's rails and top rule come from the Guides spine. It's built from Tooltip, Kbd, Sheet, Avatar, Button and Guides, with your theme's tokens for every colour. For dashboards, trackers and admin tools whose sidebar should fold away when the work needs the room.

FAQ

How does the sidebar fold?

The stage is a CSS grid whose first column goes from 10rem to 3rem with a 300 ms transition, so the rail narrows and the page beside it takes the room. Each link's label is clipped by the rail while it's folded, which keeps the link's name for a screen reader, and a tooltip names each icon while the labels are hidden (it's switched off while they show). Nothing is measured and the bar doesn't move. With reduced motion the rail changes width at once. In your app, put the same grid around your layout and keep the state where the rest of your page can read it.

How does the [ key work?

A listener on the document toggles the sidebar when you press [, unless a modifier key is down or you're typing in a field, a select or an editable area, where [ is just a character. The button announces it with aria-keyshortcuts="[" and its tooltip shows the key in a Kbd. Remove the listener (the useEffect) if the key clashes with something in your app.

What does the button say to a screen reader?

It's named Sidebar and says whether it's expanded; its aria-controls points at the mini sidebar, a nav landmark named Main. The tooltip says Toggle sidebar and shows the key, on hover and on keyboard focus. The tooltip trigger renders the button with render, and the rail's tooltips are switched off with disabled while labels show.

What happens on a phone?

Below the md breakpoint the stage is hidden and a second button, the same icon and name, opens a sheet from the start of the page with the four links at touch size, a header repeating the wordmark and a close button. Escape, the close button or choosing a link closes it and focus goes back to the button. A sheet from the start opens on the right in a right-to-left layout, so its edge and border switch sides.

How do I change the links and the page's title?

Edit ITEMS at the top of the file (an icon, a label, an href and current: true on the page you're on, which gets aria-current="page" and a tint); the stage and the phone sheet both read it. The page's title is the line after the wordmark, and the avatar's initials are in AvatarFallback. A rail row is 24 pixels tall, the smallest touch target, so keep it to about four items in this stage.

How does it sit on the page?

It's the page's first block: its rails and plus marks line up with every block under it and its top rule falls on the window's top edge. The stage is the one framed figure the bar may have; it is a preview of your real sidebar, drawn at the width of the page's column, so replace it with your own layout when you use the bar. Every link points at #.

  • Pro

    Icon-Only Navbar

    Cells and splits

    An icon-only navbar for a messaging app: six destinations in square cells on shared hairlines, each named by a tooltip, with unread dots and a Show labels toggle that widens the cells to reveal the words.

  • Pro

    Drill-Down Drawer Navbar

    Mobile and drawers

    A drawer navbar for a travel planner: a Menu button at every width opens a sheet from the end where Plan and Explore slide a second panel in with a Back button, with Pricing and Help centre as plain links and a Shine Button at the foot.

  • Pro

    Priority Plus Navbar

    Cells and splits

    A priority-plus navbar for a media-planning suite in a resizable frame: the links that fit stay on the bar and the rest drop into a More menu as you drag the frame narrower, decided by container queries.

  • Pro

    A dashboard bar whose page title is a menu of saved views, Pinned and Shared, with a star on the one that opens first, a breadcrumb that follows the choice and a drawer on phones.

  • Pro

    An admin navbar for a marketplace console: a sidebar toggle, a search field scoped to Users, Orders or Logs, and a badge that counts pending approvals.

  • Pro

    A one-bar marketing navbar for a product roadmap tool: a wordmark, a Product menu of four areas with icons and one-line descriptions, plain links with the current page marked, Sign in and a Shine Button, and a sheet with the same links on phones.