Number 6: Menu that opens into link cards
Specification
- Catalogue
- No. 6 of 110
- Section
- Navigation
- Interaction
- Press, Layout Animation
- Element
- Navigation, Card
- Preview
- Live component
- Imports
- react 19.3.1
- Added
Notes
The header opens downward into a few grouped cards of links instead of a long list.
Grouped links are easier to scan on a phone than one long column. It opens a little slower than it closes, focus goes to the first link, and Escape returns to the button.
Opens 300ms cubic-bezier(.22, 1, .36, 1); closes 200ms ease-in. Cards rise 8px and fade, 40ms apart. Focus to the first link on open; Escape closes and returns focus; leaving with Tab closes. Changed from the library: flat cards in site colours instead of tinted ones.
The names, figures and pictures in the demo are sample content; a real build uses the client's own.
Code
React and plain CSS"use client";
import { useEffect, useId, useRef, useState, type FocusEvent } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import type { Tour } from "@/components/pieces/stage/tour";
import "./card-nav.css";
const GROUPS = [
{ title: "Services", links: ["Bookkeeping", "VAT returns", "Payroll"] },
{ title: "About", links: ["The team", "How we work", "Fees"] },
{ title: "Contact", links: ["Book a call", "Email us", "Find the office"] },
];
function CardNavDemo() {
const [open, setOpen] = useState(false);
const btn = useRef<HTMLButtonElement>(null);
const panel = useRef<HTMLDivElement>(null);
const uid = useId();
useEffect(() => {
if (open) panel.current?.querySelector("a")?.focus();
}, [open]);
const close = (refocus: boolean) => {
setOpen(false);
if (refocus) btn.current?.focus();
};
const onBlur = (e: FocusEvent<HTMLElement>) => {
if (open && !e.currentTarget.contains(e.relatedTarget as Node | null)) setOpen(false);
};
return (
<div className="dm" style={{ alignContent: "start", paddingTop: 18 }}>
<nav
className="cn"
data-open={open || undefined}
aria-label="Sample site"
onBlur={onBlur}
onKeyDown={(e) => {
if (e.key === "Escape" && open) {
e.stopPropagation();
close(true);
}
}}
>
<div className="cn-bar">
<button ref={btn} type="button" className="cn-toggle" aria-expanded={open} aria-controls={`${uid}-menu`} onClick={() => setOpen((o) => !o)}>
<span className="cn-burger" aria-hidden="true">
<i />
<i />
</span>
{open ? "Close" : "Menu"}
</button>
<span className="dm-wordmark">Ledger & Lane</span>
<a href="#" className="cn-cta">
Book a call
</a>
</div>
<div className="cn-panel" id={`${uid}-menu`} ref={panel} inert={!open}>
<div className="cn-cards">
{GROUPS.map((g, i) => (
<section key={g.title} className="cn-card" style={{ animationDelay: `${i * 40}ms` }} aria-label={g.title}>
<h3>{g.title}</h3>
{g.links.map((l) => (
<a key={l} href="#" onClick={() => close(false)}>
{l}
<svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" aria-hidden="true">
<path d="M7 17 17 7M9 7h8v8" />
</svg>
</a>
))}
</section>
))}
</div>
</div>
</nav>
<p className="dm-hint">Press Menu: the links open as grouped cards</p>
</div>
);
}
// What a hovered card shows: the piece used the way a visitor would.
const TOUR: Tour = [
{ click: ".cn-toggle", wait: 2200 },
{ click: ".cn-toggle", wait: 700 },
];
export default function CardNavEntry(props: LivePreviewProps) {
return (
<PieceStage {...props} label="Menu that opens into link cards" sample tour={TOUR}>
<CardNavDemo />
</PieceStage>
);
}