Keyframery

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.

Resize preview. Focus the handle and use the left or right arrow keys. Hold Shift for larger steps.— px
Customize1 controls
Animation speed1×

Installation

npx shadcn@latest add @keyframery/sidebar

This 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
└── SidebarInset

Examples

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

MoveWhat you see
GlideThe current page's key slides from the old item to the new one, even across groups.
HeightA collapsible group opens to its real height and its items rise in one after another.
FadeOn the rail, labels leave at once; on the way back they wait for the width, then rise in out of a blur.
PressItems sink a little and spring back on release.
DirectionBelow 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.

VariableWhat it changesDefault
--sidebar-faceSidebar surface--muted at 45%; --card in dark
--sidebar-inkLabels and text--muted-foreground; --foreground for active text
--sidebar-keySelected page surface--background; --accent in dark
--sidebar-radiusMenu key corners9px
--sidebar-heightMenu item height2rem
--sidebar-widthExpanded sidebar width15rem
--sidebar-width-collapsedCollapsed rail width3.5rem
--sidebar-width-mobileMobile sheet width17rem

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:

  • Sidebar
  • SidebarHeader: a <div>
  • SidebarTrigger: a <button>
  • SidebarContent: a <div>
  • SidebarGroupLabel: a <div>
  • SidebarGroupContent: a <div>
  • SidebarMenu: a <ul>
  • SidebarMenuItem: a <li>
  • SidebarLabel: a <span>
  • SidebarInset
  • SidebarFooter: 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

KeyWhat it does
TabMoves between the trigger and the items.
EnterOpens the focused item, or folds a collapsible group from its label.
EscapeCloses 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.

All components

On this page