Navigation Menu
A row of links and menus for moving between the main sections of an app or site.
Built on Base UI
React Aria has no navigation menu primitive, so this component uses the official Base UI version. It follows the same conventions as the rest of the set, but its props come from Base UI, and links take a render prop in place of a React Aria href.
Installation
pnpm dlx shadcn@latest add @silkware/navigation-menuTo copy the component by hand, install its dependencies:
pnpm add @base-ui/react class-variance-authority cn lucide-reactThen copy the source into components/ui/navigation-menu.tsx:
import { NavigationMenu as NavigationMenuPrimitive } from "@base-ui/react/navigation-menu"import { cva } from "class-variance-authority"import { cn } from "cn"import { ChevronDownIcon } from "lucide-react"
function NavigationMenu({ align = "start", className, children, ...props}: NavigationMenuPrimitive.Root.Props & Pick<NavigationMenuPrimitive.Positioner.Props, "align">) { return ( <NavigationMenuPrimitive.Root data-slot="navigation-menu" className={cn( "group/navigation-menu relative flex max-w-max flex-1 items-center justify-center", className )} {...props} > {children} <NavigationMenuPositioner align={align} /> </NavigationMenuPrimitive.Root> )}
function NavigationMenuList({ className, ...props}: React.ComponentPropsWithRef<typeof NavigationMenuPrimitive.List>) { return ( <NavigationMenuPrimitive.List data-slot="navigation-menu-list" className={cn( "group flex flex-1 list-none items-center justify-center gap-0", className )} {...props} /> )}
function NavigationMenuItem({ className, ...props}: React.ComponentPropsWithRef<typeof NavigationMenuPrimitive.Item>) { return ( <NavigationMenuPrimitive.Item data-slot="navigation-menu-item" className={cn("relative", className)} {...props} /> )}
const navigationMenuTriggerStyle = cva( "group/navigation-menu-trigger inline-flex h-9 w-max items-center justify-center rounded-md px-4 py-2 text-sm font-medium transition-all outline-none hover:bg-muted focus:bg-muted focus-visible:ring-3 focus-visible:ring-ring/50 focus-visible:outline-1 disabled:pointer-events-none disabled:opacity-50 data-popup-open:bg-muted/50 data-popup-open:hover:bg-muted data-open:bg-muted/50 data-open:hover:bg-muted data-open:focus:bg-muted")
function NavigationMenuTrigger({ className, children, ...props}: NavigationMenuPrimitive.Trigger.Props) { return ( <NavigationMenuPrimitive.Trigger data-slot="navigation-menu-trigger" className={cn(navigationMenuTriggerStyle(), "group", className)} {...props} > {children}{" "} <ChevronDownIcon className="relative top-px ml-1 size-3 transition duration-300 group-data-popup-open/navigation-menu-trigger:rotate-180 group-data-open/navigation-menu-trigger:rotate-180" aria-hidden="true" /> </NavigationMenuPrimitive.Trigger> )}
function NavigationMenuContent({ className, ...props}: NavigationMenuPrimitive.Content.Props) { return ( <NavigationMenuPrimitive.Content data-slot="navigation-menu-content" className={cn( "data-ending-style:data-activation-direction=left:translate-x-[50%] data-ending-style:data-activation-direction=right:translate-x-[-50%] data-starting-style:data-activation-direction=left:translate-x-[-50%] data-starting-style:data-activation-direction=right:translate-x-[50%] h-full w-auto p-2 pr-2.5 transition-[opacity,transform,translate] duration-[0.35s] ease-[cubic-bezier(0.22,1,0.36,1)] group-data-[viewport=false]/navigation-menu:rounded-md group-data-[viewport=false]/navigation-menu:bg-popover group-data-[viewport=false]/navigation-menu:text-popover-foreground group-data-[viewport=false]/navigation-menu:shadow group-data-[viewport=false]/navigation-menu:ring-1 group-data-[viewport=false]/navigation-menu:ring-foreground/10 group-data-[viewport=false]/navigation-menu:duration-300 data-ending-style:opacity-0 data-starting-style:opacity-0 data-[motion=from-end]:slide-in-from-right-52 data-[motion=from-start]:slide-in-from-left-52 data-[motion=to-end]:slide-out-to-right-52 data-[motion=to-start]:slide-out-to-left-52 data-[motion^=from-]:animate-in data-[motion^=from-]:fade-in data-[motion^=to-]:animate-out data-[motion^=to-]:fade-out **:data-[slot=navigation-menu-link]:focus:ring-0 **:data-[slot=navigation-menu-link]:focus:outline-none group-data-[viewport=false]/navigation-menu:data-open:animate-in group-data-[viewport=false]/navigation-menu:data-open:fade-in-0 group-data-[viewport=false]/navigation-menu:data-open:zoom-in-95 group-data-[viewport=false]/navigation-menu:data-closed:animate-out group-data-[viewport=false]/navigation-menu:data-closed:fade-out-0 group-data-[viewport=false]/navigation-menu:data-closed:zoom-out-95", className )} {...props} /> )}
function NavigationMenuPositioner({ className, side = "bottom", sideOffset = 8, align = "start", alignOffset = 0, ...props}: NavigationMenuPrimitive.Positioner.Props) { return ( <NavigationMenuPrimitive.Portal> <NavigationMenuPrimitive.Positioner side={side} sideOffset={sideOffset} align={align} alignOffset={alignOffset} className={cn( "isolate z-50 h-(--positioner-height) w-(--positioner-width) max-w-(--available-width) transition-[top,left,right,bottom] duration-[0.35s] ease-[cubic-bezier(0.22,1,0.36,1)] data-instant:transition-none data-[side=bottom]:before:top-[-10px] data-[side=bottom]:before:right-0 data-[side=bottom]:before:left-0", className )} {...props} > <NavigationMenuPrimitive.Popup className="data-[ending-style]:easing-[ease] xs:w-(--popup-width) relative h-(--popup-height) w-(--popup-width) origin-(--transform-origin) rounded-lg bg-popover text-popover-foreground shadow ring-1 ring-foreground/10 transition-[opacity,transform,width,height,scale,translate] duration-[0.35s] ease-[cubic-bezier(0.22,1,0.36,1)] outline-none data-ending-style:scale-90 data-ending-style:opacity-0 data-ending-style:duration-150 data-starting-style:scale-90 data-starting-style:opacity-0"> <NavigationMenuPrimitive.Viewport className="relative size-full overflow-hidden" /> </NavigationMenuPrimitive.Popup> </NavigationMenuPrimitive.Positioner> </NavigationMenuPrimitive.Portal> )}
function NavigationMenuLink({ className, ...props}: NavigationMenuPrimitive.Link.Props) { return ( <NavigationMenuPrimitive.Link data-slot="navigation-menu-link" className={cn( "flex items-center gap-1.5 rounded-md p-2 text-sm transition-all outline-none hover:bg-muted focus:bg-muted focus-visible:ring-3 focus-visible:ring-ring/50 focus-visible:outline-1 in-data-[slot=navigation-menu-content]:rounded-sm data-[active=true]:bg-muted/50 data-[active=true]:hover:bg-muted data-[active=true]:focus:bg-muted [&_svg:not([class*='size-'])]:size-4", className )} {...props} /> )}
function NavigationMenuIndicator({ className, ...props}: React.ComponentPropsWithRef<typeof NavigationMenuPrimitive.Icon>) { return ( <NavigationMenuPrimitive.Icon data-slot="navigation-menu-indicator" className={cn( "top-full z-1 flex h-1.5 items-end justify-center overflow-hidden data-[state=hidden]:animate-out data-[state=hidden]:fade-out data-[state=visible]:animate-in data-[state=visible]:fade-in", className )} {...props} > <div className="relative top-[60%] h-2 w-2 rotate-45 rounded-tl-sm bg-border shadow-md" /> </NavigationMenuPrimitive.Icon> )}
export { NavigationMenu, NavigationMenuContent, NavigationMenuIndicator, NavigationMenuItem, NavigationMenuLink, NavigationMenuList, NavigationMenuTrigger, navigationMenuTriggerStyle, NavigationMenuPositioner,}Usage
import Link from "next/link"
import { NavigationMenu, NavigationMenuContent, NavigationMenuItem, NavigationMenuLink, NavigationMenuList, NavigationMenuTrigger, navigationMenuTriggerStyle,} from "@/components/ui/navigation-menu"<NavigationMenu aria-label="Main"> <NavigationMenuList> <NavigationMenuItem> <NavigationMenuTrigger>Modules</NavigationMenuTrigger> <NavigationMenuContent> <NavigationMenuLink render={<Link href="/purchasing" />}> Purchasing </NavigationMenuLink> </NavigationMenuContent> </NavigationMenuItem> <NavigationMenuItem> <NavigationMenuLink render={<Link href="/reports" />} className={navigationMenuTriggerStyle()} > Reports </NavigationMenuLink> </NavigationMenuItem> </NavigationMenuList></NavigationMenu>Pass your router link to render, as above, so navigation stays on the client. A top-level link that sits next to triggers takes navigationMenuTriggerStyle() to match their size. The popup that holds the content is created for you by NavigationMenu: do not add a viewport.
Examples
Links only
When no section needs a submenu, the menu is a row of links. Mark the current page with active, which sets aria-current="page".
With icons and descriptions
Content can hold anything: here each link has an icon, a title and a line of description. Keep the description to one line.
Controlled
value and onValueChange on NavigationMenu read and set which item is open. Give each item that needs it a value; the value is null when the menu is closed.
Open section: none
Accessibility
| Key | Action |
|---|---|
| Tab | Moves focus into the menu, then on to the content of an open item and out of the menu. |
| ArrowRight | Moves focus to the next trigger or link in a horizontal menu. |
| ArrowLeft | Moves focus to the previous trigger or link in a horizontal menu. |
| ArrowDown | Opens the content of the focused trigger in a horizontal menu. |
| Enter | Opens the focused trigger, or follows the focused link. |
| Space | Opens the focused trigger. |
| Escape | Closes the open content. |
Base UI provides:
- A
navelement as the root, a list of items inside it, and triggers that are buttons. aria-expandedandaria-controlson the open trigger.aria-current="page"on a link withactive.- Arrow key movement between the triggers and links of the list, without wrapping.
- Opening on hover with a short delay, and closing when the pointer or focus leaves.
You provide:
- An
aria-labelonNavigationMenu, such as "Main". The root is a navigation landmark, and a page with more than one needs a distinct name for each. activeon the link of the current page.- A menu that is usable on a phone: the example hides the wide item with
hidden md:flexon the item. For deeper structures at small widths, use a Sheet or a Sidebar.
Guidelines
When to use
- For the main navigation of a marketing site, a docs site or a light app with a handful of top-level sections.
- When some sections have several destinations that deserve a short description.
When not to use
- For an app with many modules and levels. Use a Sidebar, which can hold groups and stays visible.
- To show the path to the current page. Use a Breadcrumb.
- To switch views of one object. Use Tabs.
- For a list of actions on an item. Use a Dropdown Menu.
- For a menu with one link inside. Make it a link.
Content
- Keep to five or six top-level items, with one or two words each.
- Write labels as the name of the place, in sentence case: "Purchasing", not "Go to purchasing".
- Give the first level the same labels as the page titles.
- Use a description only where the title alone is ambiguous.
Use plain links for sections that have no submenu, and mark the current one.
Don’t wrap a single destination in a menu. The user opens it to find one link.
API reference
All parts except the style helpers accept the props of the Base UI part they render. See the Base UI API reference.
NavigationMenu
The root. Renders a nav and, after the list, the positioned popup that shows the open content.
| Prop | Type | Default | Description |
|---|---|---|---|
value? | string | null | None | The value of the open item, for a controlled menu. Null when closed. |
defaultValue? | string | null | None | The value of the item open at first. |
onValueChange? | (value: string | null, eventDetails: ChangeEventDetails) => void | None | Called when the open item changes. |
orientation? | "horizontal" | "vertical" | "horizontal" | The direction of the list and of the arrow keys. |
delay? | number | 50 | Milliseconds before hovering a trigger opens its content. |
closeDelay? | number | 50 | Milliseconds before leaving a trigger closes its content. |
align? | "start" | "center" | "end" | "start" | How the popup lines up with the open trigger. |
aria-label? | string | None | The name of the navigation landmark. |
className? | string | None | Classes merged after the base classes. |
NavigationMenuList
Renders the ul that holds the items.
| Prop | Type | Default | Description |
|---|---|---|---|
className? | string | None | Classes merged after the base classes. |
NavigationMenuItem
Renders an li for one trigger and its content, or for one link.
| Prop | Type | Default | Description |
|---|---|---|---|
value? | string | None | Identifies the item for value and onValueChange. Generated when omitted. |
className? | string | None | Classes merged after the base classes. |
NavigationMenuTrigger
Renders a button with the trigger styles and a chevron that turns when the item is open.
| Prop | Type | Default | Description |
|---|---|---|---|
disabled? | boolean | false | Prevents the item from opening. |
children? | ReactNode | None | The label, shown before the chevron. |
className? | string | None | Classes merged after the trigger styles. |
NavigationMenuContent
The panel shown for an open item. It is moved into the shared popup and animates between items.
| Prop | Type | Default | Description |
|---|---|---|---|
keepMounted? | boolean | false | Keeps the content in the DOM while the item is closed. |
className? | string | None | Classes merged after the base classes. |
NavigationMenuLink
Renders an a, or the element you pass to render.
| Prop | Type | Default | Description |
|---|---|---|---|
render? | ReactElement | None | The element to render in place of the anchor, for example a Next.js Link. |
active? | boolean | false | Marks the current page: sets aria-current to page and the active styles. |
closeOnClick? | boolean | false | Closes the open content when the link is clicked. |
className? | string | None | Classes merged after the base classes. |
NavigationMenuIndicator
Renders the Base UI Icon part with a small rotated square inside. Base UI meant that part as the icon inside a trigger, so it does not follow or point at the open trigger. NavigationMenuTrigger already draws its own chevron.
| Prop | Type | Default | Description |
|---|---|---|---|
className? | string | None | Classes merged after the base classes. |
NavigationMenuPositioner
Places the popup against the open trigger. NavigationMenu already renders one; use this export only to replace it.
| Prop | Type | Default | Description |
|---|---|---|---|
side? | "top" | "bottom" | "left" | "right" | "inline-start" | "inline-end" | "bottom" | The side of the trigger on which the popup appears. |
sideOffset? | number | 8 | The distance in pixels from the trigger. |
align? | "start" | "center" | "end" | "start" | The alignment along the trigger. |
alignOffset? | number | 0 | The shift in pixels along the alignment axis. |
navigationMenuTriggerStyle
The class function behind the trigger, from class-variance-authority. Call it to give a link the size and hover styles of a trigger: navigationMenuTriggerStyle().