Sidebar
An app sidebar whose current-page key glides between items, that narrows to an icon rail, and that slides in over the page on a phone.
See it as an AI agent does.See this component the way an AI agent does.
Installation
npx shadcn@latest add @keyframery/sidebarThis adds components/keyframery/sidebar.tsx and its primitive to your project, and installs UI SDK, along with collapsible, which it builds on. The motion tokens are added to your global CSS the first time you install a component.
Usage
import { Home, Inbox } from "lucide-react"
import {
Sidebar,
SidebarContent,
SidebarGroup,
SidebarGroupContent,
SidebarGroupLabel,
SidebarHeader,
SidebarInset,
SidebarLabel,
SidebarMenu,
SidebarMenuButton,
SidebarMenuItem,
SidebarProvider,
SidebarTrigger,
} from "@/components/keyframery/sidebar"
export default function Usage() {
return (
<SidebarProvider>
<Sidebar>
<SidebarHeader>
<SidebarTrigger />
</SidebarHeader>
<SidebarContent>
<SidebarGroup>
<SidebarGroupLabel>App</SidebarGroupLabel>
<SidebarGroupContent>
<SidebarMenu>
<SidebarMenuItem>
<SidebarMenuButton isActive>
<Home />
<SidebarLabel>Home</SidebarLabel>
</SidebarMenuButton>
</SidebarMenuItem>
<SidebarMenuItem>
<SidebarMenuButton>
<Inbox />
<SidebarLabel>Inbox</SidebarLabel>
</SidebarMenuButton>
</SidebarMenuItem>
</SidebarMenu>
</SidebarGroupContent>
</SidebarGroup>
</SidebarContent>
</Sidebar>
<SidebarInset>{/* Your page */}</SidebarInset>
</SidebarProvider>
)
}Anatomy
How the parts fit together:
SidebarProvider
├── Sidebar
│ ├── SidebarHeader
│ │ └── SidebarTrigger
│ └── SidebarContent
│ └── SidebarGroup
│ ├── SidebarGroupLabel
│ └── SidebarGroupContent
│ └── SidebarMenu
│ └── SidebarMenuItem
│ └── SidebarMenuButton
│ └── SidebarLabel
└── SidebarInsetExamples
Basic
Mark the current page with isActive. The highlight glides to it.
Collapsible group
Pass collapsible to a group and its label folds it away. SidebarMenuBadge shows a count.
Icon rail
SidebarTrigger narrows the sidebar to its icons and back. On the rail, tooltip names each item.
Controlled
Pass collapsed and onCollapsedChange to drive it from your own state, like a saved setting.
On a phone
Below mobileBreakpoint (640px by default) the sidebar becomes a sheet. Put a SidebarTrigger in the page to open it; choosing an item closes it.
Motion
| Move | What you see |
|---|---|
| Glide | The current page's key slides from the old item to the new one, even across groups. |
| Height | A collapsible group opens to its real height and its items rise in one after another. |
| Fade | On the rail, labels leave at once; on the way back they wait for the width, then rise in out of a blur. |
| Press | Items sink a little and spring back on release. |
| Direction | Below its mobile breakpoint the sidebar slides in over the page from the left, over a fading scrim, and slides away when you choose a page. |
Timing and springiness follow the motion style you set. To change how fast this sidebar moves, pass speed:
<SidebarProvider speed="fast">
…
</SidebarProvider>"fast" is 0.6×, "slow" 1.4×, and a number sets the multiplier. With reduced motion turned on, nothing moves: changes happen at once or with a short fade.
Theming
Set these CSS variables on the component or a parent. Leave them unset to follow the light and dark theme. The open Customize panel below the preview lets you try these settings.
| Variable | What it changes | Default |
|---|---|---|
--sidebar-face | Sidebar surface | --muted at 45%; --card in dark |
--sidebar-ink | Labels and text | --muted-foreground; --foreground for active text |
--sidebar-key | Selected page surface | --background; --accent in dark |
--sidebar-radius | Menu key corners | 9px |
--sidebar-height | Menu item height | 2rem |
--sidebar-width | Expanded sidebar width | 15rem |
--sidebar-width-collapsed | Collapsed rail width | 3.5rem |
--sidebar-width-mobile | Mobile sheet width | 17rem |
API reference
Every part takes className and children, along with the props of the element it renders. The parts are rendered by UI SDK, so they also take render, to draw your own element, and a function of their state for className and style.
SidebarProvider
Renders a <div>, and takes its props too.
Prop
Type
SidebarGroup
Renders a <div>, and takes its props too.
Prop
Type
SidebarMenuButton
Renders a <button>, and takes its props too.
Prop
Type
Other parts
These take the props of the element they render, and nothing else:
SidebarSidebarHeader: a<div>SidebarTrigger: a<button>SidebarContent: a<div>SidebarGroupLabel: a<div>SidebarGroupContent: a<div>SidebarMenu: a<ul>SidebarMenuItem: a<li>SidebarLabel: a<span>SidebarInsetSidebarFooter: a<div>SidebarMenuBadge: a<span>
Hooks
useSidebar
Reads and changes the sidebar's state from any component inside SidebarProvider, like your own trigger in the page header.
Prop
Type
Keyboard
| Key | What it does |
|---|---|
| Tab | Moves between the trigger and the items. |
| Enter | Opens the focused item, or folds a collapsible group from its label. |
| Escape | Closes the sidebar while it is a sheet on a phone, and puts focus back on the page's trigger. |
Accessibility
Roles, keyboard and focus are declared on the component's UI SDK primitive, and state is announced through the matching ARIA attributes. More in Accessibility.
Built on UI SDK's core
UI SDK doesn't ship a sidebar, so this one is defined with its core: one definePrimitive in components/keyframery/core/sidebar-core.ts, with no React in it. UI SDK's adapters can render that same definition in Vue, Svelte, Solid and web components. To keep your own styling and take only the motion, import from @/components/keyframery/primitives/sidebar.
Questions
How does the animated Sidebar for UI SDK move?
Glide: The current page's key slides from the old item to the new one, even across groups. Height: A collapsible group opens to its real height and its items rise in one after another. Fade: On the rail, labels leave at once; on the way back they wait for the width, then rise in out of a blur. Press: Items sink a little and spring back on release. Direction: Below its mobile breakpoint the sidebar slides in over the page from the left, over a fading scrim, and slides away when you choose a page.
Do I need Motion or Framer Motion for the animated Sidebar?
No. Every move is plain CSS, so there is nothing to install besides the component and UI SDK.
UI SDK has no sidebar. How is this one built on UI SDK?
Its behaviour is one definition written with UI SDK's core (definePrimitive), in components/keyframery/core/sidebar-core.ts. Its parts are rendered by UI SDK's adapter, so they take render, state-based class names and data-slot, like every UI SDK component.
What happens to the Sidebar when someone turns on reduced motion?
Nothing moves. State changes happen at once, or with a short fade.
More in Navigation
All componentsSegmented Control
The selected key stretches across toward the new option, then closes up.
Stepper
Steps that are complete, current, upcoming or in error, horizontal or vertical, as a read-only progress strip (an order) or the buttons of a multi-step form, linear or not. The line between steps draws itself up to the current one, a finished step's number gives way to a check that draws itself, and panels arrive from the side you moved toward.