Sidebar

An app's navigation column: a rail beside the page on a wide screen, a drawer on a phone, the same nav in both. It holds the workspace at the top, the pages, and the account at the foot, and can fold to icons and remember it.

Sidebar

Needs Alpine and the package's script. See getting started. Without a script it is a plain column of links: beside the page from the breakpoint up, above the page below it.

Use it

The sidebar is one half of your layout's row; the page is the other:

<body>
    <a href="#main" class="sr-only focus:not-sr-only …">Skip to content</a>

    <div class="lg:flex">
        <x-ds::sidebar id="sidebar" :collapsible="true" cookie="sidebar">
            <x-slot:header>
                <x-ds::team-switcher :teams="$teams" :current="$team->id" name="team_id" :action="route('current-team.update')" method="PUT" />
            </x-slot:header>

            <x-ds::nav-item :href="route('home')" label="Home" :active="request()->routeIs('home')">
                <x-slot:icon><x-ds::icon name="home" /></x-slot:icon>
            </x-ds::nav-item>
            <x-ds::sub-nav label="Projects" :href="route('projects.index')">
                <x-slot:icon><x-ds::icon name="folder" /></x-slot:icon>
                @foreach ($projects as $project)
                    <x-ds::nav-item :href="route('projects.show', $project)" :label="$project->name" />
                @endforeach
            </x-ds::sub-nav>

            <x-slot:footer>
                <x-ds::user-menu :name="$user->name" :email="$user->email" align="start" :signOutAction="route('logout')" />
            </x-slot:footer>
        </x-ds::sidebar>

        <div class="flex min-w-0 flex-1 flex-col">
            <header class="sticky top-0 flex h-14 items-center gap-2 border-b border-divider px-4">
                <x-ds::button variant="ghost" icon="bars-3" :iconOnly="true" aria-label="Menu"
                    aria-haspopup="dialog" aria-controls="sidebar" aria-expanded="false"
                    class="lg:hidden [@media(scripting:none)]:hidden" />
                …
            </header>
            <main id="main" tabindex="-1">{{ $slot }}</main>
        </div>
    </div>
</body>
  • The nav items need nothing more. Inside a collapsible sidebar, nav items, sub-navs, the team switcher and the user menu fold with it: no collapsedWhen or compactWhen on each, and the server draws them at the starting width.
  • The menu button is yours, in your header, because that is where a phone reader looks. Anything with aria-controls naming the sidebar's id opens it, and the sidebar keeps its aria-expanded. Hide it from the breakpoint up (lg:hidden for the default drawerBelow="lg") and when there is no script.
  • The collapse toggle is the sidebar's, at its foot, shown only on the rail and only with a script.

Props and slots

PropTypeDefaultWhat it does
labelstring"Primary"The nav landmark's name.
collapsibleboolfalseA toggle at the foot folds the rail to icons.
collapsedboolfalseWith collapsible: the starting state when no cookie says otherwise.
cookiestring—With collapsible: a cookie the script writes (collapsed or expanded) and the component reads, so the next page is drawn at the reader's width. See Remember the width.
drawerBelowsm | md | lg | xl | falselgBelow this breakpoint it is a drawer. false: a rail at every width.
menuLabelstring"Menu"The drawer's visible title and its dialog name. Use the same words on your menu button.
collapseLabelstring"Collapse sidebar"The collapse toggle's name.
widthCSS length15remThe rail's width.
railWidthCSS length3.5remThe folded rail's width.
idstringds-sidebarOn the panel; what the menu button's aria-controls names.
SlotWhat goes in it
headerAbove the nav: the workspace, a logo.
(default)The nav items, inside the nav. It scrolls on its own; class="mt-auto" on an item pins it to the foot.
footerUnder the nav: the account.

Other attributes go on the root. The rail is the window's height; set --ds-sidebar-height on an ancestor to change it (a banner above the shell, a demo frame).

In Alpine, the sidebar's scope exposes collapsed (read it to fold something of your own) and dsSidebarOpen.

Remember the width

With cookie="sidebar" the toggle writes sidebar=collapsed (or expanded) and the component reads it, so a folded rail is drawn folded on the next page's first paint, not corrected after it. Laravel encrypts its cookies and discards any it cannot decrypt, which would include this one, so let it through:

// bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
    $middleware->encryptCookies(except: ['sidebar']);
})
  • Put a "Skip to content" link first in the body, to main#main (tabindex="-1"), before the sidebar: on every page the nav is a dozen Tab stops a keyboard reader should not have to walk.
  • The sidebar is one nav named by label. The header and footer slots sit outside it.
  • A second navigation on the page needs another name: a bottom nav on phones takes label, as here.
<x-ds::bottom-nav label="Shortcuts">
    …
    <x-ds::bottom-nav-item label="More" aria-haspopup="dialog" aria-controls="sidebar">
        <x-slot:icon><x-ds::icon name="bars-3" size="6" /></x-slot:icon>
    </x-ds::bottom-nav-item>
</x-ds::bottom-nav>

Accessibility

  • As a drawer it is a modal dialog named by menuLabel: focus moves in, Tab stays inside, Escape, the Close button, the backdrop or following a link closes it, and focus goes back to the button that opened it. Grow the window past the breakpoint and it closes.
  • Closed, the drawer is hidden from everyone, not only from sight.
  • The collapse toggle is a toggle button (aria-pressed). Folded, each nav item keeps its name in its title and its hidden label, and the switchers carry their names on their buttons.
  • Without a script, below the breakpoint, the sidebar sits above the page with every link, rather than behind a menu button that cannot open.
KeyWhat it does
Enter, Space on the menu buttonOpens the drawer
Tab, Shift+TabMoves within the open drawer
EscapeCloses the drawer, back to the menu button
Enter, Space on the toggleFolds or unfolds the rail

Don't

  • Don't draw the nav twice for the phone: the sidebar is the drawer.
  • Don't fold the rail by writing collapsed from outside; the toggle keeps the cookie in step.
  • Don't name a cookie without the encryptCookies exception.
  • Don't build a top bar with this. A horizontal app bar is top nav.

More in specs/sidebar.md. A whole shell built on it: App shell with sidebar.


From the package repo: edit this page on GitHub.

Was this page helpful?