Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Feat/sidebar (#33474)

Jonathan Summers-Muirfecb0808fceb
diff --git a/apps/design-system/config/docs.ts b/apps/design-system/config/docs.ts
index 471a9c89158..e96ec0b2e5c 100644
--- a/apps/design-system/config/docs.ts
+++ b/apps/design-system/config/docs.ts
@@ -349,6 +349,11 @@ export const docsConfig: DocsConfig = {
href: '/docs/components/slider',
items: [],
},
+ {
+ title: 'Sidebar',
+ href: '/docs/components/sidebar',
+ items: [],
+ },
{
title: 'Sonner',
href: '/docs/components/sonner',
diff --git a/apps/design-system/content/docs/components/sidebar.mdx b/apps/design-system/content/docs/components/sidebar.mdx
new file mode 100644
index 00000000000..f8706d0f4ae
--- /dev/null
+++ b/apps/design-system/content/docs/components/sidebar.mdx
@@ -0,0 +1,1372 @@
+---
+title: Sidebar
+description: A composable, themeable and customizable sidebar component.
+component: true
+---
+
+<figure className="flex flex-col gap-4">
+ <ComponentPreview
+ name="sidebar-07"
+ title="Sidebar"
+ type="block"
+ description="A composable, themeable and customizable sidebar component built using shadcn/ui"
+ className="w-full"
+ />
+ <figcaption className="text-center text-sm text-gray-500">
+ A sidebar that collapses to icons.
+ </figcaption>
+</figure>
+
+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](/blocks).
+
+## Installation
+
+<Tabs defaultValue="cli">
+
+<TabsList>
+ <TabsTrigger value="cli">CLI</TabsTrigger>
+ <TabsTrigger value="manual">Manual</TabsTrigger>
+</TabsList>
+<TabsContent value="cli">
+
+<Steps>
+
+<Step>Run the following command to install `sidebar.tsx`</Step>
+
+```bash
+npx shadcn@latest add sidebar
+```
+
+<Step>Add the following colors to your CSS file</Step>
+
+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](/docs/components/sidebar#theming).
+
+```css title="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%;
+ }
+}
+```
+
+</Steps>
+
+</TabsContent>
+
+<TabsContent value="manual">
+
+<Steps>
+
+<Step>Copy and paste the following code into your project.</Step>
+
+<ComponentSource name="sidebar" />
+
+<Step>Update the import paths to match your project setup.</Step>
+
+<Step>Add the following colors to your CSS file</Step>
+
+We'll go over the colors later in the [theming section](/docs/components/sidebar#theming).
+
+```css title="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%;
+ }
+}
+```
+
+<Step>Add the Sidebar tailwind config into `tailwind.config.js`</Step>
+
+Add the following object in the `theme.extend.colors` section of your `tailwind.config.js` file
+this config enable sidebar related style utilities like bg-sidebar
+
+```javascript title="tailwind.config.js"
+// ...
+sidebar: {
+ DEFAULT: 'hsl(var(--sidebar-background))',
+ foreground: 'hsl(var(--sidebar-foreground))',
+ primary: 'hsl(var(--sidebar-primary))',
+ 'primary-foreground': 'hsl(var(--sidebar-primary-foreground))',
+ accent: 'hsl(var(--sidebar-accent))',
+ 'accent-foreground': 'hsl(var(--sidebar-accent-foreground))',
+ border: 'hsl(var(--sidebar-border))',
+ ring: 'hsl(var(--sidebar-ring))',
+},
+// ...
+```
+
+</Steps>
+
+</TabsContent>
+
+</Tabs>
+
+## 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`.
+
+<Image
+ src="/images/sidebar-structure.png"
+ width="716"
+ height="420"
+ alt="Sidebar Structure"
+ className="border dark:hidden rounded-lg overflow-hidden mt-6 w-full"
+/>
+<Image
+ src="/images/sidebar-structure-dark.png"
+ width="716"
+ height="420"
+ alt="Sidebar Structure"
+ className="border hidden dark:block rounded-lg overflow-hidden mt-6 w-full"
+/>
+
+## Usage
+
+```tsx showLineNumbers title="app/layout.tsx"
+import { SidebarProvider, SidebarTrigger } from '@/components/ui/sidebar'
+import { AppSidebar } from '@/components/app-sidebar'
+
+export default function Layout({ children }: { children: React.ReactNode }) {
+ return (
+ <SidebarProvider>
+ <AppSidebar />
+ <main>
+ <SidebarTrigger />
+ {children}
+ </main>
+ </SidebarProvider>
+ )
+}
+```
+
+```tsx showLineNumbers title="components/app-sidebar.tsx"
+import {
+ Sidebar,
+ SidebarContent,
+ SidebarFooter,
+ SidebarGroup,
+ SidebarHeader,
+} from '@/components/ui/sidebar'
+
+export function AppSidebar() {
+ return (
+ <Sidebar>
+ <SidebarHeader />
+ <SidebarContent>
+ <SidebarGroup />
+ <SidebarGroup />
+ </SidebarContent>
+ <SidebarFooter />
+ </Sidebar>
+ )
+}
+```
+
+## Your First Sidebar
+
+Let's start with the most basic sidebar. A collapsible sidebar with a menu.
+
+<Steps>
+
+<Step>Add a `SidebarProvider` and `SidebarTrigger` at the root of your application.</Step>
+
+```tsx showLineNumbers title="app/layout.tsx"
+import { SidebarProvider, SidebarTrigger } from '@/components/ui/sidebar'
+import { AppSidebar } from '@/components/app-sidebar'
+
+export default function Layout({ children }: { children: React.ReactNode }) {
+ return (
+ <SidebarProvider>
+ <AppSidebar />
+ <main>
+ <SidebarTrigger />
+ {children}
+ </main>
+ </SidebarProvider>
+ )
+}
+```
+
+<Step>Create a new sidebar component at `components/app-sidebar.tsx`.</Step>
+
+```tsx showLineNumbers title="components/app-sidebar.tsx"
+import { Sidebar, SidebarContent } from '@/components/ui/sidebar'
+
+export function AppSidebar() {
+ return (
+ <Sidebar>
+ <SidebarContent />
+ </Sidebar>
+ )
+}
+```
+
+<Step>Now, let's add a `SidebarMenu` to the sidebar.</Step>
+
+We'll use the `SidebarMenu` component in a `SidebarGroup`.
+
+```tsx showLineNumbers title="components/app-sidebar.tsx"
+import { Calendar, Home, Inbox, Search, Settings } from 'lucide-react'
+
+import {
+ Sidebar,
+ SidebarContent,
+ SidebarGroup,
+ SidebarGroupContent,
+ SidebarGroupLabel,
+ SidebarMenu,
+ SidebarMenuButton,
+ SidebarMenuItem,
+} from '@/components/ui/sidebar'
+
+// Menu items.
+const items = [
+ {
+ title: 'Home',
+ url: '#',
+ icon: Home,
+ },
+ {
+ title: 'Inbox',
+ url: '#',
+ icon: Inbox,
+ },
+ {
+ title: 'Calendar',
+ url: '#',
+ icon: Calendar,
+ },
+ {
+ title: 'Search',
+ url: '#',
+ icon: Search,
+ },
+ {
+ title: 'Settings',
+ url: '#',
+ icon: Settings,
+ },
+]
+
+export function AppSidebar() {
+ return (
+ <Sidebar>
+ <SidebarContent>
+ <SidebarGroup>
+ <SidebarGroupLabel>Application</SidebarGroupLabel>
+ <SidebarGroupContent>
+ <SidebarMenu>
+ {items.map((item) => (
+ <SidebarMenuItem key={item.title}>
+ <SidebarMenuButton asChild>
+ <a href={item.url}>
+ <item.icon />
+ <span>{item.title}</span>
+ </a>
+ </SidebarMenuButton>
+ </SidebarMenuItem>
+ ))}
+ </SidebarMenu>
+ </SidebarGroupContent>
+ </SidebarGroup>
+ </SidebarContent>
+ </Sidebar>
+ )
+}
+```
+
+<Step>You've created your first sidebar.</Step>
+
+<figure className="flex flex-col gap-4">
+ <ComponentPreview
+ name="sidebar-demo"
+ title="Sidebar"
+ type="block"
+ description="Your first sidebar."
+ className="w-full"
+ />
+ <figcaption className="text-center text-sm text-gray-500">Your first sidebar.</figcaption>
+</figure>
+
+</Steps>
+
+## 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.
+
+```tsx showLineNumbers title="components/ui/sidebar.tsx"
+const SIDEBAR_WIDTH = '16rem'
+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.
+
+```tsx showLineNumbers title="components/ui/sidebar.tsx"
+<SidebarProvider
+ style={{
+ '--sidebar-width': '20rem',
+ '--sidebar-width-mobile': '20rem',
+ }}
+>
+ <Sidebar />
+</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.
+
+```tsx showLineNumbers title="components/ui/sidebar.tsx"
+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:
+
+```tsx showLineNumbers title="app/layout.tsx"
+import { cookies } from 'next/headers'
+
+import { SidebarProvider, SidebarTrigger } from '@/components/ui/sidebar'
+import { AppSidebar } from '@/components/app-sidebar'
+
+export async function Layout({ children }: { children: React.ReactNode }) {
+ const cookieStore = await cookies()
+ const defaultOpen = cookieStore.get('sidebar_state')?.value === 'true'
+
+ return (
+ <SidebarProvider defaultOpen={defaultOpen}>
+ <AppSidebar />
+ <main>
+ <SidebarTrigger />
+ {children}
+ </main>
+ </SidebarProvider>
+ )
+}
+```
+
+You can change the name of the cookie by updating the `SIDEBAR_COOKIE_NAME` variable in `sidebar.tsx`.
+
+```tsx showLineNumbers title="components/ui/sidebar.tsx"
+const SIDEBAR_COOKIE_NAME = 'sidebar_state'
+```
+
+## Sidebar
+
+The main `Sidebar` component used to render a collapsible sidebar.
+
+```tsx showLineNumbers
+import { Sidebar } from '@/components/ui/sidebar'
+
+export function AppSidebar() {
+ return <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`.
+
+```tsx showLineNumbers
+import { Sidebar } from '@/components/ui/sidebar'
+
+export function AppSidebar() {
+ return <Sidebar side="left | right" />
+}
+```
+
+### variant
+
+Use the `variant` prop to change the variant of the sidebar.
+
+Available options are `sidebar`, `floating` and `inset`.
+
+```tsx showLineNumbers
+import { Sidebar } from '@/components/ui/sidebar'
+
+export function AppSidebar() {
+ return <Sidebar variant="sidebar | floating | inset" />
+}
+```
+
+<Callout>
+ **Note:** If you use the `inset` variant, remember to wrap your main content in a `SidebarInset`
+ component.
+</Callout>
+
+```tsx showLineNumbers
+<SidebarProvider>
+ <Sidebar variant="inset" />
+ <SidebarInset>
+ <main>{children}</main>
+ </SidebarInset>
+</SidebarProvider>
+```
+
+### collapsible
+
+Use the `collapsible` prop to make the sidebar collapsible.
+
+Available options are `offcanvas`, `icon` and `none`.
+
+```tsx showLineNumbers
+import { Sidebar } from '@/components/ui/sidebar'
+
+export function AppSidebar() {
+ return <Sidebar collapsible="offcanvas | icon | 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.
+
+```tsx showLineNumbers
+import { useSidebar } from '@/components/ui/sidebar'
+
+export function AppSidebar() {
+ const { state, open, setOpen, openMobile, setOpenMobile, isMobile, toggleSidebar } = useSidebar()
+}
+```
+
+| Property | Type | Description |
+| --------------- | ------------------------- | --------------------------------------------- |
+| `state` | `expanded` or `collapsed` | The current state of the sidebar. |
+| `open` | `boolean` | Whether the sidebar is open. |
+| `setOpen` | `(open: boolean) => void` | Sets the open state of the sidebar. |
+| `openMobile` | `boolean` | Whether the sidebar is open on mobile. |
+| `setOpenMobile` | `(open: boolean) => void` | Sets the open state of the sidebar on 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`.
+
+<figure className="flex flex-col gap-4 mt-6">
+ <ComponentPreview
+ name="sidebar-header"
+ title="Sidebar"
+ type="block"
+ description="A sidebar header with a dropdown menu."
+ className="w-full"
+ />
+ <figcaption className="text-center text-sm text-gray-500">
+ A sidebar header with a dropdown menu.
+ </figcaption>
+</figure>
+
+```tsx showLineNumbers title="components/app-sidebar.tsx"
+<Sidebar>
+ <SidebarHeader>
+ <SidebarMenu>
+ <SidebarMenuItem>
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <SidebarMenuButton>
+ Select Workspace
+ <ChevronDown className="ml-auto" />
+ </SidebarMenuButton>
+ </DropdownMenuTrigger>
+ <DropdownMenuContent className="w-[--radix-popper-anchor-width]">
+ <DropdownMenuItem>
+ <span>Acme Inc</span>
+ </DropdownMenuItem>
+ <DropdownMenuItem>
+ <span>Acme Corp.</span>
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ </SidebarMenuItem>
+ </SidebarMenu>
+ </SidebarHeader>
+</Sidebar>
+```
+
+## SidebarFooter
+
+Use the `SidebarFooter` component to add a sticky footer to the sidebar.
+
+The following example adds a `<DropdownMenu>` to the `SidebarFooter`.
+
+<figure className="flex flex-col gap-4 mt-6">
+ <ComponentPreview
+ name="sidebar-footer"
+ title="Sidebar"
+ type="block"
+ description="A sidebar footer with a dropdown menu."
+ className="w-full"
+ />
+ <figcaption className="text-center text-sm text-gray-500">
+ A sidebar footer with a dropdown menu.
+ </figcaption>
+</figure>
+
+```tsx showLineNumbers title="components/app-sidebar.tsx"
+export function AppSidebar() {
+ return (
+ <SidebarProvider>
+ <Sidebar>
+ <SidebarHeader />
+ <SidebarContent />
+ <SidebarFooter>
+ <SidebarMenu>
+ <SidebarMenuItem>
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <SidebarMenuButton>
+ <User2 /> Username
+ <ChevronUp className="ml-auto" />
+ </SidebarMenuButton>
+ </DropdownMenuTrigger>
+ <DropdownMenuContent side="top" className="w-[--radix-popper-anchor-width]">
+ <DropdownMenuItem>
+ <span>Account</span>
+ </DropdownMenuItem>
+ <DropdownMenuItem>
+ <span>Billing</span>
+ </DropdownMenuItem>
+ <DropdownMenuItem>
+ <span>Sign out</span>
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ </SidebarMenuItem>
+ </SidebarMenu>
+ </SidebarFooter>
+ </Sidebar>
+ </SidebarProvider>
+ )
+}
+```
+
+## SidebarContent
+
+The `SidebarContent` component is used to wrap the content of the sidebar. This is where you add your `SidebarGroup` components. It is scrollable.
+
+```tsx showLineNumbers
+import { Sidebar, SidebarContent } from '@/components/ui/sidebar'
+
+export function AppSidebar() {
+ return (
+ <Sidebar>
+ <SidebarContent>
+ <SidebarGroup />
+ <SidebarGroup />
+ </SidebarContent>
+ </Sidebar>
+ )
+}
+```
+
+## SidebarGroup
+
+Use the `SidebarGroup` component to create a section within the sidebar.
+
+A `SidebarGroup` has a `SidebarGroupLabel`, a `SidebarGroupContent` and an optional `SidebarGroupAction`.
+
+<figure className="flex flex-col gap-4 mt-6">
+ <ComponentPreview
+ name="sidebar-group"
+ title="Sidebar Group"
+ type="block"
+ description="A sidebar group."
+ className="w-full"
+ />
+ <figcaption className="text-center text-sm text-gray-500">A sidebar group.</figcaption>
+</figure>
+
+```tsx showLineNumbers
+import { Sidebar, SidebarContent, SidebarGroup } from '@/components/ui/sidebar'
+
+export function AppSidebar() {
+ return (
+ <Sidebar>
+ <SidebarContent>
+ <SidebarGroup>
+ <SidebarGroupLabel>Application</SidebarGroupLabel>
+ <SidebarGroupAction>
+ <Plus /> <span className="sr-only">Add Project</span>
+ </SidebarGroupAction>
+ <SidebarGroupContent></SidebarGroupContent>
+ </SidebarGroup>
+ </SidebarContent>
+ </Sidebar>
+ )
+}
+```
+
+## Collapsible SidebarGroup
+
+To make a `SidebarGroup` collapsible, wrap it in a `Collapsible`.
+
+<figure className="flex flex-col gap-4 mt-6">
+ <ComponentPreview
+ name="sidebar-group-collapsible"
+ title="Sidebar Group"
+ type="block"
+ description="A collapsible sidebar group."
+ className="w-full"
+ />
+ <figcaption className="text-center text-sm text-gray-500">
+ A collapsible sidebar group.
+ </figcaption>
+</figure>
+
+```tsx showLineNumbers
+export function AppSidebar() {
+ return (
+ <Collapsible defaultOpen className="group/collapsible">
+ <SidebarGroup>
+ <SidebarGroupLabel asChild>
+ <CollapsibleTrigger>
+ Help
+ <ChevronDown className="ml-auto transition-transform group-data-[state=open]/collapsible:rotate-180" />
+ </CollapsibleTrigger>
+ </SidebarGroupLabel>
+ <CollapsibleContent>
+ <SidebarGroupContent />
+ </CollapsibleContent>
+ </SidebarGroup>
+ </Collapsible>
+ )
+}
+```
+
+<Callout>
+ **Note:** We wrap the `CollapsibleTrigger` in a `SidebarGroupLabel` to render a button.
+</Callout>
+
+## SidebarGroupAction
+
+Use the `SidebarGroupAction` component to add an action button to the `SidebarGroup`.
+
+```tsx showLineNumbers {5-7}
+export function AppSidebar() {
+ return (
+ <SidebarGroup>
+ <SidebarGroupLabel asChild>Projects</SidebarGroupLabel>
+ <SidebarGroupAction title="Add Project">
+ <Plus /> <span className="sr-only">Add Project</span>
+ </SidebarGroupAction>
+ <SidebarGroupContent />
+ </SidebarGroup>
+ )
+}
+```
+
+<figure className="flex flex-col gap-4">
+ <ComponentPreview
+ name="sidebar-group-action"
+ title="Sidebar Group"
+ type="block"
+ description="A sidebar group with an action button."
+ className="w-full"
+ />
+ <figcaption className="text-center text-sm text-gray-500">
+ A sidebar group with an action button.
+ </figcaption>
+</figure>
+
+## 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.
+
+<Image
+ src="/images/sidebar-menu.png"
+ width="716"
+ height="420"
+ alt="Sidebar Menu"
+ className="border dark:hidden rounded-lg overflow-hidden mt-6 w-full"
+/>
+<Image
+ src="/images/sidebar-menu-dark.png"
+ width="716"
+ height="420"
+ alt="Sidebar Menu"
+ className="border hidden dark:block rounded-lg overflow-hidden mt-6 w-full"
+/>
+
+Here's an example of a `SidebarMenu` component rendering a list of projects.
+
+<figure className="flex flex-col gap-4 mt-6">
+ <ComponentPreview
+ name="sidebar-menu"
+ title="Sidebar Menu"
+ type="block"
+ description="A sidebar menu with a list of projects."
+ className="w-full"
+ />
+ <figcaption className="text-center text-sm text-gray-500">
+ A sidebar menu with a list of projects.
+ </figcaption>
+</figure>
+
+```tsx showLineNumbers
+<Sidebar>
+ <SidebarContent>
+ <SidebarGroup>
+ <SidebarGroupLabel>Projects</SidebarGroupLabel>
+ <SidebarGroupContent>
+ <SidebarMenu>
+ {projects.map((project) => (
+ <SidebarMenuItem key={project.name}>
+ <SidebarMenuButton asChild>
+ <a href={project.url}>
+ <project.icon />
+ <span>{project.name}</span>
+ </a>
+ </SidebarMenuButton>
+ </SidebarMenuItem>
+ ))}
+ </SidebarMenu>
+ </SidebarGroupContent>
+ </SidebarGroup>
+ </SidebarContent>
+</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.
+
+```tsx showLineNumbers
+<SidebarMenuButton asChild>
+ <a href="#">Home</a>
+</SidebarMenuButton>
+```
+
+### Icon and Label
+
+You can render an icon and a truncated label inside the button. Remember to wrap the label in a `<span>`.
+
+```tsx showLineNumbers
+<SidebarMenuButton asChild>
+ <a href="#">
+ <Home />
+ <span>Home</span>
+ </a>
+</SidebarMenuButton>
+```
+
+### isActive
+
+Use the `isActive` prop to mark a menu item as active.
+
+```tsx showLineNumbers
+<SidebarMenuButton asChild isActive>
+ <a href="#">Home</a>
+</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.
+
+```tsx showLineNumbers
+<SidebarMenuItem>
+ <SidebarMenuButton asChild>
+ <a href="#">
+ <Home />
+ <span>Home</span>
+ </a>
+ </SidebarMenuButton>
+ <SidebarMenuAction>
+ <Plus /> <span className="sr-only">Add Project</span>
+ </SidebarMenuAction>
+</SidebarMenuItem>
+```
+
+### DropdownMenu
+
+Here's an example of a `SidebarMenuAction` component rendering a `DropdownMenu`.
+
+<figure className="flex flex-col gap-4 mt-6">
+ <ComponentPreview
+ name="sidebar-menu-action"
+ title="Sidebar Menu Action"
+ type="block"
+ description="A sidebar menu action with a dropdown menu."
+ className="w-full"
+ />
+ <figcaption className="text-center text-sm text-gray-500">
+ A sidebar menu action with a dropdown menu.
+ </figcaption>
+</figure>
+
+```tsx showLineNumbers
+<SidebarMenuItem>
+ <SidebarMenuButton asChild>
+ <a href="#">
+ <Home />
+ <span>Home</span>
+ </a>
+ </SidebarMenuButton>
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <SidebarMenuAction>
+ <MoreHorizontal />
+ </SidebarMenuAction>
+ </DropdownMenuTrigger>
+ <DropdownMenuContent side="right" align="start">
+ <DropdownMenuItem>
+ <span>Edit Project</span>
+ </DropdownMenuItem>
+ <DropdownMenuItem>
+ <span>Delete Project</span>
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
+</SidebarMenuItem>
+```
+
+## SidebarMenuSub
+
+The `SidebarMenuSub` component is used to render a submenu within a `SidebarMenu`.
+
+Use `<SidebarMenuSubItem />` and `<SidebarMenuSubButton />` to render a submenu item.
+
+<figure className="flex flex-col gap-4 mt-6">
+ <ComponentPreview
+ name="sidebar-menu-sub"
+ title="Sidebar Menu Sub"
+ type="block"
+ description="A sidebar menu sub."
+ className="w-full"
+ />
+ <figcaption className="text-center text-sm text-gray-500">
+ A sidebar menu with a submenu.
+ </figcaption>
+</figure>
+
+```tsx showLineNumbers
+<SidebarMenuItem>
+ <SidebarMenuButton />
+ <SidebarMenuSub>
+ <SidebarMenuSubItem>
+ <SidebarMenuSubButton />
+ </SidebarMenuSubItem>
+ <SidebarMenuSubItem>
+ <SidebarMenuSubButton />
+ </SidebarMenuSubItem>
+ </SidebarMenuSub>
+</SidebarMenuItem>
+```
+
+## Collapsible SidebarMenu
+
+To make a `SidebarMenu` component collapsible, wrap it and the `SidebarMenuSub` components in a `Collapsible`.
+
+<figure className="flex flex-col gap-4 mt-6">
+ <ComponentPreview
+ name="sidebar-menu-collapsible"
+ title="Sidebar Menu"
+ type="block"
+ description="A collapsible sidebar menu."
+ className="w-full"
+ />
+ <figcaption className="text-center text-sm text-gray-500">A collapsible sidebar menu.</figcaption>
+</figure>
+
+```tsx showLineNumbers
+<SidebarMenu>
+ <Collapsible defaultOpen className="group/collapsible">
+ <SidebarMenuItem>
+ <CollapsibleTrigger asChild>
+ <SidebarMenuButton />
+ </CollapsibleTrigger>
+ <CollapsibleContent>
+ <SidebarMenuSub>
+ <SidebarMenuSubItem />
+ </SidebarMenuSub>
+ </CollapsibleContent>
+ </SidebarMenuItem>
+ </Collapsible>
+</SidebarMenu>
+```
+
+## SidebarMenuBadge
+
+The `SidebarMenuBadge` component is used to render a badge within a `SidebarMenuItem`.
+
+<figure className="flex flex-col gap-4 mt-6">
+ <ComponentPreview
+ name="sidebar-menu-badge"
+ title="Sidebar Menu Badge"
+ type="block"
+ description="A sidebar menu badge."
+ className="w-full"
+ />
+ <figcaption className="text-center text-sm text-gray-500">
+ A sidebar menu with a badge.
+ </figcaption>
+</figure>
+
+```tsx showLineNumbers
+<SidebarMenuItem>
+ <SidebarMenuButton />
+ <SidebarMenuBadge>24</SidebarMenuBadge>
+</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.
+
+```tsx showLineNumbers
+function NavProjectsSkeleton() {
+ return (
+ <SidebarMenu>
+ {Array.from({ length: 5 }).map((_, index) => (
+ <SidebarMenuItem key={index}>
+ <SidebarMenuSkeleton />
+ </SidebarMenuItem>
+ ))}
+ </SidebarMenu>
+ )
+}
+```
+
+## SidebarSeparator
+
+The `SidebarSeparator` component is used to render a separator within a `Sidebar`.
+
+```tsx showLineNumbers
+<Sidebar>
+ <SidebarHeader />
+ <SidebarSeparator />
+ <SidebarContent>
+ <SidebarGroup />
+ <SidebarSeparator />
+ <SidebarGroup />
+ </SidebarContent>
+</Sidebar>
+```
+
+## SidebarTrigger
+
+Use the `SidebarTrigger` component to render a button that toggles the sidebar.
+
+The `SidebarTrigger` component must be used within a `SidebarProvider`.
+
+```tsx showLineNumbers
+<SidebarProvider>
+ <Sidebar />
+ <main>
+ <SidebarTrigger />
+ </main>
+</SidebarProvider>
+```
+
+### Custom Trigger
+
+To create a custom trigger, you can use the `useSidebar` hook.
+
+```tsx showLineNumbers
+import { useSidebar } from '@/components/ui/sidebar'
+
+export function CustomTrigger() {
+ const { toggleSidebar } = useSidebar()
+
+ return <button onClick={toggleSidebar}>Toggle Sidebar</button>
+}
+```
+
+## SidebarRail
+
+The `SidebarRail` component is used to render a rail within a `Sidebar`. This rail can be used to toggle the sidebar.
+
+```tsx showLineNumbers
+<Sidebar>
+ <SidebarHeader />
+ <SidebarContent>
+ <SidebarGroup />
+ </SidebarContent>
+ <SidebarFooter />
+ <SidebarRail />
+</Sidebar>
+```
+
+## Data Fetching
+
+### React Server Components
+
+Here's an example of a `SidebarMenu` component rendering a list of projects using React Server Components.
+
+<figure className="flex flex-col gap-4 mt-6">
+ <ComponentPreview
+ name="sidebar-rsc"
+ title="Sidebar Menu RSC"
+ type="block"
+ description="A sidebar menu using React Server Components."
+ className="w-full"
+ />
+ <figcaption className="text-center text-sm text-gray-500">
+ A sidebar menu using React Server Components.
+ </figcaption>
+</figure>
+
+```tsx showLineNumbers {6} title="Skeleton to show loading state."
+function NavProjectsSkeleton() {
+ return (
+ <SidebarMenu>
+ {Array.from({ length: 5 }).map((_, index) => (
+ <SidebarMenuItem key={index}>
+ <SidebarMenuSkeleton showIcon />
+ </SidebarMenuItem>
+ ))}
+ </SidebarMenu>
+ )
+}
+```
+
+```tsx showLineNumbers {2} title="Server component fetching data."
+async function NavProjects() {
+ const projects = await fetchProjects()
+
+ return (
+ <SidebarMenu>
+ {projects.map((project) => (
+ <SidebarMenuItem key={project.name}>
+ <SidebarMenuButton asChild>
+ <a href={project.url}>
+ <project.icon />
+ <span>{project.name}</span>
+ </a>
+ </SidebarMenuButton>
+ </SidebarMenuItem>
+ ))}
+ </SidebarMenu>
+ )
+}
+```
+
+```tsx showLineNumbers {8-10} title="Usage with React Suspense."
+function AppSidebar() {
+ return (
+ <Sidebar>
+ <SidebarContent>
+ <SidebarGroup>
+ <SidebarGroupLabel>Projects</SidebarGroupLabel>
+ <SidebarGroupContent>
+ <React.Suspense fallback={<NavProjectsSkeleton />}>
+ <NavProjects />
+ </React.Suspense>
+ </SidebarGroupContent>
+ </SidebarGroup>
+ </SidebarContent>
+ </Sidebar>
+ )
+}
+```
+
+### SWR and React Query
+
+You can use the same approach with [SWR](https://swr.vercel.app/) or [react-query](https://tanstack.com/query/latest/docs/framework/react/overview).
+
+```tsx showLineNumbers title="SWR"
+function NavProjects() {
+ const { data, isLoading } = useSWR("/api/projects", fetcher)
+
+ if (isLoading) {
+ return (
+ <SidebarMenu>
+ {Array.from({ length: 5 }).map((_, index) => (
+ <SidebarMenuItem key={index}>
+ <SidebarMenuSkeleton showIcon />
+ </SidebarMenuItem>
+ ))}
+ </SidebarMenu>
+ )
+ }
+
+ if (!data) {
+ return ...
+ }
+
+ return (
+ <SidebarMenu>
+ {data.map((project) => (
+ <SidebarMenuItem key={project.name}>
+ <SidebarMenuButton asChild>
+ <a href={project.url}>
+ <project.icon />
+ <span>{project.name}</span>
+ </a>
+ </SidebarMenuButton>
+ </SidebarMenuItem>
+ ))}
+ </SidebarMenu>
+ )
+}
+```
+
+```tsx showLineNumbers title="React Query"
+function NavProjects() {
+ const { data, isLoading } = useQuery()
+
+ if (isLoading) {
+ return (
+ <SidebarMenu>
+ {Array.from({ length: 5 }).map((_, index) => (
+ <SidebarMenuItem key={index}>
+ <SidebarMenuSkeleton showIcon />
+ </SidebarMenuItem>
+ ))}
+ </SidebarMenu>
+ )
+ }
+
+ if (!data) {
+ return ...
+ }
+
+ return (
+ <SidebarMenu>
+ {data.map((project) => (
+ <SidebarMenuItem key={project.name}>
+ <SidebarMenuButton asChild>
+ <a href={project.url}>
+ <project.icon />
+ <span>{project.name}</span>
+ </a>
+ </SidebarMenuButton>
+ </SidebarMenuItem>
+ ))}
+ </SidebarMenu>
+ )
+}
+```
+
+## Controlled Sidebar
+
+Use the `open` and `onOpenChange` props to control the sidebar.
+
+<figure className="flex flex-col gap-4 mt-6">
+ <ComponentPreview
+ name="sidebar-controlled"
+ title="Sidebar Controlled"
+ type="block"
+ description="A controlled sidebar."
+ className="w-full"
+ />
+ <figcaption className="text-center text-sm text-gray-500">A controlled sidebar.</figcaption>
+</figure>
+
+```tsx showLineNumbers
+export function AppSidebar() {
+ const [open, setOpen] = React.useState(false)
+
+ return (
+ <SidebarProvider open={open} onOpenChange={setOpen}>
+ <Sidebar />
+ </SidebarProvider>
+ )
+}
+```
+
+## Theming
+
+We use the following CSS variables to theme the sidebar.
+
+```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: 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.
+
+```tsx
+<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.
+
+```tsx
+<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](https://x.com/shadcn/status/1842329158879420864).
+
+## Changelog
+
+### 2024-10-30 Cookie handling in setOpen
+
+- [#5593](https://github.com/shadcn-ui/ui/pull/5593) - Improved setOpen callback logic in `<SidebarProvider>`.
+
+Update the `setOpen` callback in `<SidebarProvider>` as follows:
+
+```tsx showLineNumbers
+const setOpen = React.useCallback(
+ (value: boolean | ((value: boolean) => boolean)) => {
+ const openState = typeof value === 'function' ? value(open) : value
+ if (setOpenProp) {
+ setOpenProp(openState)
+ } else {
+ _setOpen(openState)
+ }
+
+ // This sets the cookie to keep the sidebar state.
+ document.cookie = `${SIDEBAR_COOKIE_NAME}=${openState}; path=/; max-age=${SIDEBAR_COOKIE_MAX_AGE}`
+ },
+ [setOpenProp, open]
+)
+```
+
+### 2024-10-21 Fixed `text-sidebar-foreground`
+
+- [#5491](https://github.com/shadcn-ui/ui/pull/5491) - Moved `text-sidebar-foreground` from `<SidebarProvider>` to `<Sidebar>` component.
+
+### 2024-10-20 Typo in `useSidebar` hook.
+
+Fixed typo in `useSidebar` hook.
+
+```diff showLineNumbers title="sidebar.tsx"
+- throw new Error("useSidebar must be used within a Sidebar.")
++ throw new Error("useSidebar must be used within a SidebarProvider.")
+```
diff --git a/packages/ui/index.tsx b/packages/ui/index.tsx
index 166fe56b8d7..37bf296a2ae 100644
--- a/packages/ui/index.tsx
+++ b/packages/ui/index.tsx
@@ -250,6 +250,7 @@ export * from './src/components/radio-group-card'
export * from './src/components/TreeView'
export * from './src/components/shadcn/ui/chart'
+export * from './src/components/shadcn/ui/sidebar'
// links
diff --git a/packages/ui/src/components/shadcn/hooks/useMobile.ts b/packages/ui/src/components/shadcn/hooks/useMobile.ts
new file mode 100644
index 00000000000..4331d5c5620
--- /dev/null
+++ b/packages/ui/src/components/shadcn/hooks/useMobile.ts
@@ -0,0 +1,19 @@
+import * as React from 'react'
+
+const MOBILE_BREAKPOINT = 768
+
+export function useIsMobile() {
+ const [isMobile, setIsMobile] = React.useState<boolean | undefined>(undefined)
+
+ React.useEffect(() => {
+ const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`)
+ const onChange = () => {
+ setIsMobile(window.innerWidth < MOBILE_BREAKPOINT)
+ }
+ mql.addEventListener('change', onChange)
+ setIsMobile(window.innerWidth < MOBILE_BREAKPOINT)
+ return () => mql.removeEventListener('change', onChange)
+ }, [])
+
+ return !!isMobile
+}
diff --git a/packages/ui/src/components/shadcn/ui/sidebar.tsx b/packages/ui/src/components/shadcn/ui/sidebar.tsx
new file mode 100644
index 00000000000..77aea8b0612
--- /dev/null
+++ b/packages/ui/src/components/shadcn/ui/sidebar.tsx
@@ -0,0 +1,740 @@
+'use client'
+
+import * as React from 'react'
+import { Slot } from '@radix-ui/react-slot'
+import { VariantProps, cva } from 'class-variance-authority'
+import { PanelLeft } from 'lucide-react'
+
+import { useIsMobile } from '../hooks/useMobile'
+import { cn } from './../../../lib/utils'
+import { Button } from './button'
+import { Input } from './input'
+import { Separator } from './separator'
+import { Sheet, SheetContent } from './sheet'
+import { Skeleton } from './skeleton'
+import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from './tooltip'
+
+const SIDEBAR_COOKIE_NAME = 'sidebar:state'
+const SIDEBAR_COOKIE_MAX_AGE = 60 * 60 * 24 * 7
+const SIDEBAR_WIDTH = '13rem'
+const SIDEBAR_WIDTH_MOBILE = '18rem'
+const SIDEBAR_WIDTH_ICON = '3rem'
+const SIDEBAR_KEYBOARD_SHORTCUT = '['
+
+type SidebarContext = {
+ state: 'expanded' | 'collapsed'
+ open: boolean
+ setOpen: (open: boolean) => void
+ openMobile: boolean
+ setOpenMobile: (open: boolean) => void
+ isMobile: boolean
+ toggleSidebar: () => void
+}
+
+const SidebarContext = React.createContext<SidebarContext | null>(null)
+
+function useSidebar() {
+ const context = React.useContext(SidebarContext)
+ if (!context) {
+ throw new Error('useSidebar must be used within a SidebarProvider.')
+ }
+
+ return context
+}
+
+const SidebarProvider = React.forwardRef<
+ HTMLDivElement,
+ React.ComponentProps<'div'> & {
+ defaultOpen?: boolean
+ open?: boolean
+ onOpenChange?: (open: boolean) => void
+ }
+>(
+ (
+ {
+ defaultOpen = true,
+ open: openProp,
+ onOpenChange: setOpenProp,
+ className,
+ style,
+ children,
+ ...props
+ },
+ ref
+ ) => {
+ const isMobile = useIsMobile()
+ const [openMobile, setOpenMobile] = React.useState(false)
+
+ // This is the internal state of the sidebar.
+ // We use openProp and setOpenProp for control from outside the component.
+ const [_open, _setOpen] = React.useState(defaultOpen)
+ const open = openProp ?? _open
+ const setOpen = React.useCallback(
+ (value: boolean | ((value: boolean) => boolean)) => {
+ const openState = typeof value === 'function' ? value(open) : value
+ if (setOpenProp) {
+ setOpenProp(openState)
+ } else {
+ _setOpen(openState)
+ }
+
+ // This sets the cookie to keep the sidebar state.
+ document.cookie = `${SIDEBAR_COOKIE_NAME}=${openState}; path=/; max-age=${SIDEBAR_COOKIE_MAX_AGE}`
+ },
+ [setOpenProp, open]
+ )
+
+ // Helper to toggle the sidebar.
+ const toggleSidebar = React.useCallback(() => {
+ return isMobile ? setOpenMobile((open) => !open) : setOpen((open) => !open)
+ }, [isMobile, setOpen, setOpenMobile])
+
+ // Adds a keyboard shortcut to toggle the sidebar.
+ React.useEffect(() => {
+ const handleKeyDown = (event: KeyboardEvent) => {
+ if (event.key === SIDEBAR_KEYBOARD_SHORTCUT && (event.metaKey || event.ctrlKey)) {
+ event.preventDefault()
+ toggleSidebar()
+ }
+ }
+
+ window.addEventListener('keydown', handleKeyDown)
+ return () => window.removeEventListener('keydown', handleKeyDown)
+ }, [toggleSidebar])
+
+ // We add a state so that we can do data-state="expanded" or "collapsed".
+ // This makes it easier to style the sidebar with Tailwind classes.
+ const state = open ? 'expanded' : 'collapsed'
+
+ const contextValue = React.useMemo<SidebarContext>(
+ () => ({
+ state,
+ open,
+ setOpen,
+ isMobile,
+ openMobile,
+ setOpenMobile,
+ toggleSidebar,
+ }),
+ [state, open, setOpen, isMobile, openMobile, setOpenMobile, toggleSidebar]
+ )
+
+ return (
+ <SidebarContext.Provider value={contextValue}>
+ <TooltipProvider delayDuration={0}>
+ <div
+ style={
+ {
+ '--sidebar-width': SIDEBAR_WIDTH,
+ '--sidebar-width-icon': SIDEBAR_WIDTH_ICON,
+ ...style,
+ } as React.CSSProperties
+ }
+ className={cn(
+ 'group/sidebar-wrapper flex min-h-svh w-full has-[[data-variant=inset]]:bg-sidebar',
+ className
+ )}
+ ref={ref}
+ {...props}
+ >
+ {children}
+ </div>
+ </TooltipProvider>
+ </SidebarContext.Provider>
+ )
+ }
+)
+SidebarProvider.displayName = 'SidebarProvider'
+
+const Sidebar = React.forwardRef<
+ HTMLDivElement,
+ React.ComponentProps<'div'> & {
+ side?: 'left' | 'right'
+ variant?: 'sidebar' | 'floating' | 'inset'
+ collapsible?: 'offcanvas' | 'icon' | 'none'
+ }
+>(
+ (
+ {
+ side = 'left',
+ variant = 'sidebar',
+ collapsible = 'offcanvas',
+ className,
+ children,
+ ...props
+ },
+ ref
+ ) => {
+ const { isMobile, state, openMobile, setOpenMobile } = useSidebar()
+
+ if (collapsible === 'none') {
+ return (
+ <div
+ className={cn(
+ 'flex h-full w-[--sidebar-width] flex-col bg-sidebar text-sidebar-foreground',
+ className
+ )}
+ ref={ref}
+ {...props}
+ >
+ {children}
+ </div>
+ )
+ }
+
+ if (isMobile) {
+ return (
+ <Sheet open={openMobile} onOpenChange={setOpenMobile} {...props}>
+ <SheetContent
+ data-sidebar="sidebar"
+ data-mobile="true"
+ className="w-[--sidebar-width] bg-sidebar p-0 text-sidebar-foreground [&>button]:hidden"
+ style={
+ {
+ '--sidebar-width': SIDEBAR_WIDTH_MOBILE,
+ } as React.CSSProperties
+ }
+ side={side}
+ >
+ <div className="flex h-full w-full flex-col">{children}</div>
+ </SheetContent>
+ </Sheet>
+ )
+ }
+
+ return (
+ <div
+ ref={ref}
+ className="relative group peer hidden md:block text-sidebar-foreground"
+ data-state={state}
+ data-collapsible={state === 'collapsed' ? collapsible : ''}
+ data-variant={variant}
+ data-side={side}
+ >
+ {/* This is what handles the sidebar gap on desktop */}
+ <div
+ className={cn(
+ 'relative', // sidebar custom changes - to contain the absolute sidebar below
+ //
+ 'duration-200 h-svh w-[--sidebar-width] bg-transparent transition-[width] ease-linear',
+ 'group-data-[collapsible=offcanvas]:w-0',
+ 'group-data-[side=right]:rotate-180',
+ variant === 'floating' || variant === 'inset'
+ ? 'group-data-[collapsible=icon]:w-[calc(var(--sidebar-width-icon)_+_theme(spacing.4))]'
+ : 'group-data-[collapsible=icon]:w-[--sidebar-width-icon]'
+ )}
+ />
+ <div
+ className={cn(
+ 'absolute top-0 h-full', // sidebar custom changes - We have also removed 'fixed', and 'h-svh'
+ //
+ 'duration-200 inset-y-0 z-10 hidden w-[--sidebar-width] transition-[left,right,width] ease-linear md:flex',
+ side === 'left'
+ ? 'left-0 group-data-[collapsible=offcanvas]:left-[calc(var(--sidebar-width)*-1)]'
+ : 'right-0 group-data-[collapsible=offcanvas]:right-[calc(var(--sidebar-width)*-1)]',
+ // Adjust the padding for floating and inset variants.
+ variant === 'floating' || variant === 'inset'
+ ? 'p-2 group-data-[collapsible=icon]:w-[calc(var(--sidebar-width-icon)_+_theme(spacing.4)_+2px)]'
+ : 'group-data-[collapsible=icon]:w-[--sidebar-width-icon] group-data-[side=left]:border-r group-data-[side=right]:border-l',
+ className
+ )}
+ {...props}
+ >
+ <div
+ data-sidebar="sidebar"
+ className="flex h-full w-full flex-col bg-sidebar group-data-[variant=floating]:rounded-lg group-data-[variant=floating]:border group-data-[variant=floating]:border-sidebar-border group-data-[variant=floating]:shadow"
+ >
+ {children}
+ </div>
+ </div>
+ </div>
+ )
+ }
+)
+Sidebar.displayName = 'Sidebar'
+
+const SidebarTrigger = React.forwardRef<
+ React.ElementRef<typeof Button>,
+ React.ComponentProps<typeof Button>
+>(({ className, onClick, ...props }, ref) => {
+ const { toggleSidebar } = useSidebar()
+
+ return (
+ <Button
+ ref={ref}
+ data-sidebar="trigger"
+ variant="ghost"
+ size="icon"
+ className={cn('h-7 w-7', className)}
+ onClick={(event) => {
+ onClick?.(event)
+ toggleSidebar()
+ }}
+ {...props}
+ >
+ <PanelLeft />
+ <span className="sr-only">Toggle Sidebar</span>
+ </Button>
+ )
+})
+SidebarTrigger.displayName = 'SidebarTrigger'
+
+const SidebarRail = React.forwardRef<HTMLButtonElement, React.ComponentProps<'button'>>(
+ ({ className, ...props }, ref) => {
+ const { toggleSidebar } = useSidebar()
+
+ return (
+ <button
+ ref={ref}
+ data-sidebar="rail"
+ aria-label="Toggle Sidebar"
+ tabIndex={-1}
+ onClick={toggleSidebar}
+ title="Toggle Sidebar"
+ className={cn(
+ 'absolute inset-y-0 z-20 hidden w-4 -translate-x-1/2 transition-all ease-linear after:absolute after:inset-y-0 after:left-1/2 after:w-[2px] hover:after:bg-sidebar-border group-data-[side=left]:-right-4 group-data-[side=right]:left-0 sm:flex',
+ '[[data-side=left]_&]:cursor-w-resize [[data-side=right]_&]:cursor-e-resize',
+ '[[data-side=left][data-state=collapsed]_&]:cursor-e-resize [[data-side=right][data-state=collapsed]_&]:cursor-w-resize',
+ 'group-data-[collapsible=offcanvas]:translate-x-0 group-data-[collapsible=offcanvas]:after:left-full group-data-[collapsible=offcanvas]:hover:bg-sidebar',
+ '[[data-side=left][data-collapsible=offcanvas]_&]:-right-2',
+ '[[data-side=right][data-collapsible=offcanvas]_&]:-left-2',
+ className
+ )}
+ {...props}
+ />
+ )
+ }
+)
+SidebarRail.displayName = 'SidebarRail'
+
+const SidebarInset = React.forwardRef<HTMLDivElement, React.ComponentProps<'main'>>(
+ ({ className, ...props }, ref) => {
+ return (
+ <main
+ ref={ref}
+ className={cn(
+ 'relative flex min-h-svh flex-1 flex-col bg-background',
+ 'peer-data-[variant=inset]:min-h-[calc(100svh-theme(spacing.4))] md:peer-data-[variant=inset]:m-2 md:peer-data-[state=collapsed]:peer-data-[variant=inset]:ml-2 md:peer-data-[variant=inset]:ml-0 md:peer-data-[variant=inset]:rounded-xl md:peer-data-[variant=inset]:shadow',
+ className
+ )}
+ {...props}
+ />
+ )
+ }
+)
+SidebarInset.displayName = 'SidebarInset'
+
+const SidebarInput = React.forwardRef<
+ React.ElementRef<typeof Input>,
+ React.ComponentProps<typeof Input>
+>(({ className, ...props }, ref) => {
+ return (
+ <Input
+ ref={ref}
+ data-sidebar="input"
+ className={cn(
+ 'h-8 w-full bg-background shadow-none focus-visible:ring-2 focus-visible:ring-sidebar-ring',
+ className
+ )}
+ {...props}
+ />
+ )
+})
+SidebarInput.displayName = 'SidebarInput'
+
+const SidebarHeader = React.forwardRef<HTMLDivElement, React.ComponentProps<'div'>>(
+ ({ className, ...props }, ref) => {
+ return (
+ <div
+ ref={ref}
+ data-sidebar="header"
+ className={cn('flex flex-col gap-2 p-2', className)}
+ {...props}
+ />
+ )
+ }
+)
+SidebarHeader.displayName = 'SidebarHeader'
+
+const SidebarFooter = React.forwardRef<HTMLDivElement, React.ComponentProps<'div'>>(
+ ({ className, ...props }, ref) => {
+ return (
+ <div
+ ref={ref}
+ data-sidebar="footer"
+ className={cn('flex flex-col gap-2 p-2', className)}
+ {...props}
+ />
+ )
+ }
+)
+SidebarFooter.displayName = 'SidebarFooter'
+
+const SidebarSeparator = React.forwardRef<
+ React.ElementRef<typeof Separator>,
+ React.ComponentProps<typeof Separator>
+>(({ className, ...props }, ref) => {
+ return (
+ <Separator
+ ref={ref}
+ data-sidebar="separator"
+ className={cn('mx-2 w-auto bg-sidebar-border', className)}
+ {...props}
+ />
+ )
+})
+SidebarSeparator.displayName = 'SidebarSeparator'
+
+const SidebarContent = React.forwardRef<HTMLDivElement, React.ComponentProps<'div'>>(
+ ({ className, ...props }, ref) => {
+ return (
+ <div
+ ref={ref}
+ data-sidebar="content"
+ className={cn(
+ 'flex min-h-0 flex-1 flex-col gap-2 overflow-auto group-data-[collapsible=icon]:overflow-hidden',
+ className
+ )}
+ {...props}
+ />
+ )
+ }
+)
+SidebarContent.displayName = 'SidebarContent'
+
+const SidebarGroup = React.forwardRef<HTMLDivElement, React.ComponentProps<'div'>>(
+ ({ className, ...props }, ref) => {
+ return (
+ <div
+ ref={ref}
+ data-sidebar="group"
+ className={cn('relative flex w-full min-w-0 flex-col p-2', className)}
+ {...props}
+ />
+ )
+ }
+)
+SidebarGroup.displayName = 'SidebarGroup'
+
+const SidebarGroupLabel = React.forwardRef<
+ HTMLDivElement,
+ React.ComponentProps<'div'> & { asChild?: boolean }
+>(({ className, asChild = false, ...props }, ref) => {
+ const Comp = asChild ? Slot : 'div'
+
+ return (
+ <Comp
+ ref={ref}
+ data-sidebar="group-label"
+ className={cn(
+ 'duration-200 flex h-8 shrink-0 items-center rounded-md px-2 text-xs font-medium text-sidebar-foreground/70 outline-none ring-sidebar-ring transition-[margin,opa] ease-linear focus-visible:ring-2 [&>svg]:size-5 [&>svg]:shrink-0',
+ 'group-data-[collapsible=icon]:-mt-8 group-data-[collapsible=icon]:opacity-0',
+ className
+ )}
+ {...props}
+ />
+ )
+})
+SidebarGroupLabel.displayName = 'SidebarGroupLabel'
+
+const SidebarGroupAction = React.forwardRef<
+ HTMLButtonElement,
+ React.ComponentProps<'button'> & { asChild?: boolean }
+>(({ className, asChild = false, ...props }, ref) => {
+ const Comp = asChild ? Slot : 'button'
+
+ return (
+ <Comp
+ ref={ref}
+ data-sidebar="group-action"
+ className={cn(
+ 'absolute right-3 top-3.5 flex aspect-square w-5 items-center justify-center rounded-md p-0 text-sidebar-foreground outline-none ring-sidebar-ring transition-transform hover:bg-sidebar-accent hover:text-sidebar-accent-foreground focus-visible:ring-2 [&>svg]:size-5 [&>svg]:shrink-0',
+ // Increases the hit area of the button on mobile.
+ 'after:absolute after:-inset-2 after:md:hidden',
+ 'group-data-[collapsible=icon]:hidden',
+ className
+ )}
+ {...props}
+ />
+ )
+})
+SidebarGroupAction.displayName = 'SidebarGroupAction'
+
+const SidebarGroupContent = React.forwardRef<HTMLDivElement, React.ComponentProps<'div'>>(
+ ({ className, ...props }, ref) => (
+ <div
+ ref={ref}
+ data-sidebar="group-content"
+ className={cn('w-full text-sm', className)}
+ {...props}
+ />
+ )
+)
+SidebarGroupContent.displayName = 'SidebarGroupContent'
+
+const SidebarMenu = React.forwardRef<HTMLUListElement, React.ComponentProps<'ul'>>(
+ ({ className, ...props }, ref) => (
+ <ul
+ ref={ref}
+ data-sidebar="menu"
+ className={cn('flex w-full min-w-0 flex-col gap-1', className)}
+ {...props}
+ />
+ )
+)
+SidebarMenu.displayName = 'SidebarMenu'
+
+const SidebarMenuItem = React.forwardRef<HTMLLIElement, React.ComponentProps<'li'>>(
+ ({ className, ...props }, ref) => (
+ <li
+ ref={ref}
+ data-sidebar="menu-item"
+ className={cn('group/menu-item relative', className)}
+ {...props}
+ />
+ )
+)
+SidebarMenuItem.displayName = 'SidebarMenuItem'
+
+const sidebarMenuButtonVariants = cva(
+ 'peer/menu-button flex w-full items-center gap-2 overflow-hidden rounded-md py-2 px-2 text-left text-sm outline-none ring-sidebar-ring transition-[width,height,padding] hover:bg-sidebar-accent hover:text-sidebar-accent-foreground focus-visible:ring-2 active:bg-sidebar-accent active:text-sidebar-accent-foreground disabled:pointer-events-none disabled:opacity-50 group-has-[[data-sidebar=menu-action]]/menu-item:pr-8 aria-disabled:pointer-events-none aria-disabled:opacity-50 data-[active=true]:bg-sidebar-accent data-[active=true]:font-medium data-[active=true]:text-sidebar-accent-foreground data-[state=open]:hover:bg-sidebar-accent data-[state=open]:hover:text-sidebar-accent-foreground group-data-[collapsible=icon]:!size-8 group-data-[collapsible=icon]:!pl-1.5 group-data-[collapsible=icon]:!pr-2 [&>span:last-child]:truncate [&>svg]:size-5 [&>svg]:shrink-0 text-foreground-lighter data-[active=true]:text-foreground',
+ {
+ variants: {
+ variant: {
+ default: 'hover:bg-sidebar-accent hover:text-sidebar-accent-foreground',
+ outline:
+ 'bg-background shadow-[0_0_0_1px_hsl(var(--sidebar-border))] hover:bg-sidebar-accent hover:text-sidebar-accent-foreground hover:shadow-[0_0_0_1px_hsl(var(--sidebar-accent))]',
+ },
+ size: {
+ default: 'h-8 text-sm',
+ sm: 'h-7 text-xs',
+ lg: 'h-12 text-sm group-data-[collapsible=icon]:!p-0',
+ },
+ },
+ defaultVariants: {
+ variant: 'default',
+ size: 'default',
+ },
+ }
+)
+
+const SidebarMenuButton = React.forwardRef<
+ HTMLButtonElement,
+ React.ComponentProps<'button'> & {
+ asChild?: boolean
+ isActive?: boolean
+ tooltip?: string | React.ComponentProps<typeof TooltipContent>
+ } & VariantProps<typeof sidebarMenuButtonVariants>
+>(
+ (
+ {
+ asChild = false,
+ isActive = false,
+ variant = 'default',
+ size = 'default',
+ tooltip,
+ className,
+ ...props
+ },
+ ref
+ ) => {
+ const Comp = asChild ? Slot : 'button'
+ const { isMobile, state } = useSidebar()
+
+ const button = (
+ <Comp
+ ref={ref}
+ data-sidebar="menu-button"
+ data-size={size}
+ data-active={isActive}
+ className={cn(sidebarMenuButtonVariants({ variant, size }), className)}
+ {...props}
+ />
+ )
+
+ if (!tooltip) {
+ return button
+ }
+
+ if (typeof tooltip === 'string') {
+ tooltip = {
+ children: tooltip,
+ }
+ }
+
+ return (
+ <Tooltip>
+ <TooltipTrigger asChild>{button}</TooltipTrigger>
+ <TooltipContent
+ side="right"
+ align="center"
+ hidden={state !== 'collapsed' || isMobile}
+ {...tooltip}
+ />
+ </Tooltip>
+ )
+ }
+)
+SidebarMenuButton.displayName = 'SidebarMenuButton'
+
+const SidebarMenuAction = React.forwardRef<
+ HTMLButtonElement,
+ React.ComponentProps<'button'> & {
+ asChild?: boolean
+ showOnHover?: boolean
+ }
+>(({ className, asChild = false, showOnHover = false, ...props }, ref) => {
+ const Comp = asChild ? Slot : 'button'
+
+ return (
+ <Comp
+ ref={ref}
+ data-sidebar="menu-action"
+ className={cn(
+ 'absolute right-1 top-1.5 flex aspect-square w-5 items-center justify-center rounded-md p-0 text-sidebar-foreground outline-none ring-sidebar-ring transition-transform hover:bg-sidebar-accent hover:text-sidebar-accent-foreground focus-visible:ring-2 peer-hover/menu-button:text-sidebar-accent-foreground [&>svg]:size-5 [&>svg]:shrink-0',
+ // Increases the hit area of the button on mobile.
+ 'after:absolute after:-inset-2 after:md:hidden',
+ 'peer-data-[size=sm]/menu-button:top-1',
+ 'peer-data-[size=default]/menu-button:top-1.5',
+ 'peer-data-[size=lg]/menu-button:top-2.5',
+ 'group-data-[collapsible=icon]:hidden',
+ showOnHover &&
+ 'group-focus-within/menu-item:opacity-100 group-hover/menu-item:opacity-100 data-[state=open]:opacity-100 peer-data-[active=true]/menu-button:text-sidebar-accent-foreground md:opacity-0',
+ className
+ )}
+ {...props}
+ />
+ )
+})
+SidebarMenuAction.displayName = 'SidebarMenuAction'
+
+const SidebarMenuBadge = React.forwardRef<HTMLDivElement, React.ComponentProps<'div'>>(
+ ({ className, ...props }, ref) => (
+ <div
+ ref={ref}
+ data-sidebar="menu-badge"
+ className={cn(
+ 'absolute right-1 flex h-5 min-w-5 items-center justify-center rounded-md px-1 text-xs font-medium tabular-nums text-sidebar-foreground select-none pointer-events-none',
+ 'peer-hover/menu-button:text-sidebar-accent-foreground peer-data-[active=true]/menu-button:text-sidebar-accent-foreground',
+ 'peer-data-[size=sm]/menu-button:top-1',
+ 'peer-data-[size=default]/menu-button:top-1.5',
+ 'peer-data-[size=lg]/menu-button:top-2.5',
+ 'group-data-[collapsible=icon]:hidden',
+ className
+ )}
+ {...props}
+ />
+ )
+)
+SidebarMenuBadge.displayName = 'SidebarMenuBadge'
+
+const SidebarMenuSkeleton = React.forwardRef<
+ HTMLDivElement,
+ React.ComponentProps<'div'> & {
+ showIcon?: boolean
+ }
+>(({ className, showIcon = false, ...props }, ref) => {
+ // Random width between 50 to 90%.
+ const width = React.useMemo(() => {
+ return `${Math.floor(Math.random() * 40) + 50}%`
+ }, [])
+
+ return (
+ <div
+ ref={ref}
+ data-sidebar="menu-skeleton"
+ className={cn('rounded-md h-8 flex gap-2 px-2 items-center', className)}
+ {...props}
+ >
+ {showIcon && <Skeleton className="size-4 rounded-md" data-sidebar="menu-skeleton-icon" />}
+ <Skeleton
+ className="h-4 flex-1 max-w-[--skeleton-width]"
+ data-sidebar="menu-skeleton-text"
+ style={
+ {
+ '--skeleton-width': width,
+ } as React.CSSProperties
+ }
+ />
+ </div>
+ )
+})
+SidebarMenuSkeleton.displayName = 'SidebarMenuSkeleton'
+
+const SidebarMenuSub = React.forwardRef<HTMLUListElement, React.ComponentProps<'ul'>>(
+ ({ className, ...props }, ref) => (
+ <ul
+ ref={ref}
+ data-sidebar="menu-sub"
+ className={cn(
+ 'mx-3.5 flex min-w-0 translate-x-px flex-col gap-1 border-l border-sidebar-border px-2.5 py-0.5',
+ 'group-data-[collapsible=icon]:hidden',
+ className
+ )}
+ {...props}
+ />
+ )
+)
+SidebarMenuSub.displayName = 'SidebarMenuSub'
+
+const SidebarMenuSubItem = React.forwardRef<HTMLLIElement, React.ComponentProps<'li'>>(
+ ({ ...props }, ref) => <li ref={ref} {...props} />
+)
+SidebarMenuSubItem.displayName = 'SidebarMenuSubItem'
+
+const SidebarMenuSubButton = React.forwardRef<
+ HTMLAnchorElement,
+ React.ComponentProps<'a'> & {
+ asChild?: boolean
+ size?: 'sm' | 'md'
+ isActive?: boolean
+ }
+>(({ asChild = false, size = 'md', isActive, className, ...props }, ref) => {
+ const Comp = asChild ? Slot : 'a'
+
+ return (
+ <Comp
+ ref={ref}
+ data-sidebar="menu-sub-button"
+ data-size={size}
+ data-active={isActive}
+ className={cn(
+ 'flex h-6 min-w-0 -translate-x-px items-center gap-2 overflow-hidden rounded-md px-2 text-sidebar-foreground outline-none ring-sidebar-ring hover:bg-sidebar-accent hover:text-sidebar-accent-foreground focus-visible:ring-2 active:bg-sidebar-accent active:text-sidebar-accent-foreground disabled:pointer-events-none disabled:opacity-50 aria-disabled:pointer-events-none aria-disabled:opacity-50 [&>span:last-child]:truncate [&>svg]:size-5 [&>svg]:shrink-0 [&>svg]:text-sidebar-accent-foreground',
+ 'data-[active=true]:bg-sidebar-accent data-[active=true]:text-sidebar-accent-foreground',
+ size === 'sm' && 'text-xs',
+ size === 'md' && 'text-sm',
+ 'group-data-[collapsible=icon]:hidden',
+ className
+ )}
+ {...props}
+ />
+ )
+})
+SidebarMenuSubButton.displayName = 'SidebarMenuSubButton'
+
+export {
+ Sidebar,
+ SidebarContent,
+ SidebarFooter,
+ SidebarGroup,
+ SidebarGroupAction,
+ SidebarGroupContent,
+ SidebarGroupLabel,
+ SidebarHeader,
+ SidebarInput,
+ SidebarInset,
+ SidebarMenu,
+ SidebarMenuAction,
+ SidebarMenuBadge,
+ SidebarMenuButton,
+ SidebarMenuItem,
+ SidebarMenuSkeleton,
+ SidebarMenuSub,
+ SidebarMenuSubButton,
+ SidebarMenuSubItem,
+ SidebarProvider,
+ SidebarRail,
+ SidebarSeparator,
+ SidebarTrigger,
+ useSidebar,
+}