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.
Options
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>
);
}/* A phone-width top bar over a dithered accent band. The menu button's three
lines fold into a close mark, and the menu drops down over the page with
its links coming in one after another. The menu is open while the button
has aria-expanded="true". */
.dh-mnav {
--accent: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
--ease: cubic-bezier(0.23, 1, 0.32, 1);
position: relative;
isolation: isolate;
overflow: hidden;
width: min(300px, 100%);
height: 440px;
border-radius: 22px;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent),
0 14px 28px -14px color-mix(in srgb, var(--dh-fg, #171717) 28%, transparent);
color: var(--dh-fg, #171717);
font-family: inherit;
font-size: 13px;
line-height: 20px;
text-align: left;
}
.dh-mnav a {
color: inherit;
text-decoration: none;
outline: none;
}
/* The band behind the bar: solid accent that breaks into dots as it fades. */
.dh-mnav::before {
content: "";
position: absolute;
inset: 0 0 auto;
z-index: -1;
height: 190px;
background: linear-gradient(color-mix(in srgb, var(--accent) 82%, #000), var(--accent));
-webkit-mask-image: linear-gradient(#000 40%, transparent), radial-gradient(circle, #000 1.3px, transparent 1.6px);
mask-image: linear-gradient(#000 40%, transparent), radial-gradient(circle, #000 1.3px, transparent 1.6px);
-webkit-mask-size: auto, 5px 5px;
mask-size: auto, 5px 5px;
}
.dh-mnav-bar {
position: relative;
z-index: 2;
display: flex;
align-items: center;
justify-content: space-between;
height: 52px;
padding: 0 8px 0 18px;
color: var(--dh-on-accent, #fff);
transition: color 200ms var(--ease);
}
.dh-mnav:has(.dh-mnav-toggle[aria-expanded="true"]) .dh-mnav-bar {
color: var(--dh-fg, #171717);
}
.dh-mnav-brand {
border-radius: 4px;
font-size: 15px;
font-weight: 500;
letter-spacing: -0.02em;
}
/* The button: three lines. The outer two meet in the middle and cross, and the middle one fades. */
.dh-mnav-toggle {
position: relative;
width: 40px;
height: 40px;
padding: 0;
border: 0;
border-radius: 10px;
background: none;
color: inherit;
cursor: pointer;
outline: none;
-webkit-tap-highlight-color: transparent;
transition: transform 150ms var(--ease);
}
.dh-mnav-toggle:active {
transform: scale(0.94);
}
.dh-mnav-toggle i {
position: absolute;
top: 50%;
left: 50%;
width: 16px;
height: 1.5px;
margin: -0.75px 0 0 -8px;
border-radius: 2px;
background: currentColor;
transition:
transform 400ms var(--ease),
opacity 200ms var(--ease);
}
.dh-mnav-toggle i:nth-child(1) {
transform: translateY(-5px);
}
.dh-mnav-toggle i:nth-child(3) {
transform: translateY(5px);
}
.dh-mnav-toggle[aria-expanded="true"] i:nth-child(1) {
transform: rotate(45deg);
}
.dh-mnav-toggle[aria-expanded="true"] i:nth-child(2) {
opacity: 0;
transform: scaleX(0.3);
}
.dh-mnav-toggle[aria-expanded="true"] i:nth-child(3) {
transform: rotate(-45deg);
}
.dh-mnav-panel {
position: absolute;
inset: 0;
z-index: 1;
display: flex;
flex-direction: column;
padding: 56px 18px 18px;
background: var(--dh-raised, #fff);
opacity: 0;
visibility: hidden;
transform: translateY(-8px);
transition:
opacity 200ms var(--ease),
transform 200ms var(--ease),
visibility 0s 200ms;
}
.dh-mnav:has(.dh-mnav-toggle[aria-expanded="true"]) .dh-mnav-panel {
opacity: 1;
visibility: visible;
transform: none;
transition-duration: 300ms, 300ms, 0s;
transition-delay: 0s;
}
.dh-mnav-panel ul {
flex: 1;
margin: 0;
padding: 0;
list-style: none;
}
.dh-mnav-panel li + li {
border-top: 1px solid var(--line);
}
.dh-mnav-panel li a {
display: flex;
align-items: center;
justify-content: space-between;
height: 44px;
border-radius: 4px;
font-size: 15px;
font-weight: 500;
letter-spacing: -0.015em;
}
/* A small arrow at the end of each link. */
.dh-mnav-panel li a::after {
content: "";
width: 6px;
height: 6px;
margin-right: 4px;
border: solid var(--dh-faint, #8a8a8a);
border-width: 1.5px 1.5px 0 0;
rotate: 45deg;
}
/* Each link and the buttons rise in a little after the one before. */
.dh-mnav-panel li,
.dh-mnav-actions {
opacity: 0;
transform: translateY(6px);
filter: blur(4px);
transition:
opacity 150ms var(--ease),
transform 150ms var(--ease),
filter 150ms var(--ease);
}
.dh-mnav:has(.dh-mnav-toggle[aria-expanded="true"]) :is(.dh-mnav-panel li, .dh-mnav-actions) {
opacity: 1;
transform: none;
filter: none;
transition-duration: 450ms;
transition-delay: calc(60ms + var(--n, 0) * 40ms);
}
.dh-mnav-panel li:nth-child(2) {
--n: 1;
}
.dh-mnav-panel li:nth-child(3) {
--n: 2;
}
.dh-mnav-panel li:nth-child(4) {
--n: 3;
}
.dh-mnav-panel li:nth-child(5) {
--n: 4;
}
.dh-mnav-actions {
--n: 5;
display: grid;
gap: 8px;
}
/* The main button is the glossy accent one; the quiet one is raised. */
.dh-mnav-button {
display: grid;
place-items: center;
height: 36px;
padding: 0 14px;
border-radius: 9px;
background: linear-gradient(color-mix(in srgb, var(--accent) 84%, #fff), var(--accent));
box-shadow:
inset 0 1px 0 rgb(255 255 255 / 0.3),
0 0 0 1px color-mix(in srgb, var(--accent) 78%, #000),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 22%, transparent);
color: var(--dh-on-accent, #fff) !important;
font-weight: 500;
letter-spacing: -0.01em;
transition: transform 150ms var(--ease);
}
.dh-mnav-button[data-kind="quiet"] {
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
color: var(--dh-fg, #171717) !important;
}
.dh-mnav-button:active {
transform: scale(0.97);
}
.dh-mnav :is(a, button):focus-visible {
box-shadow: 0 0 0 2px var(--dh-faint, #8a8a8a);
}
/* A stand-in for the page under the bar. */
.dh-mnav-page {
display: grid;
align-content: end;
justify-items: start;
gap: 6px;
height: calc(100% - 52px);
padding: 0 18px 22px;
}
.dh-mnav-page p {
margin: 0;
font-size: 22px;
font-weight: 500;
line-height: 26px;
letter-spacing: -0.03em;
}
.dh-mnav-page span {
color: var(--dh-muted, #5c5c5c);
}
.dh-mnav-page .dh-mnav-button {
height: 32px;
margin-top: 10px;
padding: 0 12px;
border-radius: 8px;
}
@media (prefers-reduced-motion: reduce) {
.dh-mnav-toggle,
.dh-mnav-toggle i,
.dh-mnav-button {
transition: none;
}
.dh-mnav-panel,
.dh-mnav-panel li,
.dh-mnav-actions {
transform: none !important;
filter: none !important;
transition-delay: 0s !important;
}
}<div class="dh-mnav">
<header class="dh-mnav-bar">
<a class="dh-mnav-brand" href="#">Logoipsum</a>
<button type="button" class="dh-mnav-toggle" aria-label="Menu" aria-expanded="false" aria-controls="dh-mnav-panel">
<i></i>
<i></i>
<i></i>
</button>
</header>
<nav class="dh-mnav-panel" id="dh-mnav-panel" aria-label="Main">
<ul>
<li><a href="#">Product</a></li>
<li><a href="#">Pricing</a></li>
<li><a href="#">Customers</a></li>
<li><a href="#">Journal</a></li>
</ul>
<div class="dh-mnav-actions">
<a class="dh-mnav-button" href="#">Get started</a>
<a class="dh-mnav-button" data-kind="quiet" href="#">Sign in</a>
</div>
</nav>
<div class="dh-mnav-page">
<p>Design that is ready to ship</p>
<span>Press the menu button to open the navigation.</span>
<a class="dh-mnav-button" href="#">Start free</a>
</div>
</div>/* A phone-width top bar over a dithered accent band. The menu button's three
lines fold into a close mark, and the menu drops down over the page with
its links coming in one after another. The menu is open while the button
has aria-expanded="true". */
.dh-mnav {
--accent: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
--ease: cubic-bezier(0.23, 1, 0.32, 1);
position: relative;
isolation: isolate;
overflow: hidden;
width: min(300px, 100%);
height: 440px;
border-radius: 22px;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent),
0 14px 28px -14px color-mix(in srgb, var(--dh-fg, #171717) 28%, transparent);
color: var(--dh-fg, #171717);
font-family: inherit;
font-size: 13px;
line-height: 20px;
text-align: left;
}
.dh-mnav a {
color: inherit;
text-decoration: none;
outline: none;
}
/* The band behind the bar: solid accent that breaks into dots as it fades. */
.dh-mnav::before {
content: "";
position: absolute;
inset: 0 0 auto;
z-index: -1;
height: 190px;
background: linear-gradient(color-mix(in srgb, var(--accent) 82%, #000), var(--accent));
-webkit-mask-image: linear-gradient(#000 40%, transparent), radial-gradient(circle, #000 1.3px, transparent 1.6px);
mask-image: linear-gradient(#000 40%, transparent), radial-gradient(circle, #000 1.3px, transparent 1.6px);
-webkit-mask-size: auto, 5px 5px;
mask-size: auto, 5px 5px;
}
.dh-mnav-bar {
position: relative;
z-index: 2;
display: flex;
align-items: center;
justify-content: space-between;
height: 52px;
padding: 0 8px 0 18px;
color: var(--dh-on-accent, #fff);
transition: color 200ms var(--ease);
}
.dh-mnav:has(.dh-mnav-toggle[aria-expanded="true"]) .dh-mnav-bar {
color: var(--dh-fg, #171717);
}
.dh-mnav-brand {
border-radius: 4px;
font-size: 15px;
font-weight: 500;
letter-spacing: -0.02em;
}
/* The button: three lines. The outer two meet in the middle and cross, and the middle one fades. */
.dh-mnav-toggle {
position: relative;
width: 40px;
height: 40px;
padding: 0;
border: 0;
border-radius: 10px;
background: none;
color: inherit;
cursor: pointer;
outline: none;
-webkit-tap-highlight-color: transparent;
transition: transform 150ms var(--ease);
}
.dh-mnav-toggle:active {
transform: scale(0.94);
}
.dh-mnav-toggle i {
position: absolute;
top: 50%;
left: 50%;
width: 16px;
height: 1.5px;
margin: -0.75px 0 0 -8px;
border-radius: 2px;
background: currentColor;
transition:
transform 400ms var(--ease),
opacity 200ms var(--ease);
}
.dh-mnav-toggle i:nth-child(1) {
transform: translateY(-5px);
}
.dh-mnav-toggle i:nth-child(3) {
transform: translateY(5px);
}
.dh-mnav-toggle[aria-expanded="true"] i:nth-child(1) {
transform: rotate(45deg);
}
.dh-mnav-toggle[aria-expanded="true"] i:nth-child(2) {
opacity: 0;
transform: scaleX(0.3);
}
.dh-mnav-toggle[aria-expanded="true"] i:nth-child(3) {
transform: rotate(-45deg);
}
.dh-mnav-panel {
position: absolute;
inset: 0;
z-index: 1;
display: flex;
flex-direction: column;
padding: 56px 18px 18px;
background: var(--dh-raised, #fff);
opacity: 0;
visibility: hidden;
transform: translateY(-8px);
transition:
opacity 200ms var(--ease),
transform 200ms var(--ease),
visibility 0s 200ms;
}
.dh-mnav:has(.dh-mnav-toggle[aria-expanded="true"]) .dh-mnav-panel {
opacity: 1;
visibility: visible;
transform: none;
transition-duration: 300ms, 300ms, 0s;
transition-delay: 0s;
}
.dh-mnav-panel ul {
flex: 1;
margin: 0;
padding: 0;
list-style: none;
}
.dh-mnav-panel li + li {
border-top: 1px solid var(--line);
}
.dh-mnav-panel li a {
display: flex;
align-items: center;
justify-content: space-between;
height: 44px;
border-radius: 4px;
font-size: 15px;
font-weight: 500;
letter-spacing: -0.015em;
}
/* A small arrow at the end of each link. */
.dh-mnav-panel li a::after {
content: "";
width: 6px;
height: 6px;
margin-right: 4px;
border: solid var(--dh-faint, #8a8a8a);
border-width: 1.5px 1.5px 0 0;
rotate: 45deg;
}
/* Each link and the buttons rise in a little after the one before. */
.dh-mnav-panel li,
.dh-mnav-actions {
opacity: 0;
transform: translateY(6px);
filter: blur(4px);
transition:
opacity 150ms var(--ease),
transform 150ms var(--ease),
filter 150ms var(--ease);
}
.dh-mnav:has(.dh-mnav-toggle[aria-expanded="true"]) :is(.dh-mnav-panel li, .dh-mnav-actions) {
opacity: 1;
transform: none;
filter: none;
transition-duration: 450ms;
transition-delay: calc(60ms + var(--n, 0) * 40ms);
}
.dh-mnav-panel li:nth-child(2) {
--n: 1;
}
.dh-mnav-panel li:nth-child(3) {
--n: 2;
}
.dh-mnav-panel li:nth-child(4) {
--n: 3;
}
.dh-mnav-panel li:nth-child(5) {
--n: 4;
}
.dh-mnav-actions {
--n: 5;
display: grid;
gap: 8px;
}
/* The main button is the glossy accent one; the quiet one is raised. */
.dh-mnav-button {
display: grid;
place-items: center;
height: 36px;
padding: 0 14px;
border-radius: 9px;
background: linear-gradient(color-mix(in srgb, var(--accent) 84%, #fff), var(--accent));
box-shadow:
inset 0 1px 0 rgb(255 255 255 / 0.3),
0 0 0 1px color-mix(in srgb, var(--accent) 78%, #000),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 22%, transparent);
color: var(--dh-on-accent, #fff) !important;
font-weight: 500;
letter-spacing: -0.01em;
transition: transform 150ms var(--ease);
}
.dh-mnav-button[data-kind="quiet"] {
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
color: var(--dh-fg, #171717) !important;
}
.dh-mnav-button:active {
transform: scale(0.97);
}
.dh-mnav :is(a, button):focus-visible {
box-shadow: 0 0 0 2px var(--dh-faint, #8a8a8a);
}
/* A stand-in for the page under the bar. */
.dh-mnav-page {
display: grid;
align-content: end;
justify-items: start;
gap: 6px;
height: calc(100% - 52px);
padding: 0 18px 22px;
}
.dh-mnav-page p {
margin: 0;
font-size: 22px;
font-weight: 500;
line-height: 26px;
letter-spacing: -0.03em;
}
.dh-mnav-page span {
color: var(--dh-muted, #5c5c5c);
}
.dh-mnav-page .dh-mnav-button {
height: 32px;
margin-top: 10px;
padding: 0 12px;
border-radius: 8px;
}
@media (prefers-reduced-motion: reduce) {
.dh-mnav-toggle,
.dh-mnav-toggle i,
.dh-mnav-button {
transition: none;
}
.dh-mnav-panel,
.dh-mnav-panel li,
.dh-mnav-actions {
transform: none !important;
filter: none !important;
transition-delay: 0s !important;
}
}/* The button opens and closes the menu. Escape closes it and puts focus
back on the button, and so does choosing a link. */
document.querySelectorAll(".dh-mnav").forEach((nav) => {
const toggle = nav.querySelector(".dh-mnav-toggle");
const set = (open) => {
toggle.setAttribute("aria-expanded", String(open));
toggle.setAttribute("aria-label", open ? "Close menu" : "Menu");
};
toggle.addEventListener("click", () => set(toggle.getAttribute("aria-expanded") !== "true"));
nav.addEventListener("keydown", (event) => {
if (event.key !== "Escape" || toggle.getAttribute("aria-expanded") !== "true") return;
set(false);
toggle.focus();
});
nav.querySelector(".dh-mnav-panel").addEventListener("click", (event) => {
if (event.target.closest("a")) set(false);
});
});Mobile navigation is not in the Figma file yet.
Use the React or HTML code for now.