Sidebar
An interactive component which expands/collapses a panel.
Installation
Structure
A Sidebar component is composed of the following parts:
SidebarProvider- Handles collapsible state.Sidebar- The sidebar container.SidebarHeaderandSidebarFooter- Sticky at the top and bottom of the sidebar.SidebarContent- Scrollable content.SidebarGroup- Section within theSidebarContent.SidebarTrigger- Trigger for theSidebar.

Usage
Your First Sidebar
Let's start with the most basic sidebar. A collapsible sidebar with a menu.
Add the provider to root of your app
Update the app/layout.tsx file to include the SidebarProvider and SidebarTrigger components at the root of your application.
Create a new sidebar
Create a new sidebar component in the components/app-sidebar.tsx file.
Add the menu to the sidebar
We'll use the SidebarMenu components in a SidebarGroup.
Components
The components in sidebar.tsx are built to be composable i.e you build your sidebar by putting the provided components together.
They also compose well with other kosori/ui components such as DropdownMenu, Collapsible or Dialog etc.
Tip
If you need to change the code in sidebar.tsx, you are encouraged to do so.
The code is yours. Use sidebar.tsx as a starting point and build your own.
In the next sections, we'll go over each component and how to use them.
Sidebar Provider
The SidebarProvider component is used to provide the sidebar context to the Sidebar component.
You should always wrap your application in a SidebarProvider component.
API Reference
| Prop | Type | Default |
|---|---|---|
defaultOpen | boolean | true |
open | boolean | false |
onOpenChange | function | (open: boolean) => void |
Width
If you have a single sidebar in your application, you can use the SIDEBAR_WIDTH and SIDEBAR_WIDTH_MOBILE variables in sidebar.tsx to set the width of the sidebar.
For multiple sidebars in your application, you can use the style prop to set the width of the sidebar.
To set the width of the sidebar, you can use the --sidebar-width and --sidebar-width-mobile CSS variables in the style prop.
This will handle the width of the sidebar but also the layout spacing.
Keyboard Shortcut
The SIDEBAR_KEYBOARD_SHORTCUT variable is used to set the keyboard shortcut used to open and close the sidebar.
To trigger the sidebar, you use the cmd+b keyboard shortcut on Mac and ctrl+b on Windows.
You can change the keyboard shortcut by updating the SIDEBAR_KEYBOARD_SHORTCUT variable.
Persisted State
The SidebarProvider supports persisting the sidebar state across page reloads and server-side rendering.
It uses cookies to store the current state of the sidebar.
When the sidebar state changes, a default cookie named sidebar:state is set with the current open/closed state.
This cookie is then read on subsequent page loads to restore the sidebar state.
To persist sidebar state in Next.js, set up your SidebarProvider in app/layout.tsx like this:
You can change the name of the cookie by updating the SIDEBAR_COOKIE_NAME variable in sidebar.tsx.
Sidebar
The main Sidebar component used to render a collapsible sidebar.
API Reference
| Prop | Type | Default |
|---|---|---|
side | enum | left |
variant | enum | sidebar |
collapsible | enum | offcanvas |
Side
Use the side prop to change the side of the sidebar.
Available options are left and right.
Variant
Use the variant prop to change the variant of the sidebar.
Available options are sidebar, floating, and inset.
Note
If you use the inset variant, remember to wrap your main content in a
SidebarInset component.
Collapsible
Use the collapsible prop to make the sidebar collapsible.
Available options are offcanvas, icon, and none.
| Prop | Description |
|---|---|
offcanvas | A collapsible sidebar that slides in from the left or right. |
icon | A sidebar that collapses to icons. |
none | A non-collapsible sidebar. |
useSidebar
The useSidebar hook is used to control the sidebar.
API Reference
| Prop | Type | Default |
|---|---|---|
state | enum | - |
open | boolean | - |
setOpen | function | (open: boolean) => void |
openMobile | boolean | - |
setOpenMobile | function | (open: boolean) => void |
isMobile | boolean | - |
toggleSidebar | function | () => void |
Sidebar Header
Use the SidebarHeader component to add a sticky header to the sidebar.
The following example adds a <DropdownMenu> to the SidebarHeader.
Sidebar Footer
Use the SidebarFooter component to add a sticky footer to the sidebar.
The following example adds a <DropdownMenu> to the SidebarFooter.
Sidebar Content
The SidebarContent component is used to wrap the content of the sidebar.
This is where you add your SidebarGroup components.
It is scrollable.
Sidebar Group
Use the SidebarGroup component to create a section within the sidebar.
A SidebarGroup has a SidebarGroupLabel, a SidebarGroupContent and an optional SidebarGroupAction.
Collapsible Sidebar Group
To make a SidebarGroup collapsible, wrap it in a Collapsible.
Note
We wrap the CollapsibleTrigger in a SidebarGroupLabel to render a button.
Sidebar Group Action
Use the SidebarGroupAction component to add an action button to the SidebarGroup.
Sidebar Menu
The SidebarMenu component is used for building a menu within a SidebarGroup.
A SidebarMenu component is composed of SidebarMenuItem, SidebarMenuButton, <SidebarMenuAction /> and <SidebarMenuSub /> components.

Here's an example of a SidebarMenu component rendering a list of projects.
Sidebar Menu Button
The SidebarMenuButton component is used to render a menu button within a SidebarMenuItem.
Link or Anchor
By default, the SidebarMenuButton renders a button but you can use the asChild prop to render a different component such as a Link or an a tag.
Icon and Label
You can render an icon and a truncated label inside the button.
Remember to wrap the label in a <span>.
isActive
Use the isActive prop to mark a menu item as active.
Sidebar Menu Action
The SidebarMenuAction component is used to render a menu action within a SidebarMenuItem.
This button works independently of the SidebarMenuButton i.e you can have the <SidebarMenuButton /> as a clickable link and the <SidebarMenuAction /> as a button.
Dropdown Menu
Here's an example of a SidebarMenuAction component rendering a DropdownMenu.
Sidebar Menu Sub
The SidebarMenuSub component is used to render a submenu within a SidebarMenu.
Use <SidebarMenuSubItem /> and <SidebarMenuSubButton /> to render a submenu item.
Collapsible Sidebar Menu
To make a SidebarMenu component collapsible, wrap it and the SidebarMenuSub components in a Collapsible.
Sidebar Menu Badge
The SidebarMenuBadge component is used to render a badge within a SidebarMenuItem.
Sidebar Menu Skeleton
The SidebarMenuSkeleton component is used to render a skeleton for a SidebarMenu.
You can use this to show a loading state when using React Server Components, SWR or react-query.
Sidebar Separator
The SidebarSeparator component is used to render a separator within a Sidebar.
Sidebar Trigger
Use the SidebarTrigger component to render a button that toggles the sidebar.
The SidebarTrigger component must be used within a SidebarProvider.
Custom Trigger
To create a custom trigger, you can use the useSidebar hook.
Sidebar Rail
The SidebarRail component is used to render a rail within a Sidebar.
This rail can be used to toggle the sidebar.
Data Fetching
React Server Components
Here's an example of a SidebarMenu component rendering a list of projects using React Server Components.
Skeleton to show loading state:
Server component fetching data:
Using with React Suspense:
SWR and React Query
You can use the same approach with SWR or React Query.
SWR:
React Query:
Controlled Sidebar
Use the open and onOpenChange props to control the sidebar.
Styling
Here are some tips for styling the sidebar based on different states.
- Styling an element based on the sidebar collapsible state. The following will hide the SidebarGroup when the sidebar is in icon mode.
- Styling a menu action based on the menu button active state. The following will force the menu action to be visible when the menu button is active.
