A Sidebar component is composed of the following parts:
SidebarProvider - Handles collapsible state.
Sidebar - The sidebar container.
SidebarHeader and SidebarFooter - Sticky at the top and bottom of the sidebar.
SidebarContent - Scrollable content.
SidebarGroup - Section within the SidebarContent.
SidebarTrigger - Trigger for the Sidebar.
Usage
Your First Sidebar
Let's start with the most basic sidebar. A collapsible sidebar with a menu.
Add a SidebarProvider and SidebarTrigger at the root of your application.
Create a new sidebar component at components/app-sidebar.tsx.
Now, let's add a SidebarMenu to the sidebar.
We'll use the SidebarMenu component in a SidebarGroup.
You've created your first sidebar.
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 shadcn/ui components such as DropdownMenu, Collapsible or Dialog etc.
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.
SidebarProvider
The SidebarProvider component is used to provide the sidebar context to the Sidebar component. You should always wrap your application in a SidebarProvider component.
Props
Name
Type
Description
defaultOpen
boolean
Default open state of the sidebar.
open
boolean
Open state of the sidebar (controlled).
onOpenChange
(open: boolean) => void
Sets open state of the sidebar (controlled).
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.
Sidebar
The main Sidebar component used to render a collapsible sidebar.
Props
Property
Type
Description
side
left or right
The side of the sidebar.
variant
sidebar, floating, or inset
The variant of the sidebar.
collapsible
offcanvas, icon, or none
Collapsible state of the sidebar.
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.
We use the following CSS variables to theme the sidebar.
We intentionally use different variables for the sidebar and the rest of the application to make it easy to have a sidebar that is styled differently from the rest of the application. Think a sidebar with a darker shade from the main application.
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.
You can find more tips on using states for styling in this Twitter thread.