Component · top layer

Mobile nav

The phone menu: the modal dialog with a list of links. Following a link closes it, also with a client-side router, and in-page links land where they point.

The toggle stays visible here so you can try it at any width. On a real site you hide it where the desktop links show, and closeAt closes the menu when the window grows to that width.

Top sheet and submenus

With placement="top" the menu is as wide as the screen and unrolls from under your bar, which stays in view: a press on the bar, the toggle included, closes it. Put the toggle in the bar's <header>: the script measures where that header ends when the menu opens. It takes the nearest <header> around the toggle, so keep the toggle out of any other header inside the bar. Opened from elsewhere, or without the script, it starts at --sf-mobile-nav-top (by default --sf-fixed-top). While it is open, the keyboard stays in the menu, so it has its own close button.

A group in links ({ label, links }) is a submenu: a native <details> that opens by press, Enter or Space, also without JavaScript. It is open from the start when one of its links is the current page. One level only.

Try the top sheet on a page with its own bar

Astro

---
import MobileNav from '@subfloor/astro/MobileNav.astro'
const path = Astro.url.pathname
const links = [
  { href: '/', label: 'Home', current: path === '/' },
  { href: '/work/', label: 'Work', current: path === '/work/' },
  // A group (submenu): open from the start when one of its links is current.
  {
    label: 'About',
    links: [
      { href: '/team/', label: 'Team', current: path === '/team/' },
      { href: '#contact', label: 'Contact' },
    ],
  },
]
---
<button class="menu-toggle" command="show-modal" commandfor="site-nav"
  aria-haspopup="dialog">Menu</button>

<!-- placement: "end" (default) | "start" | "top" -->
<MobileNav id="site-nav" links={links} closeAt="(min-width: 768px)">
  <!-- optional: more content below the links -->
</MobileNav>

React and Next.js

renderLink renders each link with your router's Link. The demo below uses a stand-in router that changes the URL with history.pushState.

'use client'
import Link from 'next/link'
import { usePathname } from 'next/navigation'
import { MobileNav } from '@subfloor/react'

export function SiteNav() {
  const path = usePathname()
  return (
    <MobileNav
      id="site-nav"
      closeAt="(min-width: 768px)"
      links={[
        { href: '/', label: 'Home', current: path === '/' },
        { href: '/work', label: 'Work', current: path === '/work' },
      ]}
      renderLink={(props) => <Link {...props} />}
    />
  )
}

View: home. onOpenChange calls: none

React menu

Markup

<dialog id="site-nav" class="sf-dialog sf-mobile-nav" data-sf-dialog
  data-sf-mobile-nav data-placement="end" data-sf-close-at="(min-width: 768px)"
  aria-labelledby="site-nav-title" closedby="any">
  <header class="sf-dialog__header">
    <h2 id="site-nav-title" class="sf-dialog__title">Menu</h2>
    <form method="dialog" class="sf-dialog__close-form">
      <button class="sf-dialog__close" value="close" aria-label="Close menu">…</button>
    </form>
  </header>
  <div class="sf-dialog__body">
    <nav class="sf-mobile-nav__nav" aria-labelledby="site-nav-title">
      <ul class="sf-mobile-nav__list">
        <li><a class="sf-mobile-nav__link" href="/" aria-current="page">Home</a></li>
        <li>
          <details class="sf-mobile-nav__group">
            <summary class="sf-mobile-nav__summary">
              About <span class="sf-caret" aria-hidden="true"></span>
            </summary>
            <ul class="sf-mobile-nav__sublist">
              <li><a class="sf-mobile-nav__link" href="/team/">Team</a></li>
            </ul>
          </details>
        </li>
      </ul>
    </nav>
  </div>
</dialog>

Behaviour

Without JavaScript

The toggle opens it in browsers with Invoker Commands; Escape and the close button close it, and links to other pages work. Without the script, a link to this page leaves the menu open, the menu does not close when the window grows, and the closing transition is skipped.

Customisation

Set --sf-fixed-top and --sf-fixed-bottom to the heights of your fixed bars. They become the root's scroll-padding, so Tab and in-page links keep focused controls and targets clear of the bars. Your own scroll-padding overrides them, so set the tokens instead. This page has a fixed bar at the bottom and sets --sf-fixed-bottom: 3.5rem.

Safari ignores scroll-padding when Tab scrolls, so the script scrolls a focused control that still sits under a bar once more. Every mobile nav turns this on; on a page without one, call keepFocusClear() from @subfloor/core.

:root {
  /* the heights of your fixed header and bottom bar */
  --sf-fixed-top: 4rem;
  --sf-fixed-bottom: 3.5rem;
  --sf-mobile-nav-width: 100%;
  /* a top sheet opened from outside a <header> */
  --sf-mobile-nav-top: 4rem;
}

/* your own CSS, for a top sheet: the toggle shows a cross while the menu is
   open (a press on the bar closes it) */
.menu-toggle .cross,
:root:has(#site-nav[data-state='open']) .menu-toggle .bars { display: none; }
:root:has(#site-nav[data-state='open']) .menu-toggle .cross { display: block; }

/* your own CSS: hide the toggle where the desktop links show */
@media (min-width: 768px) {
  .menu-toggle { display: none; }
}
TokenDefault
--sf-color-currentrgb(37 99 235 / 10%)
--sf-touch-target44px
--sf-mobile-nav-width20rem
--sf-mobile-nav-topvar(--sf-fixed-top)
--sf-mobile-nav-indentvar(--sf-space-4)
--sf-segmented-current-bgvar(--sf-color-surface)
--sf-segmented-current-bordervar(--sf-field-border-color)
--sf-fixed-top0px
--sf-fixed-bottom0px

Background

Content 1. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Content 2. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Content 3. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Content 4. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Content 5. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Content 6. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Content 7. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Content 8. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Content 9. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Content 10. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Content 11. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Content 12. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.