Sidebar
An app sidebar that collapses to icons or off canvas, stays pinned under your header, and becomes a swipeable sheet on phones.
- Navigation components
- 29 parts
- 10 examples
- vs shadcn/ui
@base-ui/react@tabler/icons-reactclass-variance-authority
See it in a real product: Chat Sidebar, HextaAI, Prompt Library, and 1 more
View blockpnpm dlx shadcn@latest add https://hextaui.com/r/sidebar.jsonAdds the component, the HextaUI theme tokens and any HextaUI components it depends on.
Add the theme tokens to your global CSS, if you haven’t yet.
Install the dependencies.
pnpm add @base-ui/react @tabler/icons-react class-variance-authority cnCopy and paste the following code into your project.
components/ui/sidebar.tsx components/ui/button.tsx components/ui/input.tsx components/ui/sheet.tsx components/ui/skeleton.tsx components/ui/tooltip.tsx hooks/use-composed-ref.ts lib/hotkey.ts Update the import paths to match your project setup.
Wrap your layout in <SidebarProvider /> and put the page in <SidebarInset />, after the sidebar.
SidebarProviderholds the open state, the keyboard shortcut and the widths.Sidebaris a column that stays pinned while the page scrolls. Below 768px it becomes a sheet.SidebarHeaderandSidebarFooterstay in place.SidebarContentscrolls between them.SidebarGroupis a section with an optional label and action.SidebarMenuholds the links.SidebarInsetis the page next to the sidebar.
variant sets the look: a full-height sidebar, a floating panel, or inset, where the page becomes a card on the sidebar color. Switch between them to see the layout animate.
offcanvas slides the sidebar out of view, icon shrinks it to its icons and shows each label in a tooltip, and none keeps it open. Press ⌘B or Ctrl+B to toggle the sidebar you are working in.
Wrap a SidebarGroup or a SidebarMenuItem in a Collapsible, render the label or button as the trigger, and nest a SidebarMenuSub.
A store admin sidebar. SidebarMenuCollapsible keeps the row a link and opens its pages from a separate chevron, SidebarMenuBadge takes a count that becomes a dot in icon mode, SidebarMenuFlyout opens the pages beside the icon rail, and SidebarCard holds a dismissible card that hides in icon mode and on short screens.
Set side="right" and place the sidebar after SidebarInset. The rail and the mobile sheet follow the side.
The sidebar is sticky, so it starts below anything above it. With a sticky header, set --sidebar-top to its height and the sidebar pins below it and fits the rest of the screen.
Pass open and onOpenChange. The trigger, the rail and the shortcut all go through onOpenChange.
SidebarMenuSkeleton fills a menu while it loads. Widths vary per row and match between server and client.
Set dir="rtl" and side="right". Spacing, submenu lines, tooltips and the trigger icon mirror.
The sidebar is 16rem wide, 18rem on phones and 3rem when collapsed to icons. Override --sidebar-width, --sidebar-width-mobile and --sidebar-width-icon on the provider.
The sidebar has no side effects. To remember whether it was open, save it in onOpenChange and read it back into defaultOpen when rendering on the server.
| Key | Action |
|---|---|
| ⌘ + BCtrl + B | Toggles the sidebar. With several sidebars on a page, the one holding focus responds, otherwise the first. Ignored while typing in a rich text editor. |
| TabShift + Tab | Moves through the links. A collapsed off-canvas sidebar is skipped. |
| EnterSpace | Activates the focused link, button or trigger. |
| Esc | Closes the sidebar on phones. |
SidebarTriggeris labelled “Toggle Sidebar” and exposesaria-expandedandaria-controls.isActivesetsaria-current="page".- Collapsing off canvas hides the content from keyboard and screen readers. If focus was inside, it moves to the trigger.
- Collapsed to icons, labels stay in each link's accessible name and show as a tooltip on hover and keyboard focus. Group labels, actions and badges are hidden.
- On phones the sidebar is a modal sheet: focus is trapped, swiping or Esc closes it, and following a link closes it. Links that open a new tab, downloads and modified clicks keep it open.
- With reduced motion enabled, collapsing is instant.
| Prop | Type | Default |
|---|---|---|
defaultOpen | boolean | true |
open | boolean | – |
onOpenChange | (open: boolean) => void | – |
keyboardShortcutmod is ⌘ on Apple platforms and Ctrl elsewhere. null turns it off. | string | null | "mod+b" |
| Attribute | Description |
|---|---|
data-slot="sidebar-wrapper" | The layout wrapper. |
--sidebar-width | Expanded width. Defaults to 16rem. |
--sidebar-width-mobile | Width of the phone sheet. Defaults to 18rem. |
--sidebar-width-icon | Width collapsed to icons. Defaults to 3rem. |
--sidebar-top | Where the sidebar pins while scrolling, such as below a sticky header. Defaults to 0px. |
className and other props go to the sidebar container.
| Prop | Type | Default |
|---|---|---|
side | "left" | "right" | "left" |
variantplain drops the surface, the edge line, the scrollbar and the gap between menu items, for a sidebar that sits on the page. | "sidebar" | "floating" | "inset" | "plain" | "sidebar" |
collapsible | "offcanvas" | "icon" | "none" | "offcanvas" |
mobileHow it opens on phones: a sheet from the side, or sliding in to fill the screen, like chat apps. | "sheet" | "fullscreen" | "sheet" |
dirAlso sets the direction of the phone sheet. | "ltr" | "rtl" | – |
| Attribute | Description |
|---|---|
data-slot="sidebar" | The sidebar, or the sheet on phones. |
data-state | "expanded" or "collapsed". |
data-collapsible | The collapsible mode while collapsed, otherwise "". Style children with group-data-[collapsible=icon]:. |
data-variant | The variant. |
data-side | The side. |
data-mobile | Present on the phone sheet. |
data-slot="sidebar-container" | The panel that slides and resizes. |
data-slot="sidebar-inner" | The surface that holds the content. |
A ghost icon Button that calls toggleSidebar. Call event.preventDefault() in onClick to stop it. Pass children to replace the icon.
| Attribute | Description |
|---|---|
data-slot="sidebar-trigger" | The trigger. |
aria-expanded | Whether the sidebar is open. |
A thin hit area on the sidebar edge that toggles it on click. It is left out of the tab order because the trigger and shortcut cover keyboard users. When the sidebar is off canvas, the rail stays at the screen edge.
| Attribute | Description |
|---|---|
data-slot="sidebar-rail" | The rail. |
A <main> that takes the rest of the width. Next to an inset sidebar it becomes a rounded card.
| Prop | Type | Default |
|---|---|---|
renderRenders a different element. Pass a <div /> when the page already has a <main> landmark. | React.ReactElement | (props) => React.ReactElement | – |
| Attribute | Description |
|---|---|
data-slot="sidebar-inset" | The page area. |
Plain <div> elements. The content scrolls with a thin scrollbar and soft edge fades.
| Attribute | Description |
|---|---|
data-slot="sidebar-header" | Top section. |
data-slot="sidebar-content" | Scrollable middle. |
data-slot="sidebar-footer" | Bottom section. |
| Attribute | Description |
|---|---|
data-slot="sidebar-group" | A section of the sidebar. |
data-slot="sidebar-group-content" | The group's content. |
| Prop | Type | Default |
|---|---|---|
render | ReactElement | (props, state) => ReactElement | <div> |
| Attribute | Description |
|---|---|
data-slot="sidebar-group-label" | Slides up and fades when collapsed to icons. |
| Prop | Type | Default |
|---|---|---|
render | ReactElement | (props, state) => ReactElement | <button> |
| Attribute | Description |
|---|---|
data-slot="sidebar-group-action" | Needs an accessible label. |
A <ul> and its <li> items. Set gap="none" for dense lists like chat history, where rows sit flush.
| Prop | Type | Default |
|---|---|---|
gapSpace between items. Also on SidebarMenuSub. | "default" | "none" | "default" |
| Attribute | Description |
|---|---|
data-slot="sidebar-menu" | The list. |
data-slot="sidebar-menu-item" | An item. group/menu-item for hover styles. |
| Prop | Type | Default |
|---|---|---|
isActiveHighlights it and sets aria-current="page". | boolean | false |
variant | "default" | "outline" | "default" |
size | "default" | "sm" | "lg" | "default" |
tooltipShown while collapsed to icons. Content slides between items as you move along the menu. | ReactNode | TooltipContentProps | – |
render | ReactElement | (props, state) => ReactElement | <button> |
| Attribute | Description |
|---|---|
data-slot="sidebar-menu-button" | peer/menu-button for sibling styles. |
data-active | Present while isActive. |
data-size | The size. |
| Prop | Type | Default |
|---|---|---|
showOnHoverShows it only while the item is hovered or focused, or its menu is open. Always visible on touch screens. | boolean | false |
replacesBadgeHides the item's badge while the item is hovered or focused, so the action takes its place. | boolean | false |
render | ReactElement | (props, state) => ReactElement | <button> |
| Attribute | Description |
|---|---|
data-slot="sidebar-menu-action" | Needs an accessible label. |
| Prop | Type | Default |
|---|---|---|
countA number that rolls when it changes. Zero hides the badge, and in icon mode it becomes a dot. | number | – |
maxLarger counts show as 99+. | number | 99 |
labelRead after the count, like 12 unfulfilled. | string | – |
dotShows a status dot instead of text. label names it. | boolean | false |
toneThe dot or text color. | "default" | "success" | "warning" | "destructive" | "default" |
| Attribute | Description |
|---|---|
data-slot="sidebar-menu-badge" | A count or status at the end of the item. A SidebarMenuAction with replacesBadge takes its place on hover. |
A menu item whose pages open below it. The row stays a link, and the chevron is its own button with aria-expanded. Takes the Collapsible props: open, defaultOpen and onOpenChange. The content animates its height and hides in icon mode.
| Attribute | Description |
|---|---|
data-sidebar="menu-collapsible-trigger" | Needs an accessible label. data-panel-open is set while open. |
| Prop | Type | Default |
|---|---|---|
labelThe heading of the flyout menu. | ReactNode | – |
contentSidebarMenuFlyoutItem elements. | ReactNode | – |
childrenThe SidebarMenuButton. It opens the flyout on hover or click while the sidebar is collapsed to icons, and renders unchanged otherwise. | ReactElement | – |
isActiveOn SidebarMenuFlyoutItem: marks the current page with a check and aria-current. | boolean | false |
| Prop | Type | Default |
|---|---|---|
onDismissShows a dismiss button. Called after the card collapses. | () => void | – |
dismissLabelThe dismiss button's label. | string | "Dismiss" |
| Attribute | Description |
|---|---|
data-slot="sidebar-card" | A card for the footer. Hidden in icon mode and when the screen is shorter than 40rem. |
| Prop | Type | Default |
|---|---|---|
showIcon | boolean | false |
| Attribute | Description |
|---|---|
data-slot="sidebar-menu-skeleton" | Hidden from assistive tech. |
A nested list with a line on its leading edge. It folds away when the sidebar collapses to icons.
| Prop | Type | Default |
|---|---|---|
isActiveOn SidebarMenuSubButton. | boolean | false |
sizeOn SidebarMenuSubButton. | "sm" | "md" | "md" |
renderOn SidebarMenuSubButton. | ReactElement | (props, state) => ReactElement | <a> |
| Attribute | Description |
|---|---|
data-slot="sidebar-menu-sub" | The nested list. |
data-slot="sidebar-menu-sub-button" | A nested link. |
data-active | Present while isActive. |
A small Input on the page background, and a hairline separator.
| Attribute | Description |
|---|---|
data-slot="sidebar-input" | The input. |
data-slot="sidebar-separator" | The separator. |
Reads and controls the nearest sidebar. Throws outside SidebarProvider.
| Prop | Type | Default |
|---|---|---|
state | "expanded" | "collapsed" | – |
open | boolean | – |
setOpen | (open: boolean) => void | – |
openMobile | boolean | – |
setOpenMobile | (open: boolean) => void | – |
isMobile | boolean | – |
toggleSidebarToggles the sheet on phones, else the sidebar. | () => void | – |
- ButtonButtons in every variant and size, with a built-in loading, success and error flow that skips the spinner for fast requests.
- CollapsibleA panel that shows and hides with height motion you can reverse mid-way, without jumping the layout.
- Dropdown menuA menu of actions and options behind a button, with groups, submenus, checkbox and radio items, and shortcuts.
- HotkeyParse, label, announce and match keyboard shortcuts, with ⌘ on Apple platforms and Ctrl everywhere else.
- InputA text input with three sizes, invalid and read-only states, native validation styling and a 16px touch font so phones never zoom in.
- Number flowAnimated numbers where only the changed digits spin, with any Intl format and locale.
Used in Pro blocks
Blocks that build on Sidebar.
Every block comes with HextaUI Pro, and Prompt Input is free.