Sidebar

A sidebar beside the page: it collapses, to icons or off the screen, and is a sheet on phones.

NavigationExperimental

Usage

import { Component, ViewEncapsulation, signal } from '@angular/core';import { RouterLink } from '@angular/router';import {  ScSidebar,  ScSidebarContent,  ScSidebarFooter,  ScSidebarGroup,  ScSidebarGroupContent,  ScSidebarGroupLabel,  ScSidebarHeader,  ScSidebarInset,  ScSidebarMenu,  ScSidebarMenuButton,  ScSidebarMenuItem,  ScSidebarProvider,  ScSidebarRail,  ScSidebarTrigger,} from '@semantic-components/ui';import {  SiCalendarIcon,  SiGalleryVerticalEndIcon,  SiHouseIcon,  SiInboxIcon,  SiPanelLeftIcon,  SiSearchIcon,  SiSettingsIcon,} from '@semantic-icons/lucide-icons';
/** A page with a sidebar: its header, its menu in a group, its footer. The trigger (or Ctrl/⌘ + B, or its edge) opens and closes it; on a phone it is a sheet. */@Component({  selector: 'app-sidebar-usage-demo',  imports: [    RouterLink,    ScSidebar,    ScSidebarContent,    ScSidebarFooter,    ScSidebarGroup,    ScSidebarGroupContent,    ScSidebarGroupLabel,    ScSidebarHeader,    ScSidebarInset,    ScSidebarMenu,    ScSidebarMenuButton,    ScSidebarMenuItem,    ScSidebarProvider,    ScSidebarRail,    ScSidebarTrigger,    SiCalendarIcon,    SiGalleryVerticalEndIcon,    SiHouseIcon,    SiInboxIcon,    SiPanelLeftIcon,    SiSearchIcon,    SiSettingsIcon,  ],  template: `    <div scSidebarProvider [storageKey]="null">      <aside scSidebar aria-label="Sidebar, usage">        <div scSidebarHeader>          <ul scSidebarMenu>            <li scSidebarMenuItem>              <a                scSidebarMenuButton                size="lg"                routerLink="."                (click)="$event.preventDefault()"              >                <span                  class="bg-primary text-primary-foreground flex aspect-square size-8 items-center justify-center rounded-lg"                >                  <svg siGalleryVerticalEndIcon></svg>                </span>                <span class="grid flex-1 text-sm leading-tight">                  <span class="truncate font-semibold">Acme Inc</span>                  <span class="text-sidebar-foreground/70 truncate text-xs"                    >Enterprise</span                  >                </span>              </a>            </li>          </ul>        </div>        <nav scSidebarContent aria-label="Main, usage">          <div scSidebarGroup>            <div scSidebarGroupLabel>Application</div>            <div scSidebarGroupContent>              <ul scSidebarMenu>                <li scSidebarMenuItem>                  <a                    scSidebarMenuButton                    routerLink="."                    [active]="active() === 'Home'"                    (click)="go($event, 'Home')"                  >                    <svg siHouseIcon></svg>                    <span>Home</span>                  </a>                </li>                <li scSidebarMenuItem>                  <a                    scSidebarMenuButton                    routerLink="."                    [active]="active() === 'Inbox'"                    (click)="go($event, 'Inbox')"                  >                    <svg siInboxIcon></svg>                    <span>Inbox</span>                  </a>                </li>                <li scSidebarMenuItem>                  <a                    scSidebarMenuButton                    routerLink="."                    [active]="active() === 'Calendar'"                    (click)="go($event, 'Calendar')"                  >                    <svg siCalendarIcon></svg>                    <span>Calendar</span>                  </a>                </li>                <li scSidebarMenuItem>                  <a                    scSidebarMenuButton                    routerLink="."                    [active]="active() === 'Search'"                    (click)="go($event, 'Search')"                  >                    <svg siSearchIcon></svg>                    <span>Search</span>                  </a>                </li>                <li scSidebarMenuItem>                  <a                    scSidebarMenuButton                    routerLink="."                    [active]="active() === 'Settings'"                    (click)="go($event, 'Settings')"                  >                    <svg siSettingsIcon></svg>                    <span>Settings</span>                  </a>                </li>              </ul>            </div>          </div>        </nav>        <div scSidebarFooter>          <p class="text-sidebar-foreground/70 truncate px-2 text-xs">            Signed in as Ada          </p>        </div>        <button scSidebarRail></button>      </aside>      <main scSidebarInset aria-label="Usage example">        <header class="flex h-12 shrink-0 items-center gap-2 px-4">          <button scSidebarTrigger>            <svg siPanelLeftIcon></svg>            <span class="sr-only">Sidebar</span>          </button>        </header>        <div class="p-4">          <p class="text-muted-foreground text-sm">Page: {{ active() }}</p>        </div>      </main>    </div>  `,  host: { class: 'block' },  encapsulation: ViewEncapsulation.None,})export class SidebarUsageDemo {  private readonly active = signal('Home');
  private go(event: Event, page: string): void {    event.preventDefault();    this.active.set(page);  }}

Anatomy

shadcn's Sidebar, in the look of the app's style. A provider round the sidebar and the page beside it holds their shared state. The sidebar has a header and a footer that stay, and a content that scrolls between them: groups, each with a label and a menu of items.

import {  ScSidebarProvider, ScSidebar, ScSidebarInset, ScSidebarTrigger, ScSidebarRail,  ScSidebarHeader, ScSidebarContent, ScSidebarFooter, ScSidebarSeparator, ScSidebarInput,  ScSidebarGroup, ScSidebarGroupLabel, ScSidebarGroupAction, ScSidebarGroupContent,  ScSidebarMenu, ScSidebarMenuItem, ScSidebarMenuButton, ScSidebarMenuAction,  ScSidebarMenuBadge, ScSidebarMenuSkeleton,  ScSidebarMenuSub, ScSidebarMenuSubItem, ScSidebarMenuSubButton,} from '@semantic-components/ui';
<div scSidebarProvider>  <aside scSidebar aria-label="…">    <div scSidebarHeader>…</div>    <nav scSidebarContent aria-label="…">      <div scSidebarGroup>        <div scSidebarGroupLabel>…</div>        <button scSidebarGroupAction>…</button>        <div scSidebarGroupContent>          <ul scSidebarMenu>            <li scSidebarMenuItem>              <a scSidebarMenuButton href="…" tooltip="…">…</a>              <button scSidebarMenuAction>…</button>              <div scSidebarMenuBadge>…</div>              <ul scSidebarMenuSub>                <li scSidebarMenuSubItem>                  <a scSidebarMenuSubButton href="…">…</a>                </li>              </ul>            </li>          </ul>        </div>      </div>    </nav>    <div scSidebarFooter>…</div>    <button scSidebarRail></button>  </aside>  <main scSidebarInset>    <button scSidebarTrigger>…</button>  </main></div>

Accessibility

  • On an aside, the sidebar is a landmark holding its header, its navigation and its footer: name it (aria-label), and name the nav inside it. On a div it is none; only your nav is.
  • The trigger says whether the sidebar is open (aria-expanded) and which one (aria-controls): write its icon and its name. Ctrl / ⌘ + B opens and closes it too (the provider's shortcut).
  • The rail, its edge, is for the pointer only: out of the tab order and hidden from screen readers.
  • On a phone it is a sheet: a dialog named by the sidebar's aria-label, focus kept inside, closed by Escape or by following a link.
  • The page shown is marked on its link (active: aria-current). Collapsed to icons, a button keeps its text as its name, and its tooltip shows it on hover and focus.
  • The content shows its scrollbar when it has more than fits (hideScrollbar hides it).

Examples

Each example is a page of its own, shown in a frame.

Header

import { Component, ViewEncapsulation } from '@angular/core';import { RouterLink } from '@angular/router';import {  ScMenu,  ScMenuContent,  ScMenuItem,  ScMenuPortal,  ScMenuProvider,  ScMenuTrigger,  ScSidebar,

Footer

import { Component, ViewEncapsulation } from '@angular/core';import { RouterLink } from '@angular/router';import {  ScMenu,  ScMenuContent,  ScMenuItem,  ScMenuPortal,  ScMenuProvider,  ScMenuTrigger,  ScSidebar,

Group

import { Component, ViewEncapsulation } from '@angular/core';import { RouterLink } from '@angular/router';import {  ScSidebar,  ScSidebarContent,  ScSidebarGroup,  ScSidebarGroupContent,  ScSidebarGroupLabel,  ScSidebarInset,  ScSidebarMenu,

Collapsible group

import { Component, ViewEncapsulation } from '@angular/core';import { RouterLink } from '@angular/router';import {  ScCollapsible,  ScCollapsiblePanel,  ScCollapsibleTrigger,  ScSidebar,  ScSidebarContent,  ScSidebarGroup,  ScSidebarGroupContent,

Group action

import { Component, ViewEncapsulation, signal } from '@angular/core';import { RouterLink } from '@angular/router';import {  ScSidebar,  ScSidebarContent,  ScSidebarGroup,  ScSidebarGroupAction,  ScSidebarGroupContent,  ScSidebarGroupLabel,  ScSidebarInset,

Menu

import { Component, ViewEncapsulation } from '@angular/core';import { RouterLink } from '@angular/router';import {  ScSidebar,  ScSidebarContent,  ScSidebarGroup,  ScSidebarGroupContent,  ScSidebarGroupLabel,  ScSidebarInset,  ScSidebarMenu,

Menu action

import { Component, ViewEncapsulation } from '@angular/core';import { RouterLink } from '@angular/router';import {  ScMenu,  ScMenuContent,  ScMenuItem,  ScMenuPortal,  ScMenuProvider,  ScMenuTrigger,  ScSidebar,

Sub-menu

import { Component, ViewEncapsulation } from '@angular/core';import { RouterLink } from '@angular/router';import {  ScSidebar,  ScSidebarContent,  ScSidebarGroup,  ScSidebarGroupContent,  ScSidebarGroupLabel,  ScSidebarInset,  ScSidebarMenu,

Collapsible menu

import { Component, ViewEncapsulation } from '@angular/core';import { RouterLink } from '@angular/router';import {  ScCollapsible,  ScCollapsiblePanel,  ScCollapsibleTrigger,  ScSidebar,  ScSidebarContent,  ScSidebarGroup,  ScSidebarGroupContent,

Menu badge

import { Component, ViewEncapsulation } from '@angular/core';import { RouterLink } from '@angular/router';import {  ScSidebar,  ScSidebarContent,  ScSidebarGroup,  ScSidebarGroupContent,  ScSidebarGroupLabel,  ScSidebarInset,  ScSidebarMenu,

Skeleton

import {  Component,  DestroyRef,  ViewEncapsulation,  inject,  signal,} from '@angular/core';import { RouterLink } from '@angular/router';import {  ScButton,

Controlled

import { Component, ViewEncapsulation, signal } from '@angular/core';import { RouterLink } from '@angular/router';import {  ScButton,  ScSidebar,  ScSidebarContent,  ScSidebarGroup,  ScSidebarGroupContent,  ScSidebarGroupLabel,  ScSidebarInset,

Collapse to icons

import { Component, ViewEncapsulation } from '@angular/core';import { RouterLink } from '@angular/router';import {  ScSidebar,  ScSidebarContent,  ScSidebarGroup,  ScSidebarGroupContent,  ScSidebarGroupLabel,  ScSidebarHeader,  ScSidebarInset,

Floating

import { Component, ViewEncapsulation } from '@angular/core';import { RouterLink } from '@angular/router';import {  ScSidebar,  ScSidebarContent,  ScSidebarGroup,  ScSidebarGroupContent,  ScSidebarGroupLabel,  ScSidebarHeader,  ScSidebarInset,

Inset

import { Component, ViewEncapsulation } from '@angular/core';import { RouterLink } from '@angular/router';import {  ScSidebar,  ScSidebarContent,  ScSidebarGroup,  ScSidebarGroupContent,  ScSidebarGroupLabel,  ScSidebarHeader,  ScSidebarInset,

RTL

import { Dir } from '@angular/cdk/bidi';import { Component, LOCALE_ID, ViewEncapsulation } from '@angular/core';import { RouterLink } from '@angular/router';import {  ScSidebar,  ScSidebarContent,  ScSidebarGroup,  ScSidebarGroupContent,  ScSidebarGroupLabel,  ScSidebarInset,

API reference

Each part takes class, merged over the style (cn: a later class wins).

PartSelectorDescription
ScSidebarProviderdiv[scSidebarProvider] Round the sidebar and the page: [(open)] (wide screens), storageKey (where the state is kept, a cookie and localStorage; null keeps nothing), shortcut (with Ctrl or ⌘; 'b', null for none). exportAs scSidebarProvider: state.
ScSidebaraside[scSidebar], div[scSidebar] The sidebar. side ('inline-start' default, 'inline-end'; or 'left', 'right', which stay put), variant ('sidebar', 'floating', 'inset'), collapsible ('offcanvas', 'icon', 'none'); aria-label names the aside, and the sheet on phones.
ScSidebarInsetmain[scSidebarInset] The page beside the sidebar; a raised card with the inset variant.
ScSidebarTriggerbutton[scSidebarTrigger] Opens and closes the sidebar (the sheet on phones): the style's button (ghost, icon-sm; variant, size), aria-expanded, aria-controls. Write its icon and its name.
ScSidebarRailbutton[scSidebarRail] The sidebar's edge, for the pointer: a click opens or closes it.
ScSidebarHeader, ScSidebarFooterdiv[…] They stay at the top and the bottom.
ScSidebarContentnav[scSidebarContent], div[scSidebarContent] The part that scrolls, between them; its scrollbar shown (hideScrollbar hides it, as shadcn).
ScSidebarSeparator, ScSidebarInputdiv[…], input[…] A line between sections; a search input in the sidebar.
ScSidebarGroup, ScSidebarGroupLabel, ScSidebarGroupAction, ScSidebarGroupContentdiv[…], div|button[…Label], button[…Action], div[…] A group: its label (a button when it is a Collapsible trigger), a button at its end (name it), its content.
ScSidebarMenu, ScSidebarMenuItemul[…], li[…] A list of items.
ScSidebarMenuButtona[scSidebarMenuButton], button[scSidebarMenuButton] An item's link or button: your icon, then its text in a span. active (aria-current="page"), variant ('default', 'outline'), size ('default', 'sm', 'lg'), tooltip (its name, shown while collapsed to icons).
ScSidebarMenuAction, ScSidebarMenuBadgebutton[…], div[…] At an item's end: a button (showOnHover; name it), or a count.
ScSidebarMenuSkeletondiv[scSidebarMenuSkeleton] A row shown while the menu loads; showIcon.
ScSidebarMenuSub, ScSidebarMenuSubItem, ScSidebarMenuSubButtonul[…], li[…], a|button[…] A list under an item, its items and their links: active, size ('sm', 'md').