Navigation Menu
The shadcn/ui Navigation Menu with a viewport that morphs its size between panels, direction-aware panel slides and a gliding indicator — plus an animated React mega menu block built with Motion and Radix for site headers, with a hover highlight that glides between items.
- Drop-in shadcn API
- Morphing viewport
- Direction-aware
- Radix hover intent
- Motion block
"use client";
import {
BarChart3,
Blocks,
BookOpen,
Boxes,
Building2,
Code2,
GraduationCap,
LifeBuoy,
Rocket,
ShieldCheck,
Sparkles,
Zap,
} from "lucide-react";
import { MegaMenu, type MegaMenuItem } from "@/components/dusk/navigation-menu/mega-menu";
const items: MegaMenuItem[] = [
{
label: "Product",
featured: {
eyebrow: "New",
title: "Dusk Edge",
description: "Deploy animated UIs to 300+ regions in one push.",
href: "#edge",
},
columns: [
{
heading: "Platform",
links: [
{ title: "Components", href: "#components", description: "60+ animated, accessible pieces.", icon: <Blocks /> },
{ title: "Motion engine", href: "#motion", description: "Springs, layout and gestures.", icon: <Zap /> },
{ title: "Analytics", href: "#analytics", description: "See what users interact with.", icon: <BarChart3 />, badge: "Beta" },
],
},
{
heading: "Security",
links: [
{ title: "Access control", href: "#access", description: "Roles, SSO and audit logs.", icon: <ShieldCheck /> },
{ title: "Integrations", href: "#integrations", description: "Connect the tools you use.", icon: <Boxes /> },
],
},
],
},
{
label: "Solutions",
columns: [
{
links: [
{ title: "Startups", href: "#startups", description: "Ship a polished MVP fast.", icon: <Rocket /> },
{ title: "Enterprise", href: "#enterprise", description: "Scale with governance.", icon: <Building2 /> },
],
},
{
links: [
{ title: "Design systems", href: "#design-systems", description: "Start from tokens, not scratch.", icon: <Sparkles /> },
{ title: "Developers", href: "#developers", description: "Typed APIs, copy-paste code.", icon: <Code2 /> },
],
},
],
},
{
label: "Resources",
columns: [
{
heading: "Learn",
links: [
{ title: "Docs", href: "#docs", description: "Guides and API reference.", icon: <BookOpen /> },
{ title: "Academy", href: "#academy", description: "Video courses on motion.", icon: <GraduationCap /> },
{ title: "Support", href: "#support", description: "Talk to a human.", icon: <LifeBuoy /> },
],
},
],
},
{ label: "Pricing", href: "#pricing" },
];
export default function MegaMenuDemo() {
return (
<div className="flex h-full w-full justify-center pt-2">
{/* Phones get one item fewer, as a real mobile header would. */}
<MegaMenu items={items} className="max-sm:[&>div>ul>li:nth-child(3)]:hidden" />
</div>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/navigation-menu.json1. Install the dependencies.
npm install @radix-ui/react-navigation-menu class-variance-authority lucide-react2. Copy the source into
components/ui/navigation-menu.tsx.navigation-menu.tsx "use client" import * as React from "react" import * as NavigationMenuPrimitive from "@radix-ui/react-navigation-menu" import { cva } from "class-variance-authority" import { ChevronDownIcon } from "lucide-react" import { cn } from "@/lib/utils" /** * DuskUI Navigation Menu — the shadcn/ui Navigation Menu with DuskUI motion. * * Same parts, props and data-slot attributes as shadcn (including * `viewport={false}`), plus: * - The viewport eases its width and height between panels instead of * snapping, and grows out of the top edge when it opens. * - Panels shift a few pixels in from the side you're moving toward * (Radix's `data-motion`), so travel direction is always clear. * - The indicator arrow glides between triggers and fades in and out. * - Triggers ease their hover and open states; the chevron flips. * - Under `prefers-reduced-motion` everything fades; nothing slides or resizes. * CSS only (tw-animate-css). Radix handles hover intent, keyboard, focus and aria. */ function NavigationMenu({ className, children, viewport = true, ...props }: React.ComponentProps<typeof NavigationMenuPrimitive.Root> & { /** Render panels in a shared viewport that morphs between them. `false` drops each panel under its trigger. */ viewport?: boolean }) { return ( <NavigationMenuPrimitive.Root data-slot="navigation-menu" data-viewport={viewport} className={cn( "group/navigation-menu relative flex max-w-max flex-1 items-center justify-center", className )} {...props} > {children} {viewport && <NavigationMenuViewport />} </NavigationMenuPrimitive.Root> ) } /** The row of items. */ function NavigationMenuList({ className, ...props }: React.ComponentProps<typeof NavigationMenuPrimitive.List>) { return ( <NavigationMenuPrimitive.List data-slot="navigation-menu-list" className={cn( "group flex flex-1 list-none items-center justify-center gap-1", className )} {...props} /> ) } /** One entry: a Trigger + Content pair, or a plain Link. */ function NavigationMenuItem({ className, ...props }: React.ComponentProps<typeof NavigationMenuPrimitive.Item>) { return ( <NavigationMenuPrimitive.Item data-slot="navigation-menu-item" className={cn("relative", className)} {...props} /> ) } /** Trigger styles, exported so plain links in the list can match them. */ const navigationMenuTriggerStyle = cva( [ "group inline-flex h-9 w-max items-center justify-center rounded-md bg-transparent px-4 py-2 text-sm font-medium text-foreground/80 outline-none", "transition-[color,background-color,box-shadow] duration-150 ease-out motion-reduce:transition-none", "hover:bg-accent/70 hover:text-foreground focus:bg-accent/70 focus:text-foreground", "focus-visible:ring-[3px] focus-visible:ring-ring/50", "data-[state=open]:bg-accent/60 data-[state=open]:text-foreground data-[active=true]:text-foreground", "disabled:pointer-events-none disabled:opacity-50", ] ) /** Opens its panel on hover, click, Enter, Space or ArrowDown. */ function NavigationMenuTrigger({ className, children, ...props }: React.ComponentProps<typeof NavigationMenuPrimitive.Trigger>) { return ( <NavigationMenuPrimitive.Trigger data-slot="navigation-menu-trigger" className={cn(navigationMenuTriggerStyle(), "group", className)} {...props} > {children}{" "} <ChevronDownIcon className="relative top-px ml-1 size-3 text-muted-foreground transition-transform duration-200 ease-out group-data-[state=open]:rotate-180 group-data-[state=open]:text-foreground motion-reduce:transition-none" aria-hidden="true" /> </NavigationMenuPrimitive.Trigger> ) } /** A trigger's panel. Rendered into the viewport, or under the trigger with `viewport={false}`. */ function NavigationMenuContent({ className, ...props }: React.ComponentProps<typeof NavigationMenuPrimitive.Content>) { return ( <NavigationMenuPrimitive.Content data-slot="navigation-menu-content" className={cn( "top-0 left-0 w-full p-2 pr-2.5 md:absolute md:w-auto", // Direction-aware panel swaps inside the viewport. "ease-out data-[motion^=from-]:animate-in data-[motion^=to-]:animate-out data-[motion^=from-]:fade-in-0 data-[motion^=to-]:fade-out-0 data-[motion]:duration-200", "data-[motion=from-end]:slide-in-from-right-2 data-[motion=from-start]:slide-in-from-left-2 data-[motion=to-end]:slide-out-to-right-2 data-[motion=to-start]:slide-out-to-left-2", "motion-reduce:data-[motion=from-end]:slide-in-from-right-0 motion-reduce:data-[motion=from-start]:slide-in-from-left-0 motion-reduce:data-[motion=to-end]:slide-out-to-right-0 motion-reduce:data-[motion=to-start]:slide-out-to-left-0", // Without a viewport each panel is its own popover under the trigger. "group-data-[viewport=false]/navigation-menu:top-full group-data-[viewport=false]/navigation-menu:mt-1.5 group-data-[viewport=false]/navigation-menu:overflow-hidden group-data-[viewport=false]/navigation-menu:rounded-lg group-data-[viewport=false]/navigation-menu:border group-data-[viewport=false]/navigation-menu:bg-popover group-data-[viewport=false]/navigation-menu:text-popover-foreground", "group-data-[viewport=false]/navigation-menu:shadow-md", "group-data-[viewport=false]/navigation-menu:origin-top group-data-[viewport=false]/navigation-menu:duration-200", "group-data-[viewport=false]/navigation-menu:data-[state=open]:animate-in group-data-[viewport=false]/navigation-menu:data-[state=open]:fade-in-0 group-data-[viewport=false]/navigation-menu:data-[state=open]:zoom-in-95", "group-data-[viewport=false]/navigation-menu:data-[state=closed]:animate-out group-data-[viewport=false]/navigation-menu:data-[state=closed]:fade-out-0 group-data-[viewport=false]/navigation-menu:data-[state=closed]:zoom-out-95", "**:data-[slot=navigation-menu-link]:focus:ring-0 **:data-[slot=navigation-menu-link]:focus:outline-none", className )} {...props} /> ) } /** The shared surface panels render into. Morphs to the size of the active panel. */ function NavigationMenuViewport({ className, ...props }: React.ComponentProps<typeof NavigationMenuPrimitive.Viewport>) { return ( <div className="absolute top-full left-0 isolate z-50 flex justify-center"> <NavigationMenuPrimitive.Viewport data-slot="navigation-menu-viewport" className={cn( "relative mt-1.5 h-(--radix-navigation-menu-viewport-height) w-full origin-top overflow-hidden rounded-xl border bg-popover text-popover-foreground md:w-(--radix-navigation-menu-viewport-width)", "shadow-md", "transition-[width,height] duration-200 ease-out motion-reduce:transition-none", "data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95 data-[state=open]:duration-200", "data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[state=closed]:duration-150", "motion-reduce:data-[state=open]:zoom-in-100 motion-reduce:data-[state=closed]:zoom-out-100", className )} {...props} /> </div> ) } /** A link inside a panel (or a plain item with `asChild`). Set `active` for the current page. */ function NavigationMenuLink({ className, ...props }: React.ComponentProps<typeof NavigationMenuPrimitive.Link>) { return ( <NavigationMenuPrimitive.Link data-slot="navigation-menu-link" className={cn( "flex flex-col gap-1 rounded-md p-2 text-sm outline-none", "transition-[color,background-color,box-shadow] duration-150 ease-out motion-reduce:transition-none", "hover:bg-accent hover:text-accent-foreground focus:bg-accent focus:text-accent-foreground", "focus-visible:ring-[3px] focus-visible:ring-ring/50", "data-[active=true]:bg-accent/50 data-[active=true]:text-foreground data-[active=true]:hover:bg-accent", "[&_svg:not([class*='text-'])]:text-muted-foreground [&_svg:not([class*='size-'])]:size-4", className )} {...props} /> ) } /** A small arrow under the open trigger. Glides between triggers. */ function NavigationMenuIndicator({ className, ...props }: React.ComponentProps<typeof NavigationMenuPrimitive.Indicator>) { return ( <NavigationMenuPrimitive.Indicator data-slot="navigation-menu-indicator" className={cn( "top-full z-[1] flex h-1.5 items-end justify-center overflow-hidden", "transition-[transform,width,opacity] duration-200 ease-out starting:opacity-0 motion-reduce:transition-opacity", className )} {...props} > <div className="relative top-[60%] size-2 rotate-45 rounded-tl-sm bg-border shadow-md" /> </NavigationMenuPrimitive.Indicator> ) } export { NavigationMenu, NavigationMenuList, NavigationMenuItem, NavigationMenuContent, NavigationMenuTrigger, NavigationMenuLink, NavigationMenuIndicator, NavigationMenuViewport, navigationMenuTriggerStyle, }
Same exports and props as the shadcn/ui Navigation Menu, including navigationMenuTriggerStyle and viewport={false}. Animations are CSS (tw-animate-css), so the base needs no Motion.
Usage
import {
NavigationMenu,
NavigationMenuContent,
NavigationMenuItem,
NavigationMenuLink,
NavigationMenuList,
NavigationMenuTrigger,
navigationMenuTriggerStyle,
} from "@/components/ui/navigation-menu"
export function SiteNav() {
return (
<NavigationMenu>
<NavigationMenuList>
<NavigationMenuItem>
<NavigationMenuTrigger>Products</NavigationMenuTrigger>
<NavigationMenuContent>
<ul className="grid w-[400px] gap-1 p-1">
<li>
<NavigationMenuLink href="/analytics">Analytics</NavigationMenuLink>
</li>
</ul>
</NavigationMenuContent>
</NavigationMenuItem>
<NavigationMenuItem>
<NavigationMenuLink href="/docs" className={navigationMenuTriggerStyle()}>
Docs
</NavigationMenuLink>
</NavigationMenuItem>
</NavigationMenuList>
</NavigationMenu>
)
}Examples
Default
Hover or focus a trigger. Panels share one viewport that eases its width and height to fit each panel, the incoming panel shifts in from the side you're moving toward while the old one leaves the other way (Radix's data-motion), and the indicator arrow glides under the open trigger.
"use client";
import * as React from "react";
import {
NavigationMenu,
NavigationMenuContent,
NavigationMenuIndicator,
NavigationMenuItem,
NavigationMenuLink,
NavigationMenuList,
NavigationMenuTrigger,
navigationMenuTriggerStyle,
} from "@/components/ui/navigation-menu";
const components = [
{ title: "Dialog", href: "#dialog", description: "A window overlaid on the page that traps focus." },
{ title: "Hover Card", href: "#hover-card", description: "Preview content behind a link on hover." },
{ title: "Progress", href: "#progress", description: "Shows how far along a task is." },
{ title: "Tabs", href: "#tabs", description: "Layered panels shown one at a time." },
];
function ListItem({ title, href, children }: { title: string; href: string; children: React.ReactNode }) {
return (
<li>
<NavigationMenuLink asChild>
<a href={href}>
<span className="text-sm font-medium leading-none">{title}</span>
<span className="line-clamp-2 text-sm leading-snug text-muted-foreground">{children}</span>
</a>
</NavigationMenuLink>
</li>
);
}
export default function NavigationMenuBasicDemo() {
return (
<NavigationMenu>
<NavigationMenuList>
<NavigationMenuItem>
<NavigationMenuTrigger className="max-sm:px-3">Getting started</NavigationMenuTrigger>
<NavigationMenuContent>
<ul className="grid gap-1 p-1 sm:w-[440px] sm:grid-cols-[0.8fr_1fr]">
<li className="row-span-3">
<NavigationMenuLink
asChild
className="h-full justify-end rounded-md bg-muted/50 p-4"
>
<a href="#intro">
<span className="text-base font-semibold">DuskUI</span>
<span className="text-sm leading-snug text-muted-foreground">
Animated components on shadcn/ui and Motion.
</span>
</a>
</NavigationMenuLink>
</li>
<ListItem title="Introduction" href="#introduction">
What DuskUI is and how it fits with shadcn/ui.
</ListItem>
<ListItem title="Installation" href="#installation">
Add components with the shadcn CLI.
</ListItem>
<ListItem title="Theming" href="#theming">
Tokens, colors and reduced motion.
</ListItem>
</ul>
</NavigationMenuContent>
</NavigationMenuItem>
<NavigationMenuItem>
<NavigationMenuTrigger className="max-sm:px-3">Components</NavigationMenuTrigger>
<NavigationMenuContent>
<ul className="grid gap-1 p-1 sm:w-[520px] sm:grid-cols-2">
{components.map((component) => (
<ListItem key={component.title} title={component.title} href={component.href}>
{component.description}
</ListItem>
))}
</ul>
</NavigationMenuContent>
</NavigationMenuItem>
<NavigationMenuItem className="max-sm:hidden">
<NavigationMenuLink asChild className={navigationMenuTriggerStyle()}>
<a href="#docs">Docs</a>
</NavigationMenuLink>
</NavigationMenuItem>
<NavigationMenuIndicator />
</NavigationMenuList>
</NavigationMenu>
);
}Without a viewport
With viewport={false} each panel drops down under its own trigger as a small popover that scales in from the top. Set active on a link to mark the current page.
"use client";
import { CircleCheck, CircleDashed, CircleDot } from "lucide-react";
import {
NavigationMenu,
NavigationMenuContent,
NavigationMenuItem,
NavigationMenuLink,
NavigationMenuList,
NavigationMenuTrigger,
} from "@/components/ui/navigation-menu";
export default function NavigationMenuNoViewportDemo() {
return (
<NavigationMenu viewport={false}>
<NavigationMenuList>
<NavigationMenuItem>
<NavigationMenuTrigger>Status</NavigationMenuTrigger>
<NavigationMenuContent>
<ul className="grid w-48 gap-0.5">
<li>
<NavigationMenuLink asChild className="flex-row items-center gap-2">
<a href="#backlog">
<CircleDashed />
Backlog
</a>
</NavigationMenuLink>
</li>
<li>
<NavigationMenuLink asChild className="flex-row items-center gap-2">
<a href="#in-progress">
<CircleDot />
In progress
</a>
</NavigationMenuLink>
</li>
<li>
<NavigationMenuLink asChild active className="flex-row items-center gap-2">
<a href="#done">
<CircleCheck />
Done
</a>
</NavigationMenuLink>
</li>
</ul>
</NavigationMenuContent>
</NavigationMenuItem>
<NavigationMenuItem>
<NavigationMenuTrigger>Team</NavigationMenuTrigger>
<NavigationMenuContent>
<ul className="grid w-56 gap-0.5">
{["Design", "Engineering", "Marketing"].map((team) => (
<li key={team}>
<NavigationMenuLink asChild>
<a href={`#${team.toLowerCase()}`}>
<span className="font-medium">{team}</span>
<span className="text-xs text-muted-foreground">View the {team.toLowerCase()} board</span>
</a>
</NavigationMenuLink>
</li>
))}
</ul>
</NavigationMenuContent>
</NavigationMenuItem>
</NavigationMenuList>
</NavigationMenu>
);
}Blocks
Ready-made variations composed from the base component with Motion. Each installs on its own and pulls in its dependencies automatically.
Mega Menu
A data-driven header menu. A highlight glides between bar items as you hover (a shared Motion layoutId), the viewport fades open and then morphs its width and height between panels, panels slide in the direction of travel, links stagger in and an arrow tracks the open item. Panels can have a featured card and any number of link columns; items without a panel are plain links. Pass linkAs={Link} to use Next.js links.
"use client";
import {
BarChart3,
Blocks,
BookOpen,
Boxes,
Building2,
Code2,
GraduationCap,
LifeBuoy,
Rocket,
ShieldCheck,
Sparkles,
Zap,
} from "lucide-react";
import { MegaMenu, type MegaMenuItem } from "@/components/dusk/navigation-menu/mega-menu";
const items: MegaMenuItem[] = [
{
label: "Product",
featured: {
eyebrow: "New",
title: "Dusk Edge",
description: "Deploy animated UIs to 300+ regions in one push.",
href: "#edge",
},
columns: [
{
heading: "Platform",
links: [
{ title: "Components", href: "#components", description: "60+ animated, accessible pieces.", icon: <Blocks /> },
{ title: "Motion engine", href: "#motion", description: "Springs, layout and gestures.", icon: <Zap /> },
{ title: "Analytics", href: "#analytics", description: "See what users interact with.", icon: <BarChart3 />, badge: "Beta" },
],
},
{
heading: "Security",
links: [
{ title: "Access control", href: "#access", description: "Roles, SSO and audit logs.", icon: <ShieldCheck /> },
{ title: "Integrations", href: "#integrations", description: "Connect the tools you use.", icon: <Boxes /> },
],
},
],
},
{
label: "Solutions",
columns: [
{
links: [
{ title: "Startups", href: "#startups", description: "Ship a polished MVP fast.", icon: <Rocket /> },
{ title: "Enterprise", href: "#enterprise", description: "Scale with governance.", icon: <Building2 /> },
],
},
{
links: [
{ title: "Design systems", href: "#design-systems", description: "Start from tokens, not scratch.", icon: <Sparkles /> },
{ title: "Developers", href: "#developers", description: "Typed APIs, copy-paste code.", icon: <Code2 /> },
],
},
],
},
{
label: "Resources",
columns: [
{
heading: "Learn",
links: [
{ title: "Docs", href: "#docs", description: "Guides and API reference.", icon: <BookOpen /> },
{ title: "Academy", href: "#academy", description: "Video courses on motion.", icon: <GraduationCap /> },
{ title: "Support", href: "#support", description: "Talk to a human.", icon: <LifeBuoy /> },
],
},
],
},
{ label: "Pricing", href: "#pricing" },
];
export default function MegaMenuDemo() {
return (
<div className="flex h-full w-full justify-center pt-2">
{/* Phones get one item fewer, as a real mobile header would. */}
<MegaMenu items={items} className="max-sm:[&>div>ul>li:nth-child(3)]:hidden" />
</div>
);
}$ npx shadcn@latest add https://duskui.com/r/mega-menu.json1. Install the dependencies.
npm install motion @radix-ui/react-navigation-menu lucide-react2. Copy the source into
components/dusk/navigation-menu/mega-menu.tsx.mega-menu.tsx "use client"; import * as React from "react"; import * as NavigationMenuPrimitive from "@radix-ui/react-navigation-menu"; import { AnimatePresence, LayoutGroup, motion, useReducedMotion, type Transition } from "motion/react"; import { ArrowUpRight, ChevronDown } from "lucide-react"; import { cn } from "@/lib/utils"; export type MegaMenuLink = { title: string; href: string; description?: string; icon?: React.ReactNode; /** Small tag next to the title, e.g. "New". */ badge?: string; }; export type MegaMenuColumn = { heading?: string; links: MegaMenuLink[]; }; export type MegaMenuItem = { label: string; /** A plain link in the bar. Ignored when the item has a panel. */ href?: string; /** Highlighted card on the left of the panel. */ featured?: { eyebrow?: string; title: string; description?: string; href: string }; /** Link columns in the panel. */ columns?: MegaMenuColumn[]; }; export interface MegaMenuProps extends Omit<React.ComponentProps<typeof NavigationMenuPrimitive.Root>, "children"> { items: MegaMenuItem[]; /** * Component used for links, e.g. Next.js `Link`. Must forward its ref and * props to an anchor. Defaults to `"a"`. */ linkAs?: React.ElementType; } const pillSpring: Transition = { type: "spring", stiffness: 480, damping: 38, mass: 0.7 }; const surfaceSpring: Transition = { type: "spring", duration: 0.25, bounce: 0 }; /** * A mega menu for site headers. A highlight glides between bar items as you * hover, panels open in one shared viewport that morphs its width and height * to fit each panel, the incoming panel slides in from the side you're moving * toward while the old one leaves the other way, and links stagger in. An * arrow glides under the open item. Built on Radix Navigation Menu: hover * intent, keyboard (Tab, arrows, Escape), focus and aria are handled. Under * reduced motion panels fade and nothing slides or resizes. */ export function MegaMenu({ items, linkAs: LinkAs = "a", value: valueProp, defaultValue = "", onValueChange, className, delayDuration = 120, ...props }: MegaMenuProps) { const id = React.useId(); const reduceMotion = useReducedMotion() ?? false; const [internalValue, setInternalValue] = React.useState(defaultValue); const value = valueProp ?? internalValue; const [hovered, setHovered] = React.useState<string | null>(null); // The pill follows the pointer and falls back to the open item. const pillOn = hovered ?? (value || null); return ( <NavigationMenuPrimitive.Root value={value} onValueChange={(next) => { if (valueProp === undefined) setInternalValue(next); onValueChange?.(next); }} delayDuration={delayDuration} className={cn("relative z-20 flex justify-center", className)} {...props} > <LayoutGroup id={id}> <NavigationMenuPrimitive.List className="flex list-none items-center gap-0.5 rounded-full border bg-background p-1" onPointerLeave={() => setHovered(null)} onBlur={(event) => { if (!event.currentTarget.contains(event.relatedTarget as Node | null)) setHovered(null); }} > {items.map((item) => { const hasPanel = !!(item.featured || item.columns?.length); const pill = pillOn === item.label && ( <motion.span aria-hidden layoutId="mega-menu-pill" transition={reduceMotion ? { duration: 0 } : pillSpring} className="absolute inset-0 -z-10 rounded-full bg-accent" /> ); const barItem = cn( "group relative isolate inline-flex h-8 items-center gap-1 rounded-full px-2.5 text-sm sm:px-3.5 font-medium text-foreground/75 outline-none select-none", "transition-colors duration-150 hover:text-foreground focus-visible:ring-[3px] focus-visible:ring-ring/50 data-[state=open]:text-foreground" ); const track = { onPointerEnter: () => setHovered(item.label), onFocus: () => setHovered(item.label), }; return ( <NavigationMenuPrimitive.Item key={item.label} value={item.label}> {hasPanel ? ( <> <NavigationMenuPrimitive.Trigger className={barItem} {...track}> {pill} {item.label} <ChevronDown aria-hidden className="size-3.5 text-muted-foreground transition-transform duration-300 ease-[cubic-bezier(0.16,1,0.3,1)] group-data-[state=open]:rotate-180 group-data-[state=open]:text-foreground motion-reduce:transition-none" /> </NavigationMenuPrimitive.Trigger> <NavigationMenuPrimitive.Content className={cn( "absolute top-0 left-0 ease-[cubic-bezier(0.16,1,0.3,1)]", "data-[motion^=from-]:animate-in data-[motion^=from-]:fade-in-0 data-[motion^=to-]:animate-out data-[motion^=to-]:fade-out-0 data-[motion]:duration-200", "data-[motion=from-end]:slide-in-from-right-2 data-[motion=from-start]:slide-in-from-left-2 data-[motion=to-end]:slide-out-to-right-2 data-[motion=to-start]:slide-out-to-left-2", "motion-reduce:data-[motion=from-end]:slide-in-from-right-0 motion-reduce:data-[motion=from-start]:slide-in-from-left-0 motion-reduce:data-[motion=to-end]:slide-out-to-right-0 motion-reduce:data-[motion=to-start]:slide-out-to-left-0" )} > <Panel item={item} LinkAs={LinkAs} reduceMotion={reduceMotion} /> </NavigationMenuPrimitive.Content> </> ) : ( <NavigationMenuPrimitive.Link asChild> <LinkAs href={item.href ?? "#"} className={barItem} {...track}> {pill} {item.label} </LinkAs> </NavigationMenuPrimitive.Link> )} </NavigationMenuPrimitive.Item> ); })} <NavigationMenuPrimitive.Indicator className={cn( "top-full z-10 flex h-2.5 items-end justify-center overflow-hidden", "transition-[transform,width,opacity] duration-300 ease-[cubic-bezier(0.22,1,0.36,1)] starting:opacity-0 motion-reduce:transition-opacity" )} > <span className="relative top-[70%] size-2.5 rotate-45 rounded-tl-[3px] border-t border-l bg-popover" /> </NavigationMenuPrimitive.Indicator> </NavigationMenuPrimitive.List> </LayoutGroup> <div className="absolute top-full left-1/2 flex -translate-x-1/2 justify-center pt-2 perspective-[1600px]"> <AnimatePresence> {value && ( // forceMount hands mounting to AnimatePresence so the close animation can play; // Radix keeps the last panel inside while it fades out. <NavigationMenuPrimitive.Viewport forceMount asChild> <motion.div className={cn( "relative h-(--radix-navigation-menu-viewport-height) w-(--radix-navigation-menu-viewport-width) origin-top overflow-hidden rounded-xl border bg-popover text-popover-foreground", "shadow-lg", // The morph between panels: Radix updates the size variables, CSS eases them. "transition-[width,height] duration-250 ease-[cubic-bezier(0.22,1,0.36,1)] motion-reduce:transition-none" )} initial={reduceMotion ? { opacity: 0 } : { opacity: 0, scale: 0.98, y: -4 }} animate={reduceMotion ? { opacity: 1 } : { opacity: 1, scale: 1, y: 0 }} exit={ reduceMotion ? { opacity: 0, transition: { duration: 0.1 } } : { opacity: 0, scale: 0.98, y: -4, transition: { duration: 0.12, ease: "easeIn" } } } transition={surfaceSpring} /> </NavigationMenuPrimitive.Viewport> )} </AnimatePresence> </div> </NavigationMenuPrimitive.Root> ); } function Panel({ item, LinkAs, reduceMotion, }: { item: MegaMenuItem; LinkAs: React.ElementType; reduceMotion: boolean; }) { const columns = item.columns ?? []; const cols = columns.length + (item.featured ? 1 : 0); let index = 0; // Each link fades up a beat after the previous one. const enter = () => { const delay = index++ * 0.015; return { initial: reduceMotion ? { opacity: 0 } : { opacity: 0, y: 4 }, animate: { opacity: 1, y: 0 }, transition: { duration: 0.2, ease: "easeOut", delay }, } as const; }; return ( <div className={cn( "grid w-[min(var(--mega-width),calc(100vw-2rem))] gap-2 p-2", cols >= 3 ? "[--mega-width:680px] sm:grid-cols-3" : cols === 2 ? "[--mega-width:520px] sm:grid-cols-2" : "[--mega-width:300px]" )} > {item.featured && ( <motion.div {...enter()}> <NavigationMenuPrimitive.Link asChild> <LinkAs href={item.featured.href} className={cn( "group/featured relative flex h-full min-h-40 flex-col justify-end overflow-hidden rounded-lg border bg-muted/50 p-4 outline-none", "transition-colors duration-150 hover:bg-accent focus-visible:ring-[3px] focus-visible:ring-ring/50" )} > <ArrowUpRight aria-hidden className="absolute top-3 right-3 size-4 text-muted-foreground transition-colors duration-150 group-hover/featured:text-foreground" /> {item.featured.eyebrow && ( <span className="mb-2 w-fit rounded-full border px-2 py-px text-xs text-muted-foreground"> {item.featured.eyebrow} </span> )} <span className="font-semibold tracking-tight">{item.featured.title}</span> {item.featured.description && ( <span className="mt-1 text-sm leading-snug text-muted-foreground">{item.featured.description}</span> )} </LinkAs> </NavigationMenuPrimitive.Link> </motion.div> )} {columns.map((column, c) => ( <div key={column.heading ?? c} className="flex flex-col gap-0.5"> {column.heading && ( <motion.p {...enter()} className="px-2.5 pt-1.5 pb-1 text-xs font-medium text-muted-foreground"> {column.heading} </motion.p> )} <ul className="flex flex-col gap-0.5"> {column.links.map((link) => ( <motion.li key={link.href + link.title} {...enter()}> <NavigationMenuPrimitive.Link asChild> <LinkAs href={link.href} className={cn( "group/link flex items-start gap-3 rounded-md p-2.5 outline-none", "transition-colors duration-150 hover:bg-accent focus-visible:bg-accent focus-visible:ring-[3px] focus-visible:ring-ring/50" )} > {link.icon && ( <span aria-hidden className="mt-0.5 flex size-8 shrink-0 items-center justify-center rounded-md border bg-white/[0.03] text-muted-foreground transition-colors duration-150 group-hover/link:text-foreground group-focus-visible/link:text-foreground [&_svg]:size-4" > {link.icon} </span> )} <span className="min-w-0"> <span className="flex items-center gap-2 text-sm font-medium text-foreground"> {link.title} {link.badge && ( <span className="rounded-full border px-1.5 py-px text-[0.65rem] font-medium text-muted-foreground"> {link.badge} </span> )} </span> {link.description && ( <span className="mt-0.5 line-clamp-2 block text-xs leading-snug text-muted-foreground"> {link.description} </span> )} </span> </LinkAs> </NavigationMenuPrimitive.Link> </motion.li> ))} </ul> </div> ))} </div> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| items | MegaMenuItem[] | — | Bar items: { label, href?, featured?, columns? }. |
| featured | { eyebrow?, title, description?, href } | — | Highlighted card on the left of a panel. |
| columns | { heading?, links: { title, href, description?, icon?, badge? }[] }[] | — | Link columns in a panel. |
| linkAs | ElementType | "a" | Link component, e.g. Next.js Link. Must forward ref and props. |
| value / defaultValue / onValueChange | string / string / (value: string) => void | — | The open item's label (empty when closed). |
| delayDuration | number | 120 | Hover delay before a panel opens, in ms. |
API Reference
Same parts as shadcn/ui: NavigationMenu, List, Item, Trigger, Content, Link, Viewport and Indicator (all prefixed NavigationMenu), plus navigationMenuTriggerStyle(). Each forwards the Radix props.
| Prop | Type | Default | Description |
|---|---|---|---|
| viewport | boolean | true | Render panels in a shared, morphing viewport (on NavigationMenu). |
| value / defaultValue / onValueChange | string / string / (value: string) => void | — | Controlled open item (on NavigationMenu). |
| delayDuration / skipDelayDuration | number / number | 200 / 300 | Hover intent timing in ms (on NavigationMenu). |
| orientation | "horizontal" | "vertical" | "horizontal" | Layout and arrow-key direction (on NavigationMenu). |
| active | boolean | — | Marks the current page with aria-current (on NavigationMenuLink). |
| onSelect | (event: Event) => void | — | Fires when a link is chosen; the menu closes (on NavigationMenuLink). |
Accessibility
- Built on Radix Navigation Menu: triggers are buttons with aria-expanded and aria-controls, panels are linked back with aria-labelledby, and the active link gets aria-current="page".
- Tab moves through triggers and into the open panel; arrow keys move between triggers; Enter, Space or ArrowDown opens a panel and Escape closes it, returning focus to its trigger.
- Hover intent delays avoid panels flickering open as the pointer passes over the bar.
- The mega menu's highlight follows keyboard focus as well as the pointer.
- Under prefers-reduced-motion panels fade instead of sliding and the viewport changes size instantly.