Skip to content

Sidebar ​

An app sidebar that collapses to icons on desktop (remembered, Ctrl/⌘ B) and becomes a drawer on phones.

  • Version 1.0.0
  • Bootstrap 5.3+
  • Requires Bootstrap CSS + Bootstrap JS bundle
  • Source on GitHub

Installation ​

bash
npx bootcn add sidebar
bash
pnpm dlx bootcn add sidebar
bash
yarn dlx bootcn add sidebar
bash
bunx --bun bootcn add sidebar

The CLI also installs the design tokens and these components: avatar. It prints the <link> and <script> tags to add to your layout.

Usage ​

Use it as the page layout: sidebar, then the main column with a header that holds the toggle. The app-sidebar block is a richer, static variant; this component adds collapsing. Links carry a title, so the collapsed rail shows a native tooltip.

JavaScript API ​

  • data-bootcn-sidenav-layout on the layout; the sidebar is a Bootstrap offcanvas-lg <aside>; the button with data-bootcn-sidenav-toggle (aria-controls = the sidebar id) toggles it.
  • From 992px the toggle collapses the sidebar to an icon rail (class is-collapsed on the layout, saved in localStorage as bootcn-sidenav-collapsed); below it, it opens the drawer.
  • Ctrl+B / ⌘B toggles it. Event bootcn:sidebar:toggle (bubbles), detail.collapsed.
  • Widths: --bootcn-sidenav-width and --bootcn-sidenav-collapsed-width.
  • Bootcn.sidebar.toggle(layout); init() / destroy().

Accessibility ​

  • The sidebar is an <aside aria-label="Sidebar"> with a <nav>; the current page has aria-current="page".
  • Collapsed, the labels are visually hidden but still read, so every link keeps its name.
  • The toggle has a name and aria-expanded (collapsed state on desktop, drawer state on phones). As a drawer, Bootstrap traps focus and closes on Escape.

Dependencies ​

  • Bootstrap 5.3+ CSS and the JS bundle.
  • themes/tokens/tokens.css.
  • The avatar component.

Source ​

The exact files the CLI installs (sidebar@1.0.0). View on GitHub · Report an issue

html
<div class="bootcn-sidenav-layout" data-bootcn-sidenav-layout>
  <aside
    class="offcanvas-lg offcanvas-start bootcn-sidenav"
    id="bootcn-sidenav"
    tabindex="-1"
    aria-label="Sidebar"
  >
    <div class="bootcn-sidenav__header">
      <svg
        class="bootcn-icon bootcn-sidenav__logo"
        width="16"
        height="16"
        viewBox="0 0 24 24"
        fill="none"
        stroke="currentColor"
        stroke-width="2"
        stroke-linecap="round"
        stroke-linejoin="round"
        aria-hidden="true"
        focusable="false"
      >
        <rect x="3" y="3" width="8" height="8" rx="2" />
        <rect x="13" y="13" width="8" height="8" rx="2" />
        <path d="M13 7h8M17 3v8M3 17h8" />
      </svg>
      <span class="bootcn-sidenav__label bootcn-sidenav__brand">Acme Inc.</span>
    </div>
    <nav class="bootcn-sidenav__nav" aria-label="Main">
      <ul class="bootcn-sidenav__list">
        <li>
          <a class="bootcn-sidenav__link" href="#" aria-current="page" title="Dashboard">
            <svg
              class="bootcn-icon"
              width="16"
              height="16"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
              focusable="false"
            >
              <path d="M3 11l9-8 9 8" />
              <path d="M5 10v10h14V10" />
            </svg>
            <span class="bootcn-sidenav__label">Dashboard</span>
          </a>
        </li>
        <li>
          <a class="bootcn-sidenav__link" href="#" title="Inbox">
            <svg
              class="bootcn-icon"
              width="16"
              height="16"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
              focusable="false"
            >
              <path d="M3 13h5l2 3h4l2-3h5" />
              <path d="M5 5h14l2 8v6H3v-6z" />
            </svg>
            <span class="bootcn-sidenav__label">Inbox</span>
          </a>
        </li>
        <li>
          <a class="bootcn-sidenav__link" href="#" title="Reports">
            <svg
              class="bootcn-icon"
              width="16"
              height="16"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
              focusable="false"
            >
              <path d="M4 20V10" />
              <path d="M10 20V4" />
              <path d="M16 20v-7" />
              <path d="M2 20h20" />
            </svg>
            <span class="bootcn-sidenav__label">Reports</span>
          </a>
        </li>
        <li>
          <a class="bootcn-sidenav__link" href="#" title="Team">
            <svg
              class="bootcn-icon"
              width="16"
              height="16"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
              focusable="false"
            >
              <circle cx="9" cy="8" r="4" />
              <path d="M2 21c0-4 3-7 7-7s7 3 7 7" />
              <path d="M17 4a4 4 0 0 1 0 8" />
              <path d="M22 21c0-3-2-6-5-7" />
            </svg>
            <span class="bootcn-sidenav__label">Team</span>
          </a>
        </li>
        <li>
          <a class="bootcn-sidenav__link" href="#" title="Settings">
            <svg
              class="bootcn-icon"
              width="16"
              height="16"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
              focusable="false"
            >
              <path d="M4 6h9" />
              <path d="M19 6h1" />
              <circle cx="16" cy="6" r="2" />
              <path d="M4 18h3" />
              <path d="M13 18h7" />
              <circle cx="10" cy="18" r="2" />
              <path d="M4 12h1" />
              <path d="M11 12h9" />
              <circle cx="8" cy="12" r="2" />
            </svg>
            <span class="bootcn-sidenav__label">Settings</span>
          </a>
        </li>
      </ul>
    </nav>
    <div class="bootcn-sidenav__footer">
      <span class="bootcn-avatar bootcn-avatar--sm" aria-hidden="true"
        ><span class="bootcn-avatar__fallback">MP</span></span
      >
      <span class="bootcn-sidenav__label">Maria Papadopoulou</span>
    </div>
  </aside>
  <div class="bootcn-sidenav-layout__main">
    <header class="bootcn-sidenav-layout__header">
      <button
        type="button"
        class="bootcn-sidenav__toggle"
        data-bootcn-sidenav-toggle
        aria-controls="bootcn-sidenav"
        aria-expanded="true"
      >
        <svg
          class="bootcn-icon"
          width="16"
          height="16"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
          focusable="false"
        >
          <rect x="3" y="3" width="18" height="18" rx="2" />
          <path d="M9 3v18" />
        </svg>
        <span class="visually-hidden">Toggle sidebar</span>
      </button>
      <span class="bootcn-sidenav-layout__title">Dashboard</span>
    </header>
    <main class="bootcn-sidenav-layout__content">
      <h1 class="h4">Dashboard</h1>
      <p class="text-body-secondary">
        Collapse the sidebar with the button or Ctrl+B (⌘B); on small screens it opens as a drawer.
      </p>
    </main>
  </div>
</div>
css
.bootcn-sidenav-layout {
  --bootcn-sidenav-width: 15rem;
  --bootcn-sidenav-collapsed-width: 3.5rem;

  display: flex;
  min-height: 100vh;
}

.bootcn-sidenav {
  --bs-offcanvas-width: var(--bootcn-sidenav-width);
  --bs-offcanvas-bg: var(--bs-tertiary-bg);

  display: flex;
  flex-direction: column;
}

@media (min-width: 992px) {
  .bootcn-sidenav {
    position: sticky;
    top: 0;
    flex: 0 0 var(--bootcn-sidenav-width);
    width: var(--bootcn-sidenav-width);
    height: 100vh;
    overflow-x: hidden;
    border-right: var(--bootcn-border-width) solid var(--bootcn-border-color);
    background-color: var(--bs-tertiary-bg);
    transition:
      width var(--bootcn-transition),
      flex-basis var(--bootcn-transition);
  }

  /* Collapsed: an icon rail. Labels are visually hidden, not removed, so
     links keep their names. */
  .bootcn-sidenav-layout.is-collapsed .bootcn-sidenav {
    flex-basis: var(--bootcn-sidenav-collapsed-width);
    width: var(--bootcn-sidenav-collapsed-width);
  }

  .bootcn-sidenav-layout.is-collapsed .bootcn-sidenav__label {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
  }
}

.bootcn-sidenav__header,
.bootcn-sidenav__footer {
  display: flex;
  align-items: center;
  gap: 0.625rem;
  padding: 0.875rem 1rem;
  white-space: nowrap;
}

.bootcn-sidenav__footer {
  margin-top: auto;
  border-top: var(--bootcn-border-width) solid var(--bootcn-border-color);
  font-size: 0.875rem;
  font-weight: 500;
}

.bootcn-sidenav__footer .bootcn-avatar {
  --bootcn-avatar-size: 1.75rem;

  margin-left: -0.125rem;
}

.bootcn-sidenav__logo {
  flex: none;
  width: 1.25rem;
  height: 1.25rem;
}

.bootcn-sidenav__brand {
  font-weight: 600;
}

.bootcn-sidenav__nav {
  padding: 0.5rem;
}

.bootcn-sidenav__list {
  display: flex;
  flex-direction: column;
  gap: 0.125rem;
  margin: 0;
  padding: 0;
  list-style: none;
}

.bootcn-sidenav__link {
  display: flex;
  align-items: center;
  gap: 0.625rem;
  padding: 0.5rem 0.625rem;
  border-radius: var(--bootcn-radius);
  color: var(--bootcn-foreground);
  font-size: 0.875rem;
  font-weight: 500;
  text-decoration: none;
  white-space: nowrap;
}

.bootcn-sidenav__link .bootcn-icon {
  flex: none;
}

.bootcn-sidenav__link:hover,
.bootcn-sidenav__link[aria-current='page'] {
  background-color: var(--bootcn-accent);
  color: var(--bootcn-accent-foreground);
}

.bootcn-sidenav__link:focus-visible {
  outline: 0;
  box-shadow: var(--bootcn-focus-ring);
}

.bootcn-sidenav-layout__main {
  flex: 1 1 auto;
  min-width: 0;
}

.bootcn-sidenav-layout__header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  height: 3.5rem;
  padding: 0 1rem;
  border-bottom: var(--bootcn-border-width) solid var(--bootcn-border-color);
}

.bootcn-sidenav-layout__title {
  font-size: 0.875rem;
  font-weight: 500;
}

.bootcn-sidenav__toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.75rem;
  height: 1.75rem;
  border: 0;
  border-radius: var(--bootcn-radius);
  background: transparent;
  color: var(--bootcn-foreground);
}

.bootcn-sidenav__toggle:hover {
  background-color: var(--bootcn-accent);
}

.bootcn-sidenav__toggle:focus-visible {
  outline: 0;
  box-shadow: var(--bootcn-focus-ring);
}

.bootcn-sidenav-layout__content {
  padding: 1.5rem;
}
js
(function () {
  'use strict';

  const ns = (window.Bootcn = window.Bootcn || {});
  if (ns.sidebar) return; // already loaded: idempotent

  const LAYOUT = '[data-bootcn-sidenav-layout]';
  const TOGGLE = '[data-bootcn-sidenav-toggle]';
  const STORAGE_KEY = 'bootcn-sidenav-collapsed';
  const desktop = window.matchMedia('(min-width: 992px)');
  let listening = false;

  function remembered() {
    try {
      return localStorage.getItem(STORAGE_KEY) === 'true';
    } catch {
      return false;
    }
  }

  function remember(collapsed) {
    try {
      localStorage.setItem(STORAGE_KEY, String(collapsed));
    } catch {
      // Storage unavailable: the state lasts for this page only.
    }
  }

  function sidebarOf(toggle) {
    return document.getElementById(toggle.getAttribute('aria-controls'));
  }

  function sync(layout) {
    const toggle = layout.querySelector(TOGGLE);
    if (!toggle) return;
    if (desktop.matches) {
      toggle.setAttribute('aria-expanded', String(!layout.classList.contains('is-collapsed')));
    } else {
      const sidebar = sidebarOf(toggle);
      toggle.setAttribute('aria-expanded', String(!!sidebar && sidebar.classList.contains('show')));
    }
  }

  function toggle(layout) {
    const button = layout.querySelector(TOGGLE);
    if (desktop.matches) {
      const collapsed = !layout.classList.contains('is-collapsed');
      layout.classList.toggle('is-collapsed', collapsed);
      remember(collapsed);
      sync(layout);
      layout.dispatchEvent(
        new CustomEvent('bootcn:sidebar:toggle', {
          bubbles: true,
          detail: { collapsed: collapsed },
        }),
      );
    } else if (button && window.bootstrap && window.bootstrap.Offcanvas) {
      window.bootstrap.Offcanvas.getOrCreateInstance(sidebarOf(button)).toggle();
    }
  }

  function onClick(event) {
    const button = event.target.closest && event.target.closest(TOGGLE);
    if (!button) return;
    const layout = button.closest(LAYOUT);
    if (layout) toggle(layout);
  }

  function onKeydown(event) {
    if (!(event.ctrlKey || event.metaKey) || event.altKey || event.key.toLowerCase() !== 'b')
      return;
    const layout = document.querySelector(LAYOUT);
    if (!layout) return;
    event.preventDefault();
    toggle(layout);
  }

  function onOffcanvas(event) {
    const layout = event.target.closest && event.target.closest(LAYOUT);
    if (layout) sync(layout);
  }

  function onBreakpoint() {
    document.querySelectorAll(LAYOUT).forEach(sync);
  }

  function init(root = document) {
    root.querySelectorAll(LAYOUT).forEach((layout) => {
      layout.classList.toggle('is-collapsed', remembered());
      sync(layout);
    });
    if (listening) return;
    document.addEventListener('click', onClick);
    document.addEventListener('keydown', onKeydown);
    document.addEventListener('shown.bs.offcanvas', onOffcanvas);
    document.addEventListener('hidden.bs.offcanvas', onOffcanvas);
    desktop.addEventListener('change', onBreakpoint);
    listening = true;
  }

  function destroy() {
    if (!listening) return;
    document.removeEventListener('click', onClick);
    document.removeEventListener('keydown', onKeydown);
    document.removeEventListener('shown.bs.offcanvas', onOffcanvas);
    document.removeEventListener('hidden.bs.offcanvas', onOffcanvas);
    desktop.removeEventListener('change', onBreakpoint);
    listening = false;
  }

  ns.sidebar = { init: init, destroy: destroy, toggle: toggle };

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', () => init());
  } else {
    init();
  }
})();

Released under the MIT License. Not affiliated with Bootstrap or shadcn/ui.