Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Sidebar for Svelte UI SDK

An app sidebar for Svelte on UI SDK: grouped links and menus in a rail that collapses to icons or slides off screen, toggled by a button or Ctrl+B.

On this page9 sections

A sidebar holds an app's main navigation in a rail down the side of the page: groups of links, a workspace switcher at the top, the account menu at the bottom. It folds to a strip of icons or slides away, and the page takes back the room.

Reach for it when a product has more destinations than fit across a header. A site with a handful of top-level pages is better served by a Navigation Menu, and a phone app with four or five sections by Bottom Navigation.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Sidebar anatomy: header, content, group label, menu item, footer, trigger

Usage

<script lang="ts">
import TeamSwitcher from "./team-switcher.svelte"
import NavMain from "./nav-main.svelte"
import NavProjects from "./default-nav-projects.svelte"
import NavUser from "./default-nav-user.svelte"
import AudioWaveformIcon from "lucide-svelte/icons/audio-waveform"
import BookOpenIcon from "lucide-svelte/icons/book-open"
import BotIcon from "lucide-svelte/icons/bot"
import CommandIcon from "lucide-svelte/icons/command"
import FrameIcon from "lucide-svelte/icons/frame"
import GalleryVerticalEndIcon from "lucide-svelte/icons/gallery-vertical-end"
import MapIcon from "lucide-svelte/icons/map"
import PieChartIcon from "lucide-svelte/icons/pie-chart"
import Settings2Icon from "lucide-svelte/icons/settings-2"
import SquareTerminalIcon from "lucide-svelte/icons/square-terminal"
import {
  Sidebar, SidebarContent, SidebarFooter, SidebarHeader, SidebarInset,
  SidebarProvider, SidebarRail, SidebarTrigger,
} from "@/components/ui/sidebar"
const data = {
  navMain: [
    {
      icon: SquareTerminalIcon,
      isActive: true,
      items: [
        { title: "History", url: "#" },
        { title: "Starred", url: "#" },
        { title: "Settings", url: "#" },
      ],
      title: "Playground",
      url: "#",
    },
    {
      icon: BotIcon,
      items: [
        { title: "Genesis", url: "#" },
        { title: "Explorer", url: "#" },
        { title: "Quantum", url: "#" },
      ],
      title: "Models",
      url: "#",
    },
    {
      icon: BookOpenIcon,
      items: [
        { title: "Introduction", url: "#" },
        { title: "Get Started", url: "#" },
        { title: "Tutorials", url: "#" },
        { title: "Changelog", url: "#" },
      ],
      title: "Documentation",
      url: "#",
    },
    {
      icon: Settings2Icon,
      items: [
        { title: "General", url: "#" },
        { title: "Team", url: "#" },
        { title: "Billing", url: "#" },
        { title: "Limits", url: "#" },
      ],
      title: "Settings",
      url: "#",
    },
  ],
  projects: [
    { icon: FrameIcon, name: "Design Engineering", url: "#" },
    { icon: PieChartIcon, name: "Sales & Marketing", url: "#" },
    { icon: MapIcon, name: "Travel", url: "#" },
  ],
  teams: [
    {
      logo: GalleryVerticalEndIcon,
      name: "Acme Inc",
      plan: "Enterprise",
    },
    {
      logo: AudioWaveformIcon,
      name: "Acme Corp.",
      plan: "Startup",
    },
    {
      logo: CommandIcon,
      name: "Evil Corp.",
      plan: "Free",
    },
  ],
  user: {
    avatar: "https://github.com/shadcn.png",
    email: "m@example.com",
    name: "shadcn",
  },
}
type Team = (typeof data.teams)[number];
</script>
<div class="h-[400px] w-full overflow-hidden rounded-xl border"><SidebarProvider class="h-full min-h-0" persist={false} shortcut={false}><Sidebar class="h-full" collapsible="icon"><SidebarHeader><TeamSwitcher teams={data.teams} /></SidebarHeader><SidebarContent><NavMain items={data.navMain} /><NavProjects projects={data.projects} /></SidebarContent><SidebarFooter><NavUser user={data.user} /></SidebarFooter><SidebarRail /></Sidebar><SidebarInset><header class="flex h-16 shrink-0 items-center gap-2 transition-[width,height] ease-linear group-has-data-[collapsible=icon]/sidebar-wrapper:h-12"><div class="flex items-center gap-2 px-4"><SidebarTrigger class="-ms-1" /></div></header></SidebarInset></SidebarProvider></div>

Accessibility

Each row is a real link or button, so it sits in the tab order and Enter follows or presses it. SidebarTrigger is a button named Toggle Sidebar. Collapsed to icons, a row keeps its label as its accessible name, and the tooltip you give it shows that label beside the icon. Below 768px the sidebar opens as a modal sheet, which keeps focus inside until Escape or a press outside closes it.

Two things are left to you. The sidebar renders no nav element, so put one with an aria-label around the groups of links. And isActive only changes how a row looks, so add aria-current="page" to the link for the page that is open.

Tab
Moves focus to the next link or button in the sidebar, and from the last one on into the page.
Shift+Tab
Moves focus to the previous link or button.
Enter
Follows the focused link, or presses the focused button, such as a row that opens its sub-items or a menu.
Space
Presses the focused button. Links don't respond to it.
Ctrl+B
Opens or closes the sidebar from anywhere on the page, a plain text field included, or ⌘ + B on a Mac. In a rich-text editor the keys still make text bold, because the sidebar leaves them to any element with contenteditable and to a handler that has already used them. Set shortcut to false on SidebarProvider to turn it off.

Demos

RTL

The full app shell in Arabic, with dir set to rtl on the frame around it and no direction prop on the sidebar. The rail sits on the right, where an Arabic line starts, floats with a gap around it and collapses to icons, and its menus open to the left, toward the page. Use it as the starting point for an Arabic or Hebrew interface.

Preview width

FAQ

Should I use a Sidebar or a Navigation Menu for my app?

Use a sidebar for an app people work inside, with destinations in several groups, a workspace or project switcher and an account menu that stay in reach on every screen. It takes a column of the layout and earns it by being there all the time.

A Navigation Menu suits a marketing site or docs with a few top-level sections across the header, each opening a panel of links. Many products use both, the menu on the public site and the sidebar once someone signs in.

How do I open or close the Sidebar from my own state in Svelte?

Keep the value in state, pass it as open on SidebarProvider and update it in onOpenChange, which receives the next boolean. Set defaultOpen instead when you only need the first state.

Any part under the provider can also call useSidebar, which returns open, setOpen, toggleSidebar and state, either expanded or collapsed. It throws when called outside SidebarProvider, so keep the provider high enough in the layout to cover the header where your own buttons live.

How do I keep the Sidebar open or closed after a page reload?

The provider already writes the desktop state to a cookie named sidebar_state each time it changes, kept for seven days, but it never reads that cookie back. Read it where the page is rendered and pass the result to defaultOpen on SidebarProvider.

Read it on the server if you can, so the first paint already has the right width instead of jumping after hydration. Set persist to false when you would rather keep the state yourself, in local storage or a user setting.

How do I make the Sidebar collapse to icons instead of sliding away?

Set collapsible to icon on Sidebar. The default, offcanvas, slides the whole rail off the edge, icon leaves a 3rem strip with each row's icon showing, and none keeps it open with nothing to toggle.

In the icon strip, give each SidebarMenuButton a tooltip with its label, which shows only while the sidebar is collapsed. Hide a group that means nothing as icons with group-data-[collapsible=icon]:hidden, as the projects list in the demo above does.

How do I put the Sidebar on the right side of the page?

Set placement to right on Sidebar. It names the end of the layout rather than the screen's right edge, so on a right-to-left page the rail moves to the left. The border, the drag handle and the tooltips move with it.

The RTL demo shows the other case, where the page itself reads right to left and the default side already lands on the right.

What happens to the Sidebar on a phone?

Below 768px the rail is replaced by a sheet that slides in over the page from the same side, and SidebarTrigger opens it. useSidebar tells your own parts which layout is showing through isMobile.

The sheet doesn't close when a link inside it is pressed. With client-side routing, call setOpenMobile(false) from useSidebar when a link is clicked, or the menu stays over the new page.

How do I highlight the current page's link in the Sidebar?

Compare each item's address with the router's current path and set isActive on the SidebarMenuButton or SidebarMenuSubButton that matches. It sets data-active, which the accent background keys on. Add aria-current="page" to the same link, since isActive tells a screen reader nothing.

To use your router's link component, render it in the render snippet of SidebarMenuButton and spread the props the snippet passes onto it.

How do I change the Sidebar's width or colors?

Set --sidebar-width and --sidebar-width-icon in the style of SidebarProvider. The provider applies 16rem and 3rem, and what you pass replaces them. Colors come from the sidebar tokens in your CSS, --sidebar, --sidebar-foreground, --sidebar-accent and --sidebar-border among them, so the rail can differ from the page without a class on every part.

Dragging SidebarRail also resizes an open sidebar between 160 and 360 pixels, and dragging it well past the narrow end collapses it. That width isn't saved, so it resets on the next load.

API Reference

<div>

PropType
collapsible?"offcanvas" | "icon" | "none"

How it collapses: offcanvas slides it out, icon shrinks it to its * icons, none keeps it open.

Type
"offcanvas" | "icon" | "none"
Default
"offcanvas"
side?"inline-start" | "inline-end" | "left" | "right"

The edge it's on: inline-start (the default) and inline-end follow the reading direction; left and right are the screen's edges, as in shadcn/ui.

Type
"inline-start" | "inline-end" | "left" | "right"
Default
"inline-start"
variant?"sidebar" | "floating" | "inset"

The visual style.

Type
"sidebar" | "floating" | "inset"
Default
"sidebar"
class?string

Merged with the part's classes.

CSS variableType
--sidebar-widthstring
--sidebar-width-iconstring

SidebarContent

<div> data-slot="sidebar-content"

PropType
class?string

Merged with the part's classes.

AttributeType
data-sidebar"content"
Default
"content"

SidebarFooter

<div> data-slot="sidebar-footer"

PropType
class?string

Merged with the part's classes.

AttributeType
data-sidebar"footer"
Default
"footer"

SidebarGroup

<div> data-slot="sidebar-group"

PropType
class?string

Merged with the part's classes.

AttributeType
data-sidebar"group"
Default
"group"

SidebarGroupAction

PropType
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

SidebarGroupContent

<div> data-slot="sidebar-group-content"

PropType
class?string

Merged with the part's classes.

AttributeType
data-sidebar"group-content"
Default
"group-content"

SidebarGroupLabel

PropType
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

SidebarHeader

<div> data-slot="sidebar-header"

PropType
class?string

Merged with the part's classes.

AttributeType
data-sidebar"header"
Default
"header"

SidebarInput

PropType
value?string | number

The value, for a controlled field. A number is written as its text, as a native input does. Two-way with bind:value.

Type
string | number
defaultValue?string | number

The value it starts with. A form reset returns here.

Type
string | number
onValueChange?(value: string, details: ChangeDetails<InputValueChangeReason>) → void

Called when the user changes the value: typing ("input-change"), a Clear part ("clear-press"), a form reset ("reset") or a controller ("imperative"). details.cancel() keeps the value as it was.

Type
(value: string, details: ChangeDetails<InputValueChangeReason>) → void
Parameters

value – string

details – ChangeDetails<InputValueChangeReason>

Returns
void
controller?InputController

Reads and writes the field from anywhere. Create one with createInputController().

debug?boolean

Logs every state change with its reason to the console, in development builds.

disabled?boolean

Disables the field.

readOnly?boolean

Makes the field read-only.

required?boolean

Makes the field required.

render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-sidebar"input"
Default
"input"

SidebarInset

<main> data-slot="sidebar-inset"

PropType
class?string

Merged with the part's classes.

SidebarMenu

<ul> data-slot="sidebar-menu"

PropType
class?string

Merged with the part's classes.

AttributeType
data-sidebar"menu"
Default
"menu"

SidebarMenuAction

PropType
showOnHover?boolean

Hides the action until its menu item is hovered or focused, or its menu * is open. Always shown below md.

Default
false
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

SidebarMenuBadge

<div> data-slot="sidebar-menu-badge"

PropType
class?string

Merged with the part's classes.

AttributeType
data-sidebar"menu-badge"
Default
"menu-badge"

SidebarMenuButton

data-slot="sidebar-menu-action"

PropType
isActive?boolean

Whether the button is the current page: highlighted, and data-active.

Default
false
size?"default" | "lg" | "sm"

The size.

Type
"default" | "lg" | "sm"
Default
"default"
tooltip?string | React.ComponentProps<typeof TooltipContent>

The tooltip it shows while the sidebar is collapsed to icons: its text, * or the tooltip's props.

Type
string | React.ComponentProps<typeof TooltipContent>
variant?"default" | "outline"

The visual style.

Type
"default" | "outline"
Default
"default"
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-sidebar"menu-action"
Default
"menu-action"

SidebarMenuItem

<li> data-slot="sidebar-menu-item"

PropType
class?string

Merged with the part's classes.

AttributeType
data-sidebar"menu-item"
Default
"menu-item"

SidebarMenuSkeleton

<div> data-slot="sidebar-menu-skeleton"

PropType
showIcon?boolean

Draws an icon placeholder before the text line.

Default
false
class?string

Merged with the part's classes.

AttributeType
data-sidebar"menu-skeleton"
Default
"menu-skeleton"
CSS variableType
--skeleton-widthstring

SidebarMenuSub

<ul> data-slot="sidebar-menu-sub"

PropType
class?string

Merged with the part's classes.

AttributeType
data-sidebar"menu-sub"
Default
"menu-sub"

SidebarMenuSubButton

PropType
isActive?boolean

Whether the button is the current page: highlighted, and data-active.

Default
false
size?"sm" | "md"

The size.

Type
"sm" | "md"
Default
"md"
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

SidebarMenuSubItem

<li> data-slot="sidebar-menu-sub-item"

PropType
class?string

Merged with the part's classes.

AttributeType
data-sidebar"menu-sub-item"
Default
"menu-sub-item"

SidebarProvider

PropType
open?boolean

Whether it's open. Pair with onOpenChange to control it. Two-way with bind:open.

defaultOpen?boolean

Whether it starts open, when you don't control open.

Default
true
onOpenChange?(open: boolean) → void

Called when it opens or closes.

Parameters

open – boolean

Returns
void
persist?boolean

Persist the desktop open state to a cookie. * * @default true.

Default
true
shortcut?boolean

Toggle the sidebar with ⌘B / Ctrl+B. * * @default true.

Default
true
class?string

Merged with the part's classes.

SidebarRail

<button> data-slot="sidebar-rail"

PropType
class?string

Merged with the part's classes.

AttributeType
data-sidebar"rail"
Default
"rail"

SidebarSeparator

PropType
debug?boolean

Logs every state change with its reason to the console, in development builds.

decorative?boolean

Hides the separator from assistive technology with role="none", for a line that's only visual. Leave it off when the separator divides content a screen reader should hear as separate, like groups of menu items.

Default
false
orientation?SeparatorOrientation

Which way the line runs. A vertical separator divides items in a row.

Default
"horizontal"
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-sidebar"separator"
Default
"separator"

SidebarTrigger

PropType
clickEffect?boolean

Apply a click effect to the button.

Default
true
debug?boolean

Logs every state change with its reason to the console, in development builds.

disabled?boolean

Stops presses. The button is natively disabled, so it leaves the tab order and its form doesn't submit through it, unless focusableWhenDisabled is set.

Default
false
focusableWhenDisabled?boolean

Keeps a disabled button in the tab order with aria-disabled instead of the native attribute, so it can hold focus while it becomes disabled and host a tooltip that says why. Presses still do nothing.

Default
false
isLoading?boolean

Show a loading indicator.

Default
false
onclick?( event: MouseEvent, details?: ButtonActionDetails ) → unknown

Runs when the button is pressed. Return a Promise to keep it pending (data-pending) until it settles. details.source says how it was pressed; a wrapper passing the click on may leave it out.

Type
( event: MouseEvent, details?: ButtonActionDetails ) → unknown
Parameters

event – React.MouseEvent<HTMLButtonElement>

details? – ButtonActionDetails

Returns
unknown
pendingAnnouncement?string

What screen readers hear once the button has been pending for a second, so a slow action doesn't pass in silence. Set it in the page's language. An empty string says nothing.

Default
"Loading"
pill?boolean

Fully rounded corners.

Default
false
size?"default" | "icon" | "icon-lg" | "icon-md" | "icon-sm" | "icon-xl" | "icon-xs" | "lg" | "md" | "sm" | "xl" | "xs"

The size.

Type
"default" | "icon" | "icon-lg" | "icon-md" | "icon-sm" | "icon-xl" | "icon-xs" | "lg" | "md" | "sm" | "xl" | "xs"
Default
"md"
type?"button" | "reset" | "submit"

What a native button does in a form: "submit" submits it and "reset" resets it. Ignored when the button renders as something else.

Type
"button" | "reset" | "submit"
Default
"button"
variant?"default" | "destructive" | "ghost" | "link" | "outline" | "secondary"

The visual style.

Type
"default" | "destructive" | "ghost" | "link" | "outline" | "secondary"
Default
"default"
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

useSidebar