Mobile navigation

A phone-width top bar over a dithered accent band. Its menu button folds three lines into a close mark, and the menu drops over the page with its links coming in one after another.

Logoipsum

Design that is ready to ship

Press the menu button to open the navigation. Start free

Options

Screen
Colour

Code

import { useId, useRef, useState } from "react";
import "./mobile-nav.css";

/* A phone-width top bar with a menu button that folds into a close mark and
   opens the navigation over the page.

   <MobileNav brand="Logoipsum" links={[{ label: "Pricing", href: "/pricing" }]} action={{ label: "Get started", href: "/start" }}>
     the page
   </MobileNav> */
export function MobileNav({ brand, home = "#", links, action, second, children }) {
  const id = useId();
  const toggle = useRef(null);
  const [open, setOpen] = useState(false);

  return (
    <div
      className="dh-mnav"
      onKeyDown={(event) => {
        if (event.key !== "Escape" || !open) return;
        setOpen(false);
        toggle.current.focus();
      }}
    >
      <header className="dh-mnav-bar">
        <a className="dh-mnav-brand" href={home}>
          {brand}
        </a>
        <button
          type="button"
          className="dh-mnav-toggle"
          ref={toggle}
          aria-label={open ? "Close menu" : "Menu"}
          aria-expanded={open}
          aria-controls={id}
          onClick={() => setOpen(!open)}
        >
          <i />
          <i />
          <i />
        </button>
      </header>
      <nav className="dh-mnav-panel" id={id} aria-label="Main" onClick={(event) => event.target.closest("a") && setOpen(false)}>
        <ul>
          {links.map((link) => (
            <li key={link.label}>
              <a href={link.href}>{link.label}</a>
            </li>
          ))}
        </ul>
        <div className="dh-mnav-actions">
          {action && (
            <a className="dh-mnav-button" href={action.href}>
              {action.label}
            </a>
          )}
          {second && (
            <a className="dh-mnav-button" data-kind="quiet" href={second.href}>
              {second.label}
            </a>
          )}
        </div>
      </nav>
      <div className="dh-mnav-page">{children}</div>
    </div>
  );
}

export function Example() {
  return (
    <MobileNav
      brand="Logoipsum"
      links={[
        { label: "Product", href: "#" },
        { label: "Pricing", href: "#" },
        { label: "Customers", href: "#" },
        { label: "Journal", href: "#" },
      ]}
      action={{ label: "Get started", href: "#" }}
      second={{ label: "Sign in", href: "#" }}
    >
      <p>Design that is ready to ship</p>
      <span>Press the menu button to open the navigation.</span>
      <a className="dh-mnav-button" href="#">
        Start free
      </a>
    </MobileNav>
  );
}
Logoipsum

Design that is ready to ship

Press the menu button to open the navigation. Start free