Mega menu
A top bar whose dropdowns share one panel that morphs in size and crossfades its content as you move between menus.
"use client";
import {
useEffect,
useId,
useRef,
useState,
type PointerEvent as ReactPointerEvent,
} from "react";
import "./mega-menu.css";
export type MegaItem = {
title: string;
text: string;
href: string;
glyph?: string;
};
export type MegaMenuGroup = {
label: string;
items?: MegaItem[];
/** A highlighted card on the right side of the panel. */
feature?: { title: string; text: string; href: string; cta: string };
/** Plain link (no panel). */
href?: string;
};
type MegaMenuProps = {
brand?: string;
menus: MegaMenuGroup[];
theme?: "light" | "dark";
};
/**
* MegaMenu
* - All dropdowns share ONE panel. Moving from "Product" to "Solutions" morphs the panel's
* size and position and crossfades the content, instead of closing and reopening.
* - Opens on hover (with a short intent delay) for mouse users, on click/tap for everyone.
* - Escape closes and puts focus back on the menu button.
*/
export function MegaMenu({
brand = "Fieldstone",
menus,
theme = "light",
}: MegaMenuProps) {
const id = useId();
const [open, setOpen] = useState<number | null>(null);
const [panelHeight, setPanelHeight] = useState(0);
const root = useRef<HTMLElement>(null);
const inner = useRef<HTMLDivElement>(null);
const timer = useRef<number>(0);
const buttons = useRef<(HTMLButtonElement | null)[]>([]);
// Measure the open menu's content so the panel can animate its height.
useEffect(() => {
if (open === null || !inner.current) return;
const el = inner.current;
const ro = new ResizeObserver(() => setPanelHeight(el.offsetHeight));
ro.observe(el);
return () => ro.disconnect();
}, [open]);
// Escape + click outside close the panel.
useEffect(() => {
if (open === null) return;
const onKey = (e: KeyboardEvent) => {
if (e.key !== "Escape") return;
buttons.current[open]?.focus();
setOpen(null);
};
const onDown = (e: PointerEvent) => {
if (!root.current?.contains(e.target as Node)) setOpen(null);
};
window.addEventListener("keydown", onKey);
window.addEventListener("pointerdown", onDown);
return () => {
window.removeEventListener("keydown", onKey);
window.removeEventListener("pointerdown", onDown);
};
}, [open]);
const hoverOpen = (i: number | null, e: ReactPointerEvent) => {
if (e.pointerType !== "mouse") return;
window.clearTimeout(timer.current);
timer.current = window.setTimeout(() => setOpen(i), i === null ? 220 : 90);
};
const current = open !== null ? menus[open] : null;
return (
<nav
ref={root}
className="gu-mega"
data-theme={theme}
aria-label="Main"
onPointerLeave={(e) => hoverOpen(null, e)}
>
<div className="gu-mega__bar">
<a className="gu-mega__brand" href="#">
{brand}
</a>
<ul className="gu-mega__top">
{menus.map((menu, i) =>
menu.items ? (
<li key={menu.label}>
<button
ref={(el) => {
buttons.current[i] = el;
}}
type="button"
aria-expanded={open === i}
aria-controls={`${id}-panel`}
onClick={() => setOpen(open === i ? null : i)}
onPointerEnter={(e) => hoverOpen(i, e)}
>
{menu.label}
<svg viewBox="0 0 12 12" aria-hidden="true">
<path
d="m3 4.5 3 3 3-3"
fill="none"
stroke="currentColor"
strokeWidth="1.6"
strokeLinecap="round"
/>
</svg>
</button>
</li>
) : (
<li key={menu.label}>
<a href={menu.href} onPointerEnter={(e) => hoverOpen(null, e)}>
{menu.label}
</a>
</li>
),
)}
</ul>
</div>
<div
id={`${id}-panel`}
className="gu-mega__panel"
data-open={current ? "" : undefined}
style={{ height: current ? panelHeight : 0 }}
onPointerEnter={() => window.clearTimeout(timer.current)}
inert={!current}
>
{current && (
<div ref={inner} key={open} className="gu-mega__inner">
<ul className="gu-mega__items">
{current.items?.map((item) => (
<li key={item.title}>
<a href={item.href} onClick={() => setOpen(null)}>
<span className="gu-mega__glyph" aria-hidden="true">
{item.glyph ?? item.title[0]}
</span>
<span>
<strong>{item.title}</strong>
<span>{item.text}</span>
</span>
</a>
</li>
))}
</ul>
{current.feature && (
<a
className="gu-mega__feature"
href={current.feature.href}
onClick={() => setOpen(null)}
>
<span className="gu-mega__art" aria-hidden="true" />
<strong>{current.feature.title}</strong>
<span>{current.feature.text}</span>
<span className="gu-mega__more">{current.feature.cta}</span>
</a>
)}
</div>
)}
</div>
</nav>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Mega menu
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
A website top bar with dropdown "mega menus". All dropdowns share a single panel: moving from one menu to another morphs the panel's height and crossfades the content instead of closing and reopening.
## Layout and look
- Bar: rounded 18px, 1px border, background #fbf8f2 (dark #18181b), brand on the left (weight 900), top-level items as 40px-high buttons with a small chevron that flips when open.
- Panel: absolutely positioned 10px below the bar, same width, radius 20px, deep soft shadow.
- Inside: a two-column grid of links (36px square glyph tile, bold title, muted one-line description) plus a 240px feature card on the right (dark card in light mode, orange #ff5a1f card in dark mode) with a small pattern illustration, title, text and a "→" link.
- Hovering a link fills its glyph tile with orange and tilts it -6°.
## Behavior and motion
1. Mouse: hovering a menu label opens it after 90ms; leaving the whole nav closes it after 220ms (hover intent, so diagonal moves into the panel do not close it).
2. Click or tap toggles a menu. Clicking outside or pressing Escape closes it; Escape returns focus to the label.
3. The panel animates height (450ms, cubic-bezier(0.16, 1, 0.3, 1)) using a ResizeObserver measurement of the content; content re-enters with a 6px rise and fade.
4. Plain links (no submenu) close any open panel when hovered.
5. Narrow container (under 640px): bar wraps, panel content stacks into one column.
## Touch
Menus open on tap only. Large 40px+ targets.
## Accessibility
- <nav> with buttons using aria-expanded and aria-controls; the closed panel is inert.
- Focus rings #2b35ff (dark: #d7ff3a).
- prefers-reduced-motion: no height animation, no crossfade.
## Props
brand, menus ({ label, items?: { title, text, href, glyph? }[], feature?: { title, text, href, cta }, href? }[]), theme.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### MegaMenu.tsx
```tsx
"use client";
import {
useEffect,
useId,
useRef,
useState,
type PointerEvent as ReactPointerEvent,
} from "react";
import "./mega-menu.css";
export type MegaItem = {
title: string;
text: string;
href: string;
glyph?: string;
};
export type MegaMenuGroup = {
label: string;
items?: MegaItem[];
/** A highlighted card on the right side of the panel. */
feature?: { title: string; text: string; href: string; cta: string };
/** Plain link (no panel). */
href?: string;
};
type MegaMenuProps = {
brand?: string;
menus: MegaMenuGroup[];
theme?: "light" | "dark";
};
/**
* MegaMenu
* - All dropdowns share ONE panel. Moving from "Product" to "Solutions" morphs the panel's
* size and position and crossfades the content, instead of closing and reopening.
* - Opens on hover (with a short intent delay) for mouse users, on click/tap for everyone.
* - Escape closes and puts focus back on the menu button.
*/
export function MegaMenu({
brand = "Fieldstone",
menus,
theme = "light",
}: MegaMenuProps) {
const id = useId();
const [open, setOpen] = useState<number | null>(null);
const [panelHeight, setPanelHeight] = useState(0);
const root = useRef<HTMLElement>(null);
const inner = useRef<HTMLDivElement>(null);
const timer = useRef<number>(0);
const buttons = useRef<(HTMLButtonElement | null)[]>([]);
// Measure the open menu's content so the panel can animate its height.
useEffect(() => {
if (open === null || !inner.current) return;
const el = inner.current;
const ro = new ResizeObserver(() => setPanelHeight(el.offsetHeight));
ro.observe(el);
return () => ro.disconnect();
}, [open]);
// Escape + click outside close the panel.
useEffect(() => {
if (open === null) return;
const onKey = (e: KeyboardEvent) => {
if (e.key !== "Escape") return;
buttons.current[open]?.focus();
setOpen(null);
};
const onDown = (e: PointerEvent) => {
if (!root.current?.contains(e.target as Node)) setOpen(null);
};
window.addEventListener("keydown", onKey);
window.addEventListener("pointerdown", onDown);
return () => {
window.removeEventListener("keydown", onKey);
window.removeEventListener("pointerdown", onDown);
};
}, [open]);
const hoverOpen = (i: number | null, e: ReactPointerEvent) => {
if (e.pointerType !== "mouse") return;
window.clearTimeout(timer.current);
timer.current = window.setTimeout(() => setOpen(i), i === null ? 220 : 90);
};
const current = open !== null ? menus[open] : null;
return (
<nav
ref={root}
className="gu-mega"
data-theme={theme}
aria-label="Main"
onPointerLeave={(e) => hoverOpen(null, e)}
>
<div className="gu-mega__bar">
<a className="gu-mega__brand" href="#">
{brand}
</a>
<ul className="gu-mega__top">
{menus.map((menu, i) =>
menu.items ? (
<li key={menu.label}>
<button
ref={(el) => {
buttons.current[i] = el;
}}
type="button"
aria-expanded={open === i}
aria-controls={`${id}-panel`}
onClick={() => setOpen(open === i ? null : i)}
onPointerEnter={(e) => hoverOpen(i, e)}
>
{menu.label}
<svg viewBox="0 0 12 12" aria-hidden="true">
<path
d="m3 4.5 3 3 3-3"
fill="none"
stroke="currentColor"
strokeWidth="1.6"
strokeLinecap="round"
/>
</svg>
</button>
</li>
) : (
<li key={menu.label}>
<a href={menu.href} onPointerEnter={(e) => hoverOpen(null, e)}>
{menu.label}
</a>
</li>
),
)}
</ul>
</div>
<div
id={`${id}-panel`}
className="gu-mega__panel"
data-open={current ? "" : undefined}
style={{ height: current ? panelHeight : 0 }}
onPointerEnter={() => window.clearTimeout(timer.current)}
inert={!current}
>
{current && (
<div ref={inner} key={open} className="gu-mega__inner">
<ul className="gu-mega__items">
{current.items?.map((item) => (
<li key={item.title}>
<a href={item.href} onClick={() => setOpen(null)}>
<span className="gu-mega__glyph" aria-hidden="true">
{item.glyph ?? item.title[0]}
</span>
<span>
<strong>{item.title}</strong>
<span>{item.text}</span>
</span>
</a>
</li>
))}
</ul>
{current.feature && (
<a
className="gu-mega__feature"
href={current.feature.href}
onClick={() => setOpen(null)}
>
<span className="gu-mega__art" aria-hidden="true" />
<strong>{current.feature.title}</strong>
<span>{current.feature.text}</span>
<span className="gu-mega__more">{current.feature.cta}</span>
</a>
)}
</div>
)}
</div>
</nav>
);
}
```
### mega-menu.css
```css
/* MegaMenu: change these colors to match your brand. */
.gu-mega {
--bg: #fbf8f2;
--panel: #fbf8f2;
--text: #0e0e10;
--muted: #57534b;
--hover: rgb(14 14 16 / 0.06);
--line: rgb(14 14 16 / 0.12);
--accent: #ff5a1f;
--accent-text: #0e0e10;
--feature: #0e0e10;
--feature-text: #f3eee3;
--focus: #2b35ff;
--ease: cubic-bezier(0.16, 1, 0.3, 1);
container-type: inline-size;
position: relative;
width: min(100%, 860px);
font-family: system-ui, sans-serif;
color: var(--text);
}
.gu-mega[data-theme="dark"] {
--bg: #18181b;
--panel: #1f1f23;
--text: #f3eee3;
--muted: #aea89c;
--hover: rgb(243 238 227 / 0.07);
--line: rgb(243 238 227 / 0.12);
--feature: #ff5a1f;
--feature-text: #0e0e10;
--focus: #d7ff3a;
}
.gu-mega a {
color: inherit;
text-decoration: none;
}
.gu-mega a:focus-visible,
.gu-mega button:focus-visible {
outline: 2px solid var(--focus);
outline-offset: 2px;
}
.gu-mega__bar {
display: flex;
align-items: center;
gap: 24px;
padding: 10px 14px 10px 22px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--bg);
}
.gu-mega__brand {
font-weight: 900;
font-size: 1.1rem;
letter-spacing: -0.03em;
border-radius: 6px;
}
.gu-mega__top {
display: flex;
flex-wrap: wrap;
gap: 2px;
margin: 0;
padding: 0;
list-style: none;
}
.gu-mega__top button,
.gu-mega__top a {
display: inline-flex;
align-items: center;
gap: 6px;
min-height: 40px;
padding: 0 14px;
border: 0;
border-radius: 10px;
background: none;
color: var(--muted);
font-family: inherit;
font-size: 0.95rem;
font-weight: 600;
cursor: pointer;
transition:
background-color 0.2s,
color 0.2s;
}
.gu-mega__top button:hover,
.gu-mega__top a:hover,
.gu-mega__top button[aria-expanded="true"] {
background: var(--hover);
color: var(--text);
}
.gu-mega__top svg {
width: 12px;
height: 12px;
transition: transform 0.3s var(--ease);
}
.gu-mega__top button[aria-expanded="true"] svg {
transform: rotate(180deg);
}
/* The shared panel: its height animates between menus */
.gu-mega__panel {
position: absolute;
top: calc(100% + 10px);
left: 0;
right: 0;
z-index: 10;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 20px;
background: var(--panel);
box-shadow: 0 30px 60px -30px rgb(14 14 16 / 0.45);
opacity: 0;
transform: translateY(-6px);
transition:
height 0.45s var(--ease),
opacity 0.2s,
transform 0.45s var(--ease);
}
.gu-mega__panel[data-open] {
opacity: 1;
transform: none;
}
.gu-mega__inner {
display: grid;
grid-template-columns: 1fr 240px;
gap: 12px;
padding: 12px;
animation: gu-mega-in 0.35s var(--ease) both;
}
@keyframes gu-mega-in {
from {
opacity: 0;
transform: translateY(6px);
}
}
.gu-mega__items {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 2px;
margin: 0;
padding: 0;
list-style: none;
}
.gu-mega__items a {
display: flex;
gap: 12px;
padding: 12px;
border-radius: 14px;
transition: background-color 0.2s;
}
.gu-mega__items a:hover {
background: var(--hover);
}
.gu-mega__items strong {
display: block;
font-size: 0.95rem;
}
.gu-mega__items strong + span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 0.85rem;
line-height: 1.4;
}
.gu-mega__glyph {
display: grid;
flex: none;
place-items: center;
width: 36px;
height: 36px;
border: 1px solid var(--line);
border-radius: 10px;
font-weight: 800;
transition:
background-color 0.25s,
color 0.25s,
transform 0.35s var(--ease);
}
.gu-mega__items a:hover .gu-mega__glyph {
background: var(--accent);
color: var(--accent-text);
transform: rotate(-6deg);
}
.gu-mega__feature {
display: flex;
flex-direction: column;
gap: 6px;
padding: 16px;
border-radius: 16px;
background: var(--feature);
color: var(--feature-text) !important;
}
.gu-mega__feature span:not([class]) {
font-size: 0.85rem;
line-height: 1.45;
opacity: 0.85;
}
.gu-mega__art {
height: 80px;
margin-bottom: 6px;
border-radius: 10px;
background:
radial-gradient(circle at 30% 60%, var(--accent) 0 18px, transparent 19px),
repeating-linear-gradient(90deg, currentColor 0 1px, transparent 1px 14px);
opacity: 0.9;
}
.gu-mega[data-theme="dark"] .gu-mega__art {
background:
radial-gradient(circle at 30% 60%, #0e0e10 0 18px, transparent 19px),
repeating-linear-gradient(90deg, currentColor 0 1px, transparent 1px 14px);
}
.gu-mega__more {
margin-top: auto;
padding-top: 8px;
font-weight: 700;
font-size: 0.9rem;
}
.gu-mega__more::after {
content: " \2192";
}
@container (max-width: 640px) {
.gu-mega__bar {
flex-wrap: wrap;
gap: 6px 16px;
}
.gu-mega__inner {
grid-template-columns: 1fr;
}
.gu-mega__items {
grid-template-columns: 1fr;
}
}
@media (prefers-reduced-motion: reduce) {
.gu-mega__panel,
.gu-mega__top svg,
.gu-mega__glyph {
transition: none;
}
.gu-mega__inner {
animation: none;
}
}
```
You might also want
Next piece: Minimal footer with progressCollapsible sidebar
Components, intermediate
Glide nav
Components, intermediate
Split action button
Components, intermediate