0% found this document useful (0 votes)
132 views38 pages

Sidebar - Shadcn - Ui

The document provides a comprehensive guide for building a customizable and themeable sidebar component using React. It includes installation instructions, component structure, usage examples, and details on various sidebar functionalities such as collapsibility, theming, and state persistence. The sidebar is designed to be composable, allowing developers to create complex applications with ease.

Uploaded by

Felipe Montero
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
132 views38 pages

Sidebar - Shadcn - Ui

The document provides a comprehensive guide for building a customizable and themeable sidebar component using React. It includes installation instructions, component structure, usage examples, and details on various sidebar functionalities such as collapsibility, theming, and state persistence. The sidebar is designed to be composable, allowing developers to create complex applications with ease.

Uploaded by

Felipe Montero
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Search...

⌘ K

Docs Sidebar

Sidebar
A composable, themeable and customizable sidebar component.

A sidebar that collapses to icons.

Sidebars are one of the most complex components to build. They are central to any

application and often contain a lot of moving parts.

I don't like building sidebars. So I built 30+ of them. All kinds of configurations. Then I

extracted the core components into sidebar.tsx .

We now have a solid foundation to build on top of. Composable. Themeable. Customizable.

Browse the Blocks Library.


Installation

CLI Manual

1 Run the following command to install sidebar.tsx

npx shadcn@latest add sidebar

2 Add the following colors to your CSS file

The command above should install the colors for you. If not, copy and paste the

following in your CSS file.

We'll go over the colors later in the theming section.

app/globals.css

@layer base {
:root {
--sidebar-background: 0 0% 98%;
--sidebar-foreground: 240 5.3% 26.1%;
--sidebar-primary: 240 5.9% 10%;
--sidebar-primary-foreground: 0 0% 98%;
--sidebar-accent: 240 4.8% 95.9%;
--sidebar-accent-foreground: 240 5.9% 10%;
--sidebar-border: 220 13% 91%;
--sidebar-ring: 217.2 91.2% 59.8%;
}

.dark {
--sidebar-background: 240 5.9% 10%;
--sidebar-foreground: 240 4.8% 95.9%;
--sidebar-primary: 224.3 76.3% 48%;
--sidebar-primary-foreground: 0 0% 100%;
--sidebar-accent: 240 3.7% 15.9%;
--sidebar-accent-foreground: 240 4.8% 95.9%;
--sidebar-border: 240 3.7% 15.9%;
--sidebar-ring: 217.2 91.2% 59.8%;
}
}

Structure

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

app/layout.tsx

1 import { SidebarProvider, SidebarTrigger } from "@/components/ui/sidebar"


2 import { AppSidebar } from "@/components/app-sidebar"
3

4 export default function Layout({ children }: { children: React.ReactNode }


5 return (
6 <SidebarProvider>
7 <AppSidebar />
8 <main>
9 <SidebarTrigger />
10 {children}
11 </main>
12 </SidebarProvider>
13 )
14 }

components/app-sidebar.tsx

1 import {
2 Sidebar,
3 SidebarContent,
4 SidebarFooter,
5 SidebarGroup,
6 SidebarHeader,
7 } from "@/components/ui/sidebar"
8

9 export function AppSidebar() {


10 return (
11 <Sidebar>
12 <SidebarHeader />
13 <SidebarContent>
14 <SidebarGroup />
15 <SidebarGroup />
16 </SidebarContent>
17 <SidebarFooter />
18 </Sidebar>
19 )
20 }

Your First Sidebar

Let's start with the most basic sidebar. A collapsible sidebar with a menu.

1 Add a SidebarProvider and SidebarTrigger at the root of your application.

app/layout.tsx

1 import { SidebarProvider, SidebarTrigger } from "@/components/ui/sid


2 import { AppSidebar } from "@/components/app-sidebar"
3

4 export default function Layout({ children }: { children: React.React


5 return (
6 <SidebarProvider>
7 <AppSidebar />
8 <main>
9 <SidebarTrigger />
10 {children}
11 </main>
12 </SidebarProvider>
13 )
14 }

2 Create a new sidebar component at components/app-sidebar.tsx .

components/app-sidebar.tsx

1 import { Sidebar, SidebarContent } from "@/components/ui/sidebar"


2
3 export function AppSidebar() {
4 return (
5 <Sidebar>
6 <SidebarContent />
7 </Sidebar>
8 )
9 }

3 Now, let's add a SidebarMenu to the sidebar.

We'll use the SidebarMenu component in a SidebarGroup .

components/app-sidebar.tsx

1 import { Calendar, Home, Inbox, Search, Settings } from "lucide-r


2

3 import {
4 Sidebar,
5 SidebarContent,
6 SidebarGroup,
7 SidebarGroupContent,
8 SidebarGroupLabel,
9 SidebarMenu,
10 SidebarMenuButton,
11 SidebarMenuItem,
12 } from "@/components/ui/sidebar"
13

14 // Menu items.
15 const items = [
16 {
17 title: "Home",
18 url: "#",
19 icon: Home,
20 },
21 {
22 title: "Inbox",
23 url: "#",
24 icon: Inbox,
25 },
4 You've created your first sidebar.

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

(open: boolean) => Sets open state of the sidebar


onOpenChange
void (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.

components/ui/sidebar.tsx

1 const SIDEBAR_WIDTH = "16rem"


2 const SIDEBAR_WIDTH_MOBILE = "18rem"

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.

components/ui/sidebar.tsx

1 <SidebarProvider
2 style={{
3 "--sidebar-width": "20rem",
4 "--sidebar-width-mobile": "20rem",
5 }}
6 >
7 <Sidebar />
8 </SidebarProvider>

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.

components/ui/sidebar.tsx

1 const SIDEBAR_KEYBOARD_SHORTCUT = "b"

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:

app/layout.tsx

1 import { cookies } from "next/headers"


2
3 import { SidebarProvider, SidebarTrigger } from "@/components/ui/sidebar"
4 import { AppSidebar } from "@/components/app-sidebar"
5

6 export async function Layout({ children }: { children: React.ReactNode })


7 const cookieStore = await cookies()
8 const defaultOpen = cookieStore.get("sidebar:state")?.value === "true"
9

10 return (
11 <SidebarProvider defaultOpen={defaultOpen}>
12 <AppSidebar />
13 <main>
14 <SidebarTrigger />
15 {children}
16 </main>
17 </SidebarProvider>
18 )
19 }

You can change the name of the cookie by updating the SIDEBAR_COOKIE_NAME variable in

sidebar.tsx .

components/ui/sidebar.tsx

1 const SIDEBAR_COOKIE_NAME = "sidebar:state"

Sidebar

The main Sidebar component used to render a collapsible sidebar.

1 import { Sidebar } from "@/components/ui/sidebar"


2

3 export function AppSidebar() {


4 return <Sidebar />
5 }
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 .

1 import { Sidebar } from "@/components/ui/sidebar"


2

3 export function AppSidebar() {


4 return <Sidebar side="left | right" />
5 }

variant

Use the variant prop to change the variant of the sidebar.

Available options are sidebar , floating and inset .

1 import { Sidebar } from "@/components/ui/sidebar"


2

3 export function AppSidebar() {


4 return <Sidebar variant="sidebar | floating | inset" />
5 }

Note: If you use the inset variant, remember to wrap your main content in a SidebarInset
component.
1 <SidebarProvider>
2 <Sidebar variant="inset" />
3 <SidebarInset>
4 <main>{children}</main>
5 </SidebarInset>
6 </SidebarProvider>

collapsible

Use the collapsible prop to make the sidebar collapsible.

Available options are offcanvas , icon and none .

1 import { Sidebar } from "@/components/ui/sidebar"


2

3 export function AppSidebar() {


4 return <Sidebar collapsible="offcanvas | icon | none" />
5 }

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.

1 import { useSidebar } from "@/components/ui/sidebar"


2

3 export function AppSidebar() {


4 const {
5 state,
6 open,
7 setOpen,
8 openMobile,
9 setOpenMobile,
10 isMobile,
11 toggleSidebar,
12 } = useSidebar()
13 }

Property Type Description

state expanded or collapsed The current state of the sidebar.

open boolean Whether the sidebar is open.

(open: boolean) =>


setOpen Sets the open state of the sidebar.
void

openMobile boolean Whether the sidebar is open on mobile.

(open: boolean) => Sets the open state of the sidebar on


setOpenMobile
void mobile.

isMobile boolean Whether the sidebar is on mobile.

toggleSidebar () => void Toggles the sidebar. Desktop and mobile.

SidebarHeader

Use the SidebarHeader component to add a sticky header to the sidebar.

The following example adds a <DropdownMenu> to the SidebarHeader .


A sidebar header with a dropdown menu.

components/app-sidebar.tsx
1 <Sidebar>
2 <SidebarHeader>
3 <SidebarMenu>
4 <SidebarMenuItem>
5 <DropdownMenu>
6 <DropdownMenuTrigger asChild>
7 <SidebarMenuButton>
8 Select Workspace
9 <ChevronDown className="ml-auto" />
10 </SidebarMenuButton>
11 </DropdownMenuTrigger>
12 <DropdownMenuContent className="w-[--radix-popper-anchor-width]"
13 <DropdownMenuItem>
14 <span>Acme Inc</span>
15 </DropdownMenuItem>
16 <DropdownMenuItem>
17 <span>Acme Corp.</span>
18 </DropdownMenuItem>
19 </DropdownMenuContent>
20 </DropdownMenu>
21 </SidebarMenuItem>
22 </SidebarMenu>
23 </SidebarHeader>
24 </Sidebar>

SidebarFooter

Use the SidebarFooter component to add a sticky footer to the sidebar.

The following example adds a <DropdownMenu> to the SidebarFooter .


A sidebar footer with a dropdown menu.

components/app-sidebar.tsx

1 export function AppSidebar() {


2 return (
3 <SidebarProvider>
4 <Sidebar>
5 <SidebarHeader />
6 <SidebarContent />
7 <SidebarFooter>
8 <SidebarMenu>
9 <SidebarMenuItem>
10 <DropdownMenu>
11 <DropdownMenuTrigger asChild>
12 <SidebarMenuButton>
13 <User2 /> Username
14 <ChevronUp className="ml-auto" />
15 </SidebarMenuButton>
16 </DropdownMenuTrigger>
17 <DropdownMenuContent
18 side="top"
19 className="w-[--radix-popper-anchor-width]"
20 >
21 <DropdownMenuItem>
22 <span>Account</span>
23 </DropdownMenuItem>
24 <DropdownMenuItem>
25 <span>Billing</span>
26 </DropdownMenuItem>

SidebarContent

The SidebarContent component is used to wrap the content of the sidebar. This is where

you add your SidebarGroup components. It is scrollable.

1 import { Sidebar, SidebarContent } from "@/components/ui/sidebar"


2

3 export function AppSidebar() {


4 return (
5 <Sidebar>
6 <SidebarContent>
7 <SidebarGroup />
8 <SidebarGroup />
9 </SidebarContent>
10 </Sidebar>
11 )
12 }

SidebarGroup

Use the SidebarGroup component to create a section within the sidebar.

A SidebarGroup has a SidebarGroupLabel , a SidebarGroupContent and an optional

SidebarGroupAction .
A sidebar group.

1 import { Sidebar, SidebarContent, SidebarGroup } from "@/components/ui/sid


2

3 export function AppSidebar() {


4 return (
5 <Sidebar>
6 <SidebarContent>
7 <SidebarGroup>
8 <SidebarGroupLabel>Application</SidebarGroupLabel>
9 <SidebarGroupAction>
10 <Plus /> <span className="sr-only">Add Project</span>
11 </SidebarGroupAction>
12 <SidebarGroupContent></SidebarGroupContent>
13 </SidebarGroup>
14 </SidebarContent>
15 </Sidebar>
16 )
17 }

Collapsible SidebarGroup

To make a SidebarGroup collapsible, wrap it in a Collapsible .


A collapsible sidebar group.

1 export function AppSidebar() {


2 return (
3 <Collapsible defaultOpen className="group/collapsible">
4 <SidebarGroup>
5 <SidebarGroupLabel asChild>
6 <CollapsibleTrigger>
7 Help
8 <ChevronDown className="ml-auto transition-transform group-dat
9 </CollapsibleTrigger>
10 </SidebarGroupLabel>
11 <CollapsibleContent>
12 <SidebarGroupContent />
13 </CollapsibleContent>
14 </SidebarGroup>
15 </Collapsible>
16 )
17 }
Note: We wrap the CollapsibleTrigger in a SidebarGroupLabel to render a button.

SidebarGroupAction

Use the SidebarGroupAction component to add an action button to the SidebarGroup .

1 export function AppSidebar() {


2 return (
3 <SidebarGroup>
4 <SidebarGroupLabel asChild>Projects</SidebarGroupLabel>
5 <SidebarGroupAction title="Add Project">
6 <Plus /> <span className="sr-only">Add Project</span>
7 </SidebarGroupAction>
8 <SidebarGroupContent />
9 </SidebarGroup>
10 )
11 }
A sidebar group with an action button.

SidebarMenu

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.


A sidebar menu with a list of projects.

1 <Sidebar>
2 <SidebarContent>
3 <SidebarGroup>
4 <SidebarGroupLabel>Projects</SidebarGroupLabel>
5 <SidebarGroupContent>
6 <SidebarMenu>
7 {projects.map((project) => (
8 <SidebarMenuItem key={project.name}>
9 <SidebarMenuButton asChild>
10 <a href={project.url}>
11 <project.icon />
12 <span>{project.name}</span>
13 </a>
14 </SidebarMenuButton>
15 </SidebarMenuItem>
16 ))}
17 </SidebarMenu>
18 </SidebarGroupContent>
19 </SidebarGroup>
20 </SidebarContent>
21 </Sidebar>

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

1 <SidebarMenuButton asChild>
2 <a href="#">Home</a>
3 </SidebarMenuButton>

Icon and Label

You can render an icon and a truncated label inside the button. Remember to wrap the label

in a <span> .

1 <SidebarMenuButton asChild>
2 <a href="#">
3 <Home />
4 <span>Home</span>
5 </a>
6 </SidebarMenuButton>

isActive

Use the isActive prop to mark a menu item as active.

1 <SidebarMenuButton asChild isActive>


2 <a href="#">Home</a>
3 </SidebarMenuButton>
SidebarMenuAction

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.

1 <SidebarMenuItem>
2 <SidebarMenuButton asChild>
3 <a href="#">
4 <Home />
5 <span>Home</span>
6 </a>
7 </SidebarMenuButton>
8 <SidebarMenuAction>
9 <Plus /> <span className="sr-only">Add Project</span>
10 </SidebarMenuAction>
11 </SidebarMenuItem>

DropdownMenu

Here's an example of a SidebarMenuAction component rendering a DropdownMenu .


A sidebar menu action with a dropdown menu.

1 <SidebarMenuItem>
2 <SidebarMenuButton asChild>
3 <a href="#">
4 <Home />
5 <span>Home</span>
6 </a>
7 </SidebarMenuButton>
8 <DropdownMenu>
9 <DropdownMenuTrigger asChild>
10 <SidebarMenuAction>
11 <MoreHorizontal />
12 </SidebarMenuAction>
13 </DropdownMenuTrigger>
14 <DropdownMenuContent side="right" align="start">
15 <DropdownMenuItem>
16 <span>Edit Project</span>
17 </DropdownMenuItem>
18 <DropdownMenuItem>
19 <span>Delete Project</span>
20 </DropdownMenuItem>
21 </DropdownMenuContent>
22 </DropdownMenu>
23 </SidebarMenuItem>

SidebarMenuSub

The SidebarMenuSub component is used to render a submenu within a SidebarMenu .

Use <SidebarMenuSubItem /> and <SidebarMenuSubButton /> to render a submenu item.


A sidebar menu with a submenu.

1 <SidebarMenuItem>
2 <SidebarMenuButton />
3 <SidebarMenuSub>
4 <SidebarMenuSubItem>
5 <SidebarMenuSubButton />
6 </SidebarMenuSubItem>
7 <SidebarMenuSubItem>
8 <SidebarMenuSubButton />
9 </SidebarMenuSubItem>
10 </SidebarMenuSub>
11 </SidebarMenuItem>

Collapsible SidebarMenu

To make a SidebarMenu component collapsible, wrap it and the SidebarMenuSub

components in a Collapsible .
A collapsible sidebar menu.

1 <SidebarMenu>
2 <Collapsible defaultOpen className="group/collapsible">
3 <SidebarMenuItem>
4 <CollapsibleTrigger asChild>
5 <SidebarMenuButton />
6 </CollapsibleTrigger>
7 <CollapsibleContent>
8 <SidebarMenuSub>
9 <SidebarMenuSubItem />
10 </SidebarMenuSub>
11 </CollapsibleContent>
12 </SidebarMenuItem>
13 </Collapsible>
14 </SidebarMenu>

SidebarMenuBadge
The SidebarMenuBadge component is used to render a badge within a SidebarMenuItem .

A sidebar menu with a badge.

1 <SidebarMenuItem>
2 <SidebarMenuButton />
3 <SidebarMenuBadge>24</SidebarMenuBadge>
4 </SidebarMenuItem>

SidebarMenuSkeleton

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.

1 function NavProjectsSkeleton() {
2 return (
3 <SidebarMenu>
4 {Array.from({ length: 5 }).map((_, index) => (
5 <SidebarMenuItem key={index}>
6 <SidebarMenuSkeleton />
7 </SidebarMenuItem>
8 ))}
9 </SidebarMenu>
10 )
11 }

SidebarSeparator

The SidebarSeparator component is used to render a separator within a Sidebar .

1 <Sidebar>
2 <SidebarHeader />
3 <SidebarSeparator />
4 <SidebarContent>
5 <SidebarGroup />
6 <SidebarSeparator />
7 <SidebarGroup />
8 </SidebarContent>
9 </Sidebar>

SidebarTrigger

Use the SidebarTrigger component to render a button that toggles the sidebar.

The SidebarTrigger component must be used within a SidebarProvider .

1 <SidebarProvider>
2 <Sidebar />
3 <main>
4 <SidebarTrigger />
5 </main>
6 </SidebarProvider>

Custom Trigger

To create a custom trigger, you can use the useSidebar hook.

1 import { useSidebar } from "@/components/ui/sidebar"


2

3 export function CustomTrigger() {


4 const { toggleSidebar } = useSidebar()
5

6 return <button onClick={toggleSidebar}>Toggle Sidebar</button>


7 }

SidebarRail

The SidebarRail component is used to render a rail within a Sidebar . This rail can be

used to toggle the sidebar.

1 <Sidebar>
2 <SidebarHeader />
3 <SidebarContent>
4 <SidebarGroup />
5 </SidebarContent>
6 <SidebarFooter />
7 <SidebarRail />
8 </Sidebar>

Data Fetching

React Server Components


Here's an example of a SidebarMenu component rendering a list of projects using React

Server Components.

A sidebar menu using React Server Components.

Skeleton to show loading state.

1 function NavProjectsSkeleton() {
2 return (
3 <SidebarMenu>
4 {Array.from({ length: 5 }).map((_, index) => (
5 <SidebarMenuItem key={index}>
6 <SidebarMenuSkeleton showIcon />
7 </SidebarMenuItem>
8 ))}
9 </SidebarMenu>
10 )
11 }

Server component fetching data.


1 async function NavProjects() {
2 const projects = await fetchProjects()
3

4 return (
5 <SidebarMenu>
6 {projects.map((project) => (
7 <SidebarMenuItem key={project.name}>
8 <SidebarMenuButton asChild>
9 <a href={project.url}>
10 <project.icon />
11 <span>{project.name}</span>
12 </a>
13 </SidebarMenuButton>
14 </SidebarMenuItem>
15 ))}
16 </SidebarMenu>
17 )
18 }

Usage with React Suspense.

1 function AppSidebar() {
2 return (
3 <Sidebar>
4 <SidebarContent>
5 <SidebarGroup>
6 <SidebarGroupLabel>Projects</SidebarGroupLabel>
7 <SidebarGroupContent>
8 <React.Suspense fallback={<NavProjectsSkeleton />}>
9 <NavProjects />
10 </React.Suspense>
11 </SidebarGroupContent>
12 </SidebarGroup>
13 </SidebarContent>
14 </Sidebar>
15 )
16 }
SWR and React Query

You can use the same approach with SWR or react-query.

SWR

1 function NavProjects() {
2 const { data, isLoading } = useSWR("/api/projects", fetcher)
3

4 if (isLoading) {
5 return (
6 <SidebarMenu>
7 {Array.from({ length: 5 }).map((_, index) => (
8 <SidebarMenuItem key={index}>
9 <SidebarMenuSkeleton showIcon />
10 </SidebarMenuItem>
11 ))}
12 </SidebarMenu>
13 )
14 }
15

16 if (!data) {
17 return ...
18 }
19

20 return (
21 <SidebarMenu>
22 {data.map((project) => (
23 <SidebarMenuItem key={project.name}>
24 <SidebarMenuButton asChild>
25 <a href={project.url}>
26 <project.icon />
{ j t } /

React Query

1 function NavProjects() {
2 const { data, isLoading } = useQuery()
3

4 if (isLoading) {
5 return (
6 <SidebarMenu>
7 {Array.from({ length: 5 }).map((_, index) => (
8 <SidebarMenuItem key={index}>
9 <SidebarMenuSkeleton showIcon />
10 </SidebarMenuItem>
11 ))}
12 </SidebarMenu>
13 )
14 }
15

16 if (!data) {
17 return ...
18 }
19

20 return (
21 <SidebarMenu>
22 {data.map((project) => (
23 <SidebarMenuItem key={project.name}>
24 <SidebarMenuButton asChild>
25 <a href={project.url}>
26 <project.icon />

Controlled Sidebar

Use the open and onOpenChange props to control the sidebar.


A controlled sidebar.

1 export function AppSidebar() {


2 const [open, setOpen] = React.useState(false)
3

4 return (
5 <SidebarProvider open={open} onOpenChange={setOpen}>
6 <Sidebar />
7 </SidebarProvider>
8 )
9 }

Theming

We use the following CSS variables to theme the sidebar.

@layer base {
:root {
--sidebar-background: 0 0% 98%;
--sidebar-foreground: 240 5.3% 26.1%;
--sidebar-primary: 240 5.9% 10%;
--sidebar-primary-foreground: 0 0% 98%;
--sidebar-accent: 240 4.8% 95.9%;
--sidebar-accent-foreground: 240 5.9% 10%;
--sidebar-border: 220 13% 91%;
--sidebar-ring: 217.2 91.2% 59.8%;
}

.dark {
--sidebar-background: 240 5.9% 10%;
--sidebar-foreground: 240 4.8% 95.9%;
--sidebar-primary: 0 0% 98%;
--sidebar-primary-foreground: 240 5.9% 10%;
--sidebar-accent: 240 3.7% 15.9%;
--sidebar-accent-foreground: 240 4.8% 95.9%;
--sidebar-border: 240 3.7% 15.9%;
--sidebar-ring: 217.2 91.2% 59.8%;
}
}

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.

<Sidebar collapsible="icon">
<SidebarContent>
<SidebarGroup className="group-data-[collapsible=icon]:hidden" />
</SidebarContent>
</Sidebar>

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.

<SidebarMenuItem>
<SidebarMenuButton />
<SidebarMenuAction className="peer-data-[active=true]/menu-button:opacity-100
</SidebarMenuItem>
You can find more tips on using states for styling in this Twitter thread.

Changelog

2024-10-30 Cookie handling in setOpen

#5593 - Improved setOpen callback logic in <SidebarProvider> .

Update the setOpen callback in <SidebarProvider> as follows:

1 const setOpen = React.useCallback(


2 (value: boolean | ((value: boolean) => boolean)) => {
3 const openState = typeof value === "function" ? value(open) : value
4 if (setOpenProp) {
5 setOpenProp(openState)
6 } else {
7 _setOpen(openState)
8 }
9

10 // This sets the cookie to keep the sidebar state.


11 document.cookie = `${SIDEBAR_COOKIE_NAME}=${openState}; path=/; max-ag
12 },
13 [setOpenProp, open]
14 )

2024-10-21 Fixed text-sidebar-foreground

#5491 - Moved text-sidebar-foreground from <SidebarProvider> to <Sidebar>


component.

2024-10-20 Typo in useSidebar hook.

Fixed typo in useSidebar hook.


sidebar.tsx

1 - throw new Error("useSidebar must be used within a Sidebar.")


2 + throw new Error("useSidebar must be used within a SidebarProvider.")

Manual Accordion

Built by shadcn. The source code is available on GitHub.

You might also like