{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "sidebar",
  "title": "Sidebar",
  "description": "An application sidebar that collapses to icons or off canvas on desktop and opens as a sheet on mobile, with a keyboard shortcut.",
  "dependencies": [
    "@radix-ui/react-slot",
    "class-variance-authority",
    "lucide-react"
  ],
  "registryDependencies": [
    "@corsair-ui/utils",
    "@corsair-ui/button",
    "@corsair-ui/separator",
    "@corsair-ui/sheet",
    "@corsair-ui/tooltip",
    "@corsair-ui/use-media-query"
  ],
  "files": [
    {
      "path": "registry/default/ui/sidebar.tsx",
      "content": "\"use client\";\n\nimport { Slot } from \"@radix-ui/react-slot\";\nimport { cva, type VariantProps } from \"class-variance-authority\";\nimport { PanelLeftIcon } from \"lucide-react\";\nimport {\n  createContext,\n  useCallback,\n  useContext,\n  useEffect,\n  useId,\n  useMemo,\n  useRef,\n  useState,\n  type ComponentProps,\n  type CSSProperties,\n  type MouseEvent,\n  type RefObject,\n} from \"react\";\n\nimport { useMediaQuery } from \"@/registry/default/hooks/use-media-query\";\nimport { cn } from \"@/registry/default/lib/utils\";\nimport { Button, type ButtonProps } from \"@/registry/default/ui/button\";\nimport { Separator } from \"@/registry/default/ui/separator\";\nimport { Sheet, SheetContent, SheetTitle } from \"@/registry/default/ui/sheet\";\nimport { Tooltip, TooltipContent, TooltipTrigger } from \"@/registry/default/ui/tooltip\";\n\nconst SIDEBAR_MOBILE_QUERY = \"(max-width: 767px)\";\nconst SIDEBAR_WIDTH_ICON = \"3rem\";\n\ntype SidebarState = \"expanded\" | \"collapsed\";\ntype SidebarSide = \"left\" | \"right\";\ntype SidebarCollapsible = \"icon\" | \"offcanvas\" | \"none\";\n\ninterface SidebarContextValue {\n  /** Whether the desktop sidebar is expanded. */\n  open: boolean;\n  setOpen: (open: boolean) => void;\n  /** Opens or closes the sidebar for the current viewport: the column on desktop, the sheet on mobile. */\n  toggle: () => void;\n  /** True below 768px, where the sidebar is a sheet. */\n  isMobile: boolean;\n  /** Whether the mobile sheet is open. */\n  openMobile: boolean;\n  setOpenMobile: (open: boolean) => void;\n  /** `open` as a word, for `data-state`. */\n  state: SidebarState;\n}\n\ninterface SidebarInternals extends SidebarContextValue {\n  sidebarId: string;\n  width: string;\n  shortcut: string | null;\n  /** Where focus goes back to when the mobile sheet closes. */\n  returnFocusRef: RefObject<HTMLElement | null>;\n}\n\nconst SidebarContext = createContext<SidebarInternals | null>(null);\n\nfunction useSidebarInternals() {\n  const context = useContext(SidebarContext);\n  if (!context) throw new Error(\"useSidebar must be used inside a SidebarProvider.\");\n  return context;\n}\n\n/**\n * The sidebar's state, for building your own controls: `open` / `setOpen`\n * for the desktop column, `openMobile` / `setOpenMobile` for the mobile\n * sheet, `toggle` for whichever applies, `isMobile` and `state`. Call\n * `setOpenMobile(false)` after navigating to close the sheet on phones.\n * Throws outside a `SidebarProvider`.\n *\n * @example\n * const { state, toggle } = useSidebar();\n */\nfunction useSidebar(): SidebarContextValue {\n  const { open, setOpen, toggle, isMobile, openMobile, setOpenMobile, state } =\n    useSidebarInternals();\n  return { open, setOpen, toggle, isMobile, openMobile, setOpenMobile, state };\n}\n\ninterface SidebarProviderProps extends ComponentProps<\"div\"> {\n  /** Controlled: whether the desktop sidebar is expanded. */\n  open?: boolean;\n  /** Uncontrolled: whether the desktop sidebar starts expanded. */\n  defaultOpen?: boolean;\n  /** Called when the desktop sidebar expands or collapses. */\n  onOpenChange?: (open: boolean) => void;\n  /**\n   * Key that toggles the sidebar together with ⌘ (macOS) or Ctrl. `null`\n   * turns the shortcut off.\n   */\n  shortcut?: string | null;\n  /** Width of the expanded sidebar, any CSS length. Sets `--sidebar-width`. */\n  width?: string;\n}\n\n/**\n * Holds the state of an application sidebar and lays it out next to the\n * page: a flex row with `Sidebar` and `SidebarInset` inside. On desktop the\n * sidebar collapses and expands; below 768px it becomes a sheet that opens\n * over the page. ⌘B / Ctrl+B toggles it (change the key with `shortcut`,\n * turn it off with `null`). Controlled with `open` / `onOpenChange` or\n * uncontrolled with `defaultOpen`. The widths are the CSS variables\n * `--sidebar-width` and `--sidebar-width-icon` on the wrapper.\n *\n * @example\n * <SidebarProvider>\n *   <Sidebar>\n *     <SidebarContent>…</SidebarContent>\n *   </Sidebar>\n *   <SidebarInset>\n *     <SidebarTrigger />\n *     …\n *   </SidebarInset>\n * </SidebarProvider>\n */\nfunction SidebarProvider({\n  open: openProp,\n  defaultOpen = true,\n  onOpenChange,\n  shortcut = \"b\",\n  width = \"16rem\",\n  className,\n  style,\n  children,\n  ...props\n}: SidebarProviderProps) {\n  const isMobile = useMediaQuery(SIDEBAR_MOBILE_QUERY);\n  const sidebarId = useId();\n  const returnFocusRef = useRef<HTMLElement | null>(null);\n  const [openState, setOpenState] = useState(defaultOpen);\n  const [openMobile, setOpenMobileState] = useState(false);\n  const open = openProp ?? openState;\n\n  const setOpen = useCallback(\n    (next: boolean) => {\n      if (openProp === undefined) setOpenState(next);\n      onOpenChange?.(next);\n    },\n    [openProp, onOpenChange]\n  );\n\n  const setOpenMobile = useCallback((next: boolean) => {\n    // Remember what had focus, so closing the sheet can hand it back.\n    if (next && document.activeElement instanceof HTMLElement) {\n      returnFocusRef.current = document.activeElement;\n    }\n    setOpenMobileState(next);\n  }, []);\n\n  const toggle = useCallback(() => {\n    if (isMobile) setOpenMobile(!openMobile);\n    else setOpen(!open);\n  }, [isMobile, open, openMobile, setOpen, setOpenMobile]);\n\n  useEffect(() => {\n    if (!shortcut) return;\n    const key = shortcut.toLowerCase();\n    const onKeyDown = (event: KeyboardEvent) => {\n      if (event.key.toLowerCase() !== key) return;\n      if (!(event.metaKey || event.ctrlKey) || event.altKey || event.shiftKey) return;\n      event.preventDefault();\n      toggle();\n    };\n    window.addEventListener(\"keydown\", onKeyDown);\n    return () => window.removeEventListener(\"keydown\", onKeyDown);\n  }, [shortcut, toggle]);\n\n  const state: SidebarState = open ? \"expanded\" : \"collapsed\";\n  const value = useMemo<SidebarInternals>(\n    () => ({\n      open,\n      setOpen,\n      toggle,\n      isMobile,\n      openMobile,\n      setOpenMobile,\n      state,\n      sidebarId,\n      width,\n      shortcut,\n      returnFocusRef,\n    }),\n    [open, setOpen, toggle, isMobile, openMobile, setOpenMobile, state, sidebarId, width, shortcut]\n  );\n\n  return (\n    <SidebarContext.Provider value={value}>\n      <div\n        data-slot=\"sidebar-wrapper\"\n        data-state={state}\n        className={cn(\"flex min-h-svh w-full\", className)}\n        style={\n          {\n            \"--sidebar-width\": width,\n            \"--sidebar-width-icon\": SIDEBAR_WIDTH_ICON,\n            ...style,\n          } as CSSProperties\n        }\n        {...props}\n      >\n        {children}\n      </div>\n    </SidebarContext.Provider>\n  );\n}\n\ninterface SidebarPanelContextValue {\n  /** Collapsed to a strip of icons: text is visually hidden and tooltips take over. */\n  iconCollapsed: boolean;\n  side: SidebarSide;\n}\n\nconst SidebarPanelContext = createContext<SidebarPanelContextValue>({\n  iconCollapsed: false,\n  side: \"left\",\n});\n\ninterface SidebarProps extends Omit<ComponentProps<\"aside\">, \"id\"> {\n  /** The edge of the page the sidebar sits on. */\n  side?: SidebarSide;\n  /**\n   * What collapsing does: `icon` shrinks it to a strip of icons, `offcanvas`\n   * slides it out of view, `none` keeps it expanded.\n   */\n  collapsible?: SidebarCollapsible;\n  /** Title of the mobile sheet, which names it for screen readers (visually hidden). */\n  mobileLabel?: string;\n}\n\n/**\n * The sidebar itself. On desktop it is an `<aside>` column (a complementary\n * landmark named by `aria-label`, \"Sidebar\" by default) with `data-state`,\n * `data-collapsible` and `data-side` for styling; its width animates when it\n * collapses, except with reduced motion. Collapsed to icons, every menu\n * button keeps its accessible name (the text is visually hidden, not\n * removed) and shows a tooltip on hover and focus. Collapsed off-canvas, it\n * is inert, so its links leave the Tab order. Below 768px it renders in a\n * Sheet instead: a dialog that traps focus, closes on Escape and gives\n * focus back to what opened it. Its id comes from the provider and is the\n * target of `SidebarTrigger`'s `aria-controls`.\n *\n * @example\n * <Sidebar collapsible=\"icon\" aria-label=\"Main\">\n *   <SidebarHeader>Acme</SidebarHeader>\n *   <SidebarContent>\n *     <SidebarGroup>\n *       <SidebarGroupLabel>Workspace</SidebarGroupLabel>\n *       <SidebarMenu>\n *         <SidebarMenuItem>\n *           <SidebarMenuButton asChild isActive tooltip=\"Inbox\">\n *             <a href=\"/inbox\">\n *               <InboxIcon aria-hidden=\"true\" />\n *               <span>Inbox</span>\n *             </a>\n *           </SidebarMenuButton>\n *         </SidebarMenuItem>\n *       </SidebarMenu>\n *     </SidebarGroup>\n *   </SidebarContent>\n * </Sidebar>\n */\nfunction Sidebar({\n  side = \"left\",\n  collapsible = \"icon\",\n  mobileLabel = \"Navigation\",\n  \"aria-label\": label = \"Sidebar\",\n  className,\n  children,\n  ...props\n}: SidebarProps) {\n  const { isMobile, openMobile, setOpenMobile, state, sidebarId, width, returnFocusRef } =\n    useSidebarInternals();\n\n  if (isMobile && collapsible !== \"none\") {\n    return (\n      <Sheet open={openMobile} onOpenChange={setOpenMobile}>\n        <SheetContent\n          id={sidebarId}\n          side={side}\n          showCloseButton={false}\n          data-slot=\"sidebar\"\n          data-mobile=\"true\"\n          data-side={side}\n          aria-describedby={undefined}\n          className=\"bg-card text-card-foreground w-[var(--sidebar-width)] gap-0 p-0\"\n          style={{ \"--sidebar-width\": width } as CSSProperties}\n          onCloseAutoFocus={(event) => {\n            // No SheetTrigger to return to: hand focus back to what opened it.\n            event.preventDefault();\n            returnFocusRef.current?.focus();\n          }}\n        >\n          <SheetTitle className=\"sr-only\">{mobileLabel}</SheetTitle>\n          <SidebarPanelContext.Provider value={{ iconCollapsed: false, side }}>\n            <div data-slot=\"sidebar-inner\" className=\"flex h-full w-full flex-col\">\n              {children}\n            </div>\n          </SidebarPanelContext.Provider>\n        </SheetContent>\n      </Sheet>\n    );\n  }\n\n  const resolvedState: SidebarState = collapsible === \"none\" ? \"expanded\" : state;\n  const collapsed = resolvedState === \"collapsed\";\n  const iconCollapsed = collapsed && collapsible === \"icon\";\n  const offcanvasCollapsed = collapsed && collapsible === \"offcanvas\";\n\n  return (\n    <aside\n      id={sidebarId}\n      data-slot=\"sidebar\"\n      data-state={resolvedState}\n      data-collapsible={collapsible}\n      data-side={side}\n      aria-label={label}\n      inert={offcanvasCollapsed ? true : undefined}\n      className={cn(\n        \"group/sidebar text-card-foreground sticky top-0 h-svh w-[var(--sidebar-width)] shrink-0 overflow-hidden\",\n        collapsible === \"none\" ? \"flex\" : \"hidden md:flex\",\n        \"transition-[width] ease-linear motion-reduce:transition-none\",\n        side === \"right\" && \"justify-end\",\n        iconCollapsed && \"w-[var(--sidebar-width-icon)]\",\n        offcanvasCollapsed && \"w-0\",\n        className\n      )}\n      {...props}\n    >\n      <div\n        data-slot=\"sidebar-inner\"\n        className={cn(\n          \"bg-card flex h-full w-[var(--sidebar-width)] shrink-0 flex-col\",\n          \"transition-transform ease-linear motion-reduce:transition-none\",\n          side === \"left\" ? \"border-r\" : \"border-l\",\n          collapsible === \"icon\" && \"w-full\",\n          offcanvasCollapsed && (side === \"left\" ? \"-translate-x-full\" : \"translate-x-full\")\n        )}\n      >\n        <SidebarPanelContext.Provider value={{ iconCollapsed, side }}>\n          {children}\n        </SidebarPanelContext.Provider>\n      </div>\n    </aside>\n  );\n}\n\ninterface SidebarTriggerProps extends ButtonProps {\n  /** Accessible name of the button. */\n  label?: string;\n}\n\n/**\n * The button that expands and collapses the sidebar (or opens the sheet on\n * mobile). It reports `aria-expanded`, points `aria-controls` at the sidebar\n * and announces the keyboard shortcut through `aria-keyshortcuts`.\n *\n * @example\n * <SidebarTrigger label=\"Toggle navigation\" />\n */\nfunction SidebarTrigger({\n  label = \"Toggle sidebar\",\n  className,\n  onClick,\n  children,\n  ...props\n}: SidebarTriggerProps) {\n  const { toggle, isMobile, open, openMobile, sidebarId, shortcut, returnFocusRef } =\n    useSidebarInternals();\n  const expanded = isMobile ? openMobile : open;\n  const key = shortcut ? shortcut.toUpperCase() : null;\n\n  return (\n    <Button\n      data-slot=\"sidebar-trigger\"\n      data-state={expanded ? \"expanded\" : \"collapsed\"}\n      variant=\"ghost\"\n      size=\"icon\"\n      aria-label={label}\n      aria-expanded={expanded}\n      // The mobile sheet only exists while open.\n      aria-controls={!isMobile || openMobile ? sidebarId : undefined}\n      aria-keyshortcuts={key ? `Meta+${key} Control+${key}` : undefined}\n      className={cn(\"size-7\", className)}\n      onClick={(event: MouseEvent<HTMLButtonElement>) => {\n        onClick?.(event);\n        if (event.defaultPrevented) return;\n        toggle();\n        // Safari does not focus buttons on click: remember the trigger itself.\n        returnFocusRef.current = event.currentTarget;\n      }}\n      {...props}\n    >\n      {children ?? <PanelLeftIcon aria-hidden=\"true\" />}\n    </Button>\n  );\n}\n\n/** The top of the sidebar: brand, workspace switcher. */\nfunction SidebarHeader({ className, ...props }: ComponentProps<\"div\">) {\n  return (\n    <div\n      data-slot=\"sidebar-header\"\n      className={cn(\"flex flex-col gap-2 p-2\", className)}\n      {...props}\n    />\n  );\n}\n\n/** The scrolling middle of the sidebar, between header and footer. */\nfunction SidebarContent({ className, ...props }: ComponentProps<\"div\">) {\n  const { iconCollapsed } = useContext(SidebarPanelContext);\n  return (\n    <div\n      data-slot=\"sidebar-content\"\n      className={cn(\n        \"flex min-h-0 flex-1 flex-col gap-2 overflow-auto\",\n        iconCollapsed && \"overflow-x-hidden\",\n        className\n      )}\n      {...props}\n    />\n  );\n}\n\n/** The bottom of the sidebar: account menu, settings. */\nfunction SidebarFooter({ className, ...props }: ComponentProps<\"div\">) {\n  return (\n    <div\n      data-slot=\"sidebar-footer\"\n      className={cn(\"flex flex-col gap-2 p-2\", className)}\n      {...props}\n    />\n  );\n}\n\n/** A section of the sidebar, usually a label and a menu. */\nfunction SidebarGroup({ className, ...props }: ComponentProps<\"div\">) {\n  return (\n    <div\n      data-slot=\"sidebar-group\"\n      className={cn(\"relative flex w-full min-w-0 flex-col p-2\", className)}\n      {...props}\n    />\n  );\n}\n\n/**\n * The heading of a group. Collapsed to icons it is visually hidden but\n * still read by screen readers.\n */\nfunction SidebarGroupLabel({ className, ...props }: ComponentProps<\"div\">) {\n  const { iconCollapsed } = useContext(SidebarPanelContext);\n  return (\n    <div\n      data-slot=\"sidebar-group-label\"\n      className={cn(\n        \"text-muted-foreground flex h-8 shrink-0 items-center rounded-md px-2 text-xs font-medium\",\n        iconCollapsed && \"sr-only\",\n        className\n      )}\n      {...props}\n    />\n  );\n}\n\n/** A list of menu items (`<ul>`), so screen readers announce how many there are. */\nfunction SidebarMenu({ className, ...props }: ComponentProps<\"ul\">) {\n  return (\n    <ul\n      data-slot=\"sidebar-menu\"\n      className={cn(\"flex w-full min-w-0 flex-col gap-1\", className)}\n      {...props}\n    />\n  );\n}\n\n/** One entry of a SidebarMenu (`<li>`): a button or link, and optionally a badge. */\nfunction SidebarMenuItem({ className, ...props }: ComponentProps<\"li\">) {\n  return (\n    <li\n      data-slot=\"sidebar-menu-item\"\n      className={cn(\"group/menu-item relative\", className)}\n      {...props}\n    />\n  );\n}\n\nconst sidebarMenuButtonVariants = cva(\n  [\n    \"flex w-full cursor-pointer items-center gap-2 overflow-hidden rounded-md p-2 text-left text-sm\",\n    \"transition-[width,height,padding,color,background-color,box-shadow] outline-none motion-reduce:transition-none\",\n    \"hover:bg-accent hover:text-accent-foreground focus-visible:ring-ring/50 focus-visible:ring-[3px]\",\n    \"data-[active=true]:bg-accent data-[active=true]:text-accent-foreground data-[active=true]:font-medium\",\n    \"disabled:pointer-events-none disabled:opacity-50 aria-disabled:pointer-events-none aria-disabled:opacity-50\",\n    \"group-has-[[data-slot=sidebar-menu-badge]]/menu-item:pr-8\",\n    \"[&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 [&>span:last-child]:truncate\",\n  ],\n  {\n    variants: {\n      size: {\n        default: \"h-8\",\n        sm: \"h-7 text-xs\",\n        lg: \"h-12\",\n      },\n    },\n    defaultVariants: {\n      size: \"default\",\n    },\n  }\n);\n\ninterface SidebarMenuButtonProps\n  extends ComponentProps<\"button\">, VariantProps<typeof sidebarMenuButtonVariants> {\n  /** Render the single child (a link, a router Link) with the button styles instead. */\n  asChild?: boolean;\n  /**\n   * Marks the entry as the current one: `data-active`, and with `asChild`\n   * (a link) `aria-current=\"page\"`.\n   */\n  isActive?: boolean;\n  /** Shown in a tooltip while the sidebar is collapsed to icons. */\n  tooltip?: string;\n}\n\n/**\n * A button or, with `asChild`, a link in the sidebar menu. Put the icon\n * first and the text in a `<span>`: collapsed to icons, the text is visually\n * hidden and stays the accessible name, and `tooltip` appears on hover and\n * keyboard focus. The active link is announced as the current page.\n *\n * @example\n * <SidebarMenuButton asChild isActive={pathname === \"/inbox\"} tooltip=\"Inbox\">\n *   <a href=\"/inbox\">\n *     <InboxIcon aria-hidden=\"true\" />\n *     <span>Inbox</span>\n *   </a>\n * </SidebarMenuButton>\n */\nfunction SidebarMenuButton({\n  asChild = false,\n  isActive = false,\n  tooltip,\n  size,\n  className,\n  type,\n  ...props\n}: SidebarMenuButtonProps) {\n  const { iconCollapsed, side } = useContext(SidebarPanelContext);\n  const Comp = asChild ? Slot : \"button\";\n\n  const button = (\n    <Comp\n      data-slot=\"sidebar-menu-button\"\n      data-size={size ?? \"default\"}\n      data-active={isActive}\n      aria-current={isActive && asChild ? \"page\" : undefined}\n      type={asChild ? type : (type ?? \"button\")}\n      className={cn(\n        sidebarMenuButtonVariants({ size }),\n        iconCollapsed && \"size-8 justify-center p-2 [&>:not(svg)]:sr-only\",\n        className\n      )}\n      {...props}\n    />\n  );\n\n  if (!tooltip) return button;\n\n  // The tooltip stays mounted so collapsing does not remount (and blur) the button.\n  return (\n    <Tooltip>\n      <TooltipTrigger asChild>{button}</TooltipTrigger>\n      {iconCollapsed ? (\n        <TooltipContent side={side === \"left\" ? \"right\" : \"left\"} align=\"center\">\n          {tooltip}\n        </TooltipContent>\n      ) : null}\n    </Tooltip>\n  );\n}\n\n/**\n * A count or status next to a menu button. It sits outside the button, so\n * it is read after the button's name; hidden while collapsed to icons.\n */\nfunction SidebarMenuBadge({ className, ...props }: ComponentProps<\"span\">) {\n  const { iconCollapsed } = useContext(SidebarPanelContext);\n  return (\n    <span\n      data-slot=\"sidebar-menu-badge\"\n      className={cn(\n        \"text-muted-foreground pointer-events-none absolute top-1.5 right-1 flex h-5 min-w-5 items-center justify-center rounded-md px-1 text-xs font-medium tabular-nums select-none\",\n        iconCollapsed && \"hidden\",\n        className\n      )}\n      {...props}\n    />\n  );\n}\n\n/** A line between sections of the sidebar. Decorative unless `decorative={false}`. */\nfunction SidebarSeparator({ className, ...props }: ComponentProps<typeof Separator>) {\n  return (\n    <Separator data-slot=\"sidebar-separator\" className={cn(\"mx-2 w-auto\", className)} {...props} />\n  );\n}\n\n/** The page next to the sidebar: a `<main>` landmark that takes the remaining width. */\nfunction SidebarInset({ className, ...props }: ComponentProps<\"main\">) {\n  return (\n    <main\n      data-slot=\"sidebar-inset\"\n      className={cn(\"bg-background relative flex min-w-0 flex-1 flex-col\", className)}\n      {...props}\n    />\n  );\n}\n\nexport {\n  Sidebar,\n  SidebarContent,\n  SidebarFooter,\n  SidebarGroup,\n  SidebarGroupLabel,\n  SidebarHeader,\n  SidebarInset,\n  SidebarMenu,\n  SidebarMenuBadge,\n  SidebarMenuButton,\n  SidebarMenuItem,\n  SidebarProvider,\n  SidebarSeparator,\n  SidebarTrigger,\n  sidebarMenuButtonVariants,\n  useSidebar,\n  type SidebarCollapsible,\n  type SidebarContextValue,\n  type SidebarMenuButtonProps,\n  type SidebarProps,\n  type SidebarProviderProps,\n  type SidebarSide,\n  type SidebarState,\n  type SidebarTriggerProps,\n};\n",
      "type": "registry:ui"
    }
  ],
  "type": "registry:ui"
}