App shell with sidebar

The frame around every page of an app: a sidebar with the workspace, the pages and the account, a slim row over the page with search, notifications and the theme, and the page itself. Collapses to icons on a desktop and remembers it; becomes a drawer on a phone. Delivered as a layout component, components/layouts/app.blade.php, so a page reads <x-layouts.app title="Home"> and nothing else about the frame.

App shell with sidebar, light and dark

App shell with sidebar on a phone, light and dark

A block, not a component: copy the views below into your app. It assumes the base layout. It holds the command palette and the notification center, which are blocks of their own; copy their views too, or take those two lines out. The top-nav shell is the other frame, with the same name and the same data, so a page does not change when you swap one for the other.

What it shows

  • The sidebar, one sidebar: the team switcher at the top, the pages as nav items with the Inbox count, Projects as a sub-nav that opens to each project, Settings at the foot of the list, and the user menu at the bottom. The current page is marked (aria-current="page") from the route, so it is right on any page without a word from the page.

  • The top row: the menu button on a phone, the page's breadcrumb when it has one (a breadcrumb slot, from md up; see the page header detail page), the search button with its shortcut, the notification bell with its unread count, and the theme switcher.

  • Collapsed to icons, by the toggle at the sidebar's foot. Each nav item shows its icon only, its name kept in its title, which the browser shows on hover and reads as its name; the team switcher and the user menu draw their avatar only:

    The sidebar collapsed to icons, light and dark

    The choice is kept in a cookie and read by the sidebar, so the next page is drawn at the right width on its first paint, with no jump (see Remember the collapse).

  • On a phone the sidebar gives way to a menu button. It opens the same sidebar as a drawer from the left; following a link closes it, and Escape, the close button and the backdrop close it with focus back on the menu button:

    The menu open on a phone, light and dark

  • Landmarks: a "Skip to content" link first, the sidebar's nav aria-label="Primary", the top row as the header, and the page as main#main (tabindex="-1", so the skip link moves focus into it).

The sidebar is in the page once: the same element is the rail and, below lg, the drawer, so there is one nav, one set of ids and one sign-out form. Inside it the nav items, the sub-nav, the team switcher and the user menu fold with it on their own; the layout writes no collapsedWhen. The menu button is the layout's, in the top row, and names the sidebar with aria-controls.

Built from

  • Sidebar: the rail, the phone's drawer, the collapse toggle and the cookie
  • Nav item: the pages, each with an icon
  • Sub-nav: Projects and the projects in it
  • Badge: through the nav item, the Inbox count
  • Team switcher: the workspace, at the top
  • User menu: the account, at the bottom
  • Dropdown: the items in the user menu
  • Button: the menu button, search, New workspace
  • Kbd: the shortcut on the search button
  • Theme switcher: light, dark or the system's
  • Icon: each page's icon
  • Page header, Stat tile and Panel: the sample page
  • Toast: the region on every page, and the status flashed to the page load

The markup

resources/views/components/layouts/app.blade.php

{{--
    The app's frame: a sidebar on a desktop, a drawer on a phone, a slim
    row over the page with search, notifications and the theme. Every page
    of the app is wrapped in this layout (x-layouts.app, with a title), and
    a view composer (see the docs page) hands it $user, $teams, $projects,
    $inboxCount, $unreadCount, $latestNotifications and $recentIssues.
--}}
@props(['title'])

<x-layouts.base :title="$title">
    <div class="lg:flex">
        <a href="#main" class="sr-only rounded-control bg-surface px-3 py-2 text-body font-medium text-fg shadow-float focus:not-sr-only focus:fixed focus:start-4 focus:top-3 focus:z-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-focus-ring">Skip to content</a>

        {{-- The rail, the phone's drawer and the collapse toggle; the cookie keeps the width. --}}
        <x-ds::sidebar id="sidebar" :collapsible="true" cookie="sidebar">
            <x-slot:header>
                <x-ds::team-switcher
                    id="team"
                    :teams="$teams"
                    :current="$user->current_team_id"
                    name="team_id"
                    :action="route('current-team.update')"
                    method="PUT"
                    label="Switch workspace"
                >
                    <x-slot:footer>
                        <x-ds::button variant="ghost" size="sm" icon="plus" :href="route('teams.create')">New workspace</x-ds::button>
                    </x-slot:footer>
                </x-ds::team-switcher>
            </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::nav-item :href="route('inbox')" label="Inbox" :badge="$inboxCount ?: null" :active="request()->routeIs('inbox')">
                <x-slot:icon><x-ds::icon name="inbox" /></x-slot:icon>
            </x-ds::nav-item>
            <x-ds::nav-item :href="route('issues.mine')" label="My issues" :active="request()->routeIs('issues.mine')">
                <x-slot:icon><x-ds::icon name="check-circle" /></x-slot:icon>
            </x-ds::nav-item>

            <x-ds::sub-nav id="projects" 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['key'])"
                        :label="$project['name']"
                        :active="request()->is('projects/'.$project['key'], 'projects/'.$project['key'].'/*')"
                    />
                @endforeach
            </x-ds::sub-nav>

            <x-ds::nav-item :href="route('cycles.index')" label="Cycles" :active="request()->routeIs('cycles.*')">
                <x-slot:icon><x-ds::icon name="arrow-path" /></x-slot:icon>
            </x-ds::nav-item>
            <x-ds::nav-item :href="route('reports.index')" label="Reports" :active="request()->routeIs('reports.*')">
                <x-slot:icon><x-ds::icon name="chart-bar" /></x-slot:icon>
            </x-ds::nav-item>

            <x-ds::nav-item :href="route('settings.workspace')" label="Settings" :active="request()->routeIs('settings.*')" class="mt-auto">
                <x-slot:icon><x-ds::icon name="cog-6-tooth" /></x-slot:icon>
            </x-ds::nav-item>

            <x-slot:footer>
                <x-ds::user-menu id="account" :name="$user->name" :email="$user->email" align="start" :signOutAction="route('logout')">
                    <div role="group" aria-label="Account">
                        <x-ds::dropdown-item :href="route('settings.profile')" icon="user-circle">Profile</x-ds::dropdown-item>
                        <x-ds::dropdown-item :href="route('settings.notifications')" icon="bell">Notification settings</x-ds::dropdown-item>
                    </div>
                    <div role="group" aria-label="Help">
                        <x-ds::dropdown-item :href="route('help')" icon="lifebuoy">Help and shortcuts</x-ds::dropdown-item>
                    </div>
                </x-ds::user-menu>
            </x-slot:footer>
        </x-ds::sidebar>

        {{-- A sticky page header stops under the top row: --ds-sticky-offset is its height. --}}
        <div class="flex min-w-0 flex-1 flex-col [--ds-sticky-offset:3.5rem]">
            <header class="sticky top-0 z-30 flex h-14 items-center gap-2 border-b border-divider bg-surface-sunken px-4 lg:px-6">
                {{-- Opens the sidebar as a drawer: aria-controls names it. --}}
                <x-ds::button
                    variant="ghost"
                    icon="bars-3"
                    :iconOnly="true"
                    aria-label="Menu"
                    aria-haspopup="dialog"
                    aria-controls="sidebar"
                    aria-expanded="false"
                    class="-ms-2 lg:hidden [@media(scripting:none)]:hidden"
                />

                <div class="min-w-0 flex-1 max-md:hidden">{{ $breadcrumb ?? '' }}</div>
                <div class="flex-1 md:hidden"></div>

                {{-- A link to the search page, which the palette takes over when the script is there. --}}
                <x-ds::button
                    variant="secondary"
                    icon="magnifying-glass"
                    :href="route('search')"
                    aria-keyshortcuts="Control+K Meta+K"
                    x-data
                    x-on:click.prevent="$dispatch('open-command-palette', { name: 'app' })"
                >
                    <span class="max-sm:sr-only">Search</span>
                    <x-ds::kbd shortcut="mod+k" class="ms-4 max-sm:hidden" />
                </x-ds::button>

                <x-notification-bell :notifications="$latestNotifications" :unread="$unreadCount" />
                <x-ds::theme-switcher variant="button" />
            </header>

            <main id="main" tabindex="-1" class="mx-auto w-full max-w-6xl flex-1 px-4 py-6 focus:outline-hidden lg:px-8 lg:py-8">
                {{ $slot }}
            </main>
        </div>

        <x-command-menu :recent="$recentIssues" />

        {{-- On every page, so a toast put in it is announced; the one flashed to this page load is put in a moment after Alpine starts. --}}
        <x-ds::toast-region label="Messages" :flash="session('status')" />
    </div>
</x-layouts.base>

resources/views/home.blade.php

{{-- The sample page: what the controller for the home route returns. --}}
<x-layouts.app title="Home">
    <div class="flex flex-col gap-6">
        <x-ds::page-header title="Home" description="What needs you in Tidewater Ferries this week.">
            <x-slot:actions>
                <x-ds::button icon="plus" :href="route('issues.create')">New issue</x-ds::button>
            </x-slot:actions>
        </x-ds::page-header>

        <div class="grid gap-4 sm:grid-cols-3">
            <x-ds::stat-tile label="Assigned to you" :value="$stats['assigned']" caption="open issues" :countUp="false" :href="route('issues.mine')" />
            <x-ds::stat-tile label="Due this week" :value="$stats['due']" caption="before Friday" :countUp="false" />
            <x-ds::stat-tile label="Closed this cycle" :value="$stats['closed']" :delta="$stats['closedChange']" caption="on the last cycle" :countUp="false" />
        </div>

        <x-ds::panel title="Assigned to you" action="View all" :actionHref="route('issues.mine')">
            @foreach ($assigned as $issue)
                <x-ds::panel-row :href="route('issues.show', $issue['key'])">
                    <span class="flex min-w-0 items-center gap-3">
                        <span class="w-16 shrink-0 text-meta tabular-nums text-fg-muted">{{ $issue['key'] }}</span>
                        <span class="min-w-0 flex-1 truncate text-fg">{{ $issue['title'] }}</span>
                        <x-ds::badge :tone="$issue['tone']">{{ $issue['status'] }}</x-ds::badge>
                    </span>
                </x-ds::panel-row>
            @endforeach
        </x-ds::panel>
    </div>
</x-layouts.app>

Wire it up

A page in the shell

Every page of the app is wrapped in the layout, with its title, and draws its own page header for its one h1; the shell draws the skip link, the nav, the header and main. A breadcrumb slot puts the page's place in the app in the top row:

<x-layouts.app title="Cycles">
    <x-slot:breadcrumb>
        <x-ds::breadcrumb>…</x-ds::breadcrumb>
    </x-slot:breadcrumb>

    <x-ds::page-header title="Cycles" />
    …
</x-layouts.app>

The routes

The views link by route name: home, inbox, issues.mine, issues.create, issues.show, projects.index, projects.show, cycles.index, reports.index, settings.workspace, settings.profile, settings.notifications, teams.create, search and help are pages (GET); current-team.update is a PUT and logout a POST. Rename them to yours. search is a plain search page: the search button links to it, so it still goes somewhere when the palette's script is not there.

What the layout is handed: a view composer

The layout needs the signed-in person and the things every page shows around itself. Rather than every controller passing them, a view composer hands them to the layout whenever it is drawn:

// app/Providers/AppServiceProvider.php, in boot()
use Illuminate\Support\Facades\View;

View::composer('components.layouts.app', \App\View\Composers\AppLayoutComposer::class);
namespace App\View\Composers;

use Illuminate\View\View;

class AppLayoutComposer
{
    public function compose(View $view): void
    {
        $user = request()->user();

        $view->with([
            'user' => $user,
            'teams' => $user->teams->map(fn ($team) => [
                'id' => $team->id,
                'name' => $team->name,
                'meta' => $team->plan_label,
            ])->all(),
            'projects' => $user->currentTeam->projects()->orderBy('name')->get(['key', 'name'])->toArray(),
            'inboxCount' => $user->inboxItems()->unread()->count(),

            // For the notification bell and the command palette.
            'unreadCount' => $user->unreadNotifications()->count(),
            'latestNotifications' => $user->notifications()->latest()->limit(8)->get(),
            'recentIssues' => $user->recentIssues()->limit(5)->get()->map(fn ($issue) => [
                'key' => $issue->key,
                'title' => $issue->title,
                'project' => $issue->project->name,
            ])->all(),
        ]);
    }
}

The top-nav shell has the same view name and takes the same data, so the composer serves either.

Switching workspace

The team switcher posts team_id to current-team.update, as a form, so it works with or without its script:

Route::put('current-team', function (Request $request) {
    $team = $request->user()->teams()->findOrFail($request->integer('team_id'));
    $request->user()->forceFill(['current_team_id' => $team->id])->save();

    return redirect()->route('home');
})->name('current-team.update');

Remember the collapse

The toggle writes a sidebar cookie (collapsed or expanded) from the browser, and the sidebar reads it (cookie="sidebar"), so the rail is drawn at the reader's width on the first paint of every page rather than corrected after it. Laravel encrypts its cookies and throws away any it cannot decrypt, which would include this one, so let it through as it is:

// bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
    $middleware->encryptCookies(except: ['sidebar']);
})

Without that line the cookie reads as null and the rail is drawn wide on every page, then nothing corrects it. A cookie, not localStorage, because the server has to know before it draws.

A bottom bar on phones (optional)

For an app people use on a phone all day, put the three or four places they go most in a bottom nav and keep the drawer for the rest, opened by a More item. Inside the layout's root div, after the page:

<x-ds::bottom-nav label="Shortcuts">
    <x-ds::bottom-nav-item :href="route('home')" label="Home" :active="request()->routeIs('home')">
        <x-slot:icon><x-ds::icon name="home" size="6" /></x-slot:icon>
    </x-ds::bottom-nav-item>
    <x-ds::bottom-nav-item :href="route('inbox')" label="Inbox" :badge="$inboxCount" :active="request()->routeIs('inbox')">
        <x-slot:icon><x-ds::icon name="inbox" size="6" /></x-slot:icon>
    </x-ds::bottom-nav-item>
    <x-ds::bottom-nav-item :href="route('issues.mine')" label="My issues" :active="request()->routeIs('issues.mine')">
        <x-slot:icon><x-ds::icon name="check-circle" size="6" /></x-slot:icon>
    </x-ds::bottom-nav-item>
    {{-- No href: a button, opening the sidebar as the menu button does. --}}
    <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>

Then take the menu button out of the top row, and give main room for the bar, which is fixed and cannot push the page up: add pb-24 lg:pb-8 to its classes. The bar is below lg only, as the rail is above it. Give it a label of its own (label="Shortcuts"): the sidebar's nav is "Primary", and two navigation landmarks on a page need two names.

Phone, keyboard, no script

  • Phone: below lg (64rem) the rail gives way to the menu button and the drawer; the search button keeps its icon and loses its word and its shortcut. If the window grows past lg with the drawer open, the drawer closes, so it is not left open behind the rail.
  • Keyboard: the skip link is the first Tab stop. Ctrl+K (Cmd+K on a Mac) opens the palette from anywhere, and the search button says so in aria-keyshortcuts. The collapse toggle is a toggle button (aria-pressed). The drawer holds focus while open, Escape closes it, and focus goes back to the menu button.
  • No script: the menus, the drawer, the popover and the palette need Alpine, as their pages say. Without a script the shell still gets you everywhere: on a phone the rail stays, above the page, rather than vanishing behind a menu button that cannot open; the menu and collapse buttons are not drawn; the search button is a link to the search page, and the bell to the notifications page; and every page in the sidebar is a link. The workspace switcher falls back to a native select and a Switch button.

From the package repo: edit this page on GitHub.

Was this page helpful?