Navbar with mega menu
Responsive site header with grouped flyouts, optional featured links, and a primary action.
pnpm dlx shadcn@latest add @haze/navbar-mega-menu---
import { ArrowRight, ChevronDown, Layers3 } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";
import Button from "../ui/button.astro";
import LogoMark from "../ui/logo-mark.astro";
interface Link { label: string; href: string; description?: string; current?: boolean }
interface Group { title: string; links: Link[] }
interface Panel { label: string; groups: Group[]; featured?: { title: string; description: string; link: Link } }
interface Props {
id?: string;
brand?: string;
homeHref?: string;
navLabel?: string;
items?: (Link | Panel)[];
action?: Link | null;
class?: string;
}
const {
id = "haze-mega-navbar",
brand = "Fieldwork",
homeHref = "/",
navLabel = "Main navigation",
items = [
{ label: "Explore", groups: [
{ title: "Build", links: [
{ label: "UI components", href: "/browse?kind=ui", description: "Controls, overlays, and navigation." },
{ label: "Page blocks", href: "/browse?kind=block", description: "Sections ready to assemble." },
] },
{ title: "Discover", links: [
{ label: "Charts and tables", href: "/browse?cat=data-display", description: "Make product data readable." },
{ label: "Menus and sidebars", href: "/browse?cat=navigation", description: "Give every screen a clear path." },
] },
], featured: {
title: "Build your first page",
description: "Get set up and install your first block.",
link: { label: "Quickstart", href: "/docs/" },
} },
{ label: "Learn", groups: [
{ title: "Documentation", links: [
{ label: "Getting started", href: "/docs/" },
{ label: "Browse examples", href: "/browse/" },
] },
] },
{ label: "Browse", href: "/browse/" },
],
action = { label: "Get started", href: "/docs/" },
class: className,
} = Astro.props;
const linkClass = "inline-flex min-h-9 max-w-full items-center rounded-lg px-2.5 text-sm/5 font-medium wrap-anywhere text-muted-foreground transition-colors duration-150 hover:bg-foreground/8 hover:text-foreground focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring aria-[current=page]:bg-foreground/8 aria-[current=page]:text-foreground";
---
<header class={cn("border-b border-border bg-muted/40", className)} style={`anchor-name: --${id}`}>
<div class="mx-auto grid max-w-6xl grid-cols-[minmax(0,1fr)_auto] items-center gap-x-3 gap-y-2 px-4 py-3 lg:grid-cols-[auto_minmax(0,1fr)_auto] lg:gap-x-8 lg:px-6">
<a href={homeHref} aria-label={`${brand} home`} class="col-start-1 row-start-1 inline-flex min-h-9 w-fit max-w-full items-center rounded-md wrap-anywhere focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring">
<LogoMark name={brand} shape="square" class="min-w-0 text-foreground [&_svg]:shrink-0" />
</a>
<nav aria-label={navLabel} class="col-span-2 row-start-2 min-w-0 border-t border-border pt-2 lg:col-span-1 lg:col-start-2 lg:row-start-1 lg:border-0 lg:pt-0">
<ul role="list" class="flex flex-wrap items-center gap-1 lg:justify-center">
{items.map((item, index) => (
<li class="min-w-0 max-w-full [&:has(:popover-open)>button_svg]:rotate-180">
{"groups" in item ? (
<>
<button type="button" popovertarget={`${id}-panel-${index}`}
class={cn(linkClass, "gap-1.5 text-start transition-[background-color,color,scale] ease-snappy active:scale-[0.97] active:duration-75")}>
{item.label}
<ChevronDown class="size-3.5 shrink-0 transition-[rotate] duration-200 ease-snappy" aria-hidden="true" />
</button>
<div id={`${id}-panel-${index}`} popover style={`position-anchor: --${id}`}
class={cn(
"m-auto max-h-[min(32rem,calc(100dvh-2rem))] w-[min(54rem,calc(100vw-1.5rem))] max-w-none overflow-y-auto overscroll-contain rounded-2xl border border-border bg-popover p-3 text-popover-foreground shadow-float backdrop:bg-background/60 sm:p-4",
"-translate-y-1 scale-[0.97] opacity-0 transition-[opacity,scale,translate,overlay,display] transition-discrete duration-150 ease-snappy open:translate-y-0 open:scale-100 open:opacity-100 starting:open:-translate-y-1 starting:open:scale-[0.97] starting:open:opacity-0",
"md:supports-[position-area:block-end]:my-2 md:supports-[position-area:block-end]:backdrop:bg-transparent md:supports-[position-area:block-end]:[position-area:block-end] md:supports-[position-area:block-end]:[position-try-fallbacks:flip-block]",
)}>
<div class={cn("grid gap-3", item.featured && "md:grid-cols-[minmax(0,1fr)_minmax(0,14rem)]")}>
<div class="grid min-w-0 gap-3 sm:grid-cols-[repeat(auto-fit,minmax(min(100%,13rem),1fr))]">
{item.groups.map((group) => (
<div class="min-w-0">
<p class="px-2.5 pt-1.5 pb-1 text-[0.6875rem]/4 font-medium tracking-wider wrap-anywhere text-muted-foreground uppercase">{group.title}</p>
<ul role="list" class="grid gap-0.5">
{group.links.map((link) => (
<li><a href={link.href} aria-current={link.current ? "page" : undefined}
class="group flex min-h-10 items-center gap-2 rounded-lg px-2.5 py-2 transition-colors duration-75 hover:bg-foreground/8 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-ring aria-[current=page]:bg-accent">
<span class="grid min-w-0 flex-1 gap-0.5 wrap-anywhere">
<span class="text-sm/5 font-medium">{link.label}</span>
{link.description && <span class="text-[0.8125rem]/5 text-muted-foreground">{link.description}</span>}
</span>
<ArrowRight class="size-3.5 shrink-0 text-muted-foreground opacity-0 transition-opacity duration-75 group-hover:opacity-100 group-focus-visible:opacity-100 rtl:rotate-180" aria-hidden="true" />
</a></li>
))}
</ul>
</div>
))}
</div>
{item.featured && (
<div class="grid content-start gap-2 rounded-md border border-border bg-card p-3 wrap-anywhere text-card-foreground sm:p-4">
<span class="grid size-8 place-items-center rounded-lg bg-muted text-muted-foreground"><Layers3 class="size-4" aria-hidden="true" /></span>
<p class="mt-1 text-sm/5 font-semibold tracking-tight">{item.featured.title}</p>
<p class="text-[0.8125rem]/5 text-muted-foreground">{item.featured.description}</p>
<Button href={item.featured.link.href} variant="secondary" size="sm" class="mt-1 h-auto min-h-8 w-fit max-w-full py-1.5 whitespace-normal">
{item.featured.link.label}<ArrowRight aria-hidden="true" class="rtl:rotate-180" />
</Button>
</div>
)}
</div>
</div>
</>
) : (
<a href={item.href} aria-current={item.current ? "page" : undefined} class={linkClass}>{item.label}</a>
)}
</li>
))}
</ul>
</nav>
{action && (
<Button href={action.href} size="sm" class="col-start-2 row-start-1 h-auto min-h-8 max-w-36 py-1.5 whitespace-normal wrap-anywhere lg:col-start-3 lg:max-w-48">{action.label}</Button>
)}
</div>
</header>When to use
- A site has multiple related destinations under each top-level label
- Product or resource menus need short descriptions and a featured link
When not to use
- There are only a few top-level links. Use navbar-simple.
- Visitors primarily arrive looking for a specific item. Use navbar-search.
Installation
Add the Haze registry to components.json once, then install Navbar with mega menu with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/navbar-mega-menunpx shadcn@latest add @haze/navbar-mega-menuyarn dlx shadcn@latest add @haze/navbar-mega-menubunx --bun shadcn@latest add @haze/navbar-mega-menuFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| id | string | "haze-mega-navbar" | Supply a unique value per header. Used for the flyout IDs and the CSS anchor. |
| brand | string | "Fieldwork" | Text in the wordmark. |
| homeHref | string | "/" | Destination of the wordmark link. |
| navLabel | string | "Main navigation" | Accessible name of the navigation landmark. |
| items | (Link | Panel)[] | Explore, Learn, Browse | Each link has a label and href. Each panel has a label, groups of titled link lists, and an optional featured link. |
| action | Link | null | Get started | Header button destination and label. Pass null to omit. |
| class | string | – | Additional classes on the header. |
Keyboard interactions
- EnterSpace
- Open or close the focused flyout.
- TabShiftTab
- Move through controls and the open flyout links.
- Escape
- Dismiss the open flyout.
Accessibility
- The native popover keeps the flyout links out of the tab order while closed.
- Flyouts are page navigation and preserve regular link behavior and tab order.
- At narrow widths or without CSS anchors, flyouts open as scrollable centered panels.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
Install and use
Install the Haze UI block "Navbar with mega menu" with `npx shadcn@latest add @haze/navbar-mega-menu`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import NavbarMegaMenu from "@/components/haze/blocks/navbar-mega-menu.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/blocks/navbar-mega-menu.md
Customize with real content
Adapt the Haze navbar-mega-menu block for our product. Replace the demo values of these props with our real content: id, brand, homeHref, navLabel, items, action, class. Keep the layout, the heading level rules and these accessibility details: The native popover keeps the flyout links out of the tab order while closed. Flyouts are page navigation and preserve regular link behavior and tab order. At narrow widths or without CSS anchors, flyouts open as scrollable centered panels. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using navbar-mega-menu, check it fits: use it when a site has multiple related destinations under each top-level label; product or resource menus need short descriptions and a featured link. Avoid it when there are only a few top-level links. use navbar-simple.; visitors primarily arrive looking for a specific item. use navbar-search.. Alternatives: @haze/navbar-simple, @haze/navbar-centered, @haze/navbar-search, @haze/navigation-menu.
Dependencies
- npm: @lucide/astro
- @haze/button
- @haze/logo-mark
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.