Top nav

A horizontal app bar: your logo or team switcher, the primary links, and the actions on the right. Below a width of its own it folds the links into a drawer.

Top nav

Needs Alpine to fold into its drawer (collapseAt, on by default) and to close a link group on Escape or a press outside. See getting started. Without Alpine the links wrap onto a second row and a link group opens and closes as a native disclosure.

Use it

<x-ds::top-nav :sticky="true" skipTo="main">
    <x-slot:brand>
        <a href="/" class="font-semibold text-fg">Acme</a>
    </x-slot:brand>

    <x-ds::top-nav-item href="/dashboard" label="Dashboard" :active="request()->is('dashboard')" />
    <x-ds::top-nav-item href="/inbox" label="Inbox" :badge="$unread ?: null" />
    <x-ds::top-nav-item label="Reports">
        <x-ds::top-nav-item href="/reports/sales" label="Sales" :active="request()->is('reports/sales')" />
        <x-ds::top-nav-item href="/reports/costs" label="Costs" />
    </x-ds::top-nav-item>

    <x-slot:actions>
        <x-ds::theme-switcher variant="button" />
        <x-ds::user-menu :name="auth()->user()->name" :email="auth()->user()->email" signOutAction="/logout" />
    </x-slot:actions>
</x-ds::top-nav>

<main id="main">…</main>

skipTo names your main content's id and renders a "Skip to content" link as the first thing in the bar, visible only when it has keyboard focus. Give <main> that id.

Props: top-nav

PropTypeDefaultWhat it does
labelstringMainThe nav landmark's name. Name it if the page has a sidebar too.
stickyboolfalseStays at the top of the window.
borderedbooltrueA rule under the bar.
containerfull wide narrowfullHow far the contents spread, centred: no limit, 80rem, 64rem. The ground always runs edge to edge.
collapseAtsm md lg xl or nullmdBelow this width of the bar (40, 48, 64, 80rem; a container query, not the window) the links move into a drawer opened by a menu button. null never folds.
menuLabelstringMenuThe menu button's name and the drawer's title.
skipTostring—The id of the main content: renders a skip link.
skipLabelstringSkip to content

Slots: brand (left), the default slot (the top-nav-items), actions (right: search, notifications, theme switcher, user menu).

Props: top-nav-item

PropTypeDefaultWhat it does
labelstringrequired
hrefstring—Where it goes. Leave it out on a group.
activeboolfalseThe page the reader is on: aria-current="page".
iconstring—An icon name, before the label.
badgestring or int—A badge after the label: a count, or "New".

Put top-nav-items in an item's slot to make it a group: a disclosure button that opens a list of those links under it. The group is drawn current when one of its links is active.

On a phone

Below collapseAt the links leave the bar and a menu button takes their place, opening a drawer from the left with the same links in a column (a group opens in place there). Pressing a link closes the drawer, so wire:navigate and other in-page navigation leave it shut. The links are drawn into the drawer only while it is open, so ids and wire:keys in them are never on the page twice.

The width is the bar's own: a top nav inside a narrow column folds even on a wide window.

With Livewire

Nothing to wire: the bar holds no server state. Mark the current page with active (request()->routeIs('reports.*')); with wire:navigate, render the bar in the layout so it is kept between pages.

Accessibility

  • A header holding a nav named by label, with the links in a list.
  • A group is a disclosure, not a menu: a summary (a button with its expanded state) and a list of links reached by Tab. There is no role="menubar" or menu; arrow keys are not promised and not needed. Escape inside the group closes it and returns focus to its button; Tab past its last link or a press outside closes it.
  • The menu button is named by menuLabel and says aria-expanded; the drawer is a modal dialog that traps focus and gives it back on close.
  • The skip link is the first stop on the page and draws the focus ring.

Don't

  • Don't put more than about six links in the bar. Past that the bar folds on every laptop; use a sidebar instead.
  • Don't nest groups. One level of links under a group; a deeper tree belongs in a sidebar.
  • Don't add backdrop-blur or a transform to the bar. Either makes it the containing block of the drawer, which would then open inside the bar.

More in specs/top-nav.md.


From the package repo: edit this page on GitHub.

Was this page helpful?