suiss/ui · Component
Sidebar
A collapsible app sidebar for navigation, with icon, off-canvas and mobile sheet modes.
Sidebar is an open-source component from suiss/ui for React. The @esuiss/ui CLI copies its source into your project, so you own the code.
SidebarProvider holds the open state and wraps the whole layout. Inside it, Sidebar renders the panel and SidebarInset renders the main content beside it. The sidebar stacks SidebarHeader, a scrolling SidebarContent and SidebarFooter. Content is organized into SidebarGroup sections, each with a SidebarGroupLabel, an optional SidebarGroupAction and a SidebarGroupContent. Navigation lives in a SidebarMenu of SidebarMenuItem rows, each holding a SidebarMenuButton and optionally a SidebarMenuAction, SidebarMenuBadge or nested SidebarMenuSub.
SidebarTrigger and SidebarRail toggle the sidebar. On screens narrower than 48rem, the sidebar renders inside a Sheet instead. Use Sidebar for the main navigation of an app; for bottom navigation on small screens, consider TabBar.
Installation
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
- Install the following dependencies and add the
cnhelper:
-
Add the pieces it builds on: Button, Direction, Input, Separator, Sheet, Skeleton, Tooltip,
use-mobile,use-platform. -
Copy and paste the following code into your project.
- Update the import paths to match your project setup.
Usage
Examples
Floating
Set variant="floating" on Sidebar for a rounded panel with a shadow.
Inset
Set variant="inset" to also raise the main content into a rounded card.
Collapsed
Set collapsible="icon" and defaultOpen={false} to start collapsed to a rail of icons. Click the rail to expand it. Give each SidebarMenuButton a tooltip so its label stays available while collapsed.
Offcanvas
Set collapsible="offcanvas" to slide the sidebar fully out of view when collapsed.
Right side
Set side="right" to place the sidebar on the right edge.
Loading
Show SidebarMenuSkeleton placeholders while items load. Set showIcon to add a square placeholder for an icon.
Accessibility
SidebarTriggerandSidebarRailare labelledToggle Sidebar. The rail is removed from the tab order, so keep aSidebarTriggerin reach of keyboard users.- Press
Command-Bon Apple platforms, orCtrl+Belsewhere, to toggle the sidebar. - On mobile, the sidebar is a dialog with a visually hidden title,
Sidebar;Escapecloses it. - Menu-button tooltips appear only while the sidebar is collapsed, so labels stay available when only icons show.
API Reference
SidebarProvider
Holds the open state, provides it through context and wraps the layout.
| Prop | Type | Default |
|---|---|---|
defaultOpen | boolean | true |
open | boolean | - |
onOpenChange | (open: boolean) => void | - |
Also accepts the props of React.ComponentProps<"div">.
Use defaultOpen for an uncontrolled sidebar, or open with onOpenChange to control it. Every toggle writes the state to a sidebar_state cookie for 7 days; read it on the server to set defaultOpen and avoid a flash on load. Widths come from --sidebar-width (16rem) and --sidebar-width-icon (3rem), set on SidebarProvider; override them through its style.
Read and change the state from any child with the useSidebar hook, which returns state, open, setOpen, openMobile, setOpenMobile, isMobile and toggleSidebar. It throws when used outside a SidebarProvider.
Sidebar
The sidebar panel; a sheet on mobile.
| Prop | Type | Default |
|---|---|---|
side | "left" | "right" | "left" |
variant | "sidebar" | "floating" | "inset" | "sidebar" |
collapsible | "offcanvas" | "icon" | "none" | "offcanvas" |
Also accepts the props of React.ComponentProps<"div">.
variant chooses a full-height sidebar, a rounded floating panel or an inset layout. Set collapsible="icon" to shrink the sidebar to a rail of icons when collapsed, offcanvas to slide it out of view, or none to keep it fixed. On screens narrower than 48rem the sidebar renders inside a Sheet. The sidebar element sets data-state (expanded or collapsed), data-collapsible, data-variant and data-side for styling. The panel uses the sidebar-* color variables; change them to style it apart from the page.
SidebarTrigger
An icon button that toggles the sidebar.
Also accepts the props of React.ComponentProps<typeof Button>.
SidebarRail
A thin hit area along the sidebar's edge that toggles it on click.
Also accepts the props of React.ComponentProps<"button">.
SidebarInset
The main element for page content next to the sidebar.
Also accepts the props of React.ComponentProps<"main">.
SidebarInput
A compact Input styled for use in the sidebar.
Also accepts the props of React.ComponentProps<typeof Input>.
SidebarHeader
The top area, above the scrolling content.
Also accepts the props of React.ComponentProps<"div">.
SidebarFooter
The bottom area, below the scrolling content.
Also accepts the props of React.ComponentProps<"div">.
SidebarSeparator
A divider between sidebar sections.
Also accepts the props of React.ComponentProps<typeof Separator>.
SidebarContent
The scrolling region that holds the groups.
Also accepts the props of React.ComponentProps<"div">.
SidebarGroup
A section of related content.
Also accepts the props of React.ComponentProps<"div">.
SidebarGroupLabel
The title of a group; it fades out when collapsed to icons.
Also accepts the props of useRender.ComponentProps<"div">, React.ComponentProps<"div">.
Also accepts the props of React.ComponentProps<"div">, plus asChild.
Also accepts the props of React.ComponentProps<"div">, plus a render function.
SidebarGroupAction
A small button in the corner of a group, such as an add button.
Also accepts the props of useRender.ComponentProps<"button">, React.ComponentProps<"button">.
Also accepts the props of React.ComponentProps<"button">, plus asChild.
Also accepts the props of React.ComponentProps<"button">, plus a render function.
SidebarGroupContent
The body of a group.
Also accepts the props of React.ComponentProps<"div">.
SidebarMenu
A list of menu items.
Also accepts the props of React.ComponentProps<"ul">.
SidebarMenuItem
A list item that holds a menu button and its extras.
Also accepts the props of React.ComponentProps<"li">.
SidebarMenuButton
The clickable row for a destination or command.
| Prop | Type | Default |
|---|---|---|
isActive | boolean | false |
tooltip | string | React.ComponentProps<typeof TooltipContent> | - |
variant | "default" | "outline" | "default" |
size | "default" | "sm" | "lg" | "default" |
Also accepts the props of useRender.ComponentProps<"button">, React.ComponentProps<"button">.
Use the render prop to render the button as a link.
Also accepts the props of React.ComponentProps<"button">, plus asChild.
Set asChild and pass a link as the child to render the button as a link.
Also accepts the props of React.ComponentProps<"button">, plus a render function.
Pass a render function that returns a link with the props it receives to render the button as a link.
isActive marks the current item. tooltip takes text or TooltipContent props for a tooltip shown only while collapsed on desktop. The outline variant adds a background and border, and size sets the row height. Menu buttons set data-active and data-size.
SidebarMenuAction
A small button at the end of a menu item, such as a more button.
| Prop | Type | Default |
|---|---|---|
showOnHover | boolean | false |
On desktop, showOnHover hides the action until its item is hovered or focused.
Also accepts the props of useRender.ComponentProps<"button">, React.ComponentProps<"button">.
Also accepts the props of React.ComponentProps<"button">, plus asChild.
Also accepts the props of React.ComponentProps<"button">, plus a render function.
SidebarMenuBadge
A count or label shown at the end of a menu item.
Also accepts the props of React.ComponentProps<"div">.
SidebarMenuSkeleton
A loading placeholder shaped like a menu item.
| Prop | Type | Default |
|---|---|---|
showIcon | boolean | false |
Also accepts the props of React.ComponentProps<"div">.
SidebarMenuSub
A nested list of sub-items, indented with a leading line.
Also accepts the props of React.ComponentProps<"ul">.
SidebarMenuSubItem
A list item inside a sub-menu.
Also accepts the props of React.ComponentProps<"li">.
SidebarMenuSubButton
The clickable row of a sub-item, rendered as a link by default.
| Prop | Type | Default |
|---|---|---|
size | "sm" | "md" | "md" |
isActive | boolean | false |
size sets the text size, and isActive marks the sub-item as the current page.
Also accepts the props of useRender.ComponentProps<"a">, React.ComponentProps<"a">.
Also accepts the props of React.ComponentProps<"a">, plus asChild.
Also accepts the props of React.ComponentProps<"a">, plus a render function.