AppSidebar

AppSidebar keeps an application's destinations in view. Choose its children door when links must keep their router behavior; groups supports links, router links and button actions.

Examples

Responsive application shell

A rich workspace header, stationary account footer and scrolling navigation share a Shell with a native topbar trigger. Try the icon rail, compact density, keyboard shortcut and mobile offcanvas.

Workspace overview

A place for your team's work

Move between projects, people and files. This embedded shell links to the corresponding Lyra component pages.

Use the topbar control or ⌘ / Ctrl + B to switch between the expanded sidebar and icon rail. Your desktop preference stays in place when the viewport changes.

Navigation scrolls between the workspace switcher and your account. Open the switcher to try a second workspace.

Convenience groups

groups renders an anchor when an item has href, composes a router link with asChild, and keeps a button when neither is supplied. Its string label names the icon rail item.

Selected: Overview

When to use

Use AppSidebar for grouped, persistent destinations in an application shell.

Reach for something else when:

  • A short row of site-level routes is enough — use Navbar.
  • The destinations are peer views inside the current page — use Tabs.
  • You only need one navigation section — use SidebarGroup.

Width and Shell ownership

CSS-only AppSidebar markup defaults to 260px; add lyra-appsidebar--rail for its 64px rail default. Set --appsidebar-width when your application needs a different width. When AppSidebar is the direct sidebar child of a content-scroll Shell, AppSidebar owns that rail; Shell.sidebarWidth continues to size ordinary sidebar content and page-scroll Shell rails.

html
<!-- Defaults to 260px. -->
<nav class="lyra-appsidebar" aria-label="Workspace navigation"></nav>

<!-- Defaults to 64px; this explicit property is also supported. -->
<nav
  class="lyra-appsidebar lyra-appsidebar--rail"
  style="--appsidebar-width: 64px"
  aria-label="Workspace navigation"
></nav>

<!-- A consumer override for an expanded sidebar. -->
<nav
  class="lyra-appsidebar"
  style="--appsidebar-width: 296px"
  aria-label="Workspace navigation"
></nav>

Accessibility

  • AppSidebar renders a native <nav>. Pass aria-label through to it when the page has more than one navigation landmark.
  • In groups mode, href items are native links, asChild items preserve the router link, and action-only items remain buttons. The active item receives aria-current="page". The item callback runs before onSelect(id, item). Use target="_blank" for native new-tab links.
  • In composition mode, children remain their original elements. Use real anchors for routes; when rail mode is active, link descendants get a title and aria-label from an explicit label, title, or their text content.
  • The collapse control is a native button whose accessible name and tooltip come from labels (English by default). In rail mode, group labels, item text and badges are visually hidden, while the control and item names remain available.
  • A WorkspaceSwitcher in the brand slot becomes a compact avatar trigger in rail mode. Its workspace name remains available to assistive technology; its popover keeps a usable width and stays within a narrow viewport.

React Router and Next.js destinations

tsx
// React Router: the router owns client-side navigation.
<AppSidebar groups={[{ items: [{ id: 'reports', label: 'Reports', active: true,
  asChild: <Link to="/reports" /> }] }]} />

// Next.js: the same item contract accepts Next Link.
<AppSidebar groups={[{ items: [{ id: 'billing', label: 'Billing',
  asChild: <Link href="/billing" /> }] }]} />

A plain route needs only href: '/reports'. Set target: '_blank' and rel: 'noopener noreferrer' for a new tab. asChild replaces the child link's content with the item's label and icon; do not put a second label inside the Link.

Controlled React shell

The example uses Shell mainAs="div" because this documentation page already owns the main landmark. The topbar's AppSidebarTrigger controls an existing nav by id. Desktop collapse and mobile opening are independent application state; the package does not choose breakpoints or persist preferences. The initial server render is desktop, then a local media query reconciles below 768px. Only one application navigation is active. Moving between viewports closes mobile navigation, preserves the desktop rail preference and reconciles focus to the visible trigger.

AppSidebarTrigger always renders a native type="button", forwards its button ref, and provides aria-controls and aria-expanded. Its default label is “Toggle sidebar”, variant is ghost, and size is md; every size retains a 44px target. A consumer click handler runs first and may cancel with preventDefault(). Disabled triggers do not request changes.

tsx
<AppSidebarTrigger
  controls="workspace-navigation"
  collapsed={desktopCollapsed}
  onCollapsedChange={setDesktopCollapsed}
  hotkey
/>
<AppSidebar id="workspace-navigation" collapsed={desktopCollapsed} />

The optional shortcut is Cmd/Ctrl+B. It ignores editable fields, composing input, repeats, Alt/Shift and canceled events. Duplicate triggers for one id share one logical owner. The deepest eligible navigation or trigger containing the event target wins; outside those scopes, only one unambiguous owner can act. Background modal content cannot toggle. Focus inside desktop navigation returns to its eligible controlling trigger before either expansion or collapse; focus in application content stays in place. Offcanvas navigation retains its modal focus ownership.

Offcanvas navigation

AppSidebarOffcanvas adapts Drawer at the logical start edge, including RTL. It keeps Drawer's modal focus trap, inert background, scroll lock, Escape/backdrop dismissal, initial focus, return focus and exit presence. Its ref points to the outer panel; the child's ref still points to the native nav. The required title names the dialog, and closeLabel defaults to “Close”. A controlled open panel remains shortcut-eligible from inside even while its external trigger is inert. Closed navigation is absent, and an exiting panel is inert.

tsx
<AppSidebarTrigger
  controls="workspace-panel"
  collapsed={!mobileOpen}
  onCollapsedChange={(collapsed) => setMobileOpen(!collapsed)}
  hotkey
/>
<AppSidebarOffcanvas
  id="workspace-panel"
  title="Application navigation"
  open={mobileOpen}
  onClose={() => setMobileOpen(false)}
  returnFocusTo={() => triggerRef.current}
>
  <AppSidebar aria-label="Application navigation" />
</AppSidebarOffcanvas>

The host omits Shell.sidebar in mobile mode so no empty rail remains. Links keep their real hrefs and router handlers. The example explicitly closes on accepted, unmodified navigation; Offcanvas never intercepts routes. Desktop modal navigation can use the same adapter.

Rail hints and density

railTooltips defaults to false. Opt in to shared clipping-safe Tooltip hints for collapsed data items. Their accessible names, active state, hrefs and router refs remain available. A child handler's preventDefault() also cancels the sidebar selection callback. Expanded items and the legacy default retain their current presentation.

Compose <Tooltip tip="Projects" escapeClipping> explicitly around custom links or opaque router components. In rail mode AppSidebar preserves their accessible names without adding a duplicate native title. Tooltip hints do not move focus and can be dismissed with Escape.

density="compact" is local: rows use --text-sm and a 32px minimum on fine-only pointers, with 44px targets on coarse or hybrid devices. The default SidebarGroup rhythm remains 36px. The example's “Compact rows” control lets you compare both densities.

Scoped customization

Set aliases on the owning AppSidebar, or on the Offcanvas panel for its header and child sidebar. For a portal to retain an application scope, supply a container inside that scope, as this example does. These aliases introduce no global token values and do not style an independent SidebarGroup. Existing theme, brand and direction scopes continue to apply.

AliasOwning treatment and unchanged fallback
--appsidebar-backgroundNav and offcanvas surface: --surface-card
--appsidebar-foregroundItem text: --text-secondary; icons, role and toggle: --text-muted; user name and panel title: --text-primary; nav otherwise inherits
--appsidebar-primaryActive icon and active badge ink: --accent
--appsidebar-primary-foregroundActive badge surface: --surface-card
--appsidebar-accentActive row: --accent-soft; idle row/toggle hover: --surface-sunken
--appsidebar-accent-foregroundActive row: --accent-soft-text; toggle hover ink: --text-primary
--appsidebar-borderNav, footer and panel separators: --border-default
--appsidebar-ringFocus color; shadow controls fall back to --focus-ring, native outlines retain their default
--appsidebar-width-mobileOffcanvas width: 18rem, capped to the actual visual viewport width

Keep the existing --appsidebar-width contract (260px expanded, 64px rail), the global --sidebar-width token and custom inline width precedence outside Offcanvas. Inside the adapter, AppSidebar fills the panel even when its desktop width is explicit. The panel follows its owner document's visual viewport width, height and start-edge offsets during zoom, resize and scrolling. The mobile width alias still chooses the preferred width; the visible viewport caps it.

These companions and state rules are React capabilities, exported from both @lyra-ds/react and the existing @lyra-ds/react/app-sidebar entry. Canonical HTML, Alpine and Blade producers are unchanged; their tabs below describe their existing contracts, not the React adapter.

API and code

NameTypeRequiredDescription
brandReactNode—Optional brand content placed above the navigation groups.
groupsAppSidebarGroup[]—Convenience data mode. Each group is composed through {@link SidebarGroup}; destinations render as links, while items without destinations remain buttons.
footerReactNode—Optional utility links or user content separated below the navigation groups.
widthnumber—Sidebar width in pixels while expanded. Default: `260`. In a content-scroll {@link Shell} where AppSidebar is the direct sidebar child, this width owns that rail.
collapsibleboolean—Whether to render a control that switches between expanded and icon-rail modes.
collapsedboolean—Controlled icon-rail state.
defaultCollapsedboolean—Initial icon-rail state when uncontrolled.
onCollapsedChange(collapsed: boolean) => void—Called whenever the icon-rail state changes.
onSelect(id: string, item: AppSidebarGroupItem) => void—Called after an item-level callback in data mode.
railTooltipsboolean—Show clipping-safe hints for collapsed data items. Default `false`. Compose Tooltip explicitly for custom children.
density'default' | 'compact'—Local row density. Compact uses 32px fine-pointer rows and retains 44px coarse targets. Default `"default"`.
labelsAppSidebarLabels—Localized labels for the collapse control.
childrenReactNode—Composition mode. Pass {@link SidebarGroup} children (including link children) to preserve their original element type and routing behavior.

AppSidebarTrigger

NameTypeRequiredDescription
collapsedbooleanRequiredApplication-owned state: `true` means the controlled sidebar is collapsed or closed.
onCollapsedChange(collapsed: boolean) => voidRequiredRequests the next state without keeping an internal mirror.
controlsstringRequiredId of the controlled navigation or offcanvas panel.
hotkeyboolean—Opt into document-scoped Cmd/Ctrl+B ownership. Default `false`.
labelstring—Accessible name and native tooltip. Default `"Toggle sidebar"`.
variantIconButtonProps['variant']—Visual variant. Default `"ghost"`.
sizeIconButtonProps['size']—Glyph sizing preset. Default `"md"`; the target is at least 44px for every size.
childrenReactNode—Optional glyph; defaults to an inline panel-left icon.

AppSidebarOffcanvas

NameTypeRequiredDescription
openbooleanRequiredControls visibility. Closed navigation is absent; the exiting panel is inert.
titleReactNodeRequiredHeading rendered in the existing Drawer header and used as the dialog name.
closeLabelstring—Accessible name for the close button. Default `"Close"`.
containerHTMLElement—Portal host. Defaults to `document.body`; use a scoped host to retain local tokens.
initialFocusTo() => HTMLElement | null—Resolves the initial focus target inside the panel on each accepted opening.
returnFocusTo() => HTMLElement | null—Resolves the current eligible focus destination after an accepted close.
onClose() => voidRequiredRequests closure; the application owns `open`.
childrenReactElementRequiredOne AppSidebar element, filling the panel regardless of its desktop width, retaining its native nav and ref.

x-data="lyraAppSidebar({ … })"

OptionTypeRequiredDescription
defaultCollapsedboolean—Whether the sidebar starts in its icon-rail state. Default: `false`.
widthnumber—Sidebar width in pixels while expanded. Default: `260`.
labelsLyraAppSidebarLabels—Localized labels for the collapse control.

The composed-link door is ordinary navigation markup. Supply the collapse behavior, rail labels and selection handling yourself outside React. Render the collapse control as a native button with an aria-label; inject a chevron <svg> and flip it between the expanded and rail states. If you leave the button empty, the stylesheet supplies a fallback chevron so the control never renders as a blank box, but an injected icon lets you reflect collapse state:

html
<nav class="lyra-appsidebar" aria-label="Workspace navigation">
  <div class="lyra-appsidebar__brand"><strong>Acme</strong></div>
  <div class="lyra-appsidebar__groups">
    <div class="lyra-sbgroup">
      <div class="lyra-sbgroup__label">Workspace</div>
      <div class="lyra-sbgroup__items">
        <a
          class="lyra-sbgroup__item lyra-sbgroup__item--active"
          href="/overview"
          aria-current="page"
        >
          <span class="lyra-sbgroup__item-label">Overview</span>
        </a>
      </div>
    </div>
  </div>
  <button class="lyra-appsidebar__toggle" type="button" aria-label="Collapse sidebar">
    <svg
      aria-hidden="true"
      width="15"
      height="15"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      stroke-width="2"
      stroke-linecap="round"
      stroke-linejoin="round"
    >
      <!-- expanded: chevron points inward; swap to m9 18 6-6-6-6 in the rail -->
      <path d="m15 18-6-6 6-6" />
    </svg>
  </button>
</nav>

<lyra:app-sidebar> Generated from lyra-ds/blade v0.10.0.

The behavior comes from lyraAppSidebar() — install @lyra-ds/alpine and see the HTML + Alpine tab.

PropDefaultRequiredExample values
brandnull——
groups[]——
footernull——
width260——
collapsiblefalse——
defaultCollapsedfalse——
labels[]——
blade
<lyra:app-sidebar
    :groups="[
        ['heading' => 'Workspace', 'items' => [
            ['id' => 'overview', 'label' => 'Overview', 'active' => true],
            ['id' => 'projects', 'label' => 'Projects', 'badge' => '12'],
        ]],
        ['heading' => 'Account', 'items' => [
            ['id' => 'billing', 'label' => 'Billing'],
            ['id' => 'settings', 'label' => 'Settings'],
        ]],
    ]"
    collapsible
/>